From 7006e8ccc60e1ecd244e9fc0c90c1e04bd1d1607 Mon Sep 17 00:00:00 2001 From: Kwstas Drakontidis Date: Wed, 18 Jun 2025 18:44:39 +0200 Subject: [PATCH] Send msg on apps & bug fix --- can.js | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/can.js b/can.js index 274525b..5fb29ee 100644 --- a/can.js +++ b/can.js @@ -16,11 +16,13 @@ var loaded_map_name = null; var loaded_map_data = []; var throttle_output = 0; +var ERPM = 0; var enabled = false; var channel = can.createRawChannel(can_conf.network_id, true); +// Some test messages... // 404#0000245E00710186 // 019#02 // 050#9702 @@ -128,18 +130,26 @@ channel.addListener("onMessage", async function (message) { break; case can_conf.apps_id: const apps_percentage = ((message.data[1] << 8) | message.data[0]) / 1000; + var max_current = 0; + throttle_output = 0; if (loaded_map_name !== null) { throttle_output = loaded_map_data[getMapIndex(map_throttle, apps_percentage)][2]; - } else { - throttle_output = 0; + 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]) + }); + break; case erpm_id: - 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]; - + if ((enabled == true) && (loaded_map_name !== null)) { + 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({ @@ -147,7 +157,7 @@ channel.addListener("onMessage", async function (message) { ext: false, data: Buffer.from([(current >> 8) & 0xFF, current & 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]) }); - } else if (enabled && (loaded_map_data === null)) { + } else if ((enabled == true) && (loaded_map_name === null)) { channel.send({ id: brake_current_id, ext: false,