From 6f26e46e518a3d9aeb9ef2ac1a3f0980344c4b28 Mon Sep 17 00:00:00 2001 From: Hamza Date: Fri, 24 May 2024 15:20:23 +0200 Subject: [PATCH] added sm_get_state_code(), added more ErrorKinds --- Core/Inc/state_machine.h | 30 ++++++++++++++++++++---------- Core/Src/state_machine.c | 33 ++++++++++++++++++++++++++------- 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/Core/Inc/state_machine.h b/Core/Inc/state_machine.h index a0a5214..ce0356f 100644 --- a/Core/Inc/state_machine.h +++ b/Core/Inc/state_machine.h @@ -16,7 +16,7 @@ // Time to wait between closing relays #define RELAY_CLOSE_WAIT 10 // ms -typedef enum { // valid transitions: (all could transition to error) +typedef enum { // 7 states -> 3 bit. valid transitions: (all could transition to error) STATE_INACTIVE, // INACTIVE -> PRECHARGE, CHARGING, ERROR STATE_PRECHARGE, // PRECHARGE -> INACTIVE, READY, DISCHARGE, ERROR STATE_READY, // READY -> ACTIVE, DISCHARGE, ERROR @@ -26,16 +26,23 @@ typedef enum { // valid transitions: (all could transition to error) STATE_ERROR, // ERROR -> INACTIVE, DISCHARGE, ERROR } State; -typedef struct { +typedef struct { // 13 errors -> 4 bit uint16_t bms_timeout : 1; - uint16_t overtemp : 1; - uint16_t overcurrent : 1; - uint16_t overvoltage : 1; - uint16_t missing_current_reading : 1; - uint16_t missing_voltage_reading : 1; - uint16_t missing_temp_reading : 1; - uint16_t precharge_fail : 1; - uint16_t powerground_fail : 1; + uint16_t bms_checksum_fail : 1; + uint16_t bms_overtemp : 1; + uint16_t bms_fault : 1; + + uint16_t temperature_error : 1; + uint16_t current_error : 1; + uint16_t voltage_error : 1; + + uint16_t temperature_sensor_missing : 1; + uint16_t current_sensor_missing : 1; + uint16_t voltage_missing : 1; + uint16_t relay_missing : 1; + + uint16_t state_fail : 1; + uint16_t state_transition_fail : 1; } ErrorKind; //typedef enum {} WarningKind; @@ -51,6 +58,7 @@ extern StateHandle state; void sm_init(); void sm_update(); +int sm_get_state_code(); State sm_update_inactive(); State sm_update_precharge(); @@ -65,6 +73,8 @@ void sm_set_relay_positions(State state); void sm_set_relay(Relay relay, bool closed); void sm_check_precharge_discharge(int *is_closed, int should_close); +void sm_check_errors(); + void sm_handle_ams_in(const uint8_t *data); void sm_set_error(ErrorKind error_kind, bool is_errored); \ No newline at end of file diff --git a/Core/Src/state_machine.c b/Core/Src/state_machine.c index 661f54c..86afba4 100644 --- a/Core/Src/state_machine.c +++ b/Core/Src/state_machine.c @@ -8,17 +8,14 @@ StateHandle state; static bool relay_closed = 0; static bool precharge_closed = 0; -static int16_t RELAY_BAT_SIDE; -static int16_t RELAY_ESC_SIDE; -static int16_t CURRENT_MEASUREMENT; +static int16_t RELAY_BAT_SIDE = 0; +static int16_t RELAY_ESC_SIDE = 0; +static int16_t CURRENT_MEASUREMENT = 0; void sm_init(){ state.current_state = STATE_INACTIVE; state.target_state = STATE_INACTIVE; state.error_source = 0; - RELAY_BAT_SIDE = 0; - RELAY_ESC_SIDE = 0; - CURRENT_MEASUREMENT = 0; } void sm_update(){ @@ -51,10 +48,30 @@ void sm_update(){ //status_led_state(state.current_state, (ErrorKind) state.error_type); } +int sm_get_state_code(){ + switch (state.current_state) { + case STATE_INACTIVE: + return 0; + case STATE_PRECHARGE: + return 1; + case STATE_READY: + return 2; + case STATE_ACTIVE: + return 3; + case STATE_DISCHARGE: + return 4; + case STATE_CHARGING: + return 5; + default: // either STATE_ERROR or something went severly wrong + return 6; + } +} + State sm_update_inactive(){ switch (state.target_state) { case STATE_PRECHARGE: //close precharge relay, wait until both sides are similar + sm_set_relay_positions(STATE_PRECHARGE); return STATE_PRECHARGE; case STATE_CHARGING: return STATE_CHARGING; @@ -184,4 +201,6 @@ void sm_check_precharge_discharge(int *is_closed, int should_close){} void sm_handle_ams_in(const uint8_t *data){} -void sm_set_error(ErrorKind error_kind, bool is_errored); \ No newline at end of file +void sm_set_error(ErrorKind error_kind, bool is_errored); + +void sm_check_errors(){} \ No newline at end of file