diff --git a/Core/Inc/state_machine.h b/Core/Inc/state_machine.h index b7726f3..b08e0ce 100644 --- a/Core/Inc/state_machine.h +++ b/Core/Inc/state_machine.h @@ -5,6 +5,7 @@ #include #include "ADBMS_LL_Driver.h" #include "AMS_HighLevel.h" +#include "errors.h" #include "PWM_control.h" #include "TMP1075.h" #include @@ -23,6 +24,7 @@ // Time to wait between closing relays #define RELAY_CLOSE_WAIT 10 // ms +#warning typedef enum { // states -> 3 bit. valid transitions: (all could transition to error) STATE_INACTIVE, // INACTIVE -> PRECHARGE, CHARGING, ERROR STATE_PRECHARGE, // PRECHARGE -> INACTIVE, READY, DISCHARGE, ERROR @@ -35,21 +37,16 @@ typedef enum { // states -> 3 bit. valid transitions: (all could t } State; typedef struct { - uint16_t bms_timeout : 1; - uint16_t bms_overtemp : 1; - uint16_t bms_fault : 1; + uint16_t bms_timeout : 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_error : 1; uint16_t voltage_missing : 1; - uint16_t battery_missing : 1; - uint16_t relay_missing : 1; - uint16_t state_transition_fail : 1; + } ErrorKind; //typedef enum {} WarningKind; @@ -67,6 +64,8 @@ static bool precharge_closed = 0; extern int16_t RELAY_BAT_SIDE_VOLTAGE; extern int16_t RELAY_ESC_SIDE_VOLTAGE; extern int16_t CURRENT_MEASUREMENT; +extern uint8_t powerground_status; + void sm_init(); void sm_update(); @@ -84,12 +83,14 @@ typedef enum { RELAY_MAIN, RELAY_PRECHARGE } Relay; void sm_set_relay_positions(State state); void sm_set_relay(Relay relay, bool closed); void sm_check_charging(); -void sm_check_cell_temps(int8_t* id, int16_t* temp); +void sm_check_battery_temperature(int8_t* id, int16_t* temp); + +int16_t sm_return_cell_temperature(int id); +int16_t sm_return_cell_voltage(int id); void sm_handle_ams_in(const uint8 *data); void sm_check_errors(); void sm_set_error(ErrorKind error_kind, bool is_errored); - void sm_test_cycle_states(); #endif /* "INC_STATE_MACHINE_H" */ \ No newline at end of file diff --git a/Core/Src/state_machine.c b/Core/Src/state_machine.c index 60856a5..aacb4d2 100644 --- a/Core/Src/state_machine.c +++ b/Core/Src/state_machine.c @@ -1,10 +1,16 @@ #include "state_machine.h" +#include "AMS_HighLevel.h" +#include "TMP1075.h" +#include "errors.h" #include "stm32f3xx_hal.h" +#include StateHandle state; int16_t RELAY_BAT_SIDE_VOLTAGE; int16_t RELAY_ESC_SIDE_VOLTAGE; int16_t CURRENT_MEASUREMENT; +uint8_t powerground_status; + uint32_t timestamp; void sm_init(){ @@ -14,6 +20,8 @@ void sm_init(){ } void sm_update(){ + sm_check_errors(); + RELAY_BAT_SIDE_VOLTAGE = module.auxVoltages[0] * 12.42; // the calculation says the factor is 11.989. 12.42 yields the better result RELAY_ESC_SIDE_VOLTAGE = module.auxVoltages[1] * 12.42; CURRENT_MEASUREMENT = module.auxVoltages[2] / 2.2; @@ -197,14 +205,24 @@ void sm_check_charging(){ state.target_state = STATE_CHARGING_PRECHARGE; } -void sm_check_cell_temps(int8_t *id, int16_t *temp){ +/* returns the ID and temperature of the hottest cell */ +void sm_check_battery_temperature(int8_t *id, int16_t *temp){ for (int i = 0; i < N_TEMP_SENSORS; i++) { if (tmp1075_temps[i] > *temp){ *id = i; + *temp = tmp1075_temps[i]; } } } +int16_t sm_return_cell_temperature(int id){ + return tmp1075_temps[id]; +} + +int16_t sm_return_cell_voltage(int id){ + return module.cellVoltages[id]; +} + void sm_handle_ams_in(const uint8_t *data){ switch (data[0]) { case 0x00: @@ -235,11 +253,38 @@ void sm_set_error(ErrorKind error_kind, bool is_errored){} #warning TODO: add error checking for everything here void sm_check_errors(){ - if (module.status.THSD == 1) { - state.error_type.bms_overtemp = 1; + switch (error_data.data_kind) { + case SEK_OVERTEMP: + case SEK_UNDERTEMP: + case SEK_TOO_FEW_TEMPS: + state.error_type.temperature_error = 1; + case SEK_OVERVOLT: + case SEK_UNDERVOLT: + case SEK_OPENWIRE: + case SEK_EEPROM_ERR: + case SEK_INTERNAL_BMS_TIMEOUT: + state.error_type.bms_timeout = 1; + case SEK_INTERNAL_BMS_CHECKSUM_FAIL: + case SEK_INTERNAL_BMS_OVERTEMP: + case SEK_INTERNAL_BMS_FAULT: + state.error_type.bms_fault = 1; + break; } - if (RELAY_BAT_SIDE_VOLTAGE < 40){ - state.error_source = (1 << 10); + + if (1){ + state.error_type.current_error = 1; + } + + if (1){ + state.error_type.current_sensor_missing = 1; + } + + if (RELAY_BAT_SIDE_VOLTAGE < 30000){ + state.error_type.voltage_error = 1; + } + + if (1){ + state.error_type.voltage_missing = 1; } }