Cell voltages fork (SoC)
Dependencies: CUER_CAN CUER_DS1820 LTC2943 LTC6804 mbed PowerControl
CANParserBMU.cpp@5:793afeef45dc, 2017-01-16 (annotated)
- Committer:
- lcockerton62
- Date:
- Mon Jan 16 14:39:30 2017 +0000
- Revision:
- 5:793afeef45dc
- Parent:
- 3:527790e4965a
- Child:
- 9:82ba050a7e13
Merged the fork: BMS_BMUCore_Luke_with_SoC
Who changed what in which revision?
User | Revision | Line number | New 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 | CANMessage msg; |
lcockerton62 | 0:0a5f554d2a16 | 80 | msg.len = 8; |
lcockerton62 | 1:51477fe4851b | 81 | msg.id = BMS_BASE_ID + MAX_MIN_VOLTAGE; |
lcockerton62 | 1:51477fe4851b | 82 | |
lcockerton62 | 0:0a5f554d2a16 | 83 | CAN_Data data; |
lcockerton62 | 1:51477fe4851b | 84 | data.set_u16(0,min_voltage.voltage); //Min voltage |
lcockerton62 | 1:51477fe4851b | 85 | data.set_u16(1,max_voltage.voltage); //Max voltage |
lcockerton62 | 1:51477fe4851b | 86 | data.set_u8(4,min_voltage.CMU_number); //CMU number of lowest cell |
lcockerton62 | 1:51477fe4851b | 87 | data.set_u8(5,min_voltage.cell_number); //Cell number in CMU with lowest voltage |
lcockerton62 | 1:51477fe4851b | 88 | data.set_u8(6,min_voltage.CMU_number); //CMU number of maxiumum cell |
lcockerton62 | 1:51477fe4851b | 89 | data.set_u8(7,min_voltage.cell_number); //Cell number in CMU with highest voltage |
lcockerton62 | 1:51477fe4851b | 90 | |
lcockerton62 | 1:51477fe4851b | 91 | for(int i=0; i<8; i++) { |
lcockerton62 | 0:0a5f554d2a16 | 92 | msg.data[i] = data.get_u8(i); |
lcockerton62 | 0:0a5f554d2a16 | 93 | } |
lcockerton62 | 1:51477fe4851b | 94 | |
lcockerton62 | 0:0a5f554d2a16 | 95 | return msg; |
lcockerton62 | 0:0a5f554d2a16 | 96 | } |
lcockerton62 | 0:0a5f554d2a16 | 97 | |
lcockerton62 | 1:51477fe4851b | 98 | CANMessage createCellTemperatureMAXMIN(pack_temperature_extremes min_temperature, pack_temperature_extremes max_temperature) |
lcockerton62 | 0:0a5f554d2a16 | 99 | { |
lcockerton62 | 0:0a5f554d2a16 | 100 | CANMessage msg; |
lcockerton62 | 0:0a5f554d2a16 | 101 | msg.len = 8; |
lcockerton62 | 1:51477fe4851b | 102 | msg.id = BMS_BASE_ID + MAX_MIN_TEMPERATURE; |
lcockerton62 | 1:51477fe4851b | 103 | |
lcockerton62 | 0:0a5f554d2a16 | 104 | CAN_Data data; |
lcockerton62 | 1:51477fe4851b | 105 | data.set_u16(0,min_temperature.temperature); //Min temperature |
lcockerton62 | 1:51477fe4851b | 106 | data.set_u16(1,max_temperature.temperature); //Max temperature |
lcockerton62 | 1:51477fe4851b | 107 | data.set_u8(4,min_temperature.CMU_number); //CMU number of lowest temperature cell |
lcockerton62 | 0:0a5f554d2a16 | 108 | data.set_u8(5,BLANK_DATA); //Dummy data |
lcockerton62 | 1:51477fe4851b | 109 | data.set_u8(6,max_temperature.CMU_number); //CMU number of maxiumum temperature cell |
lcockerton62 | 1:51477fe4851b | 110 | data.set_u8(7,BLANK_DATA); //Dummy data |
lcockerton62 | 1:51477fe4851b | 111 | |
lcockerton62 | 1:51477fe4851b | 112 | for(int i=0; i<8; i++) { |
lcockerton62 | 0:0a5f554d2a16 | 113 | msg.data[i] = data.get_u8(i); |
lcockerton62 | 0:0a5f554d2a16 | 114 | } |
lcockerton62 | 1:51477fe4851b | 115 | |
lcockerton62 | 0:0a5f554d2a16 | 116 | return msg; |
lcockerton62 | 0:0a5f554d2a16 | 117 | } |
lcockerton62 | 0:0a5f554d2a16 | 118 | |
lcockerton62 | 0:0a5f554d2a16 | 119 | CANMessage createBatteryVI(uint32_t batteryVoltage,uint32_t batteryCurrent) |
lcockerton62 | 0:0a5f554d2a16 | 120 | { |
lcockerton62 | 0:0a5f554d2a16 | 121 | CANMessage msg; |
lcockerton62 | 0:0a5f554d2a16 | 122 | msg.len = 8; |
lcockerton62 | 1:51477fe4851b | 123 | msg.id = BMS_BASE_ID + BATTERY_VI_ID; |
lcockerton62 | 1:51477fe4851b | 124 | |
lcockerton62 | 0:0a5f554d2a16 | 125 | CAN_Data data; |
lcockerton62 | 3:527790e4965a | 126 | data.setLower_uLong(batteryVoltage); |
lcockerton62 | 3:527790e4965a | 127 | data.setHigher_uLong(batteryCurrent); |
lcockerton62 | 1:51477fe4851b | 128 | |
lcockerton62 | 1:51477fe4851b | 129 | for(int i=0; i<8; i++) { |
lcockerton62 | 0:0a5f554d2a16 | 130 | msg.data[i] = data.get_u8(i); |
lcockerton62 | 0:0a5f554d2a16 | 131 | } |
lcockerton62 | 0:0a5f554d2a16 | 132 | return msg; |
lcockerton62 | 0:0a5f554d2a16 | 133 | } |
lcockerton62 | 0:0a5f554d2a16 | 134 | |
lcockerton62 | 0:0a5f554d2a16 | 135 | CANMessage createBatteryPackStatus(uint16_t voltageThreshold[], uint8_t statusFlag,uint8_t BMS_CMU_Count,uint16_t BMS_Firmware_Build) |
lcockerton62 | 0:0a5f554d2a16 | 136 | { |
lcockerton62 | 1:51477fe4851b | 137 | CANMessage msg; |
lcockerton62 | 1:51477fe4851b | 138 | msg.len = 8; |
lcockerton62 | 1:51477fe4851b | 139 | msg.id = BMS_BASE_ID + BATTERY_PACK_STATUS_ID; |
lcockerton62 | 1:51477fe4851b | 140 | |
lcockerton62 | 1:51477fe4851b | 141 | CAN_Data data; |
lcockerton62 | 1:51477fe4851b | 142 | data.set_u16(0,voltageThreshold[0]); |
lcockerton62 | 1:51477fe4851b | 143 | data.set_u16(1,voltageThreshold[1]); |
lcockerton62 | 1:51477fe4851b | 144 | data.set_16(3,BMS_Firmware_Build); |
lcockerton62 | 1:51477fe4851b | 145 | data.set_u8(4,statusFlag); |
lcockerton62 | 1:51477fe4851b | 146 | data.set_u8(5,BMS_CMU_Count); |
lcockerton62 | 1:51477fe4851b | 147 | |
lcockerton62 | 1:51477fe4851b | 148 | for(int i=0; i<8; i++) { |
lcockerton62 | 1:51477fe4851b | 149 | msg.data[i] = data.get_u8(i); |
lcockerton62 | 1:51477fe4851b | 150 | } |
lcockerton62 | 1:51477fe4851b | 151 | return msg; |
lcockerton62 | 0:0a5f554d2a16 | 152 | } |
lcockerton62 | 0:0a5f554d2a16 | 153 | |
lcockerton62 | 0:0a5f554d2a16 | 154 | CANMessage createExtendedBatteryPackStatus(uint32_t status) |
lcockerton62 | 0:0a5f554d2a16 | 155 | { |
lcockerton62 | 1:51477fe4851b | 156 | CANMessage msg; |
lcockerton62 | 1:51477fe4851b | 157 | msg.len = 8; |
lcockerton62 | 1:51477fe4851b | 158 | msg.id = BMS_BASE_ID + BATTERY_STATUS_ID; |
lcockerton62 | 1:51477fe4851b | 159 | |
lcockerton62 | 1:51477fe4851b | 160 | CAN_Data data; |
lcockerton62 | 1:51477fe4851b | 161 | data.setLower_uLong(status); //@TODO see the data sheet for this |
lcockerton62 | 1:51477fe4851b | 162 | data.set_u8(4,0x00);//Hardware version random data @TODO check this |
lcockerton62 | 1:51477fe4851b | 163 | data.set_u8(5,0x00);//Model ID @TODO check this |
lcockerton62 | 1:51477fe4851b | 164 | data.set_u16(3,0x00); // Unused |
lcockerton62 | 1:51477fe4851b | 165 | |
lcockerton62 | 1:51477fe4851b | 166 | for(int i=0; i<8; i++) { |
lcockerton62 | 1:51477fe4851b | 167 | msg.data[i] = data.get_u8(i); |
lcockerton62 | 1:51477fe4851b | 168 | } |
lcockerton62 | 1:51477fe4851b | 169 | return msg; |
lcockerton62 | 0:0a5f554d2a16 | 170 | } |
lcockerton62 | 0:0a5f554d2a16 | 171 | |
lcockerton62 | 0:0a5f554d2a16 | 172 | void convertFloatFloat(float lower, float upper, CANMessage& msg, bool littleEndian) |
lcockerton62 | 0:0a5f554d2a16 | 173 | { |
lcockerton62 | 0:0a5f554d2a16 | 174 | // Code taken from driver_controls |
lcockerton62 | 0:0a5f554d2a16 | 175 | //two converters for lower and higher float |
lcockerton62 | 0:0a5f554d2a16 | 176 | float2byte convL; |
lcockerton62 | 0:0a5f554d2a16 | 177 | float2byte convH; |
lcockerton62 | 0:0a5f554d2a16 | 178 | convL.f = lower; |
lcockerton62 | 0:0a5f554d2a16 | 179 | convH.f = upper; |
lcockerton62 | 0:0a5f554d2a16 | 180 | if(littleEndian) { |
lcockerton62 | 0:0a5f554d2a16 | 181 | for(int i=0; i<4; i++) { |
lcockerton62 | 0:0a5f554d2a16 | 182 | msg.data[i] = convL.b[i]; |
lcockerton62 | 0:0a5f554d2a16 | 183 | //offset for upper float |
lcockerton62 | 0:0a5f554d2a16 | 184 | msg.data[i+4]=convH.b[i]; |
lcockerton62 | 0:0a5f554d2a16 | 185 | } |
lcockerton62 | 0:0a5f554d2a16 | 186 | } else { |
lcockerton62 | 0:0a5f554d2a16 | 187 | for(int i=0; i<4; i++) { |
lcockerton62 | 0:0a5f554d2a16 | 188 | /* |
lcockerton62 | 0:0a5f554d2a16 | 189 | * Subtract because output data is Big Endian |
lcockerton62 | 0:0a5f554d2a16 | 190 | * i.e. convL/H is LSB --> MSB |
lcockerton62 | 0:0a5f554d2a16 | 191 | * output is MSB --> LSB |
lcockerton62 | 0:0a5f554d2a16 | 192 | */ |
lcockerton62 | 1:51477fe4851b | 193 | |
lcockerton62 | 0:0a5f554d2a16 | 194 | msg.data[4-i] = convL.b[i]; |
lcockerton62 | 0:0a5f554d2a16 | 195 | msg.data[7-i] = convH.b[i]; |
lcockerton62 | 0:0a5f554d2a16 | 196 | } |
lcockerton62 | 0:0a5f554d2a16 | 197 | } |
lcockerton62 | 1:51477fe4851b | 198 | |
lcockerton62 | 0:0a5f554d2a16 | 199 | } |
lcockerton62 | 0:0a5f554d2a16 | 200 |