Skip to content

Commit

Permalink
Add event for missing modbus comm
Browse files Browse the repository at this point in the history
  • Loading branch information
dalathegreat committed May 14, 2024
1 parent 5afdfcb commit ca573d1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Software/src/devboard/utils/events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ void init_events(void) {
events.entries[EVENT_PRECHARGE_FAILURE].level = EVENT_LEVEL_INFO;
events.entries[EVENT_INTERNAL_OPEN_FAULT].level = EVENT_LEVEL_ERROR;
events.entries[EVENT_INVERTER_OPEN_CONTACTOR].level = EVENT_LEVEL_INFO;
events.entries[EVENT_MODBUS_INVERTER_MISSING].level = EVENT_LEVEL_INFO;
events.entries[EVENT_ERROR_OPEN_CONTACTOR].level = EVENT_LEVEL_INFO;
events.entries[EVENT_CELL_UNDER_VOLTAGE].level = EVENT_LEVEL_ERROR;
events.entries[EVENT_CELL_OVER_VOLTAGE].level = EVENT_LEVEL_ERROR;
Expand Down Expand Up @@ -247,6 +248,8 @@ const char* get_event_message_string(EVENTS_ENUM_TYPE event) {
case EVENT_ERROR_OPEN_CONTACTOR:
return "Info: Too much time spent in error state. Opening contactors, not safe to continue charging. "
"Check other error code for reason!";
case EVENT_MODBUS_INVERTER_MISSING:
return "Info: Modbus inverter has not sent any data. Inspect communication wiring!";
case EVENT_CELL_UNDER_VOLTAGE:
return "ERROR: CELL UNDERVOLTAGE!!! Stopping battery charging and discharging. Inspect battery!";
case EVENT_CELL_OVER_VOLTAGE:
Expand Down
3 changes: 2 additions & 1 deletion Software/src/devboard/utils/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

// #define INCLUDE_EVENTS_TEST // Enable to run an event test loop, see events_test_on_target.cpp

#define EE_MAGIC_HEADER_VALUE 0x0004 // 0x0000 to 0xFFFF
#define EE_MAGIC_HEADER_VALUE 0x0005 // 0x0000 to 0xFFFF

#define GENERATE_ENUM(ENUM) ENUM,
#define GENERATE_STRING(STRING) #STRING,
Expand Down Expand Up @@ -49,6 +49,7 @@
XX(EVENT_PRECHARGE_FAILURE) \
XX(EVENT_INTERNAL_OPEN_FAULT) \
XX(EVENT_INVERTER_OPEN_CONTACTOR) \
XX(EVENT_MODBUS_INVERTER_MISSING) \
XX(EVENT_ERROR_OPEN_CONTACTOR) \
XX(EVENT_CELL_UNDER_VOLTAGE) \
XX(EVENT_CELL_OVER_VOLTAGE) \
Expand Down
27 changes: 26 additions & 1 deletion Software/src/inverter/BYD-MODBUS.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
#include "../include.h"
#ifdef BYD_MODBUS
#include "../datalayer/datalayer.h"
#include "../devboard/utils/events.h"
#include "BYD-MODBUS.h"

// For modbus register definitions, see https://gitlab.com/pelle8/inverter_resources/-/blob/main/byd_registers_modbus_rtu.md

static uint8_t bms_char_dis_status = STANDBY;
static unsigned long previousMillis60s = 0; // will store last time a 60s event occured
static uint32_t previous_value_register_401 = 0x0F0F;
static uint32_t current_value_register_401 = 0xF0F0;
static uint32_t user_configured_max_discharge_W = 0;
static uint32_t user_configured_max_charge_W = 0;
static uint32_t max_discharge_W = 0;
static uint32_t max_charge_W = 0;
static uint8_t bms_char_dis_status = STANDBY;

void update_modbus_registers_inverter() {
verify_temperature_modbus();
verify_inverter_modbus();
handle_update_data_modbusp201_byd();
handle_update_data_modbusp301_byd();
}
Expand Down Expand Up @@ -107,4 +112,24 @@ void verify_temperature_modbus() {
}
}
}

void verify_inverter_modbus() {
// Every 60 seconds, the Gen24 writes to this 401 register, alternating between 00FF and FF00.
// We use this info to see if inverter is still alive, incase not, raise an event
unsigned long currentMillis = millis();

if (currentMillis - previousMillis60s >= INTERVAL_60_S) {
previousMillis60s = currentMillis;

current_value_register_401 = mbPV[401];

if (current_value_register_401 == previous_value_register_401) {
set_event(EVENT_MODBUS_INVERTER_MISSING, 0);
} else {
clear_event(EVENT_MODBUS_INVERTER_MISSING);
}

previous_value_register_401 = current_value_register_401;
}
}
#endif
1 change: 1 addition & 0 deletions Software/src/inverter/BYD-MODBUS.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ extern uint16_t mbPV[MB_RTU_NUM_VALUES];

void handle_static_data_modbus_byd();
void verify_temperature_modbus();
void verify_inverter_modbus();
void handle_update_data_modbusp201_byd();
void handle_update_data_modbusp301_byd();
#endif

0 comments on commit ca573d1

Please sign in to comment.