From e33579318e1b2defd9d2a026240b67ba42c268e3 Mon Sep 17 00:00:00 2001 From: hamza Date: Fri, 19 Jul 2024 17:40:53 +0300 Subject: [PATCH] V1.14 --- CHANGELOG.txt | 9 ++++- Core/Inc/state_machine.h | 3 ++ Core/Inc/status_LED.h | 1 - Core/Src/PWM_control.c | 1 + Core/Src/can.c | 9 ++++- Core/Src/main.c | 4 +- Core/Src/state_machine.c | 43 +++++++++++---------- Core/Src/status_LED.c | 83 +++++++++++++++++++--------------------- 8 files changed, 82 insertions(+), 71 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 82f5ddf..83eb4a7 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -81,4 +81,11 @@ V1.13 - moved macros and libraries from eeprom.c to h - added can.h to state_machine.h - added balancing to AMS_HighLevel.c (hopefully) -- fixed can_handle_dump() \ No newline at end of file +- fixed can_handle_dump() + +V1.14 +- added programming mode to program the ESCs +- removed void status_led_blink_sequence(uint8_t blinks, color color) and added proper blinking +- fixed the bit shift of temperature in the can message +- moved tmp1075_measure() and can_handle_send_status() to the state_machine +- diff --git a/Core/Inc/state_machine.h b/Core/Inc/state_machine.h index d02e79e..1463596 100644 --- a/Core/Inc/state_machine.h +++ b/Core/Inc/state_machine.h @@ -55,6 +55,8 @@ typedef struct { typedef enum { RELAY_MAIN, RELAY_PRECHARGE } Relay; extern StateHandle state; +extern bool programming_mode; + extern int32_t RELAY_BAT_SIDE_VOLTAGE; extern int32_t RELAY_ESC_SIDE_VOLTAGE; extern int32_t CURRENT_MEASUREMENT; @@ -67,6 +69,7 @@ void sm_powerground_manager(); void sm_calibrate_powerground(); void sm_balancing(); void sm_eeprom_write_status(); +void sm_program_powerground(); void sm_check_errors(); void sm_set_error_source(); diff --git a/Core/Inc/status_LED.h b/Core/Inc/status_LED.h index 44c7ec9..f476c16 100644 --- a/Core/Inc/status_LED.h +++ b/Core/Inc/status_LED.h @@ -25,7 +25,6 @@ typedef enum { void status_led_init(TIM_HandleTypeDef* r, TIM_HandleTypeDef* g, TIM_HandleTypeDef* b); void status_led_update(); -void status_led_blink_sequence(uint8_t blinks, color color); void status_led_set_color(color color); void status_led_set(uint8_t r, uint8_t g, uint8_t b); diff --git a/Core/Src/PWM_control.c b/Core/Src/PWM_control.c index ecd54d2..3a3aeb3 100644 --- a/Core/Src/PWM_control.c +++ b/Core/Src/PWM_control.c @@ -66,5 +66,6 @@ void PWM_powerground_softcontrol(){ __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_3, ccr); __HAL_TIM_SET_COMPARE(powerground, TIM_CHANNEL_4, ccr); } + void PWM_battery_cooling_control(uint8_t percent){} void PWM_esc_cooling(uint8_t percent){} diff --git a/Core/Src/can.c b/Core/Src/can.c index 0e6a1ff..922ffc9 100644 --- a/Core/Src/can.c +++ b/Core/Src/can.c @@ -16,7 +16,7 @@ #define CAN_ID_IN 0x501 #define CAN_ID_OUT 0x502 -#define CAN_STATUS_FREQ 100 +#define CAN_STATUS_FREQ 1000 #define CAN_DUMP_FREQ 10; uint8_t last_message[8]; @@ -85,7 +85,7 @@ void can_handle_send_status() { data[4] = ((RELAY_BAT_SIDE_VOLTAGE / 1000)); // 8 bit battery voltage data[5] = ((RELAY_ESC_SIDE_VOLTAGE / 1000)); // 8 bit Inverter voltage data[6] = ((CURRENT_MEASUREMENT / 1000)); // 8 bit Current - data[7] = ((highest_temp) >> 8); // 8 bit highest cell temperature + data[7] = ((highest_temp) >> 4); // 8 bit highest cell temperature //data[6] = (module.cellVoltages[7] >> 8); //data[7] = (module.cellVoltages[7]); //data[7] = state.error_source; @@ -158,6 +158,11 @@ it only checks if the id is and datalen is correct thans hands data over to can_ in MXCUBE under CAN NVIC settings "USB low priority or CAN_RX0 interrupts" has to be on */ void ftcan_msg_received_cb(uint16_t id, size_t datalen, const uint8_t *data){ + if (programming_mode == 1){ + can_handle_recieve_command(data); + return; + } + if (id == 0x501 && datalen == 2){ if (last_message[0] != data[0] || last_message[1] != data[1]){ last_message[0] = data[0]; diff --git a/Core/Src/main.c b/Core/Src/main.c index f9f54ce..41626cc 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -138,7 +138,7 @@ int main(void) PWM_control_init(&htim3, &htim2, &htim15); soc_init(); status_led_init(&htim4, &htim4, &htim4); - + sm_program_powerground(); //AMS_Loop(); //int ttrgrtd = 2000 + HAL_GetTick(); //while (ttrgrtd > HAL_GetTick()); @@ -153,9 +153,7 @@ int main(void) /* USER CODE BEGIN 3 */ AMS_Loop(); sm_update(); - tmp1075_measure(); //sm_test_cycle_states(); - can_handle_send_status(); } /* USER CODE END 3 */ } diff --git a/Core/Src/state_machine.c b/Core/Src/state_machine.c index 528471f..8250b03 100644 --- a/Core/Src/state_machine.c +++ b/Core/Src/state_machine.c @@ -7,6 +7,7 @@ #include #include "AMS_HighLevel.h" +#include "PWM_control.h" // Time to wait after reaching 95% of battery voltage before exiting precharge // Set this to 1000 in scruti to demonstrate the voltage on the multimeter @@ -22,6 +23,10 @@ // waiting time between to eeprom writes #define EEPROM_WRITE_FREQ 1000 +#define POWERGROUND_SOFTSTART_INCREMENT_DELAY 3 + +bool programming_mode; + StateHandle state; int32_t RELAY_BAT_SIDE_VOLTAGE; int32_t RELAY_ESC_SIDE_VOLTAGE; @@ -49,27 +54,27 @@ void sm_init(){ state.error_source = 0; precharge_timer = discharge_timer = powerground_calibration_timer; CAN_timer = HAL_GetTick() + 5000; + programming_mode = 0; } #warning change amsState here void sm_update(){ + can_handle_send_status(); sm_check_errors(); sm_precharge_discharge_manager(); sm_calibrate_powerground(); sm_powerground_manager(); + tmp1075_measure(); + status_led_update(); soc_update(); - if (CAN_timer < HAL_GetTick()) - state.current_state = state.target_state = STATE_ERROR; + //if (CAN_timer < HAL_GetTick()) + // state.current_state = state.target_state = STATE_ERROR; + CURRENT_MEASUREMENT = (module.auxVoltages[0] > 2494) ? (module.auxVoltages[0] - (2494.0)) * (300.0) : 0; - if (state.current_state == STATE_INACTIVE || state.current_state == STATE_ERROR){ - CURRENT_MEASUREMENT = (module.auxVoltages[0] > 2494) ? (module.auxVoltages[0] - (2494.0)) * (300.0) : 0; - } else { - CURRENT_MEASUREMENT = (module.auxVoltages[0] > 2477) ? (module.auxVoltages[0] - (2477.0)) * (4600.0) : 0; - } CURRENT_MEASUREMENT_ON = (module.auxVoltages[1] > 2400); - RELAY_ESC_SIDE_VOLTAGE = module.auxVoltages[2] * 15.98; - RELAY_BAT_SIDE_VOLTAGE = module.auxVoltages[3] * 15.98; // the calculation says the factor is 11. 11.711 yields the better result + RELAY_ESC_SIDE_VOLTAGE = module.auxVoltages[2] * 15.25; + RELAY_BAT_SIDE_VOLTAGE = module.auxVoltages[3] * 15.25; // the calculation says the factor is 11. 11.711 yields the better result switch (state.current_state) { @@ -104,6 +109,8 @@ void sm_update(){ } void sm_handle_ams_in(const uint8_t *data){ + if (programming_mode == 1 && (state.current_state == STATE_READY || state.current_state == STATE_ACTIVE)){ PWM_powerground_control(data[1]); } + CAN_timer = HAL_GetTick() + CAN_TIMEOUT; switch (data[0]) { case 0x00: @@ -181,11 +188,11 @@ void sm_powerground_manager(){ if (current_powerground_status < target_powerground_status ){ current_powerground_status++; PWM_powerground_softcontrol(); - powerground_softstart_timer = HAL_GetTick() + 10; + powerground_softstart_timer = HAL_GetTick() + POWERGROUND_SOFTSTART_INCREMENT_DELAY; } else if (current_powerground_status > target_powerground_status) { current_powerground_status--; PWM_powerground_softcontrol(); - powerground_softstart_timer = HAL_GetTick() + 10; + powerground_softstart_timer = HAL_GetTick() + POWERGROUND_SOFTSTART_INCREMENT_DELAY; } } } @@ -221,17 +228,12 @@ void sm_calibrate_powerground(){ } void sm_program_powerground(){ - powerground_calibration_timer = HAL_GetTick() + 5000; - while (powerground_calibration_timer > HAL_GetTick()){} - state.current_state = STATE_PRECHARGE; - sm_update(); - - powerground_calibration_timer = HAL_GetTick() + PRECHARGE_DURATION + 1000; + if (programming_mode == 0) + return; PWM_powerground_control(100); - while (powerground_calibration_timer > HAL_GetTick()){} - sm_update(); + state.current_state = state.target_state = STATE_ACTIVE; while (1) { - + can_handle_send_status(); } } @@ -243,6 +245,7 @@ void sm_eeprom_write_status(){ } void sm_check_errors(){ + if (programming_mode == 1) {return;} // to disable error checking 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; diff --git a/Core/Src/status_LED.c b/Core/Src/status_LED.c index 12ad8f6..b75685a 100644 --- a/Core/Src/status_LED.c +++ b/Core/Src/status_LED.c @@ -25,15 +25,14 @@ Prescaler: F_CLK = 16 MHz */ -#define STATUS_LED_ARR 255 -#define BLINK_LENGTH 300 -#define TIME_BETWEEN_BLINKS 200 -#define TIME_BETWEEN_BLINK_SEQS 500 +#define STATUS_LED_ARR 255 +#define TIME_BLINK_OFF 100 +#define TIME_BLINK_ON 1000 TIM_HandleTypeDef* red; TIM_HandleTypeDef* green; TIM_HandleTypeDef* blue; -uint8_t blinked; + uint32_t blink_timer; bool blink_state; @@ -44,7 +43,6 @@ void status_led_init(TIM_HandleTypeDef* r, TIM_HandleTypeDef* g, TIM_HandleTypeD blink_timer = 0; blink_state = 0; - blinked = 0; HAL_TIM_PWM_Start(red, TIM_CHANNEL_1); //TIM4CH1 HAL_TIM_PWM_Start(green, TIM_CHANNEL_2); //TIM4CH2 @@ -54,6 +52,24 @@ void status_led_init(TIM_HandleTypeDef* r, TIM_HandleTypeDef* g, TIM_HandleTypeD } void status_led_update(){ + if (state.current_state == STATE_ERROR){ + status_led_set_color(RED); + return; + } + if(blink_timer > HAL_GetTick()){ + return; + } + + if (blink_state == 1){ + blink_timer = HAL_GetTick() + TIME_BLINK_OFF; + blink_state = 0; + status_led_set_color(OFF); + return; + } else { + blink_timer = HAL_GetTick() + TIME_BLINK_ON; + blink_state = 1; + } + switch (state.current_state) { case STATE_INACTIVE: status_led_set_color(GREEN); @@ -74,52 +90,31 @@ void status_led_update(){ } } -void status_led_blink_sequence(uint8_t blinks, color color){ - if (blink_timer < HAL_GetTick()) - return; - - if (blink_state){ - if (blinked != blinks){ - blink_timer = HAL_GetTick() + BLINK_LENGTH; - blinks++; - status_led_set_color(GREEN); - } else { - blink_timer = HAL_GetTick() + TIME_BETWEEN_BLINK_SEQS; - blinks = 0; - } - } else { - if (blinked != blinks){ - blink_timer = HAL_GetTick() + TIME_BETWEEN_BLINKS; - status_led_set_color(OFF); - } - } -} - void status_led_set_color(color color){ switch (color) { case RED: - status_led_set(255, 0, 0); - break; - case GREEN: - status_led_set(0, 255, 0); - break; - case BLUE: - status_led_set(0, 0, 255); - break; - case YELLOW: - status_led_set(255, 255, 0); - break; - case PINK: - status_led_set(255, 0, 255); - break; - case CYAN: status_led_set(0, 255, 255); break; + case GREEN: + status_led_set(255, 0, 255); + break; + case BLUE: + status_led_set(255, 255, 0); + break; + case YELLOW: + status_led_set(0, 0, 255); + break; + case PINK: + status_led_set(0, 255, 0); + break; + case CYAN: + status_led_set(255, 0, 0); + break; case WHITE: - status_led_set(255, 255, 255); + status_led_set(0, 0, 0); break; case OFF: - status_led_set(0,0,0); + status_led_set(255,255,255); break; } }