Implemented charger protocol for can2
This commit is contained in:
169
Core/Src/main.c
169
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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user