From 8948c80d9f7ad29c556453ab07b274818f04748b Mon Sep 17 00:00:00 2001 From: modellfan Date: Wed, 29 Apr 2026 12:02:20 +0200 Subject: [PATCH 1/4] Add direct SDO CAN map clear action --- data/index.html | 2 +- data/inverter.js | 31 +++++++++++++++++++++++++++++++ data/ui.js | 23 +++++++++++++++++++++++ esp32-web-interface.ino | 3 +++ src/oi_can.cpp | 26 ++++++++++++++++++++++++++ src/oi_can.h | 1 + 6 files changed, 85 insertions(+), 1 deletion(-) diff --git a/data/index.html b/data/index.html index 5f29818..7c96223 100644 --- a/data/index.html +++ b/data/index.html @@ -506,7 +506,7 @@

Actions

- "; + msg += ""; + msg += ""; + modal.appendToModal('small', msg); + modal.showModal('small'); + }, + + /** @brief Clear all CAN mappings via direct SDO and reload the table */ + clearCanMapping: function() + { + modal.hideModal('small'); + inverter.clearCanMapping(function(values) { + ui.populateExistingCanMappingTable(values); + ui.showParamSuccessBar('CAN mappings cleared'); + }); + }, + /** @brief Populate the table of existing CAN mappings */ populateExistingCanMappingTable: function(values) { var existigCanMappingTable = document.getElementById("existingCanMappingTable"); diff --git a/esp32-web-interface.ino b/esp32-web-interface.ino index b0387e6..a4e2fc7 100644 --- a/esp32-web-interface.ino +++ b/esp32-web-interface.ino @@ -563,6 +563,9 @@ static void handleCanMap() { if (server.hasArg("add")) { res = OICan::AddCanMapping(server.arg("add")); } + else if (server.hasArg("clear")) { + res = OICan::ClearCanMapping(); + } else if (server.hasArg("remove")) { res = OICan::RemoveCanMapping(server.arg("remove")); } diff --git a/src/oi_can.cpp b/src/oi_can.cpp index b3a36d7..414ba99 100644 --- a/src/oi_can.cpp +++ b/src/oi_can.cpp @@ -57,6 +57,7 @@ #define SDO_CMD_LOAD 1 #define SDO_CMD_RESET 2 #define SDO_CMD_DEFAULTS 3 +#define SDO_CMD_CLEAR_CAN 4 #define SDO_CMD_START 4 #define SDO_CMD_STOP 5 #define MAX_ERROR_LOG_ENTRIES 100 @@ -559,6 +560,31 @@ SetResult RemoveCanMapping(String json){ return CommError; } +SetResult ClearCanMapping() { + if (state != IDLE) return CommError; + + twai_message_t rxframe; + + setValueSdo(SDO_INDEX_COMMANDS, SDO_CMD_CLEAR_CAN, 0U); + + if (twai_receive(&rxframe, pdMS_TO_TICKS(200)) == ESP_OK) { + if (rxframe.data[0] == SDO_WRITE_REPLY && + rxframe.data[1] == 0x02 && + rxframe.data[2] == 0x50 && + rxframe.data[3] == SDO_CMD_CLEAR_CAN) { + DBG_OUTPUT_PORT.println("CAN mappings cleared"); + return Ok; + } + else if (rxframe.data[0] == SDO_ABORT) { + DBG_OUTPUT_PORT.println("Clear CAN mappings aborted"); + return UnknownIndex; + } + } + + DBG_OUTPUT_PORT.println("Comm Error"); + return CommError; +} + SetResult SetValue(String name, double value) { if (state != IDLE) return CommError; diff --git a/src/oi_can.h b/src/oi_can.h index 5bbecad..dcaa8b7 100644 --- a/src/oi_can.h +++ b/src/oi_can.h @@ -30,6 +30,7 @@ void Loop(); bool SendJson(WiFiClient c); void SendCanMapping(WiFiClient c); SetResult AddCanMapping(String json); +SetResult ClearCanMapping(); SetResult RemoveCanMapping(String json); SetResult SetValue(String name, double value); double GetValue(String name); From 543d1e4991d13a1760bb7bdbc5689102037b2498 Mon Sep 17 00:00:00 2001 From: modellfan Date: Tue, 5 May 2026 19:42:11 +0200 Subject: [PATCH 2/4] Bug Fix JSON Interrupted by CAN mapping --- data/ui.js | 9 +++++++-- esp32-web-interface.ino | 6 ++++-- src/oi_can.cpp | 14 +++++++++++--- src/oi_can.h | 2 +- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/data/ui.js b/data/ui.js index 59e758d..6a9b146 100644 --- a/data/ui.js +++ b/data/ui.js @@ -29,9 +29,11 @@ var ui = { // temp variable to store updates from Parameter Database paramUpdates: "", - // Status of visibility of parameter categories. E.g. Motor, Inverter. true = visible, false = not visible. + // Status of visibility of parameter categories. E.g. Motor, Inverter. true = visible, false = not visible. categoryVisible: {}, + canMappingLoaded: false, + navbarIsBig: true, shrinkNavbar: function() { @@ -152,7 +154,6 @@ var ui = { ui.updateTables(); plot.generateChart(); ui.parameterDatabaseCheckForUpdates(); - inverter.canMapping(ui.populateExistingCanMappingTable); wifi.populateWiFiTab(); settings.populateSettingsTab(); ui.populateFileList(); @@ -345,6 +346,10 @@ var ui = { } ui.populateVersion(); ui.populateSpotValueDropDown(); + if (!ui.canMappingLoaded && Object.keys(values).length > 0) { + ui.canMappingLoaded = true; + inverter.canMapping(ui.populateExistingCanMappingTable); + } document.getElementById("paramDownload").href = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(params, null, 2)); document.getElementById("spinner-div").style.visibility = "hidden"; diff --git a/esp32-web-interface.ino b/esp32-web-interface.ino index a4e2fc7..5f58f51 100644 --- a/esp32-web-interface.ino +++ b/esp32-web-interface.ino @@ -575,8 +575,10 @@ static void handleCanMap() { res = OICan::AddCanMapping(server.arg("edit")); } - if (res == OICan::Ok) - OICan::SendCanMapping(server.client()); + if (res == OICan::Ok) { + if (!OICan::SendCanMapping(server.client())) + server.send(500, "text/plain", "CAN communication error"); + } else if (res == OICan::CommError) server.send(500, "text/plain", "CAN communication error"); else if (res == OICan::UnknownIndex) diff --git a/src/oi_can.cpp b/src/oi_can.cpp index 414ba99..4f34f3a 100644 --- a/src/oi_can.cpp +++ b/src/oi_can.cpp @@ -164,6 +164,7 @@ static void handleSdoResponse(twai_message_t *rxframe) { } else { state = OBTAIN_JSON; + toggleBit = false; DBG_OUTPUT_PORT.printf("Downloading json to %s\r\n", jsonFileName); file = SPIFFS.open(jsonFileName, "w+"); requestSdoElement(SDO_INDEX_STRINGS, 0); //Initiates JSON upload @@ -355,6 +356,7 @@ bool SendJson(WiFiClient client) { if (result != DeserializationError::Ok) { SPIFFS.remove(jsonFileName); //if json file is invalid, remove it and trigger re-download + state = OBTAINSERIAL; updstate = REQUEST_JSON; retries = 50; DBG_OUTPUT_PORT.println("JSON file invalid, re-downloading"); @@ -384,7 +386,12 @@ bool SendJson(WiFiClient client) { return failed < 5; } -void SendCanMapping(WiFiClient client) { +bool SendCanMapping(WiFiClient client) { + if (state != IDLE || updstate != UPD_IDLE) { + DBG_OUTPUT_PORT.printf("SendCanMapping rejected: CAN state=%d update state=%d\r\n", state, updstate); + return false; + } + enum ReqMapStt { START, COBID, DATAPOSLEN, GAINOFS, DONE }; twai_message_t rxframe; @@ -489,6 +496,7 @@ void SendCanMapping(WiFiClient client) { WriteBufferingStream bufferedWifiClient{client, 1000}; serializeJson(doc, bufferedWifiClient); + return true; } SetResult AddCanMapping(String json) { @@ -874,9 +882,9 @@ void Loop() { retries--; - if (recvdResponse || retries < 0) + if (state == IDLE || state == OBTAIN_JSON || retries < 0) updstate = UPD_IDLE; //if request was successful - else + else if (!recvdResponse) requestSdoElement(SDO_INDEX_SERIAL, 0); delay(100); diff --git a/src/oi_can.h b/src/oi_can.h index dcaa8b7..ffc56de 100644 --- a/src/oi_can.h +++ b/src/oi_can.h @@ -28,7 +28,7 @@ enum BaudRate { Baud125k, Baud250k, Baud500k }; void Init(uint8_t nodeId, BaudRate baud, int txPin, int rxPin); void Loop(); bool SendJson(WiFiClient c); -void SendCanMapping(WiFiClient c); +bool SendCanMapping(WiFiClient c); SetResult AddCanMapping(String json); SetResult ClearCanMapping(); SetResult RemoveCanMapping(String json); From 8db6c43c8fbfbb7c2d68e1fb0468f1a75f421a97 Mon Sep 17 00:00:00 2001 From: modellfan Date: Wed, 6 May 2026 23:05:46 +0200 Subject: [PATCH 3/4] Clear CAN ID now 6 --- src/oi_can.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/oi_can.cpp b/src/oi_can.cpp index 4f34f3a..f1a344e 100644 --- a/src/oi_can.cpp +++ b/src/oi_can.cpp @@ -60,6 +60,7 @@ #define SDO_CMD_CLEAR_CAN 4 #define SDO_CMD_START 4 #define SDO_CMD_STOP 5 +#define SDO_CMD_CLEAR_CAN 6 #define MAX_ERROR_LOG_ENTRIES 100 namespace OICan { From eccac79255a096381d1e77039ffe93035d24cfdb Mon Sep 17 00:00:00 2001 From: modellfan Date: Wed, 6 May 2026 23:19:25 +0200 Subject: [PATCH 4/4] forgot to remove the old definition --- src/oi_can.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/oi_can.cpp b/src/oi_can.cpp index f1a344e..5d09ff0 100644 --- a/src/oi_can.cpp +++ b/src/oi_can.cpp @@ -57,7 +57,6 @@ #define SDO_CMD_LOAD 1 #define SDO_CMD_RESET 2 #define SDO_CMD_DEFAULTS 3 -#define SDO_CMD_CLEAR_CAN 4 #define SDO_CMD_START 4 #define SDO_CMD_STOP 5 #define SDO_CMD_CLEAR_CAN 6