This commit is contained in:
hamza 2024-07-07 21:16:36 +03:00
parent 5a84a349d9
commit b5410639eb
9 changed files with 34 additions and 22 deletions

View File

@ -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
- 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);
-

View File

@ -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;

View File

@ -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

View File

@ -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

View File

@ -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()) {}
}

View File

@ -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"

View File

@ -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); }

View File

@ -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();
}

View File

@ -11,6 +11,7 @@
#include "TMP1075.h"
#include "errors.h"
#include "main.h"
#include "stm32f302xc.h"
#include "stm32f3xx_hal.h"
#include <stdint.h>
@ -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();