Map socket comm
This commit is contained in:
@@ -1,15 +1,18 @@
|
||||
const can = require("socketcan");
|
||||
const csv = require("fast-csv");
|
||||
const fs = require("fs");
|
||||
const net = require('net');
|
||||
|
||||
const can_conf = require("./app/config/can.conf");
|
||||
const map_conf = require("./app/config/map.conf");
|
||||
const path = require("path");
|
||||
|
||||
const SOCKET_PATH = path.join(__dirname, map_conf.map_storage_path, can_conf.socket_filename);
|
||||
|
||||
var map_rpm = [];
|
||||
var map_throttle = [];
|
||||
|
||||
var loaded_map_name = "";
|
||||
var loaded_map_name = null;
|
||||
var loaded_map_data = [];
|
||||
|
||||
var throttle_output = 0;
|
||||
@@ -31,7 +34,7 @@ function parseMap(filename) {
|
||||
let map = [];
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
csv.parseFile(path.join(__dirname, map_conf.map_storage_path, filename), { comment: "#" })
|
||||
csv.parseFile((filename), { comment: "#" })
|
||||
.on('error', error => reject(error))
|
||||
.on('data', row => map.push(row))
|
||||
.on('end', () => { name = filename; resolve([name, map]) });
|
||||
@@ -67,24 +70,58 @@ function getMapIndex(array, input) {
|
||||
return Math.abs(array[left] - input) < Math.abs(array[right] - input) ? left : right;
|
||||
}
|
||||
|
||||
function connect() {
|
||||
console.log("Connecting...");
|
||||
if (fs.existsSync(SOCKET_PATH)) {
|
||||
const client = net.createConnection(SOCKET_PATH, () => {
|
||||
console.log("Connected");
|
||||
});
|
||||
|
||||
client.on("data", async (data) => {
|
||||
let enabled_map_name = JSON.parse(data)["map"];
|
||||
console.log(`Received: ${enabled_map_name}`);
|
||||
if (enabled_map_name === null) {
|
||||
console.log("Purging memory");
|
||||
loaded_map_name = null;
|
||||
loaded_map_data = [];
|
||||
} else if (enabled_map_name != loaded_map_name) {
|
||||
console.log("Parsing map");
|
||||
[loaded_map_name, loaded_map_data] = await parseMap(path.join(__dirname, map_conf.map_storage_path, enabled_map_name));
|
||||
}
|
||||
});
|
||||
|
||||
client.on("end", () => {
|
||||
console.log("Disconnected");
|
||||
setTimeout(connect, can_conf.socket_connect_wait);
|
||||
});
|
||||
|
||||
client.on("error", (err) => {
|
||||
console.log(`Error: ${err.message}`);
|
||||
setTimeout(connect, can_conf.socket_connect_wait);
|
||||
});
|
||||
|
||||
} else {
|
||||
setTimeout(connect, can_conf.socket_connect_wait);
|
||||
}
|
||||
}
|
||||
|
||||
(async () => {
|
||||
for (let i = 0; i <= map_conf.steps; i++) {
|
||||
map_rpm[i] = i * (map_conf.max_rpm / map_conf.steps);
|
||||
map_throttle[i] = i * (map_conf.max_throttle / map_conf.steps);
|
||||
}
|
||||
|
||||
if (fs.existsSync(path.join(__dirname, map_conf.map_storage_path, "selected"))) {
|
||||
const enabled_map_name = fs.readFileSync(path.join(__dirname, map_conf.map_storage_path, "selected"));
|
||||
[loaded_map_name, loaded_map_data] = await parseMap(enabled_map_name);
|
||||
}
|
||||
connect();
|
||||
})();
|
||||
|
||||
channel.addListener("onMessage", async function (message) {
|
||||
switch (message.id) {
|
||||
case can_conf.selector.id:
|
||||
if (Buffer.compare(message.data, can_conf.selector.command) == 0) {
|
||||
console.log("Enable")
|
||||
enabled = true;
|
||||
} else {
|
||||
console.log("Disable");
|
||||
enabled = false;
|
||||
}
|
||||
|
||||
@@ -92,44 +129,31 @@ channel.addListener("onMessage", async function (message) {
|
||||
case can_conf.apps_id:
|
||||
const apps_percentage = ((message.data[1] << 8) | message.data[0]) / 1000;
|
||||
|
||||
throttle_output = loaded_map_data[getMapIndex(map_throttle, apps_percentage)][2];
|
||||
if (loaded_map_name !== null) {
|
||||
throttle_output = loaded_map_data[getMapIndex(map_throttle, apps_percentage)][2];
|
||||
} else {
|
||||
throttle_output = 0;
|
||||
}
|
||||
|
||||
break;
|
||||
case erpm_id:
|
||||
if (enabled) {
|
||||
if (fs.existsSync(path.join(__dirname, map_conf.map_storage_path, "selected"))) {
|
||||
const enabled_map_name = fs.readFileSync(path.join(__dirname, map_conf.map_storage_path, "selected"));
|
||||
|
||||
if (enabled_map_name != "") {
|
||||
if (loaded_map_name != enabled_map_name) {
|
||||
[loaded_map_name, loaded_map_data] = await parseMap(enabled_map_name);
|
||||
}
|
||||
|
||||
const ERPM = message.data[0] << 3 * 8 | message.data[1] << 2 * 8 | message.data[2] << 8 | message.data[3];
|
||||
|
||||
let max_current = loaded_map_data[getMapIndex(map_rpm, ERPM / can_conf.motor_poles)][(throttle_output > 0) ? 0 : 1];
|
||||
let current = throttle_output * max_current * 10;
|
||||
channel.send({
|
||||
id: (throttle_output > 0) ? current_id : brake_current_id,
|
||||
ext: false,
|
||||
data: Buffer.from([(current >> 8) & 0xFF, current & 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
|
||||
});
|
||||
} else {
|
||||
channel.send({
|
||||
id: brake_current_id,
|
||||
ext: false,
|
||||
data: Buffer.from([0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
|
||||
});
|
||||
}
|
||||
} else {
|
||||
channel.send({
|
||||
id: brake_current_id,
|
||||
ext: false,
|
||||
data: Buffer.from([0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
|
||||
});
|
||||
}
|
||||
if (enabled && (loaded_map_name !== null)) {
|
||||
const ERPM = message.data[0] << 3 * 8 | message.data[1] << 2 * 8 | message.data[2] << 8 | message.data[3];
|
||||
|
||||
let max_current = loaded_map_data[getMapIndex(map_rpm, ERPM / can_conf.motor_poles)][(throttle_output > 0) ? 0 : 1];
|
||||
let current = throttle_output * max_current * 10;
|
||||
channel.send({
|
||||
id: (throttle_output > 0) ? current_id : brake_current_id,
|
||||
ext: false,
|
||||
data: Buffer.from([(current >> 8) & 0xFF, current & 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
|
||||
});
|
||||
} else if (enabled && (loaded_map_data === null)) {
|
||||
channel.send({
|
||||
id: brake_current_id,
|
||||
ext: false,
|
||||
data: Buffer.from([0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
|
||||
});
|
||||
}
|
||||
|
||||
break;
|
||||
default: {
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user