From 0bc4a594800e9a46b6e76b8153327cb4be808626 Mon Sep 17 00:00:00 2001 From: Hamza Date: Sun, 14 Jul 2024 11:42:05 +0300 Subject: [PATCH] V1.12 --- Core/Src/AMS_HighLevel.c | 27 +++++++++++++++++++++++---- Core/Src/TMP1075.c | 2 +- Core/Src/can.c | 15 +++++++-------- Core/Src/eeprom.c | 5 ++++- Core/Src/errors.c | 3 +++ Core/Src/main.c | 2 ++ Core/Src/soc_estimation.c | 4 +++- Core/Src/state_machine.c | 25 +++++++++++-------------- Core/Src/status_LED.c | 11 +++++++---- 9 files changed, 61 insertions(+), 33 deletions(-) diff --git a/Core/Src/AMS_HighLevel.c b/Core/Src/AMS_HighLevel.c index 75ce0a0..e2efed9 100755 --- a/Core/Src/AMS_HighLevel.c +++ b/Core/Src/AMS_HighLevel.c @@ -6,6 +6,8 @@ */ #include "AMS_HighLevel.h" +#include "ADBMS_LL_Driver.h" +#include Cell_Module module = {}; uint32_t balancedCells = 0; @@ -21,9 +23,10 @@ uint8_t packetChecksumFails = 0; #define MAX_PACKET_CHECKSUM_FAILS 5 uint8_t deviceSleeps = 0; -#define MAX_DEVICE_SLEEP 3 //TODO: change to correct value -#define MAX_CELL_VOLTAGE 4100 //change to 4200 -#define MIN_CELL_VOLTAGE 3100 //change to 3000 +#define MAX_DEVICE_SLEEP 3 //TODO: change to correct value +#define MAX_CELL_VOLTAGE 4100 //change to 4200 +#define MIN_CELL_VOLTAGE 3100 //change to 3000 +#define CELL_VOLTAGE_DIFF_BALANCING 20 //max difference between lowest cell and any other cell amsState currentAMSState = AMSDEACTIVE; amsState lastAMSState = AMSDEACTIVE; @@ -181,6 +184,22 @@ uint8_t AMS_Charging_Loop() { return 0; } uint8_t AMS_Discharging_Loop() { return 0; } uint8_t AMS_Balancing_Loop() { - //TODO: implement + uint8_t id_cell_lowest_voltage = -1; + uint8_t num_of_cells_to_balance = 0; + for (int i = 0; i < 13; i++) { + if (module.cellVoltages[i] < module.cellVoltages[id_cell_lowest_voltage]) + id_cell_lowest_voltage = i; + } + + for (int i = 0; i < 13; i++) { + if (module.cellVoltages[i] - CELL_VOLTAGE_DIFF_BALANCING < module.cellVoltages[id_cell_lowest_voltage]){ + amsConfigBalancing((1 << i), 0xF); + num_of_cells_to_balance++; + } + } + if (num_of_cells_to_balance == 0) + return 0; + + amsStartBalancing(0); return 0; } diff --git a/Core/Src/TMP1075.c b/Core/Src/TMP1075.c index fcb1d50..65ab3bf 100644 --- a/Core/Src/TMP1075.c +++ b/Core/Src/TMP1075.c @@ -2,7 +2,7 @@ #define MAX_TEMP ((int16_t)(59 / 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" +#warning "change value for compliance with the actual number of sensors", change temps to float int16_t tmp1075_temps[N_TEMP_SENSORS] = {0}; uint32_t tmp1075_failed_sensors = 0; diff --git a/Core/Src/can.c b/Core/Src/can.c index 5f80fe9..5fd2be0 100644 --- a/Core/Src/can.c +++ b/Core/Src/can.c @@ -9,6 +9,7 @@ #include "PWM_control.h" #include "can-halal.h" #include "eeprom.h" +#include "soc_estimation.h" #include #define CAN_ID_IN 0x501 @@ -72,15 +73,13 @@ void can_handle_send_status() { uint8_t data[8] = {}; int8_t id_highest_temp = -1; int16_t highest_temp = INT16_MIN; - int8_t id_lowest_volt = -1; - int16_t lowest_volt = INT16_MIN; sm_check_battery_temperature(&id_highest_temp, &highest_temp); - data[0] = ((state.current_state << 4) | (current_powerground_status >> 4)); // 1 bit emptyy | 3 bit state | 4 bit powerground - data[1] = ((current_powerground_status << 4) | (state.error_source >> 4)); // 4 bit powerground | 4 bit error - data[2] = ((state.error_source << 4) | (0)); // 4 bit error | 4 bit state of charge - data[3] = (0); // 8 bit state of charge + data[0] = ((state.current_state << 4) | (current_powerground_status >> 4)); // 1 bit emptyy | 3 bit state | 4 bit powerground + data[1] = ((current_powerground_status << 4) | (state.error_source >> 4)); // 4 bit powerground | 4 bit error + data[2] = ((state.error_source << 4)); // 4 bit error | 4 bit state of charge + data[3] = ((int) current_soc); // 8 bit state of charge 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 @@ -139,10 +138,10 @@ void can_handle_dump() { if (can_delay_manager > HAL_GetTick()) continue; else - can_delay_manager = HAL_GetTick() + CAN_STATUS_FREQ; + can_delay_manager = HAL_GetTick() + CAN_DUMP_FREQ; eeprom_read(data, 64); for (int i = 0; i < 63; i += 8) { - ftcan_transmit(CAN_ID_OUT, data[i], 8); + ftcan_transmit(CAN_ID_OUT, &data[i], 8); } ftcan_transmit(CAN_ID_OUT, 0, 1); } diff --git a/Core/Src/eeprom.c b/Core/Src/eeprom.c index 71845d2..20ceee4 100755 --- a/Core/Src/eeprom.c +++ b/Core/Src/eeprom.c @@ -7,6 +7,7 @@ #include "ADBMS_LL_Driver.h" #include "TMP1075.h" +#include "soc_estimation.h" #include "stm32f3xx_hal_def.h" #include @@ -19,6 +20,8 @@ static I2C_HandleTypeDef* hi2c; uint32_t write_address, read_address; +#warning TEST THIS + void eeprom_init(I2C_HandleTypeDef* handle) { hi2c = handle; write_address = 0; @@ -32,7 +35,7 @@ void eeprom_write_status(){ data[0] = ((state.current_state << 4) | (current_powerground_status >> 4)); data[1] = ((current_powerground_status << 4) | (state.error_source >> 4)); // 4 bit powerground | 4 bit error data[2] = ((state.error_source << 4) | (0)); // 4 bit error | 4 bit state of charge - data[3] = (0); // 8 bit state of charge + data[3] = ((int) current_soc); // 8 bit state of charge data[4] = (RELAY_BAT_SIDE_VOLTAGE >> 8); // 16 bit battery voltage data[5] = (RELAY_BAT_SIDE_VOLTAGE); data[6] = (RELAY_ESC_SIDE_VOLTAGE >> 8); // 16 bit Inverter voltage diff --git a/Core/Src/errors.c b/Core/Src/errors.c index cc8aafe..d946b67 100644 --- a/Core/Src/errors.c +++ b/Core/Src/errors.c @@ -1,6 +1,9 @@ #include "errors.h" #include "stm32f3xx_hal.h" + +#warning move all errors here + SlaveErrorData error_data; void set_error_source(SlaveErrorKind source) { diff --git a/Core/Src/main.c b/Core/Src/main.c index 24076f0..f9f54ce 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -26,6 +26,7 @@ #include "PWM_control.h" #include "can.h" #include "AMS_HighLevel.h" +#include "soc_estimation.h" #include "state_machine.h" #include #include "TMP1075.h" @@ -135,6 +136,7 @@ int main(void) AMS_Init(&hspi1); can_init(&hcan); PWM_control_init(&htim3, &htim2, &htim15); + soc_init(); status_led_init(&htim4, &htim4, &htim4); //AMS_Loop(); diff --git a/Core/Src/soc_estimation.c b/Core/Src/soc_estimation.c index 524f55f..015ed87 100644 --- a/Core/Src/soc_estimation.c +++ b/Core/Src/soc_estimation.c @@ -6,13 +6,15 @@ #include #include -#define SOC_ESTIMATION_NO_CURRENT_THRESH 200 // mA +#define SOC_ESTIMATION_NO_CURRENT_THRESH 500 // mA #define SOC_ESTIMATION_NO_CURRENT_TIME 100000 // ms #define SOC_ESTIMATION_BATTERY_CAPACITY 70300800 // mAs #define MIN_CELL_VOLTAGE 3000 #define MAX_CELL_VOLTAGE 4200 + +#warning TODO ocv_soc_pair_t OCV_SOC_PAIRS[] = { {25000, 0.00f}, {29900, 3.97f}, {32300, 9.36f}, {33200, 12.60f}, {33500, 13.68f}, {34100, 20.15f}, {35300, 32.01f}, {38400, 66.53f}, diff --git a/Core/Src/state_machine.c b/Core/Src/state_machine.c index 1633e08..ff3a64b 100644 --- a/Core/Src/state_machine.c +++ b/Core/Src/state_machine.c @@ -9,6 +9,7 @@ #include "PWM_control.h" #include "eeprom.h" #include "main.h" +#include "soc_estimation.h" #include // Time to wait after reaching 95% of battery voltage before exiting precharge @@ -23,8 +24,7 @@ // Max time to wait for CAN messages. If we reach it then we emergency shutdown. #define CAN_TIMEOUT 1000 // waiting time between to eeprom writes -#define EEPROM_WRITE_FREQ_INACTIVE 10000 -#define EEPROM_WRITE_FREQ_ACTIVE 1000 +#define EEPROM_WRITE_FREQ_INACTIVE 1000 StateHandle state; int32_t RELAY_BAT_SIDE_VOLTAGE; @@ -61,6 +61,8 @@ void sm_update(){ sm_precharge_discharge_manager(); sm_calibrate_powerground(); sm_powerground_manager(); + soc_update(); + if (CAN_timer < HAL_GetTick()) state.current_state = state.target_state = STATE_ERROR; @@ -79,22 +81,22 @@ void sm_update(){ state.current_state = sm_update_inactive(); // monitor only break; case STATE_PRECHARGE: - state.current_state = sm_update_precharge(); // set PRECHARGE and turn on cooling at 50% or such + state.current_state = sm_update_precharge(); // set PRECHARGE break; case STATE_READY: - state.current_state = sm_update_ready(); // keep cooling at 50%, get ready to turn on powerground + state.current_state = sm_update_ready(); break; case STATE_ACTIVE: - state.current_state = sm_update_active(); // set PRECHARGE and turn on cooling at 50% or such + state.current_state = sm_update_active(); break; case STATE_DISCHARGE: - state.current_state = sm_update_discharge(); // open the main relay, keep PRECHARGE closed + state.current_state = sm_update_discharge(); break; case STATE_CHARGING_PRECHARGE: state.current_state = sm_update_charging_precharge(); break; case STATE_CHARGING: - state.current_state = sm_update_charging(); // monitor and turn on cooling if needed. + state.current_state = sm_update_charging(); break; case STATE_ERROR: state.current_state = sm_update_error(); // enter the correct ERROR state @@ -136,6 +138,8 @@ void sm_handle_ams_in(const uint8_t *data){ break; #warning implement this case 0xF1: // EEPROM + if (state.current_state == STATE_INACTIVE) + sm_eeprom_write_status(); break; case 0xFF: // EMERGENCY SHUTDOWN state.current_state = STATE_DISCHARGE; @@ -236,13 +240,6 @@ void sm_program_powerground(){ } } -void sm_balancing(){ - for (int i = 0; i < 13; i++) { - amsConfigBalancing((1 << i), 0xF); - } - amsStartBalancing(0); -} - void sm_eeprom_write_status(){ if (EEPROM_timer < HAL_GetTick()){ eeprom_write_status(); diff --git a/Core/Src/status_LED.c b/Core/Src/status_LED.c index bbd70eb..6bae223 100644 --- a/Core/Src/status_LED.c +++ b/Core/Src/status_LED.c @@ -11,6 +11,8 @@ #include #include +#warning test out pulldown and pushpull settings + /* The PWM period (1/FPWM) is defined by the following parameters: ARR value, the Prescaler value, and the internal clock itself which drives the timer module FCLK. F_PWM = (F_CLK)/((ARR + 1) * (PSC + 1)) @@ -54,17 +56,15 @@ void status_led_init(TIM_HandleTypeDef* r, TIM_HandleTypeDef* g, TIM_HandleTypeD void status_led_update(){ switch (state.current_state) { case STATE_INACTIVE: - status_led_blink_sequence(1, GREEN); + status_led_set_color(GREEN); break; case STATE_CHARGING_PRECHARGE: case STATE_PRECHARGE: case STATE_DISCHARGE: - status_led_blink_sequence(2, YELLOW); + status_led_set_color( YELLOW); break; case STATE_CHARGING: case STATE_READY: - status_led_blink_sequence(3, PINK); - break; case STATE_ACTIVE: status_led_set_color(PINK); break; @@ -118,6 +118,9 @@ void status_led_set_color(color color){ case WHITE: status_led_set(255, 255, 255); break; + case OFF: + status_led_set(0,0,0); + break; } }