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
// 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)
#define MB_RTU_NUM_VALUES 30000
uint16_t mbPV[MB_RTU_NUM_VALUES]; // Process variable memory
// Create a ModbusRTU server instance listening on Serial2 with 2000ms timeout
ModbusServerRTU MBserver(Serial2, 2000);
@ -133,6 +128,8 @@ void setup() {
#ifdef WEBSERVER
init_webserver();
init_mDNS();
#endif
init_events();
@ -151,8 +148,6 @@ void setup() {
init_battery();
init_mDNS();
// BOOT button at runtime is used as an input for various things
pinMode(0, INPUT_PULLUP);
}
@ -212,6 +207,7 @@ void loop() {
}
}
#ifdef WEBSERVER
// Initialise mDNS
void init_mDNS() {
@ -230,6 +226,7 @@ void init_mDNS() {
MDNS.addService("battery_emulator", "tcp", 80);
}
}
#endif
// Initialization functions
void init_serial() {
@ -324,6 +321,7 @@ void init_contactors() {
}
void init_modbus() {
#if defined(BYD_MODBUS) || defined(LUNA2000_MODBUS)
// Set up Modbus RTU Server
pinMode(RS485_EN_PIN, OUTPUT);
digitalWrite(RS485_EN_PIN, HIGH);
@ -336,7 +334,6 @@ void init_modbus() {
// Init Static data to the RTU Modbus
handle_static_data_modbus_byd();
#endif
#if defined(BYD_MODBUS) || defined(LUNA2000_MODBUS)
#if defined(SERIAL_LINK_RECEIVER) || defined(SERIAL_LINK_TRANSMITTER)
// 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!
@ -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
CAN_frame_t rx_frame;
if (xQueueReceive(CAN_cfg.rx_queue, &rx_frame, 3 * portTICK_PERIOD_MS) == pdTRUE) {
if (rx_frame.FIR.B.FF == CAN_frame_std) {
//printf("New standard frame");
if (rx_frame.FIR.B.FF == CAN_frame_std) { // New standard frame
// Battery
#ifndef SERIAL_LINK_RECEIVER
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
receive_can_nissanleaf_charger(rx_frame);
#endif
} else {
//printf("New extended frame");
} else { // New extended frame
#ifdef PYLON_CAN
receive_can_pylon(rx_frame);
#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];
}
if (rx_frame2.FIR.B.FF == CAN_frame_std) {
//Serial.println("New standard frame");
if (rx_frame2.FIR.B.FF == CAN_frame_std) { // New standard frame
#ifdef BYD_CAN
receive_can_byd(rx_frame2);
#endif
} else {
//Serial.println("New extended frame");
} else { // New extended frame
#ifdef PYLON_CAN
receive_can_pylon(rx_frame2);
#endif

View file

@ -12,9 +12,10 @@
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
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
@ -708,6 +709,10 @@ void send_can_battery() {
}
//Send 10ms message
if (currentMillis - previousMillis10 >= interval10) {
if (currentMillis - previousMillis10 >= interval10overrun) {
set_event(EVENT_CAN_OVERRUN, interval10);
}
previousMillis10 = currentMillis;
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
#ifdef DEBUG_VIA_USB
Serial.println("Nissan LEAF battery selected");
#endif
system_number_of_cells = 96;
system_max_design_voltage_dV = 4040; // 404.4V, over this, charging is not possible (goes into forced discharge)

View file

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