Add event for CAN overrun

This commit is contained in:
Daniel 2024-03-28 00:10:06 +02:00
parent c86d8c8741
commit 28fc1da7c0
3 changed files with 35 additions and 35 deletions

View file

@ -45,13 +45,8 @@ static ACAN2515_Buffer16 gBuffer;
#endif #endif
// ModbusRTU parameters // ModbusRTU parameters
#if defined(BYD_MODBUS)
#define MB_RTU_NUM_VALUES 30000
#endif
#if defined(LUNA2000_MODBUS)
#define MB_RTU_NUM_VALUES 30000
#endif
#if defined(BYD_MODBUS) || defined(LUNA2000_MODBUS) #if defined(BYD_MODBUS) || defined(LUNA2000_MODBUS)
#define MB_RTU_NUM_VALUES 30000
uint16_t mbPV[MB_RTU_NUM_VALUES]; // Process variable memory uint16_t mbPV[MB_RTU_NUM_VALUES]; // Process variable memory
// Create a ModbusRTU server instance listening on Serial2 with 2000ms timeout // Create a ModbusRTU server instance listening on Serial2 with 2000ms timeout
ModbusServerRTU MBserver(Serial2, 2000); ModbusServerRTU MBserver(Serial2, 2000);
@ -133,6 +128,8 @@ void setup() {
#ifdef WEBSERVER #ifdef WEBSERVER
init_webserver(); init_webserver();
init_mDNS();
#endif #endif
init_events(); init_events();
@ -151,8 +148,6 @@ void setup() {
init_battery(); init_battery();
init_mDNS();
// BOOT button at runtime is used as an input for various things // BOOT button at runtime is used as an input for various things
pinMode(0, INPUT_PULLUP); pinMode(0, INPUT_PULLUP);
} }
@ -212,6 +207,7 @@ void loop() {
} }
} }
#ifdef WEBSERVER
// Initialise mDNS // Initialise mDNS
void init_mDNS() { void init_mDNS() {
@ -230,6 +226,7 @@ void init_mDNS() {
MDNS.addService("battery_emulator", "tcp", 80); MDNS.addService("battery_emulator", "tcp", 80);
} }
} }
#endif
// Initialization functions // Initialization functions
void init_serial() { void init_serial() {
@ -324,6 +321,7 @@ void init_contactors() {
} }
void init_modbus() { void init_modbus() {
#if defined(BYD_MODBUS) || defined(LUNA2000_MODBUS)
// Set up Modbus RTU Server // Set up Modbus RTU Server
pinMode(RS485_EN_PIN, OUTPUT); pinMode(RS485_EN_PIN, OUTPUT);
digitalWrite(RS485_EN_PIN, HIGH); digitalWrite(RS485_EN_PIN, HIGH);
@ -336,7 +334,6 @@ void init_modbus() {
// Init Static data to the RTU Modbus // Init Static data to the RTU Modbus
handle_static_data_modbus_byd(); handle_static_data_modbus_byd();
#endif #endif
#if defined(BYD_MODBUS) || defined(LUNA2000_MODBUS)
#if defined(SERIAL_LINK_RECEIVER) || defined(SERIAL_LINK_TRANSMITTER) #if defined(SERIAL_LINK_RECEIVER) || defined(SERIAL_LINK_TRANSMITTER)
// Check that Dual LilyGo via RS485 option isn't enabled, this collides with Modbus! // Check that Dual LilyGo via RS485 option isn't enabled, this collides with Modbus!
#error MODBUS CANNOT BE USED IN DOUBLE LILYGO SETUPS! CHECK USER SETTINGS! #error MODBUS CANNOT BE USED IN DOUBLE LILYGO SETUPS! CHECK USER SETTINGS!
@ -415,8 +412,7 @@ void receive_can() { // This section checks if we have a complete CAN message i
// Depending on which battery/inverter is selected, we forward this to their respective CAN routines // Depending on which battery/inverter is selected, we forward this to their respective CAN routines
CAN_frame_t rx_frame; CAN_frame_t rx_frame;
if (xQueueReceive(CAN_cfg.rx_queue, &rx_frame, 3 * portTICK_PERIOD_MS) == pdTRUE) { if (xQueueReceive(CAN_cfg.rx_queue, &rx_frame, 3 * portTICK_PERIOD_MS) == pdTRUE) {
if (rx_frame.FIR.B.FF == CAN_frame_std) { if (rx_frame.FIR.B.FF == CAN_frame_std) { // New standard frame
//printf("New standard frame");
// Battery // Battery
#ifndef SERIAL_LINK_RECEIVER #ifndef SERIAL_LINK_RECEIVER
receive_can_battery(rx_frame); receive_can_battery(rx_frame);
@ -438,8 +434,7 @@ void receive_can() { // This section checks if we have a complete CAN message i
#ifdef NISSANLEAF_CHARGER #ifdef NISSANLEAF_CHARGER
receive_can_nissanleaf_charger(rx_frame); receive_can_nissanleaf_charger(rx_frame);
#endif #endif
} else { } else { // New extended frame
//printf("New extended frame");
#ifdef PYLON_CAN #ifdef PYLON_CAN
receive_can_pylon(rx_frame); receive_can_pylon(rx_frame);
#endif #endif
@ -495,13 +490,11 @@ void receive_can2() { // This function is similar to receive_can, but just take
rx_frame2.data.u8[i] = MCP2515Frame.data[i]; rx_frame2.data.u8[i] = MCP2515Frame.data[i];
} }
if (rx_frame2.FIR.B.FF == CAN_frame_std) { if (rx_frame2.FIR.B.FF == CAN_frame_std) { // New standard frame
//Serial.println("New standard frame");
#ifdef BYD_CAN #ifdef BYD_CAN
receive_can_byd(rx_frame2); receive_can_byd(rx_frame2);
#endif #endif
} else { } else { // New extended frame
//Serial.println("New extended frame");
#ifdef PYLON_CAN #ifdef PYLON_CAN
receive_can_pylon(rx_frame2); receive_can_pylon(rx_frame2);
#endif #endif

View file

@ -9,19 +9,20 @@
#include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h" #include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h"
/* Do not change code below unless you are sure what you are doing */ /* 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 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 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 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 uint8_t 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 interval100 = 100; // interval (ms) at which send CAN Messages
static const int interval10s = 10000; // interval (ms) at which send CAN Messages static const uint16_t interval10s = 10000; // interval (ms) at which send CAN Messages
static uint16_t CANerror = 0; //counter on how many CAN errors encountered static const uint8_t interval10overrun = 15; // interval (ms) at when a 10ms CAN send is considered delayed
#define MAX_CAN_FAILURES 5000 //Amount of malformed CAN messages to allow before raising a warning static uint16_t CANerror = 0; //counter on how many CAN errors encountered
static uint8_t CANstillAlive = 12; //counter for checking if CAN is still alive #define MAX_CAN_FAILURES 5000 //Amount of malformed CAN messages to allow before raising a warning
static uint8_t errorCode = 0; //stores if we have an error code active from battery control logic static uint8_t CANstillAlive = 12; //counter for checking if CAN is still alive
static uint8_t mprun10r = 0; //counter 0-20 for 0x1F2 message static uint8_t errorCode = 0; //stores if we have an error code active from battery control logic
static uint8_t mprun10 = 0; //counter 0-3 static uint8_t mprun10r = 0; //counter 0-20 for 0x1F2 message
static uint8_t mprun100 = 0; //counter 0-3 static uint8_t mprun10 = 0; //counter 0-3
static uint8_t mprun100 = 0; //counter 0-3
CAN_frame_t LEAF_1F2 = {.FIR = {.B = CAN_frame_t LEAF_1F2 = {.FIR = {.B =
{ {
@ -708,6 +709,10 @@ void send_can_battery() {
} }
//Send 10ms message //Send 10ms message
if (currentMillis - previousMillis10 >= interval10) { if (currentMillis - previousMillis10 >= interval10) {
if (currentMillis - previousMillis10 >= interval10overrun) {
set_event(EVENT_CAN_OVERRUN, interval10);
}
previousMillis10 = currentMillis; previousMillis10 = currentMillis;
switch (mprun10) { switch (mprun10) {
@ -888,7 +893,9 @@ uint16_t Temp_fromRAW_to_F(uint16_t temperature) { //This function feels horrib
} }
void setup_battery(void) { // Performs one time setup at startup void setup_battery(void) { // Performs one time setup at startup
#ifdef DEBUG_VIA_USB
Serial.println("Nissan LEAF battery selected"); Serial.println("Nissan LEAF battery selected");
#endif
system_number_of_cells = 96; system_number_of_cells = 96;
system_max_design_voltage_dV = 4040; // 404.4V, over this, charging is not possible (goes into forced discharge) system_max_design_voltage_dV = 4040; // 404.4V, over this, charging is not possible (goes into forced discharge)

View file

@ -25,11 +25,11 @@ WifiState wifi_state = INIT;
MyTimer ota_timeout_timer = MyTimer(5000); MyTimer ota_timeout_timer = MyTimer(5000);
bool ota_active = false; bool ota_active = false;
unsigned const uint16_t WIFI_MONITOR_INTERVAL_TIME = 15000; unsigned const long WIFI_MONITOR_INTERVAL_TIME = 15000;
unsigned const uint16_t INIT_WIFI_CONNECT_TIMEOUT = 8000; // Timeout for initial WiFi connect in milliseconds unsigned const long INIT_WIFI_CONNECT_TIMEOUT = 8000; // Timeout for initial WiFi connect in milliseconds
unsigned const uint16_t DEFAULT_WIFI_RECONNECT_INTERVAL = 1000; // Default WiFi reconnect interval in ms unsigned const long DEFAULT_WIFI_RECONNECT_INTERVAL = 1000; // Default WiFi reconnect interval in ms
unsigned const uint16_t MAX_WIFI_RETRY_INTERVAL = 30000; // Maximum wifi retry interval in ms unsigned const long MAX_WIFI_RETRY_INTERVAL = 30000; // Maximum wifi retry interval in ms
unsigned long last_wifi_monitor_time = millis(); //init millis so wifi monitor doesn't run immediately unsigned long last_wifi_monitor_time = millis(); //init millis so wifi monitor doesn't run immediately
unsigned long wifi_reconnect_interval = DEFAULT_WIFI_RECONNECT_INTERVAL; unsigned long wifi_reconnect_interval = DEFAULT_WIFI_RECONNECT_INTERVAL;
unsigned long last_wifi_attempt_time = millis(); //init millis so wifi monitor doesn't run immediately unsigned long last_wifi_attempt_time = millis(); //init millis so wifi monitor doesn't run immediately