add battery error handling

This commit is contained in:
Kilian Bracher 2025-04-13 15:42:53 +02:00
parent e0467fcda6
commit bdddc49dab
Signed by: k.bracher
SSH Key Fingerprint: SHA256:mXpyZkK7RDiJ7qeHCKJX108woM0cl5TrCvNBJASu6lM

View File

@ -1,12 +1,17 @@
#include "battery.h" #include "battery.h"
#include "ADBMS_Driver.h" #include "ADBMS_Driver.h"
#include "NTC.h" #include "NTC.h"
#include "can.h"
#include "config_ADBMS6830.h" #include "config_ADBMS6830.h"
#include "ts_state_machine.h"
#include <string.h> #include <string.h>
#define SWO_LOG_PREFIX "[BATTERY] " #define SWO_LOG_PREFIX "[BATTERY] "
#include "swo_log.h" #include "swo_log.h"
#define MAX_ERRORS 4 // max number of errors in window before panic
#define MAX_ERRORS_WINDOW_SIZE 16 // size of the error window for error detection
uint16_t min_voltage = 0xFFFF; uint16_t min_voltage = 0xFFFF;
uint16_t max_voltage = 0; uint16_t max_voltage = 0;
typeof(module_voltages) module_voltages = {[0 ... N_BMS - 1] = {0xFFFF, 0}}; typeof(module_voltages) module_voltages = {[0 ... N_BMS - 1] = {0xFFFF, 0}};
@ -16,6 +21,26 @@ typeof(module_temps) module_temps = {[0 ... N_BMS - 1] = {INT16_MAX, INT16_MIN}}
int16_t cellTemps[N_BMS][N_CELLS]; int16_t cellTemps[N_BMS][N_CELLS];
static bool error_window[MAX_ERRORS_WINDOW_SIZE] = {};
static size_t error_window_index = 0;
static size_t error_count = 0;
static inline void update_error_window(bool error, int id) {
error_count -= error_window[error_window_index] ? 1 : 0;
error_count += error ? 1 : 0;
if (error_count >= MAX_ERRORS) {
can_send_error(TS_ERRORKIND_SLAVE_PANIC, id);
ts_sm_set_error_source(TS_ERROR_SOURCE_SLAVES, TS_ERRORKIND_SLAVE_PANIC, true);
} else {
ts_sm_set_error_source(TS_ERROR_SOURCE_SLAVES, TS_ERRORKIND_SLAVE_PANIC, false);
}
error_window[error_window_index] = error;
error_window_index += 1;
error_window_index %= MAX_ERRORS_WINDOW_SIZE;
}
HAL_StatusTypeDef battery_init(SPI_HandleTypeDef *hspi) { HAL_StatusTypeDef battery_init(SPI_HandleTypeDef *hspi) {
auto ret = AMS_Init(hspi); auto ret = AMS_Init(hspi);
if (ret.status != ADBMS_NO_ERROR) { if (ret.status != ADBMS_NO_ERROR) {
@ -33,14 +58,18 @@ HAL_StatusTypeDef battery_init(SPI_HandleTypeDef *hspi) {
HAL_StatusTypeDef battery_update() { HAL_StatusTypeDef battery_update() {
auto ret = AMS_Idle_Loop(); auto ret = AMS_Idle_Loop();
if (ret.status != ADBMS_NO_ERROR) { if (ret.status != ADBMS_NO_ERROR) {
debug_log(LOG_LEVEL_ERROR, "Failed to update battery data: %s", debug_log(LOG_LEVEL_ERROR, "Error while updating battery data: %s",
ADBMS_Status_ToString(ret.status)); ADBMS_Status_ToString(ret.status));
if (ret.bms_id != -1) { if (ret.bms_id != -1) {
debug_log_cont(LOG_LEVEL_ERROR, " (on BMS ID: %hd)", ret.bms_id); debug_log_cont(LOG_LEVEL_ERROR, " (on BMS ID: %hd)", ret.bms_id);
} }
update_error_window(true, ret.bms_id);
return HAL_ERROR; return HAL_ERROR;
} }
update_error_window(false, ret.bms_id);
min_voltage = 0xFFFF; min_voltage = 0xFFFF;
max_voltage = 0; max_voltage = 0;
min_temp = INT16_MAX; min_temp = INT16_MAX;