CAN Update
This commit is contained in:
@@ -38,7 +38,9 @@
|
||||
#include "DPC_Timeout.h"
|
||||
#include "GSM.h"
|
||||
#include "time.h"
|
||||
#include "can.h"
|
||||
|
||||
#include "../../Libs/CAN/can.h"
|
||||
#include "../../Libs/CAN/can_messenger.h"
|
||||
|
||||
#include "SD_Card.h"
|
||||
|
||||
@@ -279,14 +281,6 @@ uint64_t output_control_clock = 0;
|
||||
bool need_shunt_charging_contractor = false;
|
||||
uint64_t shunt_charging_contractor_clock = 0;
|
||||
|
||||
uint64_t can1_transmit_clock = 0;
|
||||
uint64_t can2_transmit_clock = 0;
|
||||
|
||||
uint8_t can1_transmit_queue[500];
|
||||
uint8_t can2_transmit_queue[100];
|
||||
|
||||
uint16_t can1_transmit_queue_size = 0;
|
||||
uint16_t can2_transmit_queue_size = 0;
|
||||
|
||||
|
||||
#define __MA_WIN_SIZE (500)
|
||||
@@ -315,8 +309,6 @@ void Save_data_to_Backup();
|
||||
void Restore_shutdown_data();
|
||||
void updateLimitsFromMem();
|
||||
|
||||
void addCanPacketToQueue(uint16_t id, uint8_t* data, uint8_t* queue, uint16_t* queueSize);
|
||||
void transmitCanPacketFromQueue(uint8_t* queue, uint16_t* queueSize, CAN_HandleTypeDef* hcan);
|
||||
|
||||
void modem_init();
|
||||
|
||||
@@ -372,7 +364,6 @@ void usbTIM(uint8_t OnOff) {
|
||||
}
|
||||
}
|
||||
|
||||
u8_t TestData[8] = {1,2,3,4,5,6,7,8};
|
||||
|
||||
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
|
||||
{
|
||||
@@ -385,15 +376,23 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
|
||||
{
|
||||
if (TIM3_Clock % 10 == 0)
|
||||
{
|
||||
CAN_TransmitRTR(0x500, 8, &hcan2);
|
||||
CAN_TransmitRTR(CAN_BMS_HEARTBEAT, 8, &hcan2);
|
||||
}
|
||||
else if (TIM3_Clock % 3 == 0)
|
||||
|
||||
if (TIM3_Clock % 4 == 0)
|
||||
{
|
||||
transmitCanPacketFromQueue(can1_transmit_queue, &can1_transmit_queue_size, &hcan1);
|
||||
// transmitCanPacketFromQueue(can1_transmit_queue, &can1_transmit_queue_size, &hcan1);
|
||||
transmitCanPacketFromQueueCan1();
|
||||
}
|
||||
else if (TIM3_Clock % 3 == 1)
|
||||
#include "../../Libs/Utils/micros.h"
|
||||
if (TIM3_Clock % 3 == 0)
|
||||
{
|
||||
transmitCanPacketFromQueue(can2_transmit_queue, &can2_transmit_queue_size, &hcan2);
|
||||
volatile u32_t measure = 0;
|
||||
// StartCodeMeasure();
|
||||
// transmitCanPacketFromQueue(can2_transmit_queue, &can2_transmit_queue_size, &hcan2);
|
||||
transmitCanPacketFromQueueCan2();
|
||||
// StopCodeMeasure(&measure);
|
||||
// __NOP();
|
||||
}
|
||||
|
||||
TIM3_Clock++;
|
||||
@@ -426,8 +425,6 @@ void load_switch(uint8_t state) {
|
||||
/* USER CODE END 0 */
|
||||
|
||||
|
||||
|
||||
|
||||
uint16_t MA_Filter(uint16_t input, uint8_t array)
|
||||
{
|
||||
uint16_t Result = 0;
|
||||
@@ -1103,269 +1100,6 @@ void outputControl()
|
||||
output_control_clock = TIM_Clock;
|
||||
}
|
||||
|
||||
void transmitCan1Packet()
|
||||
{
|
||||
if ((TIM_Clock - can1_transmit_clock) < 1000)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
uint8_t data1[8] = {0x45};
|
||||
CAN_Transmit(300, data1, 8, &hcan1);
|
||||
can1_transmit_clock = TIM_Clock;
|
||||
return;
|
||||
|
||||
CAN_TxHeaderTypeDef header;
|
||||
header.IDE = CAN_ID_STD;
|
||||
header.RTR = CAN_RTR_DATA;
|
||||
header.StdId = 0x444;
|
||||
header.DLC = 1;
|
||||
|
||||
uint8_t data[32] = {};
|
||||
uint32_t mailbox = 0;
|
||||
|
||||
// sending SOC, SOH and number of cells
|
||||
uint16_t id = 0x101;
|
||||
memcpy(data, &id, sizeof(id));
|
||||
memcpy(data + 2, &packState.SoC, sizeof(packState.SoC));
|
||||
memcpy(data + 6, &packState.SoCCapacityAh, sizeof(packState.SoCCapacityAh));
|
||||
memcpy(data + 10, &generalConfig->noOfCellsSeries, sizeof(generalConfig->noOfCellsSeries));
|
||||
header.DLC = 11;
|
||||
HAL_CAN_AddTxMessage(&hcan1, &header, data, &mailbox);
|
||||
|
||||
// sending charge current, discharge current // TODO
|
||||
id = 0x102;
|
||||
memcpy(data, &id, sizeof(id));
|
||||
memcpy(data + 2, &packState.packCurrent, sizeof(packState.packCurrent));
|
||||
memcpy(data + 6, &packState.loCurrentLoadCurrent, sizeof(packState.loCurrentLoadCurrent));
|
||||
header.DLC = 10;
|
||||
HAL_CAN_AddTxMessage(&hcan1, &header, data, &mailbox);
|
||||
|
||||
// sending BMS state, input state, output state // TODO
|
||||
id = 0x103;
|
||||
memcpy(data, &id, sizeof(id));
|
||||
memcpy(data + 2, &packState.cellVoltageLow, sizeof(packState.cellVoltageLow));
|
||||
memcpy(data + 6, &packState.cellVoltageAverage, sizeof(packState.cellVoltageAverage));
|
||||
memcpy(data + 10, &packState.cellVoltageHigh, sizeof(packState.cellVoltageHigh));
|
||||
header.DLC = 12;
|
||||
HAL_CAN_AddTxMessage(&hcan1, &header, data, &mailbox);
|
||||
|
||||
// sending cell voltages
|
||||
id = 0x200;
|
||||
for (int cellPointer = 0; cellPointer < generalConfig->noOfCellsSeries * generalConfig->noOfParallelModules; ++cellPointer)
|
||||
{
|
||||
++id;
|
||||
memcpy(data, &id, sizeof(id));
|
||||
float voltage = 0;
|
||||
if (packState.cellVoltagesIndividual[cellPointer].cellBleedActive)
|
||||
voltage = packState.cellVoltagesIndividual[cellPointer].cellVoltage * -1000;
|
||||
else
|
||||
voltage = packState.cellVoltagesIndividual[cellPointer].cellVoltage * 1000;
|
||||
memcpy(data + 2, &voltage, sizeof(voltage));
|
||||
header.DLC = 6;
|
||||
HAL_CAN_AddTxMessage(&hcan1, &header, data, &mailbox);
|
||||
}
|
||||
|
||||
// sending temperatures
|
||||
id = 0x300;
|
||||
for (int sensorPointer = 0; sensorPointer < NoOfTempSensors; ++sensorPointer)
|
||||
{
|
||||
++id;
|
||||
memcpy(data, &id, sizeof(id));
|
||||
float temperature = packState.temperatures[sensorPointer];
|
||||
memcpy(data + 2, &temperature, sizeof(temperature));
|
||||
header.DLC = 6;
|
||||
HAL_CAN_AddTxMessage(&hcan1, &header, data, &mailbox);
|
||||
}
|
||||
|
||||
can1_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 > 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 common current and voltage command
|
||||
memcpy(buffer, &packState.packCurrent, sizeof(packState.packCurrent));
|
||||
memcpy(buffer + 4, &packState.packVoltage, sizeof(packState.packVoltage));
|
||||
addCanPacketToQueue(0x501, buffer, can2_transmit_queue, &can2_transmit_queue_size);
|
||||
|
||||
// sending charge voltages (start, end) command
|
||||
const float startVoltage = generalConfig->cellHardUnderVoltage * generalConfig->noOfCellsSeries;
|
||||
const float endVoltage = generalConfig->cellHardOverVoltage * generalConfig->noOfCellsSeries;
|
||||
memcpy(buffer, &startVoltage, sizeof(startVoltage));
|
||||
memcpy(buffer + 4, &endVoltage, sizeof(endVoltage));
|
||||
addCanPacketToQueue(0x502, buffer, can2_transmit_queue, &can2_transmit_queue_size);
|
||||
|
||||
// sending fault voltages (under, over) command
|
||||
const float startFaultVoltage = generalConfig->cellLCSoftUnderVoltage * generalConfig->noOfCellsSeries;
|
||||
const float endFaultVoltage = generalConfig->cellSoftOverVoltage * generalConfig->noOfCellsSeries;
|
||||
memcpy(buffer, &startFaultVoltage, sizeof(startFaultVoltage));
|
||||
memcpy(buffer + 4, &endFaultVoltage, sizeof(endFaultVoltage));
|
||||
addCanPacketToQueue(0x503, buffer, can2_transmit_queue, &can2_transmit_queue_size);
|
||||
|
||||
// sending charge start current
|
||||
float chargeStartingCurrent = 15; // TODO move to generalConfig
|
||||
uint32_t chargeStartingCurrentInterval = 20; // in seconds TODO move to generalConfig
|
||||
memcpy(buffer, &chargeStartingCurrent, sizeof(chargeStartingCurrent));
|
||||
memcpy(buffer + 4, &chargeStartingCurrentInterval, sizeof(chargeStartingCurrentInterval));
|
||||
addCanPacketToQueue(0x504, buffer, can2_transmit_queue, &can2_transmit_queue_size);
|
||||
|
||||
// sending table current and end-of-charge current command
|
||||
const int socIndex = getIndexBySoc();
|
||||
const int temperatureIndex = getIndexByTemperature();
|
||||
float tableCurrent = 0;
|
||||
|
||||
if (socIndex != -1 && temperatureIndex != -1)
|
||||
{
|
||||
float tableValue = generalConfig->externalChargeCurrentTable[temperatureIndex][socIndex];
|
||||
float pureCurrent = generalConfig->externalChargeUnitTable[temperatureIndex][socIndex];
|
||||
tableCurrent = pureCurrent ? tableValue : generalConfig->batteryCapacity * tableValue;
|
||||
}
|
||||
|
||||
float chargeEndingCurrent = 5; // TODO move to generalConfig
|
||||
memcpy(buffer + 0, &tableCurrent, sizeof(tableCurrent));
|
||||
memcpy(buffer + 4, &chargeEndingCurrent, sizeof(chargeEndingCurrent));
|
||||
addCanPacketToQueue(0x505, buffer, can2_transmit_queue, &can2_transmit_queue_size);
|
||||
|
||||
// sending charge permission, charge ending, charge/discharge state command
|
||||
const uint8_t chargeAllowed = packState.chargeAllowed ? 0xFF : 0x00;
|
||||
const uint8_t chargeEnding = packState.SoC > 95 ? 0xFF : 0x00;
|
||||
const uint8_t chargeSwitchState = charge_switch_state ? 0xFF : 0x00;
|
||||
memset(buffer, '\0', sizeof(buffer));
|
||||
memcpy(buffer + 0, &chargeAllowed, sizeof(chargeAllowed));
|
||||
memcpy(buffer + 1, &chargeEnding, sizeof(chargeEnding));
|
||||
memcpy(buffer + 2, &chargeSwitchState, sizeof(chargeSwitchState));
|
||||
addCanPacketToQueue(0x506, buffer, can2_transmit_queue, &can2_transmit_queue_size);
|
||||
}
|
||||
|
||||
void addCanPacketToQueue(uint16_t id, uint8_t* data, uint8_t* queue, uint16_t* queueSize)
|
||||
{
|
||||
memcpy(queue + *queueSize, &id, 2);
|
||||
memcpy(queue + *queueSize + 2, data, 8);
|
||||
|
||||
*queueSize += 10;
|
||||
}
|
||||
|
||||
void transmitCanPacketFromQueue(uint8_t* queue, uint16_t* queueSize, CAN_HandleTypeDef* hcan)
|
||||
{
|
||||
if (*queueSize < 10)
|
||||
return;
|
||||
|
||||
uint16_t id = 0;
|
||||
uint8_t data[8] = {};
|
||||
|
||||
memcpy(&id, queue, 2);
|
||||
memcpy(data, queue + 2, 8);
|
||||
|
||||
*queueSize -= 10;
|
||||
memmove(queue, queue + 10, *queueSize);
|
||||
|
||||
CAN_Transmit(id, data, 8, hcan);
|
||||
}
|
||||
|
||||
void modem_init(){
|
||||
|
||||
SIM800_Var_Init(&SIM800_Struct);
|
||||
@@ -2236,7 +1970,7 @@ int main(void)
|
||||
|
||||
//MX_CAN1_Init();
|
||||
//MX_CAN2_Init();
|
||||
|
||||
can_msgr_init();
|
||||
CAN_SetSpeed(CAN_SPD_100, &hcan1);
|
||||
CAN_SetSpeed(CAN_SPD_100, &hcan2);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user