From 665ee02bf2f1c920520d210dcef6e748c91abaa2 Mon Sep 17 00:00:00 2001 From: jazzpi Date: Mon, 18 Jul 2022 16:56:38 +0200 Subject: [PATCH] Working version --- Core/Inc/CAN_Communication.h | 5 +++++ Core/Inc/Check_Shunt_Limits.h | 2 +- Core/Inc/SPI_Communication.h | 3 +++ Core/Inc/SoC_Estimation.h | 2 +- Core/Src/CAN_Communication.c | 30 ++++++++++++++++-------------- Core/Src/SPI_Communication.c | 34 +++++++++++++++++++++++++++++----- Core/Src/main.c | 3 ++- STM32Make.make | 11 ++++++++++- 8 files changed, 67 insertions(+), 23 deletions(-) diff --git a/Core/Inc/CAN_Communication.h b/Core/Inc/CAN_Communication.h index 31fec40..be41a1d 100644 --- a/Core/Inc/CAN_Communication.h +++ b/Core/Inc/CAN_Communication.h @@ -33,6 +33,11 @@ #define AMS_PANIC_ID 0x42 #define AMS_LOGGING_ID 0x500 +#define AMS_LOGGING_NUM_FRAMES 6 + +// Charger->Master IDs +#define CHARGER_START_CHARGING 0x446 + #define BATTERY_VOLTAGE_TYPE 1 #define BATTERY_TEMP_TYPE 2 #define LOG_BAT_LOW_VOLTAGE 32768 diff --git a/Core/Inc/Check_Shunt_Limits.h b/Core/Inc/Check_Shunt_Limits.h index 5399413..82eb68a 100644 --- a/Core/Inc/Check_Shunt_Limits.h +++ b/Core/Inc/Check_Shunt_Limits.h @@ -12,7 +12,7 @@ #include "main.h" #define SHUNT_OVERCURRENT 0x0FFFFFFF // Shunt Overcurrent Limit -#define SHUNT_TIMEOUT 3000 // Timeout after 500ms +#define SHUNT_TIMEOUT 500 // Timeout after 500ms #define SHUNT_OVERTEMP 0xFFFFFFFF // Overtermperature of the Busbar void CheckShuntLimits(); diff --git a/Core/Inc/SPI_Communication.h b/Core/Inc/SPI_Communication.h index 3b99d7c..e9e1235 100644 --- a/Core/Inc/SPI_Communication.h +++ b/Core/Inc/SPI_Communication.h @@ -16,6 +16,8 @@ #define TS_DISCHARGE 3 #define TS_ERROR 4 #define TS_ACTIVE 1 +#define TS_CHARGING_CHECK 5 +#define TS_CHARGING 6 #define SEND_SHUNTDATA 0x01 #define SET_TSSTATE 0x02 @@ -73,6 +75,7 @@ typedef struct { } ErrorFlagHandler; extern SlaveHandler slaves[NUMBEROFSLAVES]; +extern uint16_t min_voltage, max_voltage, min_temp, max_temp; extern ErrorFlagHandler errorflags; extern TSHandler ctrltsstate; diff --git a/Core/Inc/SoC_Estimation.h b/Core/Inc/SoC_Estimation.h index 47623cc..519d475 100644 --- a/Core/Inc/SoC_Estimation.h +++ b/Core/Inc/SoC_Estimation.h @@ -4,7 +4,7 @@ #include "SPI_Communication.h" #define SOCE_SHUNT_CURRENT_OFF_THRESH 20 /* mA */ -#define CELL_VOLTAGE_CONVERSION_FACTOR 5.0f / 65535 /* V/quantum */ +#define CELL_VOLTAGE_CONVERSION_FACTOR (5.0f / 65535) /* V/quantum */ #define BATTERY_CAPACITY (N_CELLS_PARALLEL * 2.5f * 3600) /* As */ extern uint8_t current_soc; diff --git a/Core/Src/CAN_Communication.c b/Core/Src/CAN_Communication.c index 8df3318..eb510aa 100644 --- a/Core/Src/CAN_Communication.c +++ b/Core/Src/CAN_Communication.c @@ -105,7 +105,9 @@ uint8_t CAN_Receive(FDCAN_HandleTypeDef* hcan) { (rxFrame.data[4] << 8) | (rxFrame.data[5]); shuntlastmessage = framebuffer[framebufferreadpointer].timestamp; break; - + case CHARGER_START_CHARGING: + TSTargetState = TS_CHARGING; + break; case AUTOBOX_INFO: currentlap = rxFrame.data[0] >> 2; TSTargetState = rxFrame.data[0] & 0x01; @@ -122,7 +124,7 @@ uint8_t CAN_Transmit(FDCAN_HandleTypeDef* hcan, uint16_t frameid, txheader.Identifier = frameid; txheader.IdType = FDCAN_STANDARD_ID; - txheader.TxFrameType = FDCAN_FRAME_CLASSIC; + txheader.TxFrameType = FDCAN_DATA_FRAME; txheader.DataLength = ((uint32_t)datalen) << 16; txheader.ErrorStateIndicator = FDCAN_ESI_ACTIVE; txheader.BitRateSwitch = FDCAN_BRS_OFF; @@ -208,16 +210,6 @@ void CAN_SendLoggingFrame(FDCAN_HandleTypeDef* hcan) { buffer[0] = ((slavelognum << 4) | framelognum); - framelognum++; - if ((framelognum > 6)) { - framelognum = 0; - slavelognum++; - } - if ((slavelognum > NUMBEROFSLAVES)) { - slavelognum = 0; - framelognum = 0; - } - switch (framelognum) { case 0: buffer[1] = CAN_convert_logval(slaves[slavelognum].cellVoltages[0], @@ -323,12 +315,22 @@ void CAN_SendLoggingFrame(FDCAN_HandleTypeDef* hcan) { } CAN_Transmit(hcan, AMS_LOGGING_ID, buffer, 8); + + framelognum++; + if ((framelognum >= AMS_LOGGING_NUM_FRAMES)) { + framelognum = 0; + slavelognum++; + } + if ((slavelognum >= NUMBEROFSLAVES)) { + slavelognum = 0; + framelognum = 0; + } } uint8_t CAN_convert_logval(uint16_t value, uint8_t type) { if (type == BATTERY_VOLTAGE_TYPE) { - return (uint8_t)value >> 8; + return value >> 8; } else if (type == BATTERY_TEMP_TYPE) { - return (uint8_t)value >> 4; + return value >> 4; } } diff --git a/Core/Src/SPI_Communication.c b/Core/Src/SPI_Communication.c index add21aa..96ec962 100644 --- a/Core/Src/SPI_Communication.c +++ b/Core/Src/SPI_Communication.c @@ -10,6 +10,8 @@ #include "stm32g4xx_hal.h" #include "stm32g4xx_hal_spi.h" +#include + #define GET_ERROR_RESPONSE_LEN 14 #define GET_TS_STATE_RESPONSE_LEN 23 @@ -19,6 +21,7 @@ uint8_t spitxbuf[1024]; uint8_t spirxbuf[1024]; SlaveHandler slaves[NUMBEROFSLAVES] = {0}; +uint16_t min_voltage = 0, max_voltage = 0, min_temp = 0, max_temp = 0; ErrorFlagHandler errorflags = {0}; TSHandler ctrltsstate = {0}; @@ -234,6 +237,10 @@ void InterSTMFrame(uint8_t targettsstate) { return; } + uint16_t min_v = 0xFFFF; + uint16_t max_v = 0; + uint16_t min_t = 0xFFFF; + uint16_t max_t = 0; for (int n = 0; n < NUMBEROFSLAVES; n++) { slaves[n].slaveID = spirxbuf[n * 89]; slaves[n].timestamp = (spirxbuf[n * 89 + 1] << 24) | @@ -241,15 +248,32 @@ void InterSTMFrame(uint8_t targettsstate) { (spirxbuf[n * 89 + 3] << 8) | (spirxbuf[n * 89 + 4]); for (int i = 0; i < N_CELLS_SERIES; i++) { - slaves[n].cellVoltages[i] = - ((uint16_t)spirxbuf[n * 89 + 5 + 2 * i] << 8) | - spirxbuf[n * 89 + 6 + 2 * i]; + uint16_t v = ((uint16_t)spirxbuf[n * 89 + 5 + 2 * i] << 8) | + spirxbuf[n * 89 + 6 + 2 * i]; + slaves[n].cellVoltages[i] = v; + if (v < min_v) { + min_v = v; + } + if (v > max_v) { + max_v = v; + } } for (int i = 0; i < NUMBEROFTEMPS; i++) { - slaves[n].cellTemps[i] = ((uint16_t)spirxbuf[n * 89 + 25 + 2 * i] << 8) | - spirxbuf[n * 89 + 26 + 2 * i]; + uint16_t t = ((uint16_t)spirxbuf[n * 89 + 25 + 2 * i] << 8) | + spirxbuf[n * 89 + 26 + 2 * i]; + slaves[n].cellTemps[i] = t; + if (t < min_t) { + min_t = t; + } + if (t > max_t) { + max_t = t; + } } } + min_voltage = min_v; + max_voltage = max_v; + min_temp = min_t; + max_temp = max_t; uint16_t errorflagbaseaddress = NUMBEROFSLAVES * 89 + 1; diff --git a/Core/Src/main.c b/Core/Src/main.c index 6370459..e932249 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -20,10 +20,10 @@ /* Includes ------------------------------------------------------------------*/ #include "main.h" +#include "SoC_Estimation.h" #include "app_fatfs.h" #include "usb_device.h" - /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "CAN_Communication.h" @@ -151,6 +151,7 @@ int main(void) { softTSAL(); InterSTMFrame(TSTargetState); + estimate_soc(); HAL_Delay(10); } /* USER CODE END 3 */ diff --git a/STM32Make.make b/STM32Make.make index 3276461..917d63f 100644 --- a/STM32Make.make +++ b/STM32Make.make @@ -41,6 +41,7 @@ Core/Src/Check_Shunt_Limits.c \ Core/Src/Fan_Control.c \ Core/Src/SD_SPI_Driver.c \ Core/Src/SPI_Communication.c \ +Core/Src/SoC_Estimation.c \ Core/Src/USB_Shell.c \ Core/Src/main.c \ Core/Src/stm32g4xx_hal_msp.c \ @@ -48,6 +49,7 @@ Core/Src/stm32g4xx_it.c \ Core/Src/syscalls.c \ Core/Src/sysmem.c \ Core/Src/system_stm32g4xx.c \ +Core/Src/util.c \ Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal.c \ Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal_cortex.c \ Drivers/STM32G4xx_HAL_Driver/Src/stm32g4xx_hal_dma.c \ @@ -177,7 +179,8 @@ CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction- CXXFLAGS = $(MCU) $(CXX_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections -feliminate-unused-debug-types ifeq ($(DEBUG), 1) -CFLAGS += -g -gdwarf-2 +CFLAGS += -g -gdwarf -ggdb +CXXFLAGS += -g -gdwarf -ggdb endif # Add additional flags @@ -265,6 +268,12 @@ erase: $(BUILD_DIR)/$(TARGET).elf ####################################### clean: -rm -fR $(BUILD_DIR) + +####################################### +# custom makefile rules +####################################### + + ####################################### # dependencies