Added selector to can

This commit is contained in:
2025-06-05 11:49:57 +02:00
parent 4801e26a24
commit 0fcb9bf91a
+62 -26
View File
@@ -13,15 +13,17 @@ var loaded_map_data = [];
var throttle_output = 0;
var enabled = false;
var channel = can.createRawChannel(can_conf.network_id, true);
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);
}
// 404#0000245E00710186
// 019#02
// 050#9702
const current_id = (0x01 << 5) | can_conf.controller.id;
const brake_current_id = (0x02 << 5) | can_conf.controller.id;
const erpm_id = (0x20 << 5) | can_conf.controller.id;
function parseMap(filename) {
loaded_map_data = [];
@@ -63,54 +65,88 @@ function getMapIndex(array, input) {
return Math.abs(array[left] - input) < Math.abs(array[right] - input) ? left : right;
}
channel.addListener("onMessage", async function (message) {
(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(map_conf.selected_map_path)) {
let enabled_map_name = fs.readFileSync(map_conf.selected_map_path);
const enabled_map_name = fs.readFileSync(map_conf.selected_map_path);
if (enabled_map_name != "") {
if (loaded_map_name != enabled_map_name) {
await parseMap(enabled_map_name);
}
}
}
})();
if (message.id == can_conf.apps_id) {
const apps_percentage = ((message.data[1] << 8) | message.data[0]) / 1000;
let pos = getMapIndex(map_throttle, apps_percentage);
throttle_output = loaded_map_data[pos][2];
channel.addListener("onMessage", async function (message) {
switch (message.id) {
case can_conf.selector.id:
console.log("==INVOKE SELECTOR==");
if (Buffer.compare(message.data, can_conf.selector.command) == 0) {
console.log("enable");
enabled = true;
} else {
let packet_id = message.id >> 5;
let node_id = message.id & 0x1F;
console.log("disable");
enabled = false;
}
if ((node_id == can_conf.controller.id) && (packet_id == 0x20)) {
let ERPM = message.data[0] << 3 * 8 | message.data[1] << 2 * 8 | message.data[2] << 8 | message.data[3];
break;
case can_conf.apps_id:
console.log("==INVOKE APPS==");
const apps_percentage = ((message.data[1] << 8) | message.data[0]) / 1000;
if (throttle_output > 0) {
let max_current = loaded_map_data[getMapIndex(map_rpm, ERPM / can_conf.motor_poles)][0];
throttle_output = loaded_map_data[getMapIndex(map_throttle, apps_percentage)][2];
console.log(throttle_output);
break;
case erpm_id:
console.log("==INVOKE ERPM==");
if (enabled) {
console.log("enabled");
if (fs.existsSync(map_conf.selected_map_path)) {
const enabled_map_name = fs.readFileSync(map_conf.selected_map_path);
if (enabled_map_name != "") {
console.log("map selected");
if (loaded_map_name != enabled_map_name) {
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;
console.log(max_current);
console.log(current);
channel.send({
id: current_id,
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 (throttle_output < 0) {
let max_brake_current = loaded_map_data[getMapIndex(map_rpm, ERPM / can_conf.motor_poles)][1];
let brake_current = throttle_output * max_brake_current * 10;
} else {
console.log("map not selected");
channel.send({
id: brake_current_id,
ext: false,
data: Buffer.from([(brake_current >> 8) & 0xFF, brake_current & 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
data: Buffer.from([0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
});
}
} else {
channel.send({
id: current_id,
id: brake_current_id,
ext: false,
data: Buffer.from([0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF])
});
}
}
}
break;
default: {
break;
}
}
});