Has base BMU code but sends dummy temperature and voltage readings to test CAN

Dependencies:   CUER_CAN DS1820 LTC2943 LTC6804 mbed

Fork of BMS_BMUCore_Max by CUER

Committer:
lcockerton62
Date:
Fri Dec 30 16:01:59 2016 +0000
Revision:
1:51477fe4851b
Parent:
0:0a5f554d2a16
Child:
3:527790e4965a
Storing SOC in EEPROM, added temperature sensors code, changed some of the data structures, added to CAN messages, added basic error status

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lcockerton62 0:0a5f554d2a16 1 // Here are the functions to generate the CAN messages
lcockerton62 0:0a5f554d2a16 2 #include "CANParserBMU.h"
lcockerton62 0:0a5f554d2a16 3 #include "mbed.h"
lcockerton62 0:0a5f554d2a16 4
lcockerton62 1:51477fe4851b 5
lcockerton62 0:0a5f554d2a16 6 using namespace CAN_IDs;
lcockerton62 0:0a5f554d2a16 7
lcockerton62 0:0a5f554d2a16 8 CANMessage createTemperatureTelemetry(uint8_t ID, uint32_t CMUSerialNumber, uint16_t PCBTemperature, uint16_t cellTemperature)
lcockerton62 0:0a5f554d2a16 9 {
lcockerton62 0:0a5f554d2a16 10 CANMessage msg;
lcockerton62 0:0a5f554d2a16 11 msg.len = 8;
lcockerton62 1:51477fe4851b 12 msg.id = BMS_BASE_ID + ID; // for voltage 0x601 - 0x6EF
lcockerton62 0:0a5f554d2a16 13 CAN_Data data;
lcockerton62 0:0a5f554d2a16 14
lcockerton62 1:51477fe4851b 15 data.setLower_uLong(CMUSerialNumber);
lcockerton62 0:0a5f554d2a16 16 data.set_u16(2, PCBTemperature);
lcockerton62 0:0a5f554d2a16 17 data.set_u16(3, cellTemperature);
lcockerton62 0:0a5f554d2a16 18
lcockerton62 0:0a5f554d2a16 19 for (int i = 0; i<8; i++) {
lcockerton62 0:0a5f554d2a16 20 msg.data[i] = data.get_u8(i);
lcockerton62 0:0a5f554d2a16 21 }
lcockerton62 0:0a5f554d2a16 22
lcockerton62 0:0a5f554d2a16 23 return msg;
lcockerton62 0:0a5f554d2a16 24 }
lcockerton62 0:0a5f554d2a16 25
lcockerton62 0:0a5f554d2a16 26 CANMessage createVoltageTelemetry(int ID, uint16_t voltage[])
lcockerton62 0:0a5f554d2a16 27 {
lcockerton62 0:0a5f554d2a16 28 CANMessage msg;
lcockerton62 0:0a5f554d2a16 29 msg.len = 8;
lcockerton62 1:51477fe4851b 30 msg.id = BMS_BASE_ID + ID; // for voltage 0x601 - 0x6EF @TODO
lcockerton62 0:0a5f554d2a16 31 CAN_Data data;
lcockerton62 0:0a5f554d2a16 32
lcockerton62 0:0a5f554d2a16 33 data.set_u16(0, voltage[0]);
lcockerton62 0:0a5f554d2a16 34 data.set_u16(1, voltage[1]);
lcockerton62 0:0a5f554d2a16 35 data.set_u16(2, voltage[2]);
lcockerton62 0:0a5f554d2a16 36 data.set_u16(3, voltage[3]);
lcockerton62 0:0a5f554d2a16 37
lcockerton62 0:0a5f554d2a16 38 for (int i = 0; i<8; i++) {
lcockerton62 0:0a5f554d2a16 39 msg.data[i] = data.get_u8(i);
lcockerton62 0:0a5f554d2a16 40 }
lcockerton62 0:0a5f554d2a16 41
lcockerton62 0:0a5f554d2a16 42 return msg;
lcockerton62 0:0a5f554d2a16 43 }
lcockerton62 0:0a5f554d2a16 44
lcockerton62 0:0a5f554d2a16 45 CANMessage createPackSOC(float SOC, float percentageCharge)
lcockerton62 0:0a5f554d2a16 46 {
lcockerton62 0:0a5f554d2a16 47 CANMessage msg;
lcockerton62 0:0a5f554d2a16 48 msg.len = 8;
lcockerton62 0:0a5f554d2a16 49 msg.id = BMS_BASE_ID + BATTERY_SOC_ID;
lcockerton62 0:0a5f554d2a16 50 CAN_Data data;
lcockerton62 0:0a5f554d2a16 51 data.setLowerFloat(SOC);
lcockerton62 0:0a5f554d2a16 52 data.setUpperFloat(percentageCharge);
lcockerton62 1:51477fe4851b 53 for(int i=0; i<8; i++) {
lcockerton62 0:0a5f554d2a16 54 msg.data[i] = data.get_u8(i);
lcockerton62 0:0a5f554d2a16 55 }
lcockerton62 1:51477fe4851b 56
lcockerton62 0:0a5f554d2a16 57 return msg;
lcockerton62 0:0a5f554d2a16 58 }
lcockerton62 0:0a5f554d2a16 59
lcockerton62 0:0a5f554d2a16 60 CANMessage createPackBalanceSOC(float SOC, float percentageCharge)
lcockerton62 0:0a5f554d2a16 61 {
lcockerton62 0:0a5f554d2a16 62 // @TODO - check is this being used?? section 5.4 trituim BMU CAN data sheet
lcockerton62 0:0a5f554d2a16 63 CANMessage msg;
lcockerton62 0:0a5f554d2a16 64 msg.len = 8;
lcockerton62 1:51477fe4851b 65 msg.id = BMS_BASE_ID + BATTERY_SOC_BASE_ID;
lcockerton62 1:51477fe4851b 66
lcockerton62 0:0a5f554d2a16 67 CAN_Data data;
lcockerton62 0:0a5f554d2a16 68 data.setLowerFloat(SOC);
lcockerton62 0:0a5f554d2a16 69 data.setUpperFloat(percentageCharge);
lcockerton62 1:51477fe4851b 70 for(int i=0; i<8; i++) {
lcockerton62 0:0a5f554d2a16 71 msg.data[i] = data.get_u8(i);
lcockerton62 0:0a5f554d2a16 72 }
lcockerton62 1:51477fe4851b 73
lcockerton62 0:0a5f554d2a16 74 return msg;
lcockerton62 0:0a5f554d2a16 75 }
lcockerton62 0:0a5f554d2a16 76
lcockerton62 1:51477fe4851b 77 CANMessage createCellVoltageMAXMIN(pack_voltage_extremes max_voltage, pack_voltage_extremes min_voltage)
lcockerton62 0:0a5f554d2a16 78 {
lcockerton62 0:0a5f554d2a16 79 //@TODO create a structure to store this data , so that the data is stored in the correct order
lcockerton62 0:0a5f554d2a16 80 CANMessage msg;
lcockerton62 0:0a5f554d2a16 81 msg.len = 8;
lcockerton62 1:51477fe4851b 82 msg.id = BMS_BASE_ID + MAX_MIN_VOLTAGE;
lcockerton62 1:51477fe4851b 83
lcockerton62 0:0a5f554d2a16 84 CAN_Data data;
lcockerton62 1:51477fe4851b 85 data.set_u16(0,min_voltage.voltage); //Min voltage
lcockerton62 1:51477fe4851b 86 data.set_u16(1,max_voltage.voltage); //Max voltage
lcockerton62 1:51477fe4851b 87 data.set_u8(4,min_voltage.CMU_number); //CMU number of lowest cell
lcockerton62 1:51477fe4851b 88 data.set_u8(5,min_voltage.cell_number); //Cell number in CMU with lowest voltage
lcockerton62 1:51477fe4851b 89 data.set_u8(6,min_voltage.CMU_number); //CMU number of maxiumum cell
lcockerton62 1:51477fe4851b 90 data.set_u8(7,min_voltage.cell_number); //Cell number in CMU with highest voltage
lcockerton62 1:51477fe4851b 91
lcockerton62 1:51477fe4851b 92 for(int i=0; i<8; i++) {
lcockerton62 0:0a5f554d2a16 93 msg.data[i] = data.get_u8(i);
lcockerton62 0:0a5f554d2a16 94 }
lcockerton62 1:51477fe4851b 95
lcockerton62 0:0a5f554d2a16 96 return msg;
lcockerton62 0:0a5f554d2a16 97 }
lcockerton62 0:0a5f554d2a16 98
lcockerton62 1:51477fe4851b 99 CANMessage createCellTemperatureMAXMIN(pack_temperature_extremes min_temperature, pack_temperature_extremes max_temperature)
lcockerton62 0:0a5f554d2a16 100 {
lcockerton62 0:0a5f554d2a16 101 //@TODO create a structure to store this data , so that the data is stored in the correct order
lcockerton62 0:0a5f554d2a16 102 CANMessage msg;
lcockerton62 0:0a5f554d2a16 103 msg.len = 8;
lcockerton62 1:51477fe4851b 104 msg.id = BMS_BASE_ID + MAX_MIN_TEMPERATURE;
lcockerton62 1:51477fe4851b 105
lcockerton62 0:0a5f554d2a16 106 CAN_Data data;
lcockerton62 1:51477fe4851b 107 data.set_u16(0,min_temperature.temperature); //Min temperature
lcockerton62 1:51477fe4851b 108 data.set_u16(1,max_temperature.temperature); //Max temperature
lcockerton62 1:51477fe4851b 109 data.set_u8(4,min_temperature.CMU_number); //CMU number of lowest temperature cell
lcockerton62 0:0a5f554d2a16 110 data.set_u8(5,BLANK_DATA); //Dummy data
lcockerton62 1:51477fe4851b 111 data.set_u8(6,max_temperature.CMU_number); //CMU number of maxiumum temperature cell
lcockerton62 1:51477fe4851b 112 data.set_u8(7,BLANK_DATA); //Dummy data
lcockerton62 1:51477fe4851b 113
lcockerton62 1:51477fe4851b 114 for(int i=0; i<8; i++) {
lcockerton62 0:0a5f554d2a16 115 msg.data[i] = data.get_u8(i);
lcockerton62 0:0a5f554d2a16 116 }
lcockerton62 1:51477fe4851b 117
lcockerton62 0:0a5f554d2a16 118 return msg;
lcockerton62 0:0a5f554d2a16 119 }
lcockerton62 0:0a5f554d2a16 120
lcockerton62 0:0a5f554d2a16 121 CANMessage createBatteryVI(uint32_t batteryVoltage,uint32_t batteryCurrent)
lcockerton62 0:0a5f554d2a16 122 {
lcockerton62 0:0a5f554d2a16 123 CANMessage msg;
lcockerton62 0:0a5f554d2a16 124 msg.len = 8;
lcockerton62 1:51477fe4851b 125 msg.id = BMS_BASE_ID + BATTERY_VI_ID;
lcockerton62 1:51477fe4851b 126
lcockerton62 0:0a5f554d2a16 127 CAN_Data data;
lcockerton62 0:0a5f554d2a16 128 //TODO check the below lines
lcockerton62 1:51477fe4851b 129 // data.set_u32(0,batteryVoltage);
lcockerton62 0:0a5f554d2a16 130 //data.set_u32(1,batteryCurrent);
lcockerton62 1:51477fe4851b 131
lcockerton62 1:51477fe4851b 132 for(int i=0; i<8; i++) {
lcockerton62 0:0a5f554d2a16 133 msg.data[i] = data.get_u8(i);
lcockerton62 0:0a5f554d2a16 134 }
lcockerton62 0:0a5f554d2a16 135 return msg;
lcockerton62 0:0a5f554d2a16 136 }
lcockerton62 0:0a5f554d2a16 137
lcockerton62 0:0a5f554d2a16 138 CANMessage createBatteryPackStatus(uint16_t voltageThreshold[], uint8_t statusFlag,uint8_t BMS_CMU_Count,uint16_t BMS_Firmware_Build)
lcockerton62 0:0a5f554d2a16 139 {
lcockerton62 1:51477fe4851b 140 CANMessage msg;
lcockerton62 1:51477fe4851b 141 msg.len = 8;
lcockerton62 1:51477fe4851b 142 msg.id = BMS_BASE_ID + BATTERY_PACK_STATUS_ID;
lcockerton62 1:51477fe4851b 143
lcockerton62 1:51477fe4851b 144 CAN_Data data;
lcockerton62 1:51477fe4851b 145 data.set_u16(0,voltageThreshold[0]);
lcockerton62 1:51477fe4851b 146 data.set_u16(1,voltageThreshold[1]);
lcockerton62 1:51477fe4851b 147 data.set_16(3,BMS_Firmware_Build);
lcockerton62 1:51477fe4851b 148 data.set_u8(4,statusFlag);
lcockerton62 1:51477fe4851b 149 data.set_u8(5,BMS_CMU_Count);
lcockerton62 1:51477fe4851b 150
lcockerton62 1:51477fe4851b 151 for(int i=0; i<8; i++) {
lcockerton62 1:51477fe4851b 152 msg.data[i] = data.get_u8(i);
lcockerton62 1:51477fe4851b 153 }
lcockerton62 1:51477fe4851b 154 return msg;
lcockerton62 0:0a5f554d2a16 155 }
lcockerton62 0:0a5f554d2a16 156
lcockerton62 0:0a5f554d2a16 157 CANMessage createExtendedBatteryPackStatus(uint32_t status)
lcockerton62 0:0a5f554d2a16 158 {
lcockerton62 1:51477fe4851b 159 CANMessage msg;
lcockerton62 1:51477fe4851b 160 msg.len = 8;
lcockerton62 1:51477fe4851b 161 msg.id = BMS_BASE_ID + BATTERY_STATUS_ID;
lcockerton62 1:51477fe4851b 162
lcockerton62 1:51477fe4851b 163 CAN_Data data;
lcockerton62 1:51477fe4851b 164 data.setLower_uLong(status); //@TODO see the data sheet for this
lcockerton62 1:51477fe4851b 165 data.set_u8(4,0x00);//Hardware version random data @TODO check this
lcockerton62 1:51477fe4851b 166 data.set_u8(5,0x00);//Model ID @TODO check this
lcockerton62 1:51477fe4851b 167 data.set_u16(3,0x00); // Unused
lcockerton62 1:51477fe4851b 168
lcockerton62 1:51477fe4851b 169 for(int i=0; i<8; i++) {
lcockerton62 1:51477fe4851b 170 msg.data[i] = data.get_u8(i);
lcockerton62 1:51477fe4851b 171 }
lcockerton62 1:51477fe4851b 172 return msg;
lcockerton62 0:0a5f554d2a16 173 }
lcockerton62 0:0a5f554d2a16 174
lcockerton62 0:0a5f554d2a16 175 void convertFloatFloat(float lower, float upper, CANMessage& msg, bool littleEndian)
lcockerton62 0:0a5f554d2a16 176 {
lcockerton62 0:0a5f554d2a16 177 // Code taken from driver_controls
lcockerton62 0:0a5f554d2a16 178 //two converters for lower and higher float
lcockerton62 0:0a5f554d2a16 179 float2byte convL;
lcockerton62 0:0a5f554d2a16 180 float2byte convH;
lcockerton62 0:0a5f554d2a16 181 convL.f = lower;
lcockerton62 0:0a5f554d2a16 182 convH.f = upper;
lcockerton62 0:0a5f554d2a16 183 if(littleEndian) {
lcockerton62 0:0a5f554d2a16 184 for(int i=0; i<4; i++) {
lcockerton62 0:0a5f554d2a16 185 msg.data[i] = convL.b[i];
lcockerton62 0:0a5f554d2a16 186 //offset for upper float
lcockerton62 0:0a5f554d2a16 187 msg.data[i+4]=convH.b[i];
lcockerton62 0:0a5f554d2a16 188 }
lcockerton62 0:0a5f554d2a16 189 } else {
lcockerton62 0:0a5f554d2a16 190 for(int i=0; i<4; i++) {
lcockerton62 0:0a5f554d2a16 191 /*
lcockerton62 0:0a5f554d2a16 192 * Subtract because output data is Big Endian
lcockerton62 0:0a5f554d2a16 193 * i.e. convL/H is LSB --> MSB
lcockerton62 0:0a5f554d2a16 194 * output is MSB --> LSB
lcockerton62 0:0a5f554d2a16 195 */
lcockerton62 1:51477fe4851b 196
lcockerton62 0:0a5f554d2a16 197 msg.data[4-i] = convL.b[i];
lcockerton62 0:0a5f554d2a16 198 msg.data[7-i] = convH.b[i];
lcockerton62 0:0a5f554d2a16 199 }
lcockerton62 0:0a5f554d2a16 200 }
lcockerton62 1:51477fe4851b 201
lcockerton62 0:0a5f554d2a16 202 }
lcockerton62 0:0a5f554d2a16 203