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