Refactor CAN interval parameters

This commit is contained in:
Daniel 2024-03-28 18:05:02 +02:00
parent d0efc4f3fc
commit df374d67ce
12 changed files with 77 additions and 95 deletions

View file

@ -14,15 +14,6 @@ static unsigned long previousMillis640 = 0; // will store last time a 600ms C
static unsigned long previousMillis1000 = 0; // will store last time a 1000ms CAN Message was send
static unsigned long previousMillis5000 = 0; // will store last time a 5000ms CAN Message was send
static unsigned long previousMillis10000 = 0; // will store last time a 10000ms CAN Message was send
static const uint8_t interval20 = 20; // interval (ms) at which send CAN Messages
static const uint8_t interval100 = 100; // interval (ms) at which send CAN Messages
static const uint8_t interval200 = 200; // interval (ms) at which send CAN Messages
static const uint16_t interval500 = 500; // interval (ms) at which send CAN Messages
static const uint16_t interval640 = 640; // interval (ms) at which send CAN Messages
static const uint16_t interval1000 = 1000; // interval (ms) at which send CAN Messages
static const uint16_t interval5000 = 5000; // interval (ms) at which send CAN Messages
static const uint16_t interval10000 = 10000; // interval (ms) at which send CAN Messages
static const uint8_t interval20overrun = 30; // interval (ms) at when a 20ms CAN send is considered delayed
static uint8_t CANstillAlive = 12; // counter for checking if CAN is still alive
static uint16_t CANerror = 0; // counter on how many CAN errors encountered
#define MAX_CAN_FAILURES 500 // Amount of malformed CAN messages to allow before raising a warning
@ -562,9 +553,9 @@ void send_can_battery() {
if (battery_awake) {
//Send 20ms message
if (currentMillis - previousMillis20 >= interval20) {
if (currentMillis - previousMillis20 >= INTERVAL_20_MS) {
// Check if sending of CAN messages has been delayed too much.
if ((currentMillis - previousMillis20 >= interval20overrun) && (currentMillis > 1000)) {
if ((currentMillis - previousMillis20 >= INTERVAL_20_MS_DELAYED) && (currentMillis > BOOTUP_TIME)) {
set_event(EVENT_CAN_OVERRUN, (currentMillis - previousMillis20));
}
previousMillis20 = currentMillis;
@ -590,7 +581,7 @@ void send_can_battery() {
ESP32Can.CANWriteFrame(&BMW_10B);
}
// Send 100ms CAN Message
if (currentMillis - previousMillis100 >= interval100) {
if (currentMillis - previousMillis100 >= INTERVAL_100_MS) {
previousMillis100 = currentMillis;
BMW_12F.data.u8[1] = ((BMW_12F.data.u8[1] & 0xF0) + alive_counter_100ms);
@ -601,7 +592,7 @@ void send_can_battery() {
ESP32Can.CANWriteFrame(&BMW_12F);
}
// Send 200ms CAN Message
if (currentMillis - previousMillis200 >= interval200) {
if (currentMillis - previousMillis200 >= INTERVAL_200_MS) {
previousMillis200 = currentMillis;
BMW_19B.data.u8[1] = ((BMW_19B.data.u8[1] & 0xF0) + alive_counter_200ms);
@ -612,7 +603,7 @@ void send_can_battery() {
ESP32Can.CANWriteFrame(&BMW_19B);
}
// Send 500ms CAN Message
if (currentMillis - previousMillis500 >= interval500) {
if (currentMillis - previousMillis500 >= INTERVAL_500_MS) {
previousMillis500 = currentMillis;
BMW_30B.data.u8[1] = ((BMW_30B.data.u8[1] & 0xF0) + alive_counter_500ms);
@ -623,14 +614,14 @@ void send_can_battery() {
ESP32Can.CANWriteFrame(&BMW_30B);
}
// Send 640ms CAN Message
if (currentMillis - previousMillis640 >= interval640) {
if (currentMillis - previousMillis640 >= INTERVAL_640_MS) {
previousMillis640 = currentMillis;
ESP32Can.CANWriteFrame(&BMW_512); // Keep BMS alive
ESP32Can.CANWriteFrame(&BMW_5F8);
}
// Send 1000ms CAN Message
if (currentMillis - previousMillis1000 >= interval1000) {
if (currentMillis - previousMillis1000 >= INTERVAL_1_S) {
previousMillis1000 = currentMillis;
BMW_328_counter++; // Used to increment seconds
@ -674,7 +665,7 @@ void send_can_battery() {
BMW_3E8.data.u8[0] = 0xF1; // First 3E8 message byte0 we send is unique, once we sent initial value send this
}
// Send 5000ms CAN Message
if (currentMillis - previousMillis5000 >= interval5000) {
if (currentMillis - previousMillis5000 >= INTERVAL_5_S) {
previousMillis5000 = currentMillis;
BMW_3FC.data.u8[1] = ((BMW_3FC.data.u8[1] & 0xF0) + alive_counter_5000ms);
@ -694,7 +685,7 @@ void send_can_battery() {
}
}
// Send 10000ms CAN Message
if (currentMillis - previousMillis10000 >= interval10000) {
if (currentMillis - previousMillis10000 >= INTERVAL_10_S) {
previousMillis10000 = currentMillis;
ESP32Can.CANWriteFrame(&BMW_3E5); //Order comes from CAN logs

View file

@ -7,9 +7,6 @@
/* Do not change code below unless you are sure what you are doing */
static unsigned long previousMillis100 = 0; // will store last time a 100ms CAN Message was send
static const uint8_t interval100 = 100; // interval (ms) at which send CAN Messages
static const uint8_t interval100overrun = 120; // interval (ms) at when a 100ms CAN send is considered delayed
const int rx_queue_size = 10; // Receive Queue size
static uint8_t CANstillAlive = 12; //counter for checking if CAN is still alive
static uint8_t errorCode = 0; //stores if we have an error code active from battery control logic
@ -195,9 +192,9 @@ void receive_can_battery(CAN_frame_t rx_frame) {
void send_can_battery() {
unsigned long currentMillis = millis();
// Send 100ms CAN Message
if (currentMillis - previousMillis100 >= interval100) {
if (currentMillis - previousMillis100 >= INTERVAL_100_MS) {
// Check if sending of CAN messages has been delayed too much.
if ((currentMillis - previousMillis100 >= interval100overrun) && (currentMillis > 1000)) {
if ((currentMillis - previousMillis100 >= INTERVAL_100_MS_DELAYED) && (currentMillis > BOOTUP_TIME)) {
set_event(EVENT_CAN_OVERRUN, (currentMillis - previousMillis100));
}
previousMillis100 = currentMillis;

View file

@ -16,8 +16,6 @@ static uint8_t CMU_Detected = 0;
static unsigned long previousMillis10 = 0; // will store last time a 10ms CAN Message was sent
static unsigned long previousMillis100 = 0; // will store last time a 100ms CAN Message was sent
static const uint8_t interval100 = 100; // interval (ms) at which send CAN Messages
static const uint8_t interval100overrun = 120; // interval (ms) at when a 100ms CAN send is considered delayed
static int pid_index = 0;
static int cmu_id = 0;
@ -227,9 +225,9 @@ void receive_can_battery(CAN_frame_t rx_frame) {
void send_can_battery() {
unsigned long currentMillis = millis();
// Send 100ms CAN Message
if (currentMillis - previousMillis100 >= interval100) {
if (currentMillis - previousMillis100 >= INTERVAL_100_MS) {
// Check if sending of CAN messages has been delayed too much.
if ((currentMillis - previousMillis100 >= interval100overrun) && (currentMillis > 1000)) {
if ((currentMillis - previousMillis100 >= INTERVAL_100_MS_DELAYED) && (currentMillis > BOOTUP_TIME)) {
set_event(EVENT_CAN_OVERRUN, (currentMillis - previousMillis100));
}
previousMillis100 = currentMillis;

View file

@ -8,9 +8,6 @@
/* Do not change code below unless you are sure what you are doing */
static unsigned long previousMillis100 = 0; // will store last time a 100ms CAN Message was send
static unsigned long previousMillis10ms = 0; // will store last time a 10s CAN Message was send
static const uint8_t interval100 = 100; // interval (ms) at which send CAN Messages
static const uint8_t interval10ms = 10; // interval (ms) at which send CAN Messages
static const uint8_t interval10overrun = 15; // interval (ms) at when a 10ms CAN send is considered delayed
static uint8_t CANstillAlive = 12; //counter for checking if CAN is still alive
#define MAX_CELL_VOLTAGE 4250 //Battery is put into emergency stop if one cell goes over this value
@ -533,7 +530,7 @@ void receive_can_battery(CAN_frame_t rx_frame) {
void send_can_battery() {
unsigned long currentMillis = millis();
//Send 100ms message
if (currentMillis - previousMillis100 >= interval100) {
if (currentMillis - previousMillis100 >= INTERVAL_100_MS) {
previousMillis100 = currentMillis;
ESP32Can.CANWriteFrame(&KIA64_553);
@ -541,9 +538,9 @@ void send_can_battery() {
ESP32Can.CANWriteFrame(&KIA64_2A1);
}
// Send 10ms CAN Message
if (currentMillis - previousMillis10ms >= interval10ms) {
if (currentMillis - previousMillis10ms >= INTERVAL_10_MS) {
// Check if sending of CAN messages has been delayed too much.
if ((currentMillis - previousMillis10 >= interval10overrun) && (currentMillis > 1000)) {
if ((currentMillis - previousMillis10 >= INTERVAL_10_MS_DELAYED) && (currentMillis > BOOTUP_TIME)) {
set_event(EVENT_CAN_OVERRUN, (currentMillis - previousMillis10));
}
previousMillis10 = currentMillis;

View file

@ -12,10 +12,6 @@
static unsigned long previousMillis10 = 0; // will store last time a 10ms CAN Message was send
static unsigned long previousMillis100 = 0; // will store last time a 100ms CAN Message was send
static unsigned long previousMillis10s = 0; // will store last time a 1s CAN Message was send
static const uint8_t interval10 = 10; // interval (ms) at which send CAN Messages
static const uint8_t interval100 = 100; // interval (ms) at which send CAN Messages
static const uint16_t interval10s = 10000; // interval (ms) at which send CAN Messages
static const uint8_t interval10overrun = 15; // interval (ms) at when a 10ms CAN send is considered delayed
static uint16_t CANerror = 0; //counter on how many CAN errors encountered
#define MAX_CAN_FAILURES 5000 //Amount of malformed CAN messages to allow before raising a warning
static uint8_t CANstillAlive = 12; //counter for checking if CAN is still alive
@ -668,7 +664,7 @@ void receive_can_battery(CAN_frame_t rx_frame) {
void send_can_battery() {
unsigned long currentMillis = millis();
// Send 100ms CAN Message
if (currentMillis - previousMillis100 >= interval100) {
if (currentMillis - previousMillis100 >= INTERVAL_100_MS) {
previousMillis100 = currentMillis;
//When battery requests heating pack status change, ack this
@ -708,9 +704,9 @@ void send_can_battery() {
mprun100 = (mprun100 + 1) % 4; // mprun100 cycles between 0-1-2-3-0-1...
}
//Send 10ms message
if (currentMillis - previousMillis10 >= interval10) {
if (currentMillis - previousMillis10 >= INTERVAL_10_MS) {
// Check if sending of CAN messages has been delayed too much.
if ((currentMillis - previousMillis10 >= interval10overrun) && (currentMillis > 1000)) {
if ((currentMillis - previousMillis10 >= INTERVAL_10_MS_DELAYED) && (currentMillis > BOOTUP_TIME)) {
set_event(EVENT_CAN_OVERRUN, (currentMillis - previousMillis10));
}
previousMillis10 = currentMillis;
@ -834,7 +830,7 @@ void send_can_battery() {
mprun10 = (mprun10 + 1) % 4; // mprun10 cycles between 0-1-2-3-0-1...
}
//Send 10s CAN messages
if (currentMillis - previousMillis10s >= interval10s) {
if (currentMillis - previousMillis10s >= INTERVAL_10_S) {
previousMillis10s = currentMillis;
//Every 10s, ask diagnostic data from the battery. Don't ask if someone is already polling on the bus (Leafspy?)

View file

@ -51,9 +51,6 @@ static unsigned long previousMillis10 = 0; // will store last time a 10ms CAN
static unsigned long previousMillis100 = 0; // will store last time a 100ms CAN Message was sent
static unsigned long previousMillis1000 = 0; // will store last time a 1000ms CAN Message was sent
static unsigned long GVL_pause = 0;
static const int interval10 = 10; // interval (ms) at which send CAN Messages
static const int interval100 = 100; // interval (ms) at which send CAN Messages
static const int interval1000 = 1000; // interval (ms) at which send CAN Messages
void update_values_battery() { //This function maps all the values fetched via CAN to the correct parameters used for modbus
@ -226,7 +223,7 @@ void receive_can_battery(CAN_frame_t rx_frame) //GKOE reworked
void send_can_battery() {
unsigned long currentMillis = millis();
// Send 100ms CAN Message (for 2.4s, then pause 10s)
if ((currentMillis - previousMillis100) >= (interval100 + GVL_pause)) {
if ((currentMillis - previousMillis100) >= (INTERVAL_100_MS + GVL_pause)) {
previousMillis100 = currentMillis;
ESP32Can.CANWriteFrame(&KANGOO_423);
GVI_Pollcounter++;
@ -237,7 +234,7 @@ void send_can_battery() {
}
}
// 1000ms CAN handling
if (currentMillis - previousMillis1000 >= interval1000) {
if (currentMillis - previousMillis1000 >= INTERVAL_1_S) {
previousMillis1000 = currentMillis;
if (GVB_79B_Continue)
ESP32Can.CANWriteFrame(&KANGOO_79B_Continue);

View file

@ -36,9 +36,6 @@ static unsigned long previousMillis10 = 0; // will store last time a 10ms CAN
static unsigned long previousMillis100 = 0; // will store last time a 100ms CAN Message was sent
static unsigned long previousMillis1000 = 0; // will store last time a 1000ms CAN Message was sent
static unsigned long GVL_pause = 0;
static const uint8_t interval100 = 100; // interval (ms) at which send CAN Messages
static const uint8_t interval1000 = 1000; // interval (ms) at which send CAN Messages
static const uint8_t interval100overrun = 120; // interval (ms) at when a 100ms CAN send is considered delayed
void update_values_battery() { //This function maps all the values fetched via CAN to the correct parameters used for modbus
system_SOH_pptt = (LB_SOH * 100); //Increase range from 99% -> 99.00%
@ -141,16 +138,16 @@ void receive_can_battery(CAN_frame_t rx_frame) {
void send_can_battery() {
unsigned long currentMillis = millis();
// Send 100ms CAN Message
if (currentMillis - previousMillis100 >= interval100) {
if (currentMillis - previousMillis100 >= INTERVAL_100_MS) {
// Check if sending of CAN messages has been delayed too much.
if ((currentMillis - previousMillis100 >= interval100overrun) && (currentMillis > 1000)) {
if ((currentMillis - previousMillis100 >= INTERVAL_100_MS_DELAYED) && (currentMillis > BOOTUP_TIME)) {
set_event(EVENT_CAN_OVERRUN, (currentMillis - previousMillis100));
}
previousMillis100 = currentMillis;
//ESP32Can.CANWriteFrame(&ZOE_423);
}
// 1000ms CAN handling
if (currentMillis - previousMillis1000 >= interval1000) {
if (currentMillis - previousMillis1000 >= INTERVAL_1_S) {
previousMillis1000 = currentMillis;
//ESP32Can.CANWriteFrame(&ZOE_423);
}

View file

@ -17,9 +17,6 @@ TODO: Map all values from battery CAN messages
/* Do not change code below unless you are sure what you are doing */
static unsigned long previousMillis10 = 0; // will store last time a 10ms CAN Message was send
static unsigned long previousMillis100 = 0; // will store last time a 100ms CAN Message was send
static const int interval10 = 10; // interval (ms) at which send CAN Messages
static const int interval100 = 100; // interval (ms) at which send CAN Messages
static const uint8_t interval10overrun = 15; // interval (ms) at when a 10ms CAN send is considered delayed
static uint8_t CANstillAlive = 12; //counter for checking if CAN is still alive
static int SOC_1 = 0;
@ -130,9 +127,9 @@ void receive_can_battery(CAN_frame_t rx_frame) {
void send_can_battery() {
unsigned long currentMillis = millis();
//Send 10ms message
if (currentMillis - previousMillis10 >= interval10) {
if (currentMillis - previousMillis10 >= INTERVAL_10_MS) {
// Check if sending of CAN messages has been delayed too much.
if ((currentMillis - previousMillis10 >= interval10overrun) && (currentMillis > 1000)) {
if ((currentMillis - previousMillis10 >= INTERVAL_10_MS_DELAYED) && (currentMillis > BOOTUP_TIME)) {
set_event(EVENT_CAN_OVERRUN, (currentMillis - previousMillis10));
}
previousMillis10 = currentMillis;
@ -155,7 +152,7 @@ void send_can_battery() {
}
}
// Send 100ms CAN Message
if (currentMillis - previousMillis100 >= interval100) {
if (currentMillis - previousMillis100 >= INTERVAL_100_MS) {
previousMillis100 = currentMillis;
ESP32Can.CANWriteFrame(&SANTAFE_523);

View file

@ -9,8 +9,6 @@
/* 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 const uint8_t interval30 = 30; // interval (ms) at which send CAN Messages
static const uint8_t interval30overrun = 40; // interval (ms) at which a 30ms CAN message is considered overrun
static uint8_t stillAliveCAN = 6; //counter for checking if CAN is still alive
CAN_frame_t TESLA_221_1 = {
@ -585,9 +583,9 @@ the first, for a few cycles, then stop all messages which causes the contactor
unsigned long currentMillis = millis();
//Send 30ms message
if (currentMillis - previousMillis30 >= interval30) {
if (currentMillis - previousMillis30 >= INTERVAL_30_MS) {
// Check if sending of CAN messages has been delayed too much.
if ((currentMillis - previousMillis30 >= interval30overrun) && (currentMillis > 1000)) {
if ((currentMillis - previousMillis30 >= INTERVAL_30_MS_DELAYED) && (currentMillis > BOOTUP_TIME)) {
set_event(EVENT_CAN_OVERRUN, (currentMillis - previousMillis30));
}
previousMillis30 = currentMillis;

View file

@ -8,9 +8,6 @@
static unsigned long previousMillis10 = 0; // will store last time a 10ms CAN Message was send
static unsigned long previousMillis100 = 0; // will store last time a 100ms CAN Message was send
static unsigned long previousMillis10s = 0; // will store last time a 1s CAN Message was send
static const int interval10 = 10; // interval (ms) at which send CAN Messages
static const int interval100 = 100; // interval (ms) at which send CAN Messages
static const int interval10s = 10000; // interval (ms) at which send CAN Messages
void print_units(char* header, int value, char* units) {
Serial.print(header);
@ -76,7 +73,7 @@ void receive_can_battery(CAN_frame_t rx_frame) {
void send_can_battery() {
unsigned long currentMillis = millis();
// Send 100ms CAN Message
if (currentMillis - previousMillis100 >= interval100) {
if (currentMillis - previousMillis100 >= INTERVAL_100_MS) {
previousMillis100 = currentMillis;
// Put fake messages here incase you want to test sending CAN
}

View file

@ -7,9 +7,6 @@
/* Do not change code below unless you are sure what you are doing */
static unsigned long previousMillis100 = 0; // will store last time a 100ms CAN Message was send
static unsigned long previousMillis60s = 0; // will store last time a 60s CAN Message was send
static const uint8_t interval100 = 100; // interval (ms) at which send CAN Messages
static const uint8_t interval100overrun = 120; // interval (ms) at when a 100ms CAN send is considered delayed
static const uint16_t interval60s = 60000; // interval (ms) at which send CAN Messages
static uint8_t CANstillAlive = 12; //counter for checking if CAN is still alive
#define MAX_CELL_VOLTAGE 4210 //Battery is put into emergency stop if one cell goes over this value
@ -360,9 +357,9 @@ void readCellVoltages() {
void send_can_battery() {
unsigned long currentMillis = millis();
// Send 100ms CAN Message
if (currentMillis - previousMillis100 >= interval100) {
if (currentMillis - previousMillis100 >= INTERVAL_100_MS) {
// Check if sending of CAN messages has been delayed too much.
if ((currentMillis - previousMillis100 >= interval100overrun) && (currentMillis > 1000)) {
if ((currentMillis - previousMillis100 >= INTERVAL_100_MS_DELAYED) && (currentMillis > BOOTUP_TIME)) {
set_event(EVENT_CAN_OVERRUN, (currentMillis - previousMillis100));
}
previousMillis100 = currentMillis;
@ -376,7 +373,7 @@ void send_can_battery() {
batteryAllowsContactorClosing = false;
}
}
if (currentMillis - previousMillis60s >= interval60s) {
if (currentMillis - previousMillis60s >= INTERVAL_60_S) {
previousMillis60s = currentMillis;
if (system_bms_status == ACTIVE) {
readCellVoltages();

View file

@ -56,4 +56,24 @@
// Common definitions
#define MAX_AMOUNT_CELLS 192
#define INTERVAL_10_MS 10
#define INTERVAL_20_MS 20
#define INTERVAL_30_MS 30
#define INTERVAL_50_MS 50
#define INTERVAL_100_MS 100
#define INTERVAL_200_MS 200
#define INTERVAL_500_MS 500
#define INTERVAL_640_MS 640
#define INTERVAL_1_S 1000
#define INTERVAL_5_S 5000
#define INTERVAL_10_S 10000
#define INTERVAL_60_S 60000
#define INTERVAL_10_MS_DELAYED 15
#define INTERVAL_20_MS_DELAYED 30
#define INTERVAL_30_MS_DELAYED 40
#define INTERVAL_100_MS_DELAYED 120
#define BOOTUP_TIME 1000 // Time in ms it takes before system is considered fully started up
#endif