This commit is contained in:
hamza
2024-07-07 18:52:54 +03:00
parent e7b5054e02
commit a4a856eb31
14 changed files with 126 additions and 260 deletions

View File

@ -1,3 +1,10 @@
/*
* state_machine.h
*
* Created on: 07.07.2024
* Author: Hamza
*/
#include "state_machine.h"
#include "AMS_HighLevel.h"
#include "PWM_control.h"
@ -23,8 +30,8 @@ uint8_t powerground_calibration_stage;
static uint32_t timestamp;
void sm_init(){
state.current_state = STATE_ERROR;
state.target_state = STATE_ERROR;
state.current_state = STATE_INACTIVE;
state.target_state = STATE_INACTIVE;
state.error_source = 0;
precharge_timer = discharge_timer = powerground_calibration_timer = 0;
}
@ -329,39 +336,14 @@ bool sm_is_errored(){
*/
#warning TODO: add error checking for everything here
void sm_check_errors(){
switch (error_data.data_kind) {
case SEK_OVERTEMP:
case SEK_UNDERTEMP:
case SEK_TOO_FEW_TEMPS:
state.error_type.temperature_error = 1;
break;
case SEK_OVERVOLT:
case SEK_UNDERVOLT:
case SEK_OPENWIRE:
state.error_type.voltage_error = 1;
break;
case SEK_EEPROM_ERR:
//state.error_type.eeprom_error = 1;
break;
case SEK_INTERNAL_BMS_TIMEOUT:
state.error_type.bms_timeout = 1;
break;
case SEK_INTERNAL_BMS_CHECKSUM_FAIL:
case SEK_INTERNAL_BMS_OVERTEMP:
case SEK_INTERNAL_BMS_FAULT:
state.error_type.bms_fault = 1;
break;
}
state.error_type.temperature_error = (error_data.data_kind == SEK_OVERTEMP || error_data.data_kind == SEK_UNDERTEMP ||error_data.data_kind == SEK_TOO_FEW_TEMPS) ? 1 : 0;
state.error_type.voltage_error = (error_data.data_kind == SEK_OVERVOLT || error_data.data_kind == SEK_UNDERVOLT ||error_data.data_kind == SEK_OPENWIRE) ? 1 : 0;
void sm_check_errors(){
state.error_type.temperature_error = (error_data.data_kind == SEK_OVERTEMP || error_data.data_kind == SEK_UNDERTEMP || error_data.data_kind == SEK_TOO_FEW_TEMPS) ? 1 : 0;
state.error_type.voltage_error = (error_data.data_kind == SEK_OVERVOLT || error_data.data_kind == SEK_UNDERVOLT || error_data.data_kind == SEK_OPENWIRE || RELAY_BAT_SIDE_VOLTAGE < 30000) ? 1 : 0;
state.error_type.bms_timeout = (error_data.data_kind == SEK_INTERNAL_BMS_TIMEOUT) ? 1 : 0;
state.error_type.bms_fault = (error_data.data_kind == SEK_INTERNAL_BMS_CHECKSUM_FAIL || error_data.data_kind == SEK_INTERNAL_BMS_FAULT || error_data.data_kind == SEK_INTERNAL_BMS_OVERTEMP) ? 1 : 0;
state.error_type.bms_fault = (error_data.data_kind == SEK_INTERNAL_BMS_CHECKSUM_FAIL || error_data.data_kind == SEK_INTERNAL_BMS_FAULT /*|| error_data.data_kind == SEK_INTERNAL_BMS_OVERTEMP*/) ? 1 : 0;
//SEK_EEPROM_ERR: state.error_type.eeprom_error = 1;
state.error_type.current_error = (powerground_status > 10 && CURRENT_MEASUREMENT < 1000) ? 1 : 0;
state.error_type.current_sensor_missing = (!CURRENT_MEASUREMENT_ON) ? 1 : 0;
state.error_type.voltage_error = (RELAY_BAT_SIDE_VOLTAGE < 30000) ? 1 : 0;
state.error_type.voltage_missing = (RELAY_BAT_SIDE_VOLTAGE < 1000) ? 1 : 0;
@ -376,7 +358,7 @@ void sm_check_errors(){
if (state.current_state == STATE_ERROR)
state.target_state = STATE_INACTIVE;
}
sm_error_source();
}
int16_t sm_return_cell_temperature(int id){ return tmp1075_temps[id]; }
@ -422,3 +404,16 @@ void sm_test_cycle_states(){
state.target_state = state.current_state;
}
void sm_error_source(){
state.error_source = 0;
state.error_source |= (state.error_type.bms_timeout << 0);
state.error_source |= (state.error_type.bms_fault << 1);
state.error_source |= (state.error_type.temperature_error << 2);
state.error_source |= (state.error_type.current_error << 3);
state.error_source |= (state.error_type.current_sensor_missing << 4);
state.error_source |= (state.error_type.voltage_error << 5);
state.error_source |= (state.error_type.voltage_missing << 6);
state.error_source |= (state.error_type.state_transition_fail << 7);
}