V1.3
This commit is contained in:
@ -16,7 +16,6 @@ bool CURRENT_MEASUREMENT_ON;
|
||||
uint8_t powerground_status;
|
||||
uint32_t precharge_timer;
|
||||
uint32_t discharge_timer;
|
||||
uint32_t charging_timer;
|
||||
|
||||
uint32_t powerground_calibration_timer;
|
||||
uint8_t powerground_calibration_stage;
|
||||
@ -27,14 +26,15 @@ void sm_init(){
|
||||
state.current_state = STATE_INACTIVE;
|
||||
state.target_state = STATE_INACTIVE;
|
||||
state.error_source = 0;
|
||||
precharge_timer = discharge_timer = charging_timer = powerground_calibration_timer = 0;
|
||||
precharge_timer = discharge_timer = powerground_calibration_timer = 0;
|
||||
}
|
||||
|
||||
#warning change amsState here
|
||||
void sm_update(){
|
||||
sm_check_errors();
|
||||
sm_precharge_discharge_manager();
|
||||
sm_calibrate_powerground();
|
||||
|
||||
|
||||
int16_t base_offset = 0;
|
||||
if (state.current_state == STATE_INACTIVE){
|
||||
base_offset = module.auxVoltages[0];
|
||||
@ -202,24 +202,13 @@ void sm_set_relay(Relay relay, bool closed){
|
||||
switch (relay) {
|
||||
case RELAY_MAIN:
|
||||
HAL_GPIO_WritePin(RELAY_ENABLE_GPIO_Port, RELAY_ENABLE_Pin, state);
|
||||
relay_closed = closed;
|
||||
break;
|
||||
case RELAY_PRECHARGE:
|
||||
HAL_GPIO_WritePin(PRECHARGE_ENABLE_GPIO_Port, PRECHARGE_ENABLE_Pin, state);
|
||||
precharge_closed = closed;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void sm_check_charging(){
|
||||
#warning fix this timestamp check
|
||||
if (RELAY_BAT_SIDE_VOLTAGE < RELAY_ESC_SIDE_VOLTAGE && timestamp == 0)
|
||||
timestamp = HAL_GetTick() + 5000;
|
||||
|
||||
if (timestamp < HAL_GetTick())
|
||||
state.target_state = STATE_CHARGING_PRECHARGE;
|
||||
}
|
||||
|
||||
/* 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++) {
|
||||
@ -232,13 +221,6 @@ void sm_check_battery_temperature(int8_t *id, int16_t *temp){
|
||||
|
||||
void sm_precharge_discharge_manager(){
|
||||
|
||||
if (state.current_state != STATE_DISCHARGE && state.target_state == STATE_DISCHARGE){
|
||||
discharge_timer = HAL_GetTick() + DISCHARGE_DURATION;
|
||||
} else if (state.current_state == STATE_DISCHARGE && discharge_timer < HAL_GetTick()) {
|
||||
state.target_state = STATE_INACTIVE;
|
||||
discharge_timer = 0;
|
||||
}
|
||||
|
||||
if (state.current_state != STATE_PRECHARGE && state.target_state == STATE_PRECHARGE){
|
||||
precharge_timer = HAL_GetTick() + PRECHARGE_DURATION;
|
||||
} else if (state.current_state == STATE_PRECHARGE && precharge_timer < HAL_GetTick()) {
|
||||
@ -249,32 +231,17 @@ void sm_precharge_discharge_manager(){
|
||||
if (state.current_state != STATE_CHARGING_PRECHARGE && state.target_state == STATE_CHARGING_PRECHARGE){
|
||||
precharge_timer = HAL_GetTick() + PRECHARGE_DURATION;
|
||||
} else if (state.current_state == STATE_CHARGING_PRECHARGE && precharge_timer < HAL_GetTick()) {
|
||||
state.target_state = STATE_READY;
|
||||
state.target_state = STATE_CHARGING;
|
||||
precharge_timer = 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// source 0 -> sm_update()
|
||||
// source 1 -> sm_ams_in()
|
||||
void sm_powerground_manager(int8_t percent, bool source){
|
||||
if (powerground_calibration_stage != 4 && state.current_state == STATE_ACTIVE){
|
||||
sm_calibrate_powerground();
|
||||
} else if (powerground_calibration_stage == 4){
|
||||
if (state.current_state == STATE_PRECHARGE){
|
||||
PWM_powerground_control(0);
|
||||
} else if (state.current_state == STATE_READY || state.current_state == STATE_ACTIVE){
|
||||
if (percent < 10){
|
||||
PWM_powerground_control(0);
|
||||
} else if (percent > 100){
|
||||
PWM_powerground_control(255);
|
||||
state.current_state = STATE_ACTIVE;
|
||||
}
|
||||
PWM_powerground_control(percent);
|
||||
} else {
|
||||
PWM_powerground_control(255);
|
||||
}
|
||||
if (state.current_state != STATE_DISCHARGE && state.target_state == STATE_DISCHARGE){
|
||||
discharge_timer = HAL_GetTick() + DISCHARGE_DURATION;
|
||||
} else if (state.current_state == STATE_DISCHARGE && discharge_timer < HAL_GetTick()) {
|
||||
state.target_state = STATE_INACTIVE;
|
||||
discharge_timer = 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void sm_calibrate_powerground(){
|
||||
@ -330,7 +297,15 @@ void sm_handle_ams_in(const uint8_t *data){
|
||||
state.target_state = STATE_ACTIVE; // READY -> ACTIVE
|
||||
}
|
||||
break;
|
||||
case 0xFF: // emergency shutdown or EEPROM
|
||||
case 0xF0:
|
||||
if (state.current_state == STATE_INACTIVE){
|
||||
state.target_state = STATE_CHARGING_PRECHARGE;
|
||||
}
|
||||
break;
|
||||
#warning implement this
|
||||
case 0xF1: // EEPROM
|
||||
break;
|
||||
case 0xFF: // EMERGENCY SHUTDOWN
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -380,13 +355,9 @@ void sm_check_errors(){
|
||||
}
|
||||
}
|
||||
|
||||
int16_t sm_return_cell_temperature(int id){
|
||||
return tmp1075_temps[id];
|
||||
}
|
||||
int16_t sm_return_cell_temperature(int id){ return tmp1075_temps[id]; }
|
||||
|
||||
int16_t sm_return_cell_voltage(int id){
|
||||
return module.cellVoltages[id];
|
||||
}
|
||||
int16_t sm_return_cell_voltage(int id){ return module.cellVoltages[id]; }
|
||||
|
||||
void sm_test_cycle_states(){
|
||||
RELAY_BAT_SIDE_VOLTAGE = module.auxVoltages[0];
|
||||
@ -419,7 +390,11 @@ void sm_test_cycle_states(){
|
||||
state.current_state = STATE_INACTIVE;
|
||||
timestamp = HAL_GetTick() + 10000;
|
||||
break;
|
||||
}
|
||||
case STATE_CHARGING_PRECHARGE:
|
||||
case STATE_CHARGING:
|
||||
case STATE_ERROR:
|
||||
break;
|
||||
}
|
||||
|
||||
state.target_state = state.current_state;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user