clean up
This commit is contained in:
parent
61e455dfcd
commit
b87a0ca55c
@ -26,7 +26,7 @@ typedef enum { // 7 states -> 3 bit. valid transitions: (all could transition
|
|||||||
STATE_ERROR, // ERROR -> INACTIVE, DISCHARGE, ERROR
|
STATE_ERROR, // ERROR -> INACTIVE, DISCHARGE, ERROR
|
||||||
} State;
|
} State;
|
||||||
|
|
||||||
typedef struct { // 13 errors -> 4 bit
|
typedef struct {
|
||||||
uint16_t bms_timeout : 1;
|
uint16_t bms_timeout : 1;
|
||||||
uint16_t bms_checksum_fail : 1;
|
uint16_t bms_checksum_fail : 1;
|
||||||
uint16_t bms_overtemp : 1;
|
uint16_t bms_overtemp : 1;
|
||||||
@ -58,7 +58,6 @@ 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();
|
||||||
@ -71,7 +70,7 @@ State sm_update_error();
|
|||||||
typedef enum { RELAY_MAIN, RELAY_PRECHARGE } Relay;
|
typedef enum { RELAY_MAIN, RELAY_PRECHARGE } Relay;
|
||||||
void sm_set_relay_positions(State state);
|
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(bool *is_closed, bool should_close);
|
||||||
|
|
||||||
void sm_check_errors();
|
void sm_check_errors();
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
#include "AMS_HighLevel.h"
|
#include "AMS_HighLevel.h"
|
||||||
//#include "TMP1075.h"
|
//#include "TMP1075.h"
|
||||||
#include "can-halal.h"
|
#include "can-halal.h"
|
||||||
|
#include "state_machine.h"
|
||||||
|
|
||||||
void can_init(CAN_HandleTypeDef* hcan) { ftcan_init(hcan); }
|
void can_init(CAN_HandleTypeDef* hcan) { ftcan_init(hcan); }
|
||||||
|
|
||||||
@ -20,7 +21,7 @@ with format of:
|
|||||||
|
|
||||||
*/
|
*/
|
||||||
void can_handle_send_status() {
|
void can_handle_send_status() {
|
||||||
static uint8_t data[8];
|
static uint8_t data[8] = {};
|
||||||
//data[0] = state. (sm_get_state_code() << 5); //save 5 bit since codes are from 0-6
|
data[0] = (state.current_state << 5); //save 5 bit since codes are from 0-6
|
||||||
//ftcan_transmit(id, data, sizeof(data));
|
ftcan_transmit(CAN_ID_OUT, data, sizeof(data));
|
||||||
}
|
}
|
||||||
|
@ -21,52 +21,33 @@ void sm_init(){
|
|||||||
void sm_update(){
|
void sm_update(){
|
||||||
|
|
||||||
switch (state.current_state) {
|
switch (state.current_state) {
|
||||||
case STATE_INACTIVE:
|
case STATE_INACTIVE:
|
||||||
state.current_state = sm_update_inactive(); // moniter only
|
state.current_state = sm_update_inactive(); // monitor only
|
||||||
break;
|
break;
|
||||||
case STATE_PRECHARGE:
|
case STATE_PRECHARGE:
|
||||||
state.current_state = sm_update_precharge(); // set PRECHARGE and turn on cooling at 50% or such
|
state.current_state = sm_update_precharge(); // set PRECHARGE and turn on cooling at 50% or such
|
||||||
break;
|
break;
|
||||||
case STATE_READY:
|
case STATE_READY:
|
||||||
state.current_state = sm_update_ready(); // keep cooling at 50%, get ready to turn on powerground
|
state.current_state = sm_update_ready(); // keep cooling at 50%, get ready to turn on powerground
|
||||||
break;
|
break;
|
||||||
case STATE_ACTIVE:
|
case STATE_ACTIVE:
|
||||||
state.current_state = sm_update_active(); // set PRECHARGE and turn on cooling at 50% or such
|
state.current_state = sm_update_active(); // set PRECHARGE and turn on cooling at 50% or such
|
||||||
break;
|
break;
|
||||||
case STATE_DISCHARGE:
|
case STATE_DISCHARGE:
|
||||||
state.current_state = sm_update_discharge(); // open the main relay, keep PRECHARGE closed
|
state.current_state = sm_update_discharge(); // open the main relay, keep PRECHARGE closed
|
||||||
break;
|
break;
|
||||||
case STATE_CHARGING:
|
case STATE_CHARGING:
|
||||||
state.current_state = sm_update_charging(); // monitor and turn on cooling if needed.
|
state.current_state = sm_update_charging(); // monitor and turn on cooling if needed.
|
||||||
break;
|
break;
|
||||||
case STATE_ERROR:
|
case STATE_ERROR:
|
||||||
state.current_state = sm_update_error(); // enter the correct ERROR state
|
state.current_state = sm_update_error(); // enter the correct ERROR state
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
sm_set_relay_positions(state.current_state);
|
sm_set_relay_positions(state.current_state);
|
||||||
//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:
|
||||||
@ -182,19 +163,19 @@ void sm_set_relay_positions(State current_state){
|
|||||||
void sm_set_relay(Relay relay, bool closed){
|
void sm_set_relay(Relay relay, bool closed){
|
||||||
GPIO_PinState state = closed ? GPIO_PIN_SET : GPIO_PIN_RESET;
|
GPIO_PinState state = closed ? GPIO_PIN_SET : GPIO_PIN_RESET;
|
||||||
switch (relay) {
|
switch (relay) {
|
||||||
case RELAY_MAIN:
|
case RELAY_MAIN:
|
||||||
HAL_GPIO_WritePin(RELAY_EN_GPIO_Port, RELAY_EN_Pin, state);
|
HAL_GPIO_WritePin(RELAY_EN_GPIO_Port, RELAY_EN_Pin, state);
|
||||||
relay_closed = closed;
|
relay_closed = closed;
|
||||||
break;
|
break;
|
||||||
case RELAY_PRECHARGE:
|
case RELAY_PRECHARGE:
|
||||||
HAL_GPIO_WritePin(PRECHARGE_EN_GPIO_Port, PRECHARGE_EN_Pin, state);
|
HAL_GPIO_WritePin(PRECHARGE_EN_GPIO_Port, PRECHARGE_EN_Pin, state);
|
||||||
precharge_closed = closed;
|
precharge_closed = closed;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void sm_check_precharge_discharge(int *is_closed, int should_close){}
|
void sm_check_precharge_discharge(bool *is_closed, bool should_close){}
|
||||||
// compare RELAY_BATT_SIDE and RELAY_ESC_SIDE
|
// compare RELAY_BATT_SIDE and RELAY_ESC_SIDE
|
||||||
// if (state.current_state == STATE_PRECHARGE && (RELAY_ESC_SIDE < RELAY_BAT_SIDE)) //-> don't switch from PRECHARGE to READY
|
// if (state.current_state == STATE_PRECHARGE && (RELAY_ESC_SIDE < RELAY_BAT_SIDE)) //-> don't switch from PRECHARGE to READY
|
||||||
// if (state.current_state == STATE_DISCHARGE && (RELAY_ESC_SIDE > 12V)) -> don't switch from DISCHARGE to INACTIVE
|
// if (state.current_state == STATE_DISCHARGE && (RELAY_ESC_SIDE > 12V)) -> don't switch from DISCHARGE to INACTIVE
|
||||||
|
Loading…
x
Reference in New Issue
Block a user