Merge pull request #716 from josiahhiggs/enhancement/tesla-safety-check

Enhancement/tesla safety check
This commit is contained in:
Daniel Öster 2024-12-30 17:42:29 +03:00 committed by GitHub
commit 22e49abb6d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -8,8 +8,8 @@
/* Do not change code below unless you are sure what you are doing */
/* Credits: Most of the code comes from Per Carlen's bms_comms_tesla_model3.py (https://gitlab.com/pelle8/batt2gen24/) */
static unsigned long previousMillis30 = 0; // will store last time a 30ms CAN Message was send
static unsigned long previousMillis50 = 0; // will store last time a 50ms CAN Message was send
//0x221 545 VCFRONT_LVPowerState: "GenMsgCycleTime" 50ms
CAN_frame TESLA_221_1 = {
.FD = false,
.ext_ID = false,
@ -24,27 +24,28 @@ CAN_frame TESLA_221_2 = {
.data = {0x61, 0x15, 0x01, 0x00, 0x00, 0x00, 0x20, 0xBA}}; //Contactor Frame 221 - hv_up_for_drive
static uint16_t sendContactorClosingMessagesStill = 300;
static uint16_t battery_cell_max_v = 3700;
static uint16_t battery_cell_min_v = 3700;
static uint16_t battery_cell_max_v = 3300;
static uint16_t battery_cell_min_v = 3300;
static uint16_t battery_cell_deviation_mV = 0; //contains the deviation between highest and lowest cell in mV
static bool cellvoltagesRead = false;
//0x3d2: 978 BMS_kwhCounter
static uint32_t battery_total_discharge = 0;
static uint32_t battery_total_charge = 0;
//0x352: 850 BMS_energyStatus
static uint16_t battery_energy_buffer = 0; // kWh
static uint16_t battery_energy_buffer_m1 = 0; // kWh
static uint16_t battery_energy_to_charge_complete = 0; // kWh
static uint16_t battery_energy_to_charge_complete_m1 = 0; // kWh
static uint16_t battery_expected_energy_remaining = 0; // kWh
static uint16_t battery_expected_energy_remaining_m1 = 0; // kWh
static bool battery_full_charge_complete = false; // Changed to bool
static bool battery_fully_charged = false; // Changed to bool
static uint16_t battery_ideal_energy_remaining = 0; // kWh
static uint16_t battery_ideal_energy_remaining_m0 = 0; // kWh
static uint16_t battery_nominal_energy_remaining = 0; // kWh
static uint16_t battery_nominal_energy_remaining_m0 = 0; // kWh
static uint16_t battery_nominal_full_pack_energy = 600; // Kwh
static uint16_t battery_nominal_full_pack_energy_m0 = 600; // Kwh
static uint16_t battery_energy_buffer = 0; // kWh
static uint16_t battery_energy_buffer_m1 = 0; // kWh
static uint16_t battery_energy_to_charge_complete = 0; // kWh
static uint16_t battery_energy_to_charge_complete_m1 = 0; // kWh
static uint16_t battery_expected_energy_remaining = 0; // kWh
static uint16_t battery_expected_energy_remaining_m1 = 0; // kWh
static bool battery_full_charge_complete = false; // Changed to bool
static bool battery_fully_charged = false; // Changed to bool
static uint16_t battery_ideal_energy_remaining = 0; // kWh
static uint16_t battery_ideal_energy_remaining_m0 = 0; // kWh
static uint16_t battery_nominal_energy_remaining = 0; // kWh
static uint16_t battery_nominal_energy_remaining_m0 = 0; // kWh
static uint16_t battery_nominal_full_pack_energy = 0; // Kwh
static uint16_t battery_nominal_full_pack_energy_m0 = 0; // Kwh
//0x132 306 HVBattAmpVolt
static uint16_t battery_volts = 0; // V
static int16_t battery_amps = 0; // A
@ -69,7 +70,7 @@ static uint16_t battery_dcdcLvBusVolt = 0; // Change name from battery_low_volt
static uint16_t battery_dcdcLvOutputCurrent =
0; // Change name from battery_output_current to battery_dcdcLvOutputCurrent
//0x292: 658 BMS_socStatus
static uint16_t battery_beginning_of_life = 600; // kWh
static uint16_t battery_beginning_of_life = 0; // kWh
static uint16_t battery_soc_min = 0;
static uint16_t battery_soc_max = 0;
static uint16_t battery_soc_ui = 0; //Change name from battery_soc_vi to reflect DBC battery_soc_ui
@ -408,9 +409,10 @@ static bool battery_BMS_a180_SW_ECU_reset_blocked = false;
#ifdef DOUBLE_BATTERY //need to update for battery2
static uint16_t battery2_cell_max_v = 3700;
static uint16_t battery2_cell_min_v = 3700;
static uint16_t battery2_cell_max_v = 3300;
static uint16_t battery2_cell_min_v = 3300;
static uint16_t battery2_cell_deviation_mV = 0; //contains the deviation between highest and lowest cell in mV
static bool battery2_cellvoltagesRead = false;
//0x3d2: 978 BMS_kwhCounter
static uint32_t battery2_total_discharge = 0;
static uint32_t battery2_total_charge = 0;
@ -427,8 +429,8 @@ static uint16_t battery2_ideal_energy_remaining = 0;
static uint16_t battery2_ideal_energy_remaining_m0 = 0; // kWh
static uint16_t battery2_nominal_energy_remaining = 0;
static uint16_t battery2_nominal_energy_remaining_m0 = 0; // kWh
static uint16_t battery2_nominal_full_pack_energy = 600;
static uint16_t battery2_nominal_full_pack_energy_m0 = 600; // Kwh
static uint16_t battery2_nominal_full_pack_energy = 0;
static uint16_t battery2_nominal_full_pack_energy_m0 = 0; // Kwh
//0x132 306 HVBattAmpVolt
static uint16_t battery2_volts = 0; // V
static int16_t battery2_amps = 0; // A
@ -452,7 +454,7 @@ static uint16_t battery2_dcdcHvBusVolt = 0; //update name
static uint16_t battery2_dcdcLvBusVolt = 0; //update name
static uint16_t battery2_dcdcLvOutputCurrent = 0; //update name
//0x292: 658 BMS_socStatus
static uint16_t battery2_beginning_of_life = 600;
static uint16_t battery2_beginning_of_life = 0;
static uint16_t battery2_soc_min = 0;
static uint16_t battery2_soc_max = 0;
static uint16_t battery2_soc_ui = 0;
@ -1346,6 +1348,7 @@ void handle_incoming_can_frame_battery(CAN_frame rx_frame) {
temp = ((rx_frame.data.u8[3] << 8) | rx_frame.data.u8[2]);
temp = (temp & 0xFFF);
battery_cell_min_v = temp * 2;
cellvoltagesRead = true;
//BattBrickVoltageMax m1 : 2|12@1+ (0.002,0) [0|0] "V" Receiver ((_d[1] & (0x3FU)) << 6) | ((_d[0] >> 2) & (0x3FU));
battery_BrickVoltageMax =
((rx_frame.data.u8[1] & (0x3F)) << 6) | ((rx_frame.data.u8[0] >> 2) & (0x3F)); //to datalayer_extended
@ -1991,6 +1994,7 @@ void handle_incoming_can_frame_battery2(CAN_frame rx_frame) {
temp = ((rx_frame.data.u8[3] << 8) | rx_frame.data.u8[2]);
temp = (temp & 0xFFF);
battery2_cell_min_v = temp * 2;
battery2_cellvoltagesRead = true;
//BattBrickVoltageMax m1 : 2|12@1+ (0.002,0) [0|0] "V" Receiver ((_d[1] & (0x3FU)) << 6) | ((_d[0] >> 2) & (0x3FU));
battery2_BrickVoltageMax =
((rx_frame.data.u8[1] & (0x3F)) << 6) | ((rx_frame.data.u8[0] >> 2) & (0x3F)); //to datalayer_extended
@ -2629,6 +2633,10 @@ the first, for a few cycles, then stop all messages which causes the contactor
unsigned long currentMillis = millis();
if (!cellvoltagesRead) {
return; //All cellvoltages not read yet, do not proceed with contactor closing
}
#if defined(TESLA_MODEL_SX_BATTERY) || defined(EXP_TESLA_BMS_DIGITAL_HVIL)
if ((datalayer.system.status.inverter_allows_contactor_closing) && (datalayer.battery.status.bms_status != FAULT)) {
if (currentMillis - lastSend1CF >= 10) {
@ -2658,14 +2666,14 @@ the first, for a few cycles, then stop all messages which causes the contactor
#endif //defined(TESLA_MODEL_SX_BATTERY) || defined(EXP_TESLA_BMS_DIGITAL_HVIL)
//Send 30ms message
if (currentMillis - previousMillis30 >= INTERVAL_30_MS) {
if (currentMillis - previousMillis50 >= INTERVAL_50_MS) {
// Check if sending of CAN messages has been delayed too much.
if ((currentMillis - previousMillis30 >= INTERVAL_30_MS_DELAYED) && (currentMillis > BOOTUP_TIME)) {
set_event(EVENT_CAN_OVERRUN, (currentMillis - previousMillis30));
if ((currentMillis - previousMillis50 >= INTERVAL_50_MS_DELAYED) && (currentMillis > BOOTUP_TIME)) {
set_event(EVENT_CAN_OVERRUN, (currentMillis - previousMillis50));
} else {
clear_event(EVENT_CAN_OVERRUN);
}
previousMillis30 = currentMillis;
previousMillis50 = currentMillis;
if ((datalayer.system.status.inverter_allows_contactor_closing == true) &&
(datalayer.battery.status.bms_status != FAULT)) {