diff --git a/Core/Inc/state_machine.h b/Core/Inc/state_machine.h index ce0356f..e1326f2 100644 --- a/Core/Inc/state_machine.h +++ b/Core/Inc/state_machine.h @@ -26,7 +26,7 @@ typedef enum { // 7 states -> 3 bit. valid transitions: (all could transition STATE_ERROR, // ERROR -> INACTIVE, DISCHARGE, ERROR } State; -typedef struct { // 13 errors -> 4 bit +typedef struct { uint16_t bms_timeout : 1; uint16_t bms_checksum_fail : 1; uint16_t bms_overtemp : 1; @@ -58,7 +58,6 @@ extern StateHandle state; void sm_init(); void sm_update(); -int sm_get_state_code(); State sm_update_inactive(); State sm_update_precharge(); @@ -71,7 +70,7 @@ State sm_update_error(); 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_precharge_discharge(int *is_closed, int should_close); +void sm_check_precharge_discharge(bool *is_closed, bool should_close); void sm_check_errors(); diff --git a/Core/Src/can.c b/Core/Src/can.c index a8e239a..963c179 100644 --- a/Core/Src/can.c +++ b/Core/Src/can.c @@ -9,6 +9,7 @@ #include "AMS_HighLevel.h" //#include "TMP1075.h" #include "can-halal.h" +#include "state_machine.h" void can_init(CAN_HandleTypeDef* hcan) { ftcan_init(hcan); } @@ -20,7 +21,7 @@ with format of: */ void can_handle_send_status() { - static uint8_t data[8]; - //data[0] = state. (sm_get_state_code() << 5); //save 5 bit since codes are from 0-6 - //ftcan_transmit(id, data, sizeof(data)); + static uint8_t data[8] = {}; + data[0] = (state.current_state << 5); //save 5 bit since codes are from 0-6 + ftcan_transmit(CAN_ID_OUT, data, sizeof(data)); } diff --git a/Core/Src/state_machine.c b/Core/Src/state_machine.c index 86afba4..b79f3e0 100644 --- a/Core/Src/state_machine.c +++ b/Core/Src/state_machine.c @@ -21,52 +21,33 @@ void sm_init(){ void sm_update(){ switch (state.current_state) { - case STATE_INACTIVE: - state.current_state = sm_update_inactive(); // moniter only - break; - case STATE_PRECHARGE: - state.current_state = sm_update_precharge(); // set PRECHARGE and turn on cooling at 50% or such - break; - case STATE_READY: - state.current_state = sm_update_ready(); // keep cooling at 50%, get ready to turn on powerground - break; - case STATE_ACTIVE: - state.current_state = sm_update_active(); // set PRECHARGE and turn on cooling at 50% or such - break; - case STATE_DISCHARGE: - state.current_state = sm_update_discharge(); // open the main relay, keep PRECHARGE closed - break; - case STATE_CHARGING: - state.current_state = sm_update_charging(); // monitor and turn on cooling if needed. - break; - case STATE_ERROR: - state.current_state = sm_update_error(); // enter the correct ERROR state - break; + case STATE_INACTIVE: + state.current_state = sm_update_inactive(); // monitor only + break; + case STATE_PRECHARGE: + state.current_state = sm_update_precharge(); // set PRECHARGE and turn on cooling at 50% or such + break; + case STATE_READY: + state.current_state = sm_update_ready(); // keep cooling at 50%, get ready to turn on powerground + break; + case STATE_ACTIVE: + state.current_state = sm_update_active(); // set PRECHARGE and turn on cooling at 50% or such + break; + case STATE_DISCHARGE: + state.current_state = sm_update_discharge(); // open the main relay, keep PRECHARGE closed + break; + case STATE_CHARGING: + state.current_state = sm_update_charging(); // monitor and turn on cooling if needed. + break; + case STATE_ERROR: + state.current_state = sm_update_error(); // enter the correct ERROR state + break; } sm_set_relay_positions(state.current_state); //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(){ switch (state.target_state) { case STATE_PRECHARGE: @@ -182,19 +163,19 @@ void sm_set_relay_positions(State current_state){ void sm_set_relay(Relay relay, bool closed){ GPIO_PinState state = closed ? GPIO_PIN_SET : GPIO_PIN_RESET; switch (relay) { - case RELAY_MAIN: - HAL_GPIO_WritePin(RELAY_EN_GPIO_Port, RELAY_EN_Pin, state); - relay_closed = closed; - break; - case RELAY_PRECHARGE: - HAL_GPIO_WritePin(PRECHARGE_EN_GPIO_Port, PRECHARGE_EN_Pin, state); - precharge_closed = closed; - break; + case RELAY_MAIN: + HAL_GPIO_WritePin(RELAY_EN_GPIO_Port, RELAY_EN_Pin, state); + relay_closed = closed; + break; + case RELAY_PRECHARGE: + HAL_GPIO_WritePin(PRECHARGE_EN_GPIO_Port, PRECHARGE_EN_Pin, state); + precharge_closed = closed; + 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 // 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