Implemented charger protocol for can2

This commit is contained in:
Yury Shuvakin
2023-03-29 18:38:35 +03:00
parent 714bf3ae8c
commit fb819e50a1
8 changed files with 214 additions and 15 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -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 */
}

View File

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