Volvo/Polestar: Add contactor closing and DTC reset (#744)

* Add contactor closing and DTC reset
This commit is contained in:
Daniel Öster 2025-01-03 23:53:38 +03:00 committed by GitHub
parent 7df7992149
commit 01ae0c1278
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 316 additions and 10 deletions

View file

@ -1,6 +1,7 @@
#include "../include.h"
#ifdef VOLVO_SPA_BATTERY
#include "../datalayer/datalayer.h"
#include "../datalayer/datalayer_extended.h" //For "More battery info" webpage
#include "../devboard/utils/events.h"
#include "VOLVO-SPA-BATTERY.h"
@ -23,6 +24,9 @@ static uint16_t CELL_U_MAX = 3700; //0x37D
static uint16_t CELL_U_MIN = 3700; //0x37D
static uint8_t CELL_ID_U_MAX = 0; //0x37D
static uint16_t HvBattPwrLimDchaSoft = 0; //0x369
static uint16_t HvBattPwrLimDcha1 = 0; //0x175
static uint16_t HvBattPwrLimDchaSlowAgi = 0; //0x177
static uint16_t HvBattPwrLimChrgSlowAgi = 0; //0x177
static uint8_t batteryModuleNumber = 0x10; // First battery module
static uint8_t battery_request_idx = 0;
static uint8_t rxConsecutiveFrames = 0;
@ -35,7 +39,21 @@ CAN_frame VOLVO_536 = {.FD = false,
.ext_ID = false,
.DLC = 8,
.ID = 0x536,
.data = {0x00, 0x40, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00}}; //Network manage frame
//.data = {0x00, 0x40, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00}}; //Network manage frame
.data = {0x00, 0x40, 0x40, 0x01, 0x00, 0x00, 0x00, 0x00}}; //Network manage frame
CAN_frame VOLVO_140_CLOSE = {.FD = false,
.ext_ID = false,
.DLC = 8,
.ID = 0x140,
.data = {0x00, 0x02, 0x00, 0xB7, 0xFF, 0x03, 0xFF, 0x83}}; //Close contactors message
CAN_frame VOLVO_140_OPEN = {.FD = false,
.ext_ID = false,
.DLC = 8,
.ID = 0x140,
.data = {0x00, 0x02, 0x00, 0x9E, 0xFF, 0x03, 0xFF, 0x83}}; //Open contactor message
CAN_frame VOLVO_372 = {
.FD = false,
.ext_ID = false,
@ -57,10 +75,62 @@ CAN_frame VOLVO_SOH_Req = {.FD = false,
.DLC = 8,
.ID = 0x735,
.data = {0x03, 0x22, 0x49, 0x6D, 0x00, 0x00, 0x00, 0x00}}; //Battery SOH request frame
CAN_frame VOLVO_BECMsupplyVoltage_Req = {
.FD = false,
.ext_ID = false,
.DLC = 8,
.ID = 0x735,
.data = {0x03, 0x22, 0xF4, 0x42, 0x00, 0x00, 0x00, 0x00}}; //BECM supply voltage request frame
CAN_frame VOLVO_DTC_Erase = {.FD = false,
.ext_ID = false,
.DLC = 8,
.ID = 0x7FF,
.data = {0x04, 0x14, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00}}; //Global DTC erase
CAN_frame VOLVO_BECM_ECUreset = {
.FD = false,
.ext_ID = false,
.DLC = 8,
.ID = 0x735,
.data = {0x02, 0x11, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00}}; //BECM ECU reset command (reboot/powercycle BECM)
CAN_frame VOLVO_DTCreadout = {.FD = false,
.ext_ID = false,
.DLC = 8,
.ID = 0x7FF,
.data = {0x02, 0x19, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00}}; //Global DTC readout
void update_values_battery() { //This function maps all the values fetched via CAN to the correct parameters used for the inverter
uint8_t cnt = 0;
// Update webserver datalayer
datalayer_extended.VolvoPolestar.soc_bms = SOC_BMS;
datalayer_extended.VolvoPolestar.soc_calc = SOC_CALC;
datalayer_extended.VolvoPolestar.soc_rescaled = datalayer.battery.status.reported_soc;
datalayer_extended.VolvoPolestar.soh_bms = datalayer.battery.status.soh_pptt;
datalayer_extended.VolvoPolestar.BECMBatteryVoltage = BATT_U;
datalayer_extended.VolvoPolestar.BECMBatteryCurrent = BATT_I;
datalayer_extended.VolvoPolestar.BECMUDynMaxLim = MAX_U;
datalayer_extended.VolvoPolestar.BECMUDynMinLim = MIN_U;
datalayer_extended.VolvoPolestar.HvBattPwrLimDcha1 = HvBattPwrLimDcha1;
datalayer_extended.VolvoPolestar.HvBattPwrLimDchaSoft = HvBattPwrLimDchaSoft;
datalayer_extended.VolvoPolestar.HvBattPwrLimDchaSlowAgi = HvBattPwrLimDchaSlowAgi;
datalayer_extended.VolvoPolestar.HvBattPwrLimChrgSlowAgi = HvBattPwrLimChrgSlowAgi;
// Update requests from webserver datalayer
if (datalayer_extended.VolvoPolestar.UserRequestDTCreset) {
transmit_can_frame(&VOLVO_DTC_Erase, can_config.battery); //Send global DTC erase command
datalayer_extended.VolvoPolestar.UserRequestDTCreset = false;
}
if (datalayer_extended.VolvoPolestar.UserRequestBECMecuReset) {
transmit_can_frame(&VOLVO_BECM_ECUreset, can_config.battery); //Send BECM ecu reset command
datalayer_extended.VolvoPolestar.UserRequestBECMecuReset = false;
}
if (datalayer_extended.VolvoPolestar.UserRequestDTCreadout) {
transmit_can_frame(&VOLVO_DTCreadout, can_config.battery); //Send DTC readout command
datalayer_extended.VolvoPolestar.UserRequestDTCreadout = false;
}
remaining_capacity = (78200 - CHARGE_ENERGY);
//datalayer.battery.status.real_soc = SOC_BMS; // Use BMS reported SOC, havent figured out how to get the BMS to calibrate empty/full yet
@ -175,6 +245,25 @@ void handle_incoming_can_frame_battery(CAN_frame rx_frame) {
logging.println("BATT_U not valid");
#endif
}
if ((rx_frame.data.u8[0] & 0x40) == 0x40)
datalayer_extended.VolvoPolestar.HVSysRlySts = ((rx_frame.data.u8[0] & 0x30) >> 4);
else
datalayer_extended.VolvoPolestar.HVSysRlySts = 0xFF;
if ((rx_frame.data.u8[2] & 0x40) == 0x40)
datalayer_extended.VolvoPolestar.HVSysDCRlySts1 = ((rx_frame.data.u8[2] & 0x30) >> 4);
else
datalayer_extended.VolvoPolestar.HVSysDCRlySts1 = 0xFF;
if ((rx_frame.data.u8[2] & 0x80) == 0x80)
datalayer_extended.VolvoPolestar.HVSysDCRlySts2 = ((rx_frame.data.u8[4] & 0x30) >> 4);
else
datalayer_extended.VolvoPolestar.HVSysDCRlySts2 = 0xFF;
if ((rx_frame.data.u8[0] & 0x80) == 0x80)
datalayer_extended.VolvoPolestar.HVSysIsoRMonrSts = ((rx_frame.data.u8[4] & 0xC0) >> 6);
else
datalayer_extended.VolvoPolestar.HVSysIsoRMonrSts = 0xFF;
break;
case 0x1A1:
if ((rx_frame.data.u8[4] & 0x10) == 0x10)
@ -216,6 +305,25 @@ void handle_incoming_can_frame_battery(CAN_frame rx_frame) {
#endif
}
break;
case 0x175:
if ((rx_frame.data.u8[4] & 0x80) == 0x80) {
HvBattPwrLimDcha1 = (((rx_frame.data.u8[2] & 0x07) * 256 + rx_frame.data.u8[3]) >> 2);
} else {
HvBattPwrLimDcha1 = 0;
}
break;
case 0x177:
if ((rx_frame.data.u8[4] & 0x08) == 0x08) {
HvBattPwrLimDchaSlowAgi = (((rx_frame.data.u8[4] & 0x07) * 256 + rx_frame.data.u8[5]) >> 2);
} else {
HvBattPwrLimDchaSlowAgi = 0;
}
if ((rx_frame.data.u8[2] & 0x08) == 0x08) {
HvBattPwrLimChrgSlowAgi = (((rx_frame.data.u8[2] & 0x07) * 256 + rx_frame.data.u8[3]) >> 2);
} else {
HvBattPwrLimChrgSlowAgi = 0;
}
break;
case 0x37D:
if ((rx_frame.data.u8[0] & 0x40) == 0x40) {
SOC_BMS = ((rx_frame.data.u8[6] & 0x03) * 256 + rx_frame.data.u8[7]);
@ -258,6 +366,11 @@ void handle_incoming_can_frame_battery(CAN_frame rx_frame) {
(rx_frame.data.u8[3] == 0x6D)) // SOH response frame
{
datalayer.battery.status.soh_pptt = ((rx_frame.data.u8[6] << 8) | rx_frame.data.u8[7]);
transmit_can_frame(&VOLVO_BECMsupplyVoltage_Req, can_config.battery); //Send BECM supply voltage req
} else if ((rx_frame.data.u8[0] == 0x05) && (rx_frame.data.u8[1] == 0x62) && (rx_frame.data.u8[2] == 0xF4) &&
(rx_frame.data.u8[3] == 0x42)) // BECM module voltage supply
{
datalayer_extended.VolvoPolestar.BECMsupplyVoltage = ((rx_frame.data.u8[4] << 8) | rx_frame.data.u8[5]);
} else if ((rx_frame.data.u8[0] == 0x10) && (rx_frame.data.u8[1] == 0x0B) && (rx_frame.data.u8[2] == 0x62) &&
(rx_frame.data.u8[3] == 0x4B)) // First response frame of cell voltages
{
@ -265,6 +378,10 @@ void handle_incoming_can_frame_battery(CAN_frame rx_frame) {
cell_voltages[battery_request_idx] = (rx_frame.data.u8[7] << 8);
transmit_can_frame(&VOLVO_FlowControl, can_config.battery); // Send flow control
rxConsecutiveFrames = 1;
} else if ((rx_frame.data.u8[0] == 0x10) && (rx_frame.data.u8[2] == 0x59) &&
(rx_frame.data.u8[3] == 0x03)) // First response frame for DTC with more than one code
{
transmit_can_frame(&VOLVO_FlowControl, can_config.battery); // Send flow control
} else if ((rx_frame.data.u8[0] == 0x21) && (rxConsecutiveFrames == 1)) {
cell_voltages[battery_request_idx++] = cell_voltages[battery_request_idx] | rx_frame.data.u8[1];
cell_voltages[battery_request_idx++] = (rx_frame.data.u8[2] << 8) | rx_frame.data.u8[3];
@ -283,7 +400,6 @@ void handle_incoming_can_frame_battery(CAN_frame rx_frame) {
if (min_max_voltage[1] < cell_voltages[cellcounter])
min_max_voltage[1] = cell_voltages[cellcounter];
}
transmit_can_frame(&VOLVO_SOH_Req, can_config.battery); //Send SOH read request
}
rxConsecutiveFrames = 0;
@ -319,8 +435,10 @@ void transmit_can_battery() {
if (datalayer.battery.status.bms_status == ACTIVE) {
datalayer.system.status.battery_allows_contactor_closing = true;
transmit_can_frame(&VOLVO_140_CLOSE, can_config.battery); //Send 0x140 Close contactors message
} else { //datalayer.battery.status.bms_status == FAULT or inverter requested opening contactors
datalayer.system.status.battery_allows_contactor_closing = false;
transmit_can_frame(&VOLVO_140_OPEN, can_config.battery); //Send 0x140 Open contactors message
}
}
if (currentMillis - previousMillis60s >= INTERVAL_60_S) {

View file

@ -523,6 +523,36 @@ typedef struct {
int32_t BMS_voltage_dV = 0;
} DATALAYER_INFO_MEB;
typedef struct {
uint16_t soc_bms = 0;
uint16_t soc_calc = 0;
uint16_t soc_rescaled = 0;
uint16_t soh_bms = 0;
uint16_t BECMsupplyVoltage = 0;
uint16_t BECMBatteryVoltage = 0;
uint16_t BECMBatteryCurrent = 0;
uint16_t BECMUDynMaxLim = 0;
uint16_t BECMUDynMinLim = 0;
uint16_t HvBattPwrLimDcha1 = 0;
uint16_t HvBattPwrLimDchaSoft = 0;
uint16_t HvBattPwrLimDchaSlowAgi = 0;
uint16_t HvBattPwrLimChrgSlowAgi = 0;
uint8_t HVSysRlySts = 0;
uint8_t HVSysDCRlySts1 = 0;
uint8_t HVSysDCRlySts2 = 0;
uint8_t HVSysIsoRMonrSts = 0;
/** User requesting DTC reset via WebUI*/
bool UserRequestDTCreset = false;
/** User requesting DTC readout via WebUI*/
bool UserRequestDTCreadout = false;
/** User requesting BECM reset via WebUI*/
bool UserRequestBECMecuReset = false;
} DATALAYER_INFO_VOLVO_POLESTAR;
typedef struct {
/** uint16_t */
/** Values WIP*/
@ -579,6 +609,7 @@ class DataLayerExtended {
DATALAYER_INFO_TESLA tesla;
DATALAYER_INFO_NISSAN_LEAF nissanleaf;
DATALAYER_INFO_MEB meb;
DATALAYER_INFO_VOLVO_POLESTAR VolvoPolestar;
DATALAYER_INFO_ZOE_PH2 zoePH2;
};

View file

@ -1030,10 +1030,107 @@ String advanced_battery_processor(const String& var) {
content += "<h4>soc max: " + String(datalayer_extended.zoePH2.battery_soc_max) + "</h4>";
#endif //RENAULT_ZOE_GEN2_BATTERY
#ifdef VOLVO_SPA_BATTERY
content += "<h4>BECM reported SOC: " + String(datalayer_extended.VolvoPolestar.soc_bms) + "</h4>";
content += "<h4>Calculated SOC: " + String(datalayer_extended.VolvoPolestar.soc_calc) + "</h4>";
content += "<h4>Rescaled SOC: " + String(datalayer_extended.VolvoPolestar.soc_rescaled / 10) + "</h4>";
content += "<h4>BECM reported SOH: " + String(datalayer_extended.VolvoPolestar.soh_bms) + "</h4>";
content += "<h4>BECM supply voltage: " + String(datalayer_extended.VolvoPolestar.BECMsupplyVoltage) + " mV</h4>";
content += "<h4>HV voltage: " + String(datalayer_extended.VolvoPolestar.BECMBatteryVoltage) + " V</h4>";
content += "<h4>HV current: " + String(datalayer_extended.VolvoPolestar.BECMBatteryCurrent) + " A</h4>";
content += "<h4>Dynamic max voltage: " + String(datalayer_extended.VolvoPolestar.BECMUDynMaxLim) + " V</h4>";
content += "<h4>Dynamic min voltage: " + String(datalayer_extended.VolvoPolestar.BECMUDynMinLim) + " V</h4>";
content +=
"<h4>Discharge power limit 1: " + String(datalayer_extended.VolvoPolestar.HvBattPwrLimDcha1) + " kW</h4>";
content +=
"<h4>Discharge soft power limit: " + String(datalayer_extended.VolvoPolestar.HvBattPwrLimDchaSoft) + " kW</h4>";
content +=
"<h4>Discharge power limit slow aging: " + String(datalayer_extended.VolvoPolestar.HvBattPwrLimDchaSlowAgi) +
" kW</h4>";
content +=
"<h4>Charge power limit slow aging: " + String(datalayer_extended.VolvoPolestar.HvBattPwrLimChrgSlowAgi) +
" kW</h4>";
content += "<h4>HV system relay status: ";
switch (datalayer_extended.VolvoPolestar.HVSysRlySts) {
case 0:
content += String("Open");
break;
case 1:
content += String("Closed");
break;
case 2:
content += String("KeepStatus");
break;
case 3:
content += String("OpenAndRequestActiveDischarge");
break;
default:
content += String("Not valid");
}
content += "</h4><h4>HV system relay status 1: ";
switch (datalayer_extended.VolvoPolestar.HVSysDCRlySts1) {
case 0:
content += String("Open");
break;
case 1:
content += String("Closed");
break;
case 2:
content += String("KeepStatus");
break;
case 3:
content += String("Fault");
break;
default:
content += String("Not valid");
}
content += "</h4><h4>HV system relay status 2: ";
switch (datalayer_extended.VolvoPolestar.HVSysDCRlySts2) {
case 0:
content += String("Open");
break;
case 1:
content += String("Closed");
break;
case 2:
content += String("KeepStatus");
break;
case 3:
content += String("Fault");
break;
default:
content += String("Not valid");
}
content += "</h4><h4>HV system isolation resistance monitoring status: ";
switch (datalayer_extended.VolvoPolestar.HVSysIsoRMonrSts) {
case 0:
content += String("Not valid 1");
break;
case 1:
content += String("False");
break;
case 2:
content += String("True");
break;
case 3:
content += String("Not valid 2");
break;
default:
content += String("Not valid");
}
content += "<br><br><button onclick='Volvo_askEraseDTC()'>Erase DTC</button><br>";
content += "<button onclick='Volvo_askReadDTC()'>Read DTC (result must be checked in CANlog)</button><br>";
content += "<button onclick='Volvo_BECMecuReset()'>Restart BECM module</button>";
#endif // VOLVO_SPA_BATTERY
#if !defined(BMW_IX_BATTERY) && !defined(BOLT_AMPERA_BATTERY) && !defined(TESLA_BATTERY) && \
!defined(NISSAN_LEAF_BATTERY) && !defined(BMW_I3_BATTERY) && !defined(BYD_ATTO_3_BATTERY) && \
!defined(RENAULT_ZOE_GEN2_BATTERY) && !defined(CELLPOWER_BMS) && \
!defined(MEB_BATTERY) // Only the listed types have extra info
!defined(RENAULT_ZOE_GEN2_BATTERY) && !defined(CELLPOWER_BMS) && !defined(MEB_BATTERY) && \
!defined(VOLVO_SPA_BATTERY) //Only the listed types have extra info
content += "No extra information available for this battery type";
#endif
@ -1051,7 +1148,40 @@ String advanced_battery_processor(const String& var) {
content += "}";
content += "function goToMainPage() { window.location.href = '/'; }";
content += "</script>";
return content;
content += "<script>";
content +=
"function Volvo_askEraseDTC() { if (window.confirm('Are you sure you want to erase DTCs?')) { "
"volvoEraseDTC(); } }";
content += "function volvoEraseDTC() {";
content += " var xhr = new XMLHttpRequest();";
content += " xhr.open('GET', '/volvoEraseDTC', true);";
content += " xhr.send();";
content += "}";
content += "function goToMainPage() { window.location.href = '/'; }";
content += "</script>";
content += "<script>";
content += "function Volvo_askReadDTC() { volvoReadDTC(); } ";
content += "function volvoReadDTC() {";
content += " var xhr = new XMLHttpRequest();";
content += " xhr.open('GET', '/volvoReadDTC', true);";
content += " xhr.send();";
content += "}";
content += "function goToMainPage() { window.location.href = '/'; }";
content += "</script>";
content += "<script>";
content +=
"function Volvo_BECMecuReset() { if (window.confirm('Are you sure you want to restart BECM?')) { "
"volvoBECMecuReset(); } }";
content += "function volvoBECMecuReset() {";
content += " var xhr = new XMLHttpRequest();";
content += " xhr.open('GET', '/volvoBECMecuReset', true);";
content += " xhr.send();";
content += "}";
content += "function goToMainPage() { window.location.href = '/'; }";
content += "</script>";
}
return String();
}

View file

@ -394,6 +394,33 @@ void init_webserver() {
request->send(200, "text/plain", "Updated successfully");
});
// Route for erasing DTC on Volvo/Polestar batteries
server.on("/volvoEraseDTC", HTTP_GET, [](AsyncWebServerRequest* request) {
if (WEBSERVER_AUTH_REQUIRED && !request->authenticate(http_username, http_password)) {
return request->requestAuthentication();
}
datalayer_extended.VolvoPolestar.UserRequestDTCreset = true;
request->send(200, "text/plain", "Updated successfully");
});
// Route for reading DTC on Volvo/Polestar batteries
server.on("/volvoReadDTC", HTTP_GET, [](AsyncWebServerRequest* request) {
if (WEBSERVER_AUTH_REQUIRED && !request->authenticate(http_username, http_password)) {
return request->requestAuthentication();
}
datalayer_extended.VolvoPolestar.UserRequestDTCreadout = true;
request->send(200, "text/plain", "Updated successfully");
});
// Route for performing ECU reset on Volvo/Polestar batteries
server.on("/volvoBECMecuReset", HTTP_GET, [](AsyncWebServerRequest* request) {
if (WEBSERVER_AUTH_REQUIRED && !request->authenticate(http_username, http_password)) {
return request->requestAuthentication();
}
datalayer_extended.VolvoPolestar.UserRequestBECMecuReset = true;
request->send(200, "text/plain", "Updated successfully");
});
#ifdef TEST_FAKE_BATTERY
// Route for editing FakeBatteryVoltage
server.on("/updateFakeBatteryVoltage", HTTP_GET, [](AsyncWebServerRequest* request) {