From fb819e50a1a3760b4c1fa7b8f5815db4bfc90335 Mon Sep 17 00:00:00 2001 From: Yury Shuvakin Date: Wed, 29 Mar 2023 18:38:35 +0300 Subject: [PATCH] Implemented charger protocol for can2 --- bms_s_cube.ioc => BMS_v3.ioc | 0 Core/Inc/modConfig.h | 2 + Core/Inc/stm32f1xx_it.h | 2 + Core/Src/can.c | 4 +- Core/Src/main.c | 169 ++++++++++++++++++++++++++++++++--- Core/Src/modConfig.c | 33 +++++++ Core/Src/stm32f1xx_hal_msp.c | 6 +- Core/Src/stm32f1xx_it.c | 13 +++ 8 files changed, 214 insertions(+), 15 deletions(-) rename bms_s_cube.ioc => BMS_v3.ioc (100%) diff --git a/bms_s_cube.ioc b/BMS_v3.ioc similarity index 100% rename from bms_s_cube.ioc rename to BMS_v3.ioc diff --git a/Core/Inc/modConfig.h b/Core/Inc/modConfig.h index 0f4ac82..71b8abc 100644 --- a/Core/Inc/modConfig.h +++ b/Core/Inc/modConfig.h @@ -156,6 +156,8 @@ typedef struct { float floatCurrentK1; // First factor of current calculation float floatCurrentK2; // Second factor of current calculation + float externalChargeCurrentTable[9][11]; + bool externalChargeUnitTable[9][11]; // uint8_t dummy; } modConfigGeneralConfigStructTypedef; diff --git a/Core/Inc/stm32f1xx_it.h b/Core/Inc/stm32f1xx_it.h index 39958b1..1e12b29 100644 --- a/Core/Inc/stm32f1xx_it.h +++ b/Core/Inc/stm32f1xx_it.h @@ -57,6 +57,8 @@ void DebugMon_Handler(void); void PendSV_Handler(void); void SysTick_Handler(void); void OTG_FS_IRQHandler(void); +void CAN1_RX0_IRQHandler(void); +void CAN2_RX1_IRQHandler(void); /* USER CODE BEGIN EFP */ /* USER CODE END EFP */ diff --git a/Core/Src/can.c b/Core/Src/can.c index a9aa717..edb67ca 100644 --- a/Core/Src/can.c +++ b/Core/Src/can.c @@ -318,13 +318,13 @@ void CAN_TransmitExtRTR(u32_t id, u8_t len, CAN_HandleTypeDef *can) { void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan) { if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &can1RX, can1Data) == HAL_OK) { - can_irq_receive(&can1RX, can1Data, 1); + //can_irq_receive(&can1RX, can1Data, 1); } } void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan) { if (HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO1, &can2RX, can2Data) == HAL_OK) { - can_irq_receive(&can2RX, can2Data, 2); + //can_irq_receive(&can2RX, can2Data, 2); } } diff --git a/Core/Src/main.c b/Core/Src/main.c index 3547358..e8ef623 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -1164,16 +1164,162 @@ void transmitCan1Packet() can1_transmit_clock = TIM_Clock; } -// -//void transmitCan2Packet() -//{ -// if ((TIM_Clock - can2_transmit_clock) < 25) -// { -// return; -// } -// -// can2_transmit_clock = TIM_Clock; -//} + +int getIndexBySoc() +{ + const float soc = packState.SoC; + if (soc == 0) + { + return 0; + } + else if (soc <= 10 && soc > 0) + { + return 1; + } + else if (soc <= 20 && soc > 10) + { + return 2; + } + else if (soc <= 30 && soc > 20) + { + return 3; + } + else if (soc <= 40 && soc > 30) + { + return 4; + } + else if (soc <= 50 && soc > 40) + { + return 5; + } + else if (soc <= 60 && soc > 50) + { + return 6; + } + else if (soc <= 70 && soc > 60) + { + return 7; + } + else if (soc <= 80 && soc > 70) + { + return 8; + } + else if (soc <= 95 && soc > 80) + { + return 9; + } + else if (soc <= 100 && soc > 95) + { + return 10; + } + + return -1; +} + +int getIndexByTemperature() +{ + const float temp = packState.tempBatteryAverage; + if (temp > 0) + { + return 0; + } + else if (temp < 2 && temp >= 0) + { + return 1; + } + else if (temp < 7 && temp >= 2) + { + return 2; + } + else if (temp < 15 && temp >= 7) + { + return 3; + } + else if (temp < 20 && temp >= 15) + { + return 4; + } + else if (temp < 45 && temp >= 20) + { + return 5; + } + else if (temp < 50 && temp >= 45) + { + return 6; + } + else if (temp < 55 && temp >= 50) + { + return 7; + } + else if (temp >= 55) + { + return 8; + } + + return -1; +} + +void transmitCan2Packet() +{ + if ((TIM_Clock - can2_transmit_clock) < 500) + { + return; + } + + can2_transmit_clock = TIM_Clock; + + uint8_t buffer[8] = {0}; + + // sending heartbeat command + const uint32_t rtr = 1; + memcpy(buffer, &rtr, sizeof(rtr)); + CAN_Transmit(0x500, buffer, 8, &hcan2); + + // sending common current and voltage command + memcpy(buffer, &packState.packCurrent, sizeof(packState.packCurrent)); + memcpy(buffer + 4, &packState.packVoltage, sizeof(packState.packVoltage)); + CAN_Transmit(0x501, buffer, 8, &hcan2); + + // sending charge voltages (start, end) command + memcpy(buffer, &generalConfig->cellLCSoftUnderVoltage, sizeof(generalConfig->cellLCSoftUnderVoltage)); + memcpy(buffer + 4, &generalConfig->cellSoftOverVoltage, sizeof(generalConfig->cellSoftOverVoltage)); + CAN_Transmit(0x502, buffer, 8, &hcan2); + + // sending fault voltages (under, over) command + memcpy(buffer, &generalConfig->cellHardUnderVoltage, sizeof(generalConfig->cellHardUnderVoltage)); + memcpy(buffer + 4, &generalConfig->cellHardOverVoltage, sizeof(generalConfig->cellHardOverVoltage)); + CAN_Transmit(0x503, buffer, 8, &hcan2); + + // sending charge current command + const int socIndex = getIndexBySoc(); + const int temperatureIndex = getIndexByTemperature(); + float tableCurrent = 0; + float chargeEndingCurrent = 5; // TODO move to generalConfig + + if (socIndex != -1 && temperatureIndex != -1) + { + float tableValue = generalConfig->externalChargeCurrentTable[temperatureIndex][socIndex]; + float pureCurrent = generalConfig->externalChargeUnitTable[temperatureIndex][socIndex]; + tableCurrent = pureCurrent ? tableValue : generalConfig->batteryCapacity * tableValue; + } + + memcpy(buffer, &tableCurrent, sizeof(tableCurrent)); + memcpy(buffer + 4, &chargeEndingCurrent, sizeof(chargeEndingCurrent)); + CAN_Transmit(0x504, buffer, 8, &hcan2); + + // sending charge permission, charge ending and charge/discharge state command + const bool chargeAllowed = packState.chargeAllowed; + const bool chargeEnding = packState.SoC > 95; + const bool chargeSwitchState = charge_switch_state; + + uint32_t data = chargeAllowed; + data |= (chargeEnding << 1); + data |= (chargeSwitchState << 2); + + memset(buffer, '\0', sizeof(buffer)); + memcpy(buffer, &data, sizeof(data)); + CAN_Transmit(0x505, buffer, 8, &hcan2); +} void modem_init(){ @@ -2046,6 +2192,7 @@ int main(void) //MX_CAN2_Init(); CAN_SetSpeed(CAN_SPD_100, &hcan1); + CAN_SetSpeed(CAN_SPD_100, &hcan2); //HAL_GPIO_WritePin(HL4_GPIO_Port, HL4_Pin, 1); @@ -2292,7 +2439,7 @@ int main(void) // mainWatchDogReset(); transmitCan1Packet(); - //transmitCan2Packet(); + transmitCan2Packet(); outputControl(); diff --git a/Core/Src/modConfig.c b/Core/Src/modConfig.c index 348f0d1..1e9687d 100644 --- a/Core/Src/modConfig.c +++ b/Core/Src/modConfig.c @@ -236,6 +236,39 @@ void modConfigLoadDefaultConfig(modConfigGeneralConfigStructTypedef *configLocat configLocation->floatCurrentK1 = 0.0f; // First factor of current calculation configLocation->floatCurrentK2 = 0.777f; // Second factor of current calculation + float currentTable[9][11] = { + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5}, + {0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12}, + {0.30, 0.30, 0.30, 0.30, 0.30, 0.30, 0.30, 0.30, 0.30, 0.20, 0.20}, + {0.50, 0.50, 0.50, 0.50, 0.50, 0.50, 0.50, 0.50, 0.50, 0.40, 0.40}, + {1, 1, 1, 1, 1, 1, 1, 1, 1, 0.80, 0.80}, + {0.50, 0.50, 0.50, 0.50, 0.50, 0.50, 0.50, 0.50, 0.50, 0.40, 0.40}, + {0.30, 0.30, 0.30, 0.30, 0.30, 0.30, 0.30, 0.30, 0.30, 0.20, 0.20}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} + }; + + float unitTable[9][11] = { + {false, false, false, false, false, false, false, false, false, false, false}, + {true, true, true, true, true, true, true, true, true, true, true}, + {false, false, false, false, false, false, false, false, false, false, false}, + {false, false, false, false, false, false, false, false, false, false, false}, + {false, false, false, false, false, false, false, false, false, false, false}, + {false, false, false, false, false, false, false, false, false, false, false}, + {false, false, false, false, false, false, false, false, false, false, false}, + {false, false, false, false, false, false, false, false, false, false, false}, + {false, false, false, false, false, false, false, false, false, false, false} + }; + + for (int i = 0; i < 9; ++i) + { + for (int j = 0; j < 11; ++j) + { + configLocation->externalChargeCurrentTable[i][j] = currentTable[i][j]; + configLocation->externalChargeUnitTable[i][j] = unitTable[i][j]; + } + } + #elif ENNOID_HV configLocation->noOfCellsSeries = 8; // Total number of cells in series in the battery pack configLocation->noOfCellsParallel = 10; // Number of cells in parallel diff --git a/Core/Src/stm32f1xx_hal_msp.c b/Core/Src/stm32f1xx_hal_msp.c index 2e9a1ca..831bd17 100644 --- a/Core/Src/stm32f1xx_hal_msp.c +++ b/Core/Src/stm32f1xx_hal_msp.c @@ -181,7 +181,8 @@ void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan) __HAL_AFIO_REMAP_CAN1_3(); /* USER CODE BEGIN CAN1_MspInit 1 */ - + HAL_NVIC_SetPriority(CAN1_RX0_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn); /* USER CODE END CAN1_MspInit 1 */ } else if(hcan->Instance==CAN2) @@ -212,7 +213,8 @@ void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan) HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /* USER CODE BEGIN CAN2_MspInit 1 */ - + HAL_NVIC_SetPriority(CAN2_RX1_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(CAN2_RX1_IRQn); /* USER CODE END CAN2_MspInit 1 */ } diff --git a/Core/Src/stm32f1xx_it.c b/Core/Src/stm32f1xx_it.c index b34d151..6e933db 100644 --- a/Core/Src/stm32f1xx_it.c +++ b/Core/Src/stm32f1xx_it.c @@ -28,6 +28,8 @@ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN TD */ extern UART_HandleTypeDef huart2; +extern CAN_HandleTypeDef hcan1; +extern CAN_HandleTypeDef hcan2; /* USER CODE END TD */ void USART2_IRQHandler(void) @@ -273,6 +275,17 @@ void OTG_FS_IRQHandler(void) /* USER CODE END OTG_FS_IRQn 1 */ } +void CAN1_RX0_IRQHandler(void) +{ + HAL_CAN_IRQHandler(&hcan1); +} + + +void CAN2_RX1_IRQHandler(void) +{ + HAL_CAN_IRQHandler(&hcan2); +} + /* USER CODE BEGIN 1 */ /* USER CODE END 1 */