Make CAN alive define instead of magic number

This commit is contained in:
Daniel 2024-05-13 21:42:33 +03:00
parent 8fa17489c1
commit ca9322aa81
14 changed files with 30 additions and 23 deletions

View file

@ -528,7 +528,7 @@ void receive_can_battery(CAN_frame_t rx_frame) {
case 0x112: //BMS [10ms] Status Of High-Voltage Battery - 2 case 0x112: //BMS [10ms] Status Of High-Voltage Battery - 2
battery_awake = true; battery_awake = true;
datalayer.battery.status.CAN_battery_still_alive = datalayer.battery.status.CAN_battery_still_alive =
12; //This message is only sent if 30C (Wakeup pin on battery) is energized with 12V CAN_STILL_ALIVE; //This message is only sent if 30C (Wakeup pin on battery) is energized with 12V
battery_current = (rx_frame.data.u8[1] << 8 | rx_frame.data.u8[0]) - 8192; //deciAmps (-819.2 to 819.0A) battery_current = (rx_frame.data.u8[1] << 8 | rx_frame.data.u8[0]) - 8192; //deciAmps (-819.2 to 819.0A)
battery_volts = (rx_frame.data.u8[3] << 8 | rx_frame.data.u8[2]); //500.0 V battery_volts = (rx_frame.data.u8[3] << 8 | rx_frame.data.u8[2]); //500.0 V
battery_HVBatt_SOC = ((rx_frame.data.u8[5] & 0x0F) << 8 | rx_frame.data.u8[4]); battery_HVBatt_SOC = ((rx_frame.data.u8[5] & 0x0F) << 8 | rx_frame.data.u8[4]);

View file

@ -111,8 +111,8 @@ void update_values_battery() { //This function maps all the values fetched via
} }
void receive_can_battery(CAN_frame_t rx_frame) { void receive_can_battery(CAN_frame_t rx_frame) {
datalayer.battery.status.CAN_battery_still_alive == datalayer.battery.status.CAN_battery_still_alive =
12; //We are getting CAN messages from the vehicle, inform the watchdog CAN_STILL_ALIVE; //We are getting CAN messages from the vehicle, inform the watchdog
switch (rx_frame.MsgID) { switch (rx_frame.MsgID) {
case 0x100: case 0x100:

View file

@ -136,7 +136,7 @@ void update_values_battery() { //This function maps all the values fetched via
void receive_can_battery(CAN_frame_t rx_frame) { void receive_can_battery(CAN_frame_t rx_frame) {
datalayer.battery.status.CAN_battery_still_alive = datalayer.battery.status.CAN_battery_still_alive =
12; //TODO: move this inside a known message ID to prevent CAN inverter from keeping battery alive detection going CAN_STILL_ALIVE; //TODO: move this inside a known message ID to prevent CAN inverter from keeping battery alive detection going
switch (rx_frame.MsgID) { switch (rx_frame.MsgID) {
case 0x374: //BMU message, 10ms - SOC case 0x374: //BMU message, 10ms - SOC
temp_value = ((rx_frame.data.u8[1] - 10) / 2); temp_value = ((rx_frame.data.u8[1] - 10) / 2);

View file

@ -198,7 +198,7 @@ void send_canfd_frame(CANFDMessage frame) {
} }
void receive_canfd_battery(CANFDMessage frame) { void receive_canfd_battery(CANFDMessage frame) {
datalayer.battery.status.CAN_battery_still_alive = 12; datalayer.battery.status.CAN_battery_still_alive = CAN_STILL_ALIVE;
switch (frame.id) { switch (frame.id) {
case 0x7EC: case 0x7EC:
// print_canfd_frame(frame); // print_canfd_frame(frame);

View file

@ -287,8 +287,8 @@ void receive_can_battery(CAN_frame_t rx_frame) {
break; break;
case 0x542: //BMS SOC case 0x542: //BMS SOC
startedUp = true; startedUp = true;
datalayer.battery.status.CAN_battery_still_alive = 12; //We use this message to verify that BMS is still alive datalayer.battery.status.CAN_battery_still_alive = CAN_STILL_ALIVE;
SOC_Display = rx_frame.data.u8[0] * 5; //100% = 200 ( 200 * 5 = 1000 ) SOC_Display = rx_frame.data.u8[0] * 5; //100% = 200 ( 200 * 5 = 1000 )
break; break;
case 0x594: case 0x594:
startedUp = true; startedUp = true;

View file

@ -129,7 +129,7 @@ static bool Batt_Heater_Mail_Send_Request = false; //Stores info when a heat re
static uint8_t battery_request_idx = 0; static uint8_t battery_request_idx = 0;
static uint8_t group_7bb = 0; static uint8_t group_7bb = 0;
static uint8_t group = 1; static uint8_t group = 1;
static uint8_t stop_battery_query = 1; static bool stop_battery_query = true;
static uint8_t hold_off_with_polling_10seconds = 10; static uint8_t hold_off_with_polling_10seconds = 10;
static uint16_t cell_voltages[97]; //array with all the cellvoltages static uint16_t cell_voltages[97]; //array with all the cellvoltages
static uint8_t cellcounter = 0; static uint8_t cellcounter = 0;
@ -387,8 +387,7 @@ void receive_can_battery(CAN_frame_t rx_frame) {
LB_Capacity_Empty = (bool)((rx_frame.data.u8[6] & 0x80) >> 7); LB_Capacity_Empty = (bool)((rx_frame.data.u8[6] & 0x80) >> 7);
break; break;
case 0x5BC: case 0x5BC:
datalayer.battery.status.CAN_battery_still_alive = datalayer.battery.status.CAN_battery_still_alive = CAN_STILL_ALIVE; // Let system know battery is sending CAN
12; //Indicate that we are still getting CAN messages from the BMS
LB_MAX = ((rx_frame.data.u8[5] & 0x10) >> 4); LB_MAX = ((rx_frame.data.u8[5] & 0x10) >> 4);
if (LB_MAX) { if (LB_MAX) {
@ -441,7 +440,7 @@ void receive_can_battery(CAN_frame_t rx_frame) {
LEAF_Battery_Type = ZE1_BATTERY; LEAF_Battery_Type = ZE1_BATTERY;
break; break;
case 0x79B: case 0x79B:
stop_battery_query = 1; //Someone is trying to read data with Leafspy, stop our own polling! stop_battery_query = true; //Someone is trying to read data with Leafspy, stop our own polling!
hold_off_with_polling_10seconds = 10; //Polling is paused for 100s hold_off_with_polling_10seconds = 10; //Polling is paused for 100s
break; break;
case 0x7BB: case 0x7BB:
@ -766,7 +765,7 @@ void send_can_battery() {
if (hold_off_with_polling_10seconds > 0) { if (hold_off_with_polling_10seconds > 0) {
hold_off_with_polling_10seconds--; hold_off_with_polling_10seconds--;
} else { } else {
stop_battery_query = 0; stop_battery_query = false;
} }
} }
} }

View file

@ -98,7 +98,7 @@ void update_values_battery() {
} }
void receive_can_battery(CAN_frame_t rx_frame) { void receive_can_battery(CAN_frame_t rx_frame) {
datalayer.battery.status.CAN_battery_still_alive = 12; datalayer.battery.status.CAN_battery_still_alive = CAN_STILL_ALIVE;
switch (rx_frame.MsgID) { switch (rx_frame.MsgID) {
case 0x7310: case 0x7310:
case 0x7311: case 0x7311:

View file

@ -174,14 +174,14 @@ void receive_can_battery(CAN_frame_t rx_frame) {
switch (rx_frame.MsgID) { switch (rx_frame.MsgID) {
case 0x155: //BMS1 case 0x155: //BMS1
datalayer.battery.status.CAN_battery_still_alive = datalayer.battery.status.CAN_battery_still_alive =
12; //Indicate that we are still getting CAN messages from the BMS CAN_STILL_ALIVE; //Indicate that we are still getting CAN messages from the BMS
LB_MaxChargeAllowed_W = (rx_frame.data.u8[0] * 300); LB_MaxChargeAllowed_W = (rx_frame.data.u8[0] * 300);
LB_Current = word((rx_frame.data.u8[1] & 0xF), rx_frame.data.u8[2]) * 0.25 - 500; //OK! LB_Current = word((rx_frame.data.u8[1] & 0xF), rx_frame.data.u8[2]) * 0.25 - 500; //OK!
LB_SOC = ((rx_frame.data.u8[4] << 8) | (rx_frame.data.u8[5])) * 0.0025; //OK! LB_SOC = ((rx_frame.data.u8[4] << 8) | (rx_frame.data.u8[5])) * 0.0025; //OK!
break; break;
case 0x424: //BMS2 case 0x424: //BMS2
datalayer.battery.status.CAN_battery_still_alive = datalayer.battery.status.CAN_battery_still_alive =
12; //Indicate that we are still getting CAN messages from the BMS CAN_STILL_ALIVE; //Indicate that we are still getting CAN messages from the BMS
LB_EOCR = (rx_frame.data.u8[0] & 0x03); LB_EOCR = (rx_frame.data.u8[0] & 0x03);
LB_HVBUV = (rx_frame.data.u8[0] & 0x0C) >> 2; LB_HVBUV = (rx_frame.data.u8[0] & 0x0C) >> 2;
LB_HVBIR = (rx_frame.data.u8[0] & 0x30) >> 4; LB_HVBIR = (rx_frame.data.u8[0] & 0x30) >> 4;
@ -198,12 +198,12 @@ void receive_can_battery(CAN_frame_t rx_frame) {
break; break;
case 0x425: case 0x425:
datalayer.battery.status.CAN_battery_still_alive = datalayer.battery.status.CAN_battery_still_alive =
12; //Indicate that we are still getting CAN messages from the BMS CAN_STILL_ALIVE; //Indicate that we are still getting CAN messages from the BMS
LB_kWh_Remaining = word((rx_frame.data.u8[0] & 0x1), rx_frame.data.u8[1]) / 10; //OK! LB_kWh_Remaining = word((rx_frame.data.u8[0] & 0x1), rx_frame.data.u8[1]) / 10; //OK!
break; break;
case 0x445: case 0x445:
datalayer.battery.status.CAN_battery_still_alive = datalayer.battery.status.CAN_battery_still_alive =
12; //Indicate that we are still getting CAN messages from the BMS CAN_STILL_ALIVE; //Indicate that we are still getting CAN messages from the BMS
LB_Cell_Max_Voltage = 1000 + word((rx_frame.data.u8[3] & 0x1), rx_frame.data.u8[4]) * 10; //OK! LB_Cell_Max_Voltage = 1000 + word((rx_frame.data.u8[3] & 0x1), rx_frame.data.u8[4]) * 10; //OK!
LB_Cell_Min_Voltage = 1000 + (word(rx_frame.data.u8[5], rx_frame.data.u8[6]) >> 7) * 10; //OK! LB_Cell_Min_Voltage = 1000 + (word(rx_frame.data.u8[5], rx_frame.data.u8[6]) >> 7) * 10; //OK!
@ -215,7 +215,7 @@ void receive_can_battery(CAN_frame_t rx_frame) {
break; break;
case 0x7BB: case 0x7BB:
datalayer.battery.status.CAN_battery_still_alive = datalayer.battery.status.CAN_battery_still_alive =
12; //Indicate that we are still getting CAN messages from the BMS CAN_STILL_ALIVE; //Indicate that we are still getting CAN messages from the BMS
if (rx_frame.data.u8[0] == 0x10) { //1st response Bytes 0-7 if (rx_frame.data.u8[0] == 0x10) { //1st response Bytes 0-7
GVB_79B_Continue = true; GVB_79B_Continue = true;

View file

@ -105,7 +105,7 @@ void update_values_battery() { //This function maps all the values fetched via
} }
void receive_can_battery(CAN_frame_t rx_frame) { void receive_can_battery(CAN_frame_t rx_frame) {
datalayer.battery.status.CAN_battery_still_alive = 12; datalayer.battery.status.CAN_battery_still_alive = CAN_STILL_ALIVE;
switch (rx_frame.MsgID) { switch (rx_frame.MsgID) {
case 0x42E: //HV SOC & Battery Temp & Charging Power case 0x42E: //HV SOC & Battery Temp & Charging Power
break; break;

View file

@ -82,7 +82,7 @@ void update_values_battery() { //This function maps all the values fetched via
} }
void receive_can_battery(CAN_frame_t rx_frame) { void receive_can_battery(CAN_frame_t rx_frame) {
datalayer.battery.status.CAN_battery_still_alive = 12; datalayer.battery.status.CAN_battery_still_alive = CAN_STILL_ALIVE;
switch (rx_frame.MsgID) { switch (rx_frame.MsgID) {
case 0x200: case 0x200:
break; break;

View file

@ -504,7 +504,7 @@ void receive_can_battery(CAN_frame_t rx_frame) {
output_current = (((rx_frame.data.u8[4] & 0x0F) << 8) | rx_frame.data.u8[3]) / 100; output_current = (((rx_frame.data.u8[4] & 0x0F) << 8) | rx_frame.data.u8[3]) / 100;
break; break;
case 0x292: case 0x292:
datalayer.battery.status.CAN_battery_still_alive = 12; //We are getting CAN messages from the BMS datalayer.battery.status.CAN_battery_still_alive = CAN_STILL_ALIVE; //We are getting CAN messages from the BMS
bat_beginning_of_life = (((rx_frame.data.u8[6] & 0x03) << 8) | rx_frame.data.u8[5]); bat_beginning_of_life = (((rx_frame.data.u8[6] & 0x03) << 8) | rx_frame.data.u8[5]);
soc_min = (((rx_frame.data.u8[1] & 0x03) << 8) | rx_frame.data.u8[0]); soc_min = (((rx_frame.data.u8[1] & 0x03) << 8) | rx_frame.data.u8[0]);
soc_vi = (((rx_frame.data.u8[2] & 0x0F) << 6) | ((rx_frame.data.u8[1] & 0xFC) >> 2)); soc_vi = (((rx_frame.data.u8[2] & 0x0F) << 6) | ((rx_frame.data.u8[1] & 0xFC) >> 2));

View file

@ -16,6 +16,7 @@ void print_units(char* header, int value, char* units) {
} }
void update_values_battery() { /* This function puts fake values onto the parameters sent towards the inverter */ void update_values_battery() { /* This function puts fake values onto the parameters sent towards the inverter */
datalayer.battery.status.real_soc = 5000; // 50.00% datalayer.battery.status.real_soc = 5000; // 50.00%
datalayer.battery.status.soh_pptt = 9900; // 99.00% datalayer.battery.status.soh_pptt = 9900; // 99.00%
@ -46,6 +47,9 @@ void update_values_battery() { /* This function puts fake values onto the parame
datalayer.battery.status.cell_voltages_mV[i] = 3500 + i; datalayer.battery.status.cell_voltages_mV[i] = 3500 + i;
} }
//Fake that we get CAN messages
datalayer.battery.status.CAN_battery_still_alive = CAN_STILL_ALIVE;
/*Finally print out values to serial if configured to do so*/ /*Finally print out values to serial if configured to do so*/
#ifdef DEBUG_VIA_USB #ifdef DEBUG_VIA_USB
Serial.println("FAKE Values going to inverter"); Serial.println("FAKE Values going to inverter");
@ -62,7 +66,9 @@ void update_values_battery() { /* This function puts fake values onto the parame
#endif #endif
} }
void receive_can_battery(CAN_frame_t rx_frame) { // All CAN messages recieved will be logged via serial void receive_can_battery(CAN_frame_t rx_frame) {
datalayer.battery.status.CAN_battery_still_alive = CAN_STILL_ALIVE;
// All CAN messages recieved will be logged via serial
Serial.print(millis()); // Example printout, time, ID, length, data: 7553 1DB 8 FF C0 B9 EA 0 0 2 5D Serial.print(millis()); // Example printout, time, ID, length, data: 7553 1DB 8 FF C0 B9 EA 0 0 2 5D
Serial.print(" "); Serial.print(" ");
Serial.print(rx_frame.MsgID, HEX); Serial.print(rx_frame.MsgID, HEX);

View file

@ -157,7 +157,7 @@ void update_values_battery() { //This function maps all the values fetched via
} }
void receive_can_battery(CAN_frame_t rx_frame) { void receive_can_battery(CAN_frame_t rx_frame) {
datalayer.battery.status.CAN_battery_still_alive = 12; datalayer.battery.status.CAN_battery_still_alive = CAN_STILL_ALIVE;
switch (rx_frame.MsgID) { switch (rx_frame.MsgID) {
case 0x3A: case 0x3A:
if ((rx_frame.data.u8[6] & 0x80) == 0x80) if ((rx_frame.data.u8[6] & 0x80) == 0x80)

View file

@ -28,4 +28,6 @@ enum led_color { GREEN, YELLOW, RED, BLUE, RGB };
#define INTERVAL_100_MS_DELAYED 120 #define INTERVAL_100_MS_DELAYED 120
#define INTERVAL_500_MS_DELAYED 550 #define INTERVAL_500_MS_DELAYED 550
#define CAN_STILL_ALIVE 12
#endif #endif