From b5410639eb7354d1e67b091d78f2b30a6cbe431a Mon Sep 17 00:00:00 2001 From: hamza Date: Sun, 7 Jul 2024 21:16:36 +0300 Subject: [PATCH] V1.5 --- CHANGELOG.txt | 8 +++++++- Core/Inc/ADBMS_Abstraction.h | 2 +- Core/Inc/errors.h | 8 ++++---- Core/Inc/state_machine.h | 2 ++ Core/Src/PWM_control.c | 6 ++---- Core/Src/TMP1075.c | 2 +- Core/Src/errors.c | 6 +++--- Core/Src/main.c | 1 + Core/Src/state_machine.c | 21 +++++++++++++-------- 9 files changed, 34 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 0170e31..c84e494 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -23,4 +23,10 @@ V1.3 V1.4 - replaced ADBMS code with the newest version from the Slaves - added the Author to things i made -- error_source is now set in the state_machine.c \ No newline at end of file +- error_source is now set in the state_machine.c + +V1.5 +- int16_t auxVoltages[MAXIMUM_AUX_VOLTAGES] to float +- errors out when no messages are recieved for CAN_TIMEOUT messages +- void set_error_source(int source); -> void set_error_source(SlaveErrorKind source); +- diff --git a/Core/Inc/ADBMS_Abstraction.h b/Core/Inc/ADBMS_Abstraction.h index c9aacc6..15aab62 100755 --- a/Core/Inc/ADBMS_Abstraction.h +++ b/Core/Inc/ADBMS_Abstraction.h @@ -47,7 +47,7 @@ struct ADBMS6830_Internal_Status { typedef struct { int16_t cellVoltages[MAXIMUM_CELL_VOLTAGES]; - int16_t auxVoltages[MAXIMUM_AUX_VOLTAGES]; + float auxVoltages[MAXIMUM_AUX_VOLTAGES]; struct ADBMS6830_Internal_Status status; uint16 internalDieTemp; diff --git a/Core/Inc/errors.h b/Core/Inc/errors.h index 6d62462..599ab46 100644 --- a/Core/Inc/errors.h +++ b/Core/Inc/errors.h @@ -12,7 +12,7 @@ #define ERROR_TIME_THRESH 150 // ms -typedef enum { +typedef enum : uint16_t { SEK_OVERTEMP = 0x0, SEK_UNDERTEMP = 0x1, SEK_OVERVOLT = 0x2, @@ -27,7 +27,7 @@ typedef enum { } SlaveErrorKind; typedef struct { - int error_sources; + uint16_t error_sources; SlaveErrorKind data_kind; uint8_t data[4]; uint32_t errors_since; @@ -35,7 +35,7 @@ typedef struct { extern SlaveErrorData error_data; -void set_error_source(int source); -void clear_error_source(int source); +void set_error_source(SlaveErrorKind source); +void clear_error_source(SlaveErrorKind source); #endif // INC_ERRORS_H diff --git a/Core/Inc/state_machine.h b/Core/Inc/state_machine.h index c05d67c..891b5b2 100644 --- a/Core/Inc/state_machine.h +++ b/Core/Inc/state_machine.h @@ -25,6 +25,8 @@ #define MAX_CHARGING_CHECK_DURATION 2000 // ms // Time to wait between closing relays #define RELAY_CLOSE_WAIT 10 // ms +// Max time to wait for CAN messages. If we reach it then we emergency shutdown. +#define CAN_TIMEOUT 100000 typedef enum { // states -> 3 bit. valid transitions: (all could transition to error) STATE_INACTIVE, // INACTIVE -> PRECHARGE, CHARGING, ERROR diff --git a/Core/Src/PWM_control.c b/Core/Src/PWM_control.c index 014293b..abe38ba 100644 --- a/Core/Src/PWM_control.c +++ b/Core/Src/PWM_control.c @@ -64,13 +64,11 @@ void PWM_set_throttle(){ uint32_t timestamp = HAL_GetTick() + 5000; while (timestamp > HAL_GetTick()) {} - __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_3, 4000); - __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_4, 4000); + PWM_powerground_control(100); timestamp = HAL_GetTick() + 2000; while (timestamp > HAL_GetTick()) {} - __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_3, 2000); - __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_4, 2000); + PWM_powerground_control(0); timestamp = HAL_GetTick() + 1000; while (timestamp > HAL_GetTick()) {} } diff --git a/Core/Src/TMP1075.c b/Core/Src/TMP1075.c index fcb1d50..ee86e9d 100644 --- a/Core/Src/TMP1075.c +++ b/Core/Src/TMP1075.c @@ -1,6 +1,6 @@ #include "TMP1075.h" -#define MAX_TEMP ((int16_t)(59 / 0.0625f)) +#define MAX_TEMP ((int16_t)(30 / 0.0625f)) #define MAX_FAILED_TEMP 2 //TODO: change value for compliance with the actual number of sensors #warning "change value for compliance with the actual number of sensors" diff --git a/Core/Src/errors.c b/Core/Src/errors.c index 8934b3c..cc8aafe 100644 --- a/Core/Src/errors.c +++ b/Core/Src/errors.c @@ -3,11 +3,11 @@ SlaveErrorData error_data; -void set_error_source(int source) { +void set_error_source(SlaveErrorKind source) { if (!error_data.error_sources) { error_data.errors_since = HAL_GetTick(); } - error_data.error_sources |= source; + error_data.error_sources |= (1 << source); } -void clear_error_source(int source) { error_data.error_sources &= ~source; } +void clear_error_source(SlaveErrorKind source) { error_data.error_sources &= ~(1 << source); } diff --git a/Core/Src/main.c b/Core/Src/main.c index ea3898e..ac8462c 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -148,6 +148,7 @@ int main(void) /* USER CODE BEGIN 3 */ AMS_Loop(); sm_update(); + tmp1075_measure(); //sm_test_cycle_states(); can_handle_send_status(); } diff --git a/Core/Src/state_machine.c b/Core/Src/state_machine.c index a4fb82f..916629b 100644 --- a/Core/Src/state_machine.c +++ b/Core/Src/state_machine.c @@ -11,6 +11,7 @@ #include "TMP1075.h" #include "errors.h" #include "main.h" +#include "stm32f302xc.h" #include "stm32f3xx_hal.h" #include @@ -23,6 +24,7 @@ bool CURRENT_MEASUREMENT_ON; uint8_t powerground_status; uint32_t precharge_timer; uint32_t discharge_timer; +uint32_t CAN_timer; uint32_t powerground_calibration_timer; uint8_t powerground_calibration_stage; @@ -33,7 +35,8 @@ void sm_init(){ state.current_state = STATE_INACTIVE; state.target_state = STATE_INACTIVE; state.error_source = 0; - precharge_timer = discharge_timer = powerground_calibration_timer = 0; + precharge_timer = discharge_timer = powerground_calibration_timer; + CAN_timer = HAL_GetTick() + 5000; } #warning change amsState here @@ -41,7 +44,9 @@ void sm_update(){ sm_check_errors(); sm_precharge_discharge_manager(); sm_calibrate_powerground(); - + if (CAN_timer < HAL_GetTick()) + state.current_state = state.target_state = STATE_ERROR; + int16_t base_offset = 0; if (state.current_state == STATE_INACTIVE){ base_offset = module.auxVoltages[0]; @@ -288,6 +293,7 @@ void sm_calibrate_powerground(){ } void sm_handle_ams_in(const uint8_t *data){ + CAN_timer = HAL_GetTick() + CAN_TIMEOUT; switch (data[0]) { case 0x00: if (state.current_state != STATE_INACTIVE){ @@ -337,10 +343,10 @@ bool sm_is_errored(){ #warning TODO: add error checking for everything here 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.temperature_error = (error_data.error_sources & (1 << 0) || error_data.error_sources & (1 << 1) || error_data.error_sources & (1 << 4)) ? 1 : 0; + state.error_type.voltage_error = (error_data.error_sources & (1 << 2)|| error_data.error_sources & (1 << 3)|| error_data.error_sources & (1 << 5) || RELAY_BAT_SIDE_VOLTAGE < 30000) ? 1 : 0; + state.error_type.bms_timeout = (error_data.error_sources & (1 << 7)) ? 1 : 0; + state.error_type.bms_fault = (error_data.error_sources & (1 << 8) || error_data.error_sources & (1 << 10) || error_data.error_sources & (1 << 9)) ? 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; @@ -354,8 +360,7 @@ void sm_check_errors(){ state.current_state = STATE_DISCHARGE; state.target_state = STATE_ERROR; PWM_powerground_control(255); - } else { - if (state.current_state == STATE_ERROR) + } else if (state.current_state == STATE_ERROR){ state.target_state = STATE_INACTIVE; } sm_error_source();