Skip to content

Commit

Permalink
Merge branch 'dalathegreat:main' into tesla-bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
josiahhiggs authored Jan 26, 2025
2 parents d57d853 + 759cfcf commit 87ac87c
Show file tree
Hide file tree
Showing 18 changed files with 550 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/compile-all-batteries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,4 @@ jobs:
# in the build matrix, and using build flags to define the
# battery and inverter set in the build matrix.
- name: Compile Sketch
run: arduino-cli compile --fqbn ${{ matrix.fqbn }} --build-property "build.extra_flags=-Wall -DESP32 -D${{ matrix.battery}} -D${{ matrix.inverter}} -D${{ matrix.hardware}}" ./Software
run: arduino-cli compile --fqbn ${{ matrix.fqbn }} --build-property "build.extra_flags=-Wall -Wextra -Wpedantic -Werror -DESP32 -D${{ matrix.battery}} -D${{ matrix.inverter}} -D${{ matrix.hardware}}" ./Software
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,4 @@ jobs:
# in the build matrix, and using build flags to define the
# battery and inverter set in the build matrix.
- name: Compile Sketch
run: arduino-cli compile --fqbn ${{ matrix.fqbn }} --build-property "build.extra_flags=-Wall -DESP32 -D${{ matrix.battery}} -D${{ matrix.inverter}} -D${{ matrix.hardware}}" ./Software
run: arduino-cli compile --fqbn ${{ matrix.fqbn }} --build-property "build.extra_flags=-Wall -Wextra -Wpedantic -Werror -DESP32 -D${{ matrix.battery}} -D${{ matrix.inverter}} -D${{ matrix.hardware}}" ./Software
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,4 @@ jobs:
# in the build matrix, and using build flags to define the
# battery and inverter set in the build matrix.
- name: Compile Sketch
run: arduino-cli compile --fqbn ${{ matrix.fqbn }} --build-property "build.extra_flags=-Wall -DESP32 -D${{ matrix.battery}} -D${{ matrix.inverter}} -D${{ matrix.hardware}}" ./Software
run: arduino-cli compile --fqbn ${{ matrix.fqbn }} --build-property "build.extra_flags=-Wall -Wextra -Wpedantic -Werror -DESP32 -D${{ matrix.battery}} -D${{ matrix.inverter}} -D${{ matrix.hardware}}" ./Software
2 changes: 1 addition & 1 deletion .github/workflows/compile-all-hardware.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,4 @@ jobs:
# in the build matrix, and using build flags to define the
# battery and inverter set in the build matrix.
- name: Compile Sketch
run: arduino-cli compile --fqbn ${{ matrix.fqbn }} --build-property "build.extra_flags=-Wall -DESP32 -D${{ matrix.battery}} -D${{ matrix.inverter}} -D${{ matrix.hardware}}" ./Software
run: arduino-cli compile --fqbn ${{ matrix.fqbn }} --build-property "build.extra_flags=-Wall -Wextra -Wpedantic -Werror -DESP32 -D${{ matrix.battery}} -D${{ matrix.inverter}} -D${{ matrix.hardware}}" ./Software
2 changes: 1 addition & 1 deletion .github/workflows/compile-all-inverters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,4 @@ jobs:
# in the build matrix, and using build flags to define the
# battery and inverter set in the build matrix.
- name: Compile Sketch
run: arduino-cli compile --fqbn ${{ matrix.fqbn }} --build-property "build.extra_flags=-Wall -DESP32 -D${{ matrix.battery}} -D${{ matrix.inverter}} -D${{ matrix.hardware}}" ./Software
run: arduino-cli compile --fqbn ${{ matrix.fqbn }} --build-property "build.extra_flags=-Wall -Wextra -Wpedantic -Werror -DESP32 -D${{ matrix.battery}} -D${{ matrix.inverter}} -D${{ matrix.hardware}}" ./Software
2 changes: 1 addition & 1 deletion Software/Software.ino
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
#endif // WIFI

// The current software version, shown on webserver
const char* version_number = "8.3.dev";
const char* version_number = "8.3.0";

// Interval settings
uint16_t intervalUpdateValues = INTERVAL_1_S; // Interval at which to update inverter values / Modbus registers
Expand Down
449 changes: 449 additions & 0 deletions Software/src/battery/KIA-HYUNDAI-64-BATTERY.cpp

Large diffs are not rendered by default.

25 changes: 17 additions & 8 deletions Software/src/battery/NISSAN-LEAF-BATTERY.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ void update_values_battery() { /* This function maps all the values fetched via

datalayer.battery.status.max_charge_power_W = (battery_Charge_Power_Limit * 1000); //kW to W

//Allow contactors to close
if (battery_can_alive) {
datalayer.system.status.battery_allows_contactor_closing = true;
}

/*Extra safety functions below*/
if (battery_GIDS < 10) //700Wh left in battery!
{ //Battery is running abnormally low, some discharge logic might have failed. Zero it all out.
Expand Down Expand Up @@ -766,11 +771,6 @@ void handle_incoming_can_frame_battery(CAN_frame rx_frame) {
battery_Relay_Cut_Request = ((rx_frame.data.u8[1] & 0x18) >> 3);
battery_Failsafe_Status = (rx_frame.data.u8[1] & 0x07);
battery_MainRelayOn_flag = (bool)((rx_frame.data.u8[3] & 0x20) >> 5);
if (battery_MainRelayOn_flag) {
datalayer.system.status.battery_allows_contactor_closing = true;
} else {
datalayer.system.status.battery_allows_contactor_closing = false;
}
battery_Full_CHARGE_flag = (bool)((rx_frame.data.u8[3] & 0x10) >> 4);
battery_Interlock = (bool)((rx_frame.data.u8[3] & 0x08) >> 3);
break;
Expand Down Expand Up @@ -1064,9 +1064,19 @@ void handle_incoming_can_frame_battery(CAN_frame rx_frame) {
}
}
void transmit_can_battery() {
if (battery_can_alive) {

unsigned long currentMillis = millis();
unsigned long currentMillis = millis();

if (datalayer.system.status.BMS_reset_in_progress) {
// Transmitting towards battery is halted while BMS is being reset
// Reset sending counters to avoid overrun messages when reset is over
previousMillis10 = currentMillis;
previousMillis100 = currentMillis;
previousMillis10s = currentMillis;
return;
}

if (battery_can_alive) {

//Send 10ms message
if (currentMillis - previousMillis10 >= INTERVAL_10_MS) {
Expand Down Expand Up @@ -1499,7 +1509,6 @@ void decodeChallengeData(unsigned int incomingChallenge, unsigned char* solvedCh
void setup_battery(void) { // Performs one time setup at startup
strncpy(datalayer.system.info.battery_protocol, "Nissan LEAF battery", 63);
datalayer.system.info.battery_protocol[63] = '\0';

datalayer.battery.info.number_of_cells = 96;
datalayer.battery.info.max_design_voltage_dV = MAX_PACK_VOLTAGE_DV;
datalayer.battery.info.min_design_voltage_dV = MIN_PACK_VOLTAGE_DV;
Expand Down
3 changes: 2 additions & 1 deletion Software/src/communication/can/comm_can.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ void init_CAN() {
// Transmit functions
void transmit_can() {
if (!allowed_to_send_CAN) {
return;
return; //Global block of CAN messages
}

transmit_can_battery();

#ifdef CAN_INVERTER_SELECTED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ unsigned long currentTime = 0;
unsigned long lastPowerRemovalTime = 0;
const unsigned long powerRemovalInterval = 24 * 60 * 60 * 1000; // 24 hours in milliseconds
const unsigned long powerRemovalDuration = 30000; // 30 seconds in milliseconds
bool isBMSResetActive = false;

void set(uint8_t pin, bool direction, uint32_t pwm_freq = 0xFFFF) {
#ifdef PWM_CONTACTOR_CONTROL
Expand Down Expand Up @@ -256,48 +255,38 @@ void handle_BMSpower() {
#endif //PERIODIC_BMS_RESET

// If power has been removed for 30 seconds, restore the power and resume the emulator
if (isBMSResetActive && currentTime - lastPowerRemovalTime >= powerRemovalDuration) {
if (datalayer.system.status.BMS_reset_in_progress && currentTime - lastPowerRemovalTime >= powerRemovalDuration) {
// Reapply power to the BMS
digitalWrite(BMS_POWER, HIGH);
#ifdef BMS_2_POWER
digitalWrite(BMS_2_POWER, HIGH); // Same for battery 2
#endif

//Resume the battery pause and CAN communication
//Resume from the power pause
setBatteryPause(false, false, false, false);

isBMSResetActive = false; // Reset the power removal flag
datalayer.system.status.BMS_reset_in_progress = false; // Reset the power removal flag
}
#endif //defined(PERIODIC_BMS_RESET) || defined(REMOTE_BMS_RESET)
}

void start_bms_reset() {
#if defined(PERIODIC_BMS_RESET) || defined(REMOTE_BMS_RESET)
if (!isBMSResetActive) {
if (!datalayer.system.status.BMS_reset_in_progress) {
lastPowerRemovalTime = currentTime; // Record the time when BMS reset was started

// Set a flag to let the rest of the system know we are cutting power to the BMS.
// The battery CAN sending routine will then know not to try to send anything towards battery while active
datalayer.system.status.BMS_reset_in_progress = true;

// Set emulator state to paused (Max Charge/Discharge = 0 & CAN = stop)
// TODO: We try to keep contactors engaged during this pause, and just ramp power down to 0.
// If this turns out to not work properly, set also the third option to true to open contactors
setBatteryPause(true, true, false, false);
// We try to keep contactors engaged during this pause, and just ramp power down to 0.
setBatteryPause(true, false, false, false);

digitalWrite(BMS_POWER, LOW); // Remove power by setting the BMS power pin to LOW
#ifdef BMS_2_POWER
digitalWrite(BMS_2_POWER, LOW); // Same for battery 2
#endif

isBMSResetActive = true; // Set a flag to indicate power removal is active
}

// If power has been removed for 30 seconds, restore the power and resume the emulator
if (isBMSResetActive && currentTime - lastPowerRemovalTime >= powerRemovalDuration) {
// Reapply power to the BMS
digitalWrite(BMS_POWER, HIGH);
#ifdef BMS_2_POWER
digitalWrite(BMS_2_POWER, HIGH); // Same for battery 2
#endif

//Resume the battery pause and CAN communication
setBatteryPause(false, false, false, false);

isBMSResetActive = false; // Reset the power removal flag
}
#endif //PERIODIC_BMS_RESET
#endif //defined(PERIODIC_BMS_RESET) || defined(REMOTE_BMS_RESET)
}
2 changes: 2 additions & 0 deletions Software/src/datalayer/datalayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ typedef struct {
/** True if the contactor controlled by battery-emulator is closed. Determined by check_interconnect_available(); if voltage is OK */
bool contactors_battery2_engaged = false;
#endif
/** True if the BMS is being reset, by cutting power towards it */
bool BMS_reset_in_progress = false;
} DATALAYER_SYSTEM_STATUS_TYPE;

typedef struct {
Expand Down
10 changes: 10 additions & 0 deletions Software/src/datalayer/datalayer_extended.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,16 @@ typedef struct {
uint8_t batteryManagementMode = 0;
uint8_t BMS_ign = 0;
uint8_t batteryRelay = 0;
#ifdef DOUBLE_BATTERY
uint8_t battery2_total_cell_count = 0;
int16_t battery2_battery_12V = 0;
uint8_t battery2_waterleakageSensor = 0;
int8_t battery2_temperature_water_inlet = 0;
int8_t battery2_powerRelayTemperature = 0;
uint8_t battery2_batteryManagementMode = 0;
uint8_t battery2_BMS_ign = 0;
uint8_t battery2_batteryRelay = 0;
#endif //DOUBLE BATTERY
} DATALAYER_INFO_KIAHYUNDAI64;

typedef struct {
Expand Down
2 changes: 1 addition & 1 deletion Software/src/devboard/safety/safety.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ void update_machineryprotection() {
}

if (soh_diff_pptt > MAX_SOH_DEVIATION_PPTT) {
set_event(EVENT_SOH_DIFFERENCE, MAX_SOH_DEVIATION_PPTT);
set_event(EVENT_SOH_DIFFERENCE, (uint8_t)(MAX_SOH_DEVIATION_PPTT / 100));
} else {
clear_event(EVENT_SOH_DIFFERENCE);
}
Expand Down
16 changes: 16 additions & 0 deletions Software/src/devboard/webserver/advanced_battery_html.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,22 @@ String advanced_battery_processor(const String& var) {
content += "<h4>Batterymanagement mode: " + String(datalayer_extended.KiaHyundai64.batteryManagementMode) + "</h4>";
content += "<h4>BMS ignition: " + String(datalayer_extended.KiaHyundai64.BMS_ign) + "</h4>";
content += "<h4>Battery relay: " + String(datalayer_extended.KiaHyundai64.batteryRelay) + "</h4>";
#ifdef DOUBLE_BATTERY
content += "<h4>Values from battery 2</h4>";
content += "<h4>Cells: " + String(datalayer_extended.KiaHyundai64.battery2_total_cell_count) + "S</h4>";
content += "<h4>12V voltage: " + String(datalayer_extended.KiaHyundai64.battery2_battery_12V / 10.0, 1) + "</h4>";
content += "<h4>Waterleakage: " + String(datalayer_extended.KiaHyundai64.battery2_waterleakageSensor) + "</h4>";
content +=
"<h4>Temperature, water inlet: " + String(datalayer_extended.KiaHyundai64.battery2_temperature_water_inlet) +
"</h4>";
content +=
"<h4>Temperature, power relay: " + String(datalayer_extended.KiaHyundai64.battery2_powerRelayTemperature) +
"</h4>";
content += "<h4>Batterymanagement mode: " + String(datalayer_extended.KiaHyundai64.battery2_batteryManagementMode) +
"</h4>";
content += "<h4>BMS ignition: " + String(datalayer_extended.KiaHyundai64.battery2_BMS_ign) + "</h4>";
content += "<h4>Battery relay: " + String(datalayer_extended.KiaHyundai64.battery2_batteryRelay) + "</h4>";
#endif //DOUBLE_BATTERY
#endif //KIA_HYUNDAI_64_BATTERY

#ifdef BYD_ATTO_3_BATTERY
Expand Down
18 changes: 9 additions & 9 deletions Software/src/devboard/webserver/webserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,26 @@ void init_webserver() {
server.on("/GetFirmwareInfo", HTTP_GET, [](AsyncWebServerRequest* request) {
if (WEBSERVER_AUTH_REQUIRED && !request->authenticate(http_username, http_password))
return request->requestAuthentication();
request->send_P(200, "application/json", get_firmware_info_html, get_firmware_info_processor);
request->send(200, "application/json", get_firmware_info_html, get_firmware_info_processor);
});

// Route for root / web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest* request) {
if (WEBSERVER_AUTH_REQUIRED && !request->authenticate(http_username, http_password))
return request->requestAuthentication();
request->send_P(200, "text/html", index_html, processor);
request->send(200, "text/html", index_html, processor);
});

// Route for going to settings web page
server.on("/settings", HTTP_GET, [](AsyncWebServerRequest* request) {
if (WEBSERVER_AUTH_REQUIRED && !request->authenticate(http_username, http_password))
return request->requestAuthentication();
request->send_P(200, "text/html", index_html, settings_processor);
request->send(200, "text/html", index_html, settings_processor);
});

// Route for going to advanced battery info web page
server.on("/advanced", HTTP_GET, [](AsyncWebServerRequest* request) {
request->send_P(200, "text/html", index_html, advanced_battery_processor);
request->send(200, "text/html", index_html, advanced_battery_processor);
});

// Route for going to CAN logging web page
Expand All @@ -76,7 +76,7 @@ void init_webserver() {
// Define the handler to stop can logging
server.on("/stop_can_logging", HTTP_GET, [](AsyncWebServerRequest* request) {
datalayer.system.info.can_logging_active = false;
request->send_P(200, "text/plain", "Logging stopped");
request->send(200, "text/plain", "Logging stopped");
});

#ifndef LOG_CAN_TO_SD
Expand Down Expand Up @@ -119,15 +119,15 @@ void init_webserver() {
// Define the handler to delete can log
server.on("/delete_can_log", HTTP_GET, [](AsyncWebServerRequest* request) {
delete_can_log();
request->send_P(200, "text/plain", "Log file deleted");
request->send(200, "text/plain", "Log file deleted");
});
#endif

#ifdef LOG_TO_SD
// Define the handler to delete log file
server.on("/delete_log", HTTP_GET, [](AsyncWebServerRequest* request) {
delete_log();
request->send_P(200, "text/plain", "Log file deleted");
request->send(200, "text/plain", "Log file deleted");
});

// Define the handler to export debug log
Expand Down Expand Up @@ -171,14 +171,14 @@ void init_webserver() {
server.on("/cellmonitor", HTTP_GET, [](AsyncWebServerRequest* request) {
if (WEBSERVER_AUTH_REQUIRED && !request->authenticate(http_username, http_password))
return request->requestAuthentication();
request->send_P(200, "text/html", index_html, cellmonitor_processor);
request->send(200, "text/html", index_html, cellmonitor_processor);
});

// Route for going to event log web page
server.on("/events", HTTP_GET, [](AsyncWebServerRequest* request) {
if (WEBSERVER_AUTH_REQUIRED && !request->authenticate(http_username, http_password))
return request->requestAuthentication();
request->send_P(200, "text/html", index_html, events_processor);
request->send(200, "text/html", index_html, events_processor);
});

// Route for clearing all events
Expand Down
10 changes: 5 additions & 5 deletions Software/src/devboard/wifi/wifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ void wifi_monitor() {
}

// Function to force a full reconnect to Wi-Fi
static void FullReconnectToWiFi() {
void FullReconnectToWiFi() {

// Increase the current reconnect interval if it's not at the maximum
if (current_full_reconnect_interval + STEP_WIFI_FULL_RECONNECT_INTERVAL <= MAX_WIFI_FULL_RECONNECT_INTERVAL) {
Expand All @@ -120,7 +120,7 @@ static void FullReconnectToWiFi() {
}

// Function to handle Wi-Fi connection
static void connectToWiFi() {
void connectToWiFi() {
if (WiFi.status() != WL_CONNECTED) {
lastReconnectAttempt = millis(); // Reset the reconnect attempt timer
#ifdef DEBUG_LOG
Expand All @@ -135,7 +135,7 @@ static void connectToWiFi() {
}

// Event handler for successful Wi-Fi connection
static void onWifiConnect(WiFiEvent_t event, WiFiEventInfo_t info) {
void onWifiConnect(WiFiEvent_t event, WiFiEventInfo_t info) {
clear_event(EVENT_WIFI_DISCONNECT);
set_event(EVENT_WIFI_CONNECT, 0);
connected_once = true;
Expand All @@ -153,7 +153,7 @@ static void onWifiConnect(WiFiEvent_t event, WiFiEventInfo_t info) {
}

// Event handler for Wi-Fi Got IP
static void onWifiGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
void onWifiGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
//clear disconnects events if we got a IP
clear_event(EVENT_WIFI_DISCONNECT);
#ifdef DEBUG_LOG
Expand All @@ -164,7 +164,7 @@ static void onWifiGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
}

// Event handler for Wi-Fi disconnection
static void onWifiDisconnect(WiFiEvent_t event, WiFiEventInfo_t info) {
void onWifiDisconnect(WiFiEvent_t event, WiFiEventInfo_t info) {
if (connected_once)
set_event(EVENT_WIFI_DISCONNECT, 0);
#ifdef DEBUG_LOG
Expand Down
10 changes: 5 additions & 5 deletions Software/src/devboard/wifi/wifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ extern const char* passwordAP;

void init_WiFi();
void wifi_monitor();
static void connectToWiFi();
static void FullReconnectToWiFi();
static void onWifiConnect(WiFiEvent_t event, WiFiEventInfo_t info);
static void onWifiDisconnect(WiFiEvent_t event, WiFiEventInfo_t info);
static void onWifiGotIP(WiFiEvent_t event, WiFiEventInfo_t info);
void connectToWiFi();
void FullReconnectToWiFi();
void onWifiConnect(WiFiEvent_t event, WiFiEventInfo_t info);
void onWifiDisconnect(WiFiEvent_t event, WiFiEventInfo_t info);
void onWifiGotIP(WiFiEvent_t event, WiFiEventInfo_t info);

#ifdef WIFIAP
void init_WiFi_AP();
Expand Down
Loading

0 comments on commit 87ac87c

Please sign in to comment.