added sm_get_state_code(), added more ErrorKinds
This commit is contained in:
		@ -16,7 +16,7 @@
 | 
				
			|||||||
// Time to wait between closing relays
 | 
					// Time to wait between closing relays
 | 
				
			||||||
#define RELAY_CLOSE_WAIT 10 // ms
 | 
					#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_INACTIVE,       // INACTIVE   ->  PRECHARGE, CHARGING, ERROR  
 | 
				
			||||||
  STATE_PRECHARGE,      // PRECHARGE  ->  INACTIVE, READY, DISCHARGE, ERROR
 | 
					  STATE_PRECHARGE,      // PRECHARGE  ->  INACTIVE, READY, DISCHARGE, ERROR
 | 
				
			||||||
  STATE_READY,          // READY      ->  ACTIVE, 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_ERROR,          // ERROR      ->  INACTIVE, DISCHARGE, ERROR
 | 
				
			||||||
} State;
 | 
					} State;
 | 
				
			||||||
  
 | 
					  
 | 
				
			||||||
typedef struct {
 | 
					typedef struct {  // 13 errors -> 4 bit
 | 
				
			||||||
  uint16_t bms_timeout : 1;
 | 
					  uint16_t bms_timeout : 1;
 | 
				
			||||||
  uint16_t overtemp : 1;
 | 
					  uint16_t bms_checksum_fail : 1;
 | 
				
			||||||
  uint16_t overcurrent : 1;
 | 
					  uint16_t bms_overtemp : 1;
 | 
				
			||||||
  uint16_t overvoltage : 1;
 | 
					  uint16_t bms_fault : 1;
 | 
				
			||||||
  uint16_t missing_current_reading : 1;
 | 
					
 | 
				
			||||||
  uint16_t missing_voltage_reading : 1;
 | 
					  uint16_t temperature_error : 1;
 | 
				
			||||||
  uint16_t missing_temp_reading : 1;
 | 
					  uint16_t current_error : 1;
 | 
				
			||||||
  uint16_t precharge_fail : 1;
 | 
					  uint16_t voltage_error : 1;
 | 
				
			||||||
  uint16_t powerground_fail : 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;
 | 
					} ErrorKind;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//typedef enum {} WarningKind;
 | 
					//typedef enum {} WarningKind;
 | 
				
			||||||
@ -51,6 +58,7 @@ extern StateHandle state;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
void sm_init();
 | 
					void sm_init();
 | 
				
			||||||
void sm_update();
 | 
					void sm_update();
 | 
				
			||||||
 | 
					int sm_get_state_code();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
State sm_update_inactive();
 | 
					State sm_update_inactive();
 | 
				
			||||||
State sm_update_precharge();
 | 
					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_set_relay(Relay relay, bool closed);
 | 
				
			||||||
void sm_check_precharge_discharge(int *is_closed, int should_close);
 | 
					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_handle_ams_in(const uint8_t *data);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void sm_set_error(ErrorKind error_kind, bool is_errored);
 | 
					void sm_set_error(ErrorKind error_kind, bool is_errored);
 | 
				
			||||||
@ -8,17 +8,14 @@
 | 
				
			|||||||
StateHandle state;
 | 
					StateHandle state;
 | 
				
			||||||
static bool relay_closed = 0;
 | 
					static bool relay_closed = 0;
 | 
				
			||||||
static bool precharge_closed = 0;
 | 
					static bool precharge_closed = 0;
 | 
				
			||||||
static int16_t RELAY_BAT_SIDE;
 | 
					static int16_t RELAY_BAT_SIDE = 0;
 | 
				
			||||||
static int16_t RELAY_ESC_SIDE;
 | 
					static int16_t RELAY_ESC_SIDE = 0;
 | 
				
			||||||
static int16_t CURRENT_MEASUREMENT;
 | 
					static int16_t CURRENT_MEASUREMENT = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void sm_init(){
 | 
					void sm_init(){
 | 
				
			||||||
  state.current_state = STATE_INACTIVE;
 | 
					  state.current_state = STATE_INACTIVE;
 | 
				
			||||||
  state.target_state = STATE_INACTIVE;
 | 
					  state.target_state = STATE_INACTIVE;
 | 
				
			||||||
  state.error_source = 0;
 | 
					  state.error_source = 0;
 | 
				
			||||||
  RELAY_BAT_SIDE = 0;
 | 
					 | 
				
			||||||
  RELAY_ESC_SIDE = 0;
 | 
					 | 
				
			||||||
  CURRENT_MEASUREMENT = 0;
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void sm_update(){
 | 
					void sm_update(){
 | 
				
			||||||
@ -51,10 +48,30 @@ void sm_update(){
 | 
				
			|||||||
  //status_led_state(state.current_state, (ErrorKind) state.error_type);
 | 
					  //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(){
 | 
					State sm_update_inactive(){
 | 
				
			||||||
  switch (state.target_state) {
 | 
					  switch (state.target_state) {
 | 
				
			||||||
    case STATE_PRECHARGE:
 | 
					    case STATE_PRECHARGE:
 | 
				
			||||||
      //close precharge relay, wait until both sides are similar
 | 
					      //close precharge relay, wait until both sides are similar
 | 
				
			||||||
 | 
					      sm_set_relay_positions(STATE_PRECHARGE);
 | 
				
			||||||
      return STATE_PRECHARGE;
 | 
					      return STATE_PRECHARGE;
 | 
				
			||||||
    case STATE_CHARGING:
 | 
					    case STATE_CHARGING:
 | 
				
			||||||
      return 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_handle_ams_in(const uint8_t *data){}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void sm_set_error(ErrorKind error_kind, bool is_errored);
 | 
					void sm_set_error(ErrorKind error_kind, bool is_errored);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void sm_check_errors(){}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user