diff --git a/firmware/Libs/CAN/can_messenger.c b/firmware/Libs/CAN/can_messenger.c index 167eec2..2a72f93 100644 --- a/firmware/Libs/CAN/can_messenger.c +++ b/firmware/Libs/CAN/can_messenger.c @@ -62,78 +62,103 @@ bool get_dummy_soc(void){ } void transmitCan1Packet() { - if ((TIM_Clock - can1_transmit_clock) < 1000) { + if ((TIM_Clock - can1_transmit_clock) < 500) + { 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; + // SOC and SOH (TODO) + can1_buf.id = 0x101; + can1_buf.len = 8; + memcpy(can1_buf.msg, &packState.SoC, sizeof(packState.SoC)); + memcpy(can1_buf.msg + 4, &packState.SoCCapacityAh, sizeof(packState.SoCCapacityAh)); + RingBuf_CellPut(&can1_buf, &can1_ring); - uint8_t data[32] = {}; - uint32_t mailbox = 0; + // pack voltage and current + can1_buf.id = 0x102; + can1_buf.len = 8; + memcpy(can1_buf.msg, &packState.packVoltage, sizeof(packState.packVoltage)); + memcpy(can1_buf.msg + 4, &packState.packCurrent, sizeof(packState.packCurrent)); + RingBuf_CellPut(&can1_buf, &can1_ring); - // 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); + // inputs and outputs state + can1_buf.id = 0x103; + can1_buf.len = 8; - // 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); + const uint8_t input0 = HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_7); + const uint8_t input1 = HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_8); + const uint8_t input2 = HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_9); - // 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); + const uint8_t output0 = HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_0); + const uint8_t output1 = HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_1); + const uint8_t output2 = HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_2); + const uint8_t output3 = HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_3); - // 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); + memcpy(can1_buf.msg + 0, &input0, sizeof(input0)); + memcpy(can1_buf.msg + 1, &input1, sizeof(input1)); + memcpy(can1_buf.msg + 2, &input2, sizeof(input2)); + + memcpy(can1_buf.msg + 4, &output0, sizeof(output0)); + memcpy(can1_buf.msg + 5, &output1, sizeof(output1)); + memcpy(can1_buf.msg + 6, &output2, sizeof(output2)); + memcpy(can1_buf.msg + 7, &output3, sizeof(output3)); + + RingBuf_CellPut(&can1_buf, &can1_ring); + + // number of cells and temperature sensors + can1_buf.id = 0x104; + can1_buf.len = 8; + const uint32_t numberOfCells = generalConfig->noOfCellsSeries * generalConfig->noOfParallelModules; + const uint32_t numberOfTemperatures = generalConfig->cellMonitorICCount * generalConfig->noOfTempSensorPerModule; + memcpy(can1_buf.msg, &numberOfCells, sizeof(numberOfCells)); + memcpy(can1_buf.msg + 4, &numberOfTemperatures, sizeof(numberOfTemperatures)); + RingBuf_CellPut(&can1_buf, &can1_ring); + + // cell voltages + can1_buf.id = 0x201; + can1_buf.len = 8; + + for (int i = 0; i < numberOfCells; i += 2) + { + for (int j = 0; j < 2; ++j) + { + const int index = i + j; + float voltage = 0; + if (packState.cellVoltagesIndividual[index].cellBleedActive) + voltage = packState.cellVoltagesIndividual[index].cellVoltage * -1000; + else + voltage = packState.cellVoltagesIndividual[index].cellVoltage * 1000; + + memcpy(can1_buf.msg + j * 4, &voltage, sizeof(voltage)); + } + + if (i + 2 > numberOfCells) + can1_buf.len = 4; + + RingBuf_CellPut(&can1_buf, &can1_ring); + can1_buf.id++; } - // 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); - } + // temperatures + can1_buf.id = 0x301; + can1_buf.len = 8; - can1_transmit_clock = TIM_Clock; + for (int i = 0; i < numberOfTemperatures; i += 2) + { + for (int j = 0; j < 2; ++j) + { + float temperature = packState.temperatures[i + j]; + memcpy(can1_buf.msg + j * 4, &temperature, sizeof(temperature)); + } + + if (i + 2 > numberOfTemperatures) + can1_buf.len = 4; + + RingBuf_CellPut(&can1_buf, &can1_ring); + can1_buf.id++; + } } int getIndexBySoc() {