added state_machine.c
This commit is contained in:
		
							
								
								
									
										71
									
								
								Core/Inc/state_machine.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										71
									
								
								Core/Inc/state_machine.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,71 @@
 | 
			
		||||
//int errorcode[2] = {0,0}; 1 Bit per error
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
 | 
			
		||||
// Minimum vehicle side voltage to exit precharge
 | 
			
		||||
#define MIN_VEHICLE_SIDE_VOLTAGE 150000 // mV
 | 
			
		||||
// Time to wait after reaching 95% of battery voltage before exiting precharge
 | 
			
		||||
// Set this to 1000 in scruti to demonstrate the voltage on the multimeter
 | 
			
		||||
#define PRECHARGE_95_DURATION 0 // ms
 | 
			
		||||
// Time to wait for discharge
 | 
			
		||||
#define DISCHARGE_DURATION 5000 // ms
 | 
			
		||||
// Time to wait after there is no more error condition before exiting TS_ERROR
 | 
			
		||||
#define NO_ERROR_TIME 1000 // ms
 | 
			
		||||
// Time to wait for charger voltage before going to TS_ERROR
 | 
			
		||||
#define MAX_CHARGING_CHECK_DURATION 2000 // ms
 | 
			
		||||
// Time to wait between closing relays
 | 
			
		||||
#define RELAY_CLOSE_WAIT 10 // ms
 | 
			
		||||
 | 
			
		||||
typedef enum {    // valid transitions: (all could transition to error)
 | 
			
		||||
  INACTIVE,       // INACTIVE   ->  PRECHARGE, CHARGING, ERROR  
 | 
			
		||||
  PRECHARGE,      // PRECHARGE  ->  INACTIVE, READY, DISCHARGE, ERROR
 | 
			
		||||
  READY,          // READY      ->  ACTIVE, DISCHARGE, ERROR
 | 
			
		||||
  ACTIVE,         // ACTIVE     ->  READY, DISCHARGE, ERROR
 | 
			
		||||
  DISCHARGE,      // DISCHARGE  ->  INACTIVE, PRECHARGE, ERROR
 | 
			
		||||
  CHARGING,       // CHARGING   ->  INACTIVE, DISCHARGE, ERROR
 | 
			
		||||
  ERROR,          // ERROR      ->  INACTIVE, DISCHARGE, ERROR
 | 
			
		||||
} State;
 | 
			
		||||
  
 | 
			
		||||
typedef enum { 
 | 
			
		||||
  ERRORKIND_NONE = 0x0,
 | 
			
		||||
  ERRORKIND_BMS_TIMEOUT = 0x1,
 | 
			
		||||
  ERRORKIND_OVERTEMPTURE = 0x2,
 | 
			
		||||
  ERRORKIND_OVERCURRENT = 0x3,
 | 
			
		||||
  ERRORKIND_OVERVOLTAGE = 0x4,
 | 
			
		||||
  ERRORKIND_MISSING_CURRENT_READING = 0x5,
 | 
			
		||||
  ERRORKIND_MISSING_VOLTAGE_READING = 0x6,
 | 
			
		||||
  ERRORKIND_MISSING_TEMP_READING = 0x7,
 | 
			
		||||
  ERRORKIND_PRECHARGE_FAIL = 0x8,
 | 
			
		||||
  ERRORKIND_POWERGROUND_FAIL = 0x9
 | 
			
		||||
} ErrorKind;
 | 
			
		||||
 | 
			
		||||
//typedef enum {} WarningKind;
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
  State current_state;
 | 
			
		||||
  State target_state;
 | 
			
		||||
  uint16_t error_source; // TSErrorSource (bitmask)
 | 
			
		||||
  uint16_t error_type; // TSErrorKind
 | 
			
		||||
} StateHandle;
 | 
			
		||||
 | 
			
		||||
extern StateHandle state;
 | 
			
		||||
 | 
			
		||||
void sm_init();
 | 
			
		||||
void sm_update();
 | 
			
		||||
 | 
			
		||||
State sm_update_inactive();
 | 
			
		||||
State sm_update_active();
 | 
			
		||||
State sm_update_precharge();
 | 
			
		||||
State sm_update_discharge();
 | 
			
		||||
State sm_update_error();
 | 
			
		||||
State sm_update_charging_check();
 | 
			
		||||
State sm_update_charging();
 | 
			
		||||
 | 
			
		||||
typedef enum { RELAY_POS, RELAY_PRECHARGE } Relay;
 | 
			
		||||
void sm_set_relay_positions(State state);
 | 
			
		||||
void sm_set_relay_position(Relay relay, int closed);
 | 
			
		||||
void sm_check_close_wait(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);
 | 
			
		||||
		Reference in New Issue
	
	Block a user