added sm_check_errors

This commit is contained in:
hamza 2024-06-06 23:13:55 +03:00
parent b4ee2b1b3b
commit 48854dc80a
2 changed files with 62 additions and 16 deletions

View File

@ -5,6 +5,7 @@
#include <stdbool.h>
#include "ADBMS_LL_Driver.h"
#include "AMS_HighLevel.h"
#include "errors.h"
#include "PWM_control.h"
#include "TMP1075.h"
#include <math.h>
@ -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" */

View File

@ -1,10 +1,16 @@
#include "state_machine.h"
#include "AMS_HighLevel.h"
#include "TMP1075.h"
#include "errors.h"
#include "stm32f3xx_hal.h"
#include <stdint.h>
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;
}
}