Cell voltages fork (SoC)

Dependencies:   CUER_CAN CUER_DS1820 LTC2943 LTC6804 mbed PowerControl

Committer:
DasSidG
Date:
Sat Sep 16 01:10:01 2017 +0000
Revision:
66:c884fba9eaea
Parent:
53:4277cdcff69b
Added more flags to the BMU status; Removed some unused functions, re-ordered some functions slightly; Tidied up some bits that were generating warnings

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"
maxv008 13:7b42af989cd1 4 #include "Data_Types_BMU.h"
maxv008 17:94dd9a0d3870 5 #include "CAN_IDs.h"
lcockerton62 0:0a5f554d2a16 6
lcockerton62 1:51477fe4851b 7
lcockerton62 0:0a5f554d2a16 8 using namespace CAN_IDs;
lcockerton62 0:0a5f554d2a16 9
maxv008 13:7b42af989cd1 10 /**
maxv008 13:7b42af989cd1 11 * This function is rewritten to give readings for individual probes rather than
maxv008 13:7b42af989cd1 12 * for specific CMU. As a consequence, 0x800 onwards is being used for these probes,
maxv008 14:e0e88a009f4c 13 * as everything above about 0x700 is unused by the Tritium standard. The ID value
maxv008 14:e0e88a009f4c 14 * for the probe is based on the ROM field of DS1820, entries 1-6 being the unique
maxv008 14:e0e88a009f4c 15 * serial value.
maxv008 13:7b42af989cd1 16 */
maxv008 14:e0e88a009f4c 17 CANMessage createTemperatureTelemetry(uint8_t offset, char ProbeROM[8], float Temperature)
lcockerton62 0:0a5f554d2a16 18 {
lcockerton62 0:0a5f554d2a16 19 CANMessage msg;
lcockerton62 0:0a5f554d2a16 20 msg.len = 8;
maxv008 13:7b42af989cd1 21 msg.id = TEMPERATURE_BASE_ID + offset; // for temp it is 0x800 onwards
lcockerton62 0:0a5f554d2a16 22 CAN_Data data;
lcockerton62 0:0a5f554d2a16 23
maxv008 14:e0e88a009f4c 24 for(int i = 1; i <= 6; i++) //ID portion of ROM array
maxv008 14:e0e88a009f4c 25 {
maxv008 14:e0e88a009f4c 26 data.set_u8(i - 1, ProbeROM[i]);
maxv008 14:e0e88a009f4c 27 }
maxv008 14:e0e88a009f4c 28 //Conversion of float to a short, requires multiplying by 100 to not lose precision
maxv008 14:e0e88a009f4c 29 float temp100 = Temperature * 100;
maxv008 14:e0e88a009f4c 30 short shortTemp = (short) temp100;
maxv008 14:e0e88a009f4c 31 data.set_16(3, shortTemp);//There seems to be an error in the function definition for set_16, (ushort instead of short)
maxv008 14:e0e88a009f4c 32
lcockerton62 0:0a5f554d2a16 33 for (int i = 0; i<8; i++) {
lcockerton62 0:0a5f554d2a16 34 msg.data[i] = data.get_u8(i);
lcockerton62 0:0a5f554d2a16 35 }
lcockerton62 0:0a5f554d2a16 36
lcockerton62 0:0a5f554d2a16 37 return msg;
lcockerton62 0:0a5f554d2a16 38 }
maxv008 13:7b42af989cd1 39 /**
maxv008 13:7b42af989cd1 40 * Takes a CANMessage with precondition that it stores temperature of an individual
maxv008 13:7b42af989cd1 41 * probe and returns an individual_temperature object containing ID and reading.
maxv008 14:e0e88a009f4c 42 * The ID is stores in ROM array entry 1-6, other entries may be invalid.
maxv008 13:7b42af989cd1 43 */
maxv008 13:7b42af989cd1 44 individual_temperature decodeTemperatureTelemetry(CANMessage msg)
maxv008 13:7b42af989cd1 45 {
maxv008 13:7b42af989cd1 46 individual_temperature probe_reading;
maxv008 13:7b42af989cd1 47 CAN_Data decode;
maxv008 53:4277cdcff69b 48 long fullID = 0;
maxv008 13:7b42af989cd1 49
maxv008 13:7b42af989cd1 50 decode.importCANData(msg);
maxv008 14:e0e88a009f4c 51 short shortTemp = decode.get_16(3);
maxv008 14:e0e88a009f4c 52 probe_reading.measurement = ((float)shortTemp) / 100;
maxv008 14:e0e88a009f4c 53
maxv008 14:e0e88a009f4c 54 for(int i = 1; i <=6; i++)
maxv008 14:e0e88a009f4c 55 {
maxv008 17:94dd9a0d3870 56 probe_reading.ROMID[i] = decode.get_u8(i-1);
maxv008 53:4277cdcff69b 57 fullID += (probe_reading.ROMID[i] << (8 * (i-1))); //Bit order not particularly important, must be consistent
maxv008 14:e0e88a009f4c 58 }
maxv008 17:94dd9a0d3870 59 probe_reading.ID = fullID;
maxv008 13:7b42af989cd1 60 return probe_reading;
maxv008 13:7b42af989cd1 61 }
lcockerton62 0:0a5f554d2a16 62
msharma97 9:82ba050a7e13 63 CANMessage createVoltageTelemetry(int offset_id, uint16_t voltage[])
lcockerton62 0:0a5f554d2a16 64 {
lcockerton62 0:0a5f554d2a16 65 CANMessage msg;
lcockerton62 0:0a5f554d2a16 66 msg.len = 8;
msharma97 9:82ba050a7e13 67 msg.id = BMS_BASE_ID + offset_id; // for voltage 0x601 - 0x6EF @TODO
lcockerton62 0:0a5f554d2a16 68 CAN_Data data;
lcockerton62 0:0a5f554d2a16 69
lcockerton62 0:0a5f554d2a16 70 data.set_u16(0, voltage[0]);
lcockerton62 0:0a5f554d2a16 71 data.set_u16(1, voltage[1]);
lcockerton62 0:0a5f554d2a16 72 data.set_u16(2, voltage[2]);
lcockerton62 0:0a5f554d2a16 73 data.set_u16(3, voltage[3]);
lcockerton62 0:0a5f554d2a16 74
lcockerton62 0:0a5f554d2a16 75 for (int i = 0; i<8; i++) {
lcockerton62 0:0a5f554d2a16 76 msg.data[i] = data.get_u8(i);
lcockerton62 0:0a5f554d2a16 77 }
lcockerton62 0:0a5f554d2a16 78
lcockerton62 0:0a5f554d2a16 79 return msg;
lcockerton62 0:0a5f554d2a16 80 }
maxv008 17:94dd9a0d3870 81 /**
maxv008 17:94dd9a0d3870 82 * This function will properly fill the appropriate entry in an array of voltage
maxv008 17:94dd9a0d3870 83 * readings of the form CMU_voltage voltage[NO_CMUS]. Uses the msg ID and the standard
maxv008 17:94dd9a0d3870 84 * meanings of them as decided in the transmit data function (modified Tritium specs).
maxv008 17:94dd9a0d3870 85 * Function must only be called when the msg has a valid ID for voltage!
maxv008 17:94dd9a0d3870 86 */
maxv008 18:521ffdd724f3 87 bool decodeVoltageTelemetry(CANMessage msg, CMU_voltage readings[NO_CMUS])
maxv008 17:94dd9a0d3870 88 {
maxv008 17:94dd9a0d3870 89 CAN_Data voltData;
maxv008 17:94dd9a0d3870 90 voltData.importCANData(msg);
maxv008 17:94dd9a0d3870 91 int repeating_length = NO_READINGS_PER_CMU /4 + 1;
maxv008 17:94dd9a0d3870 92 int offset = msg.id - BMS_BASE_ID;
ItsJustZi 29:44924d2b1293 93 if(offset <= 0 || offset >= 0x10 || offset % 4 == 1)
maxv008 18:521ffdd724f3 94 return false;
maxv008 17:94dd9a0d3870 95
maxv008 17:94dd9a0d3870 96 int cellsubset = ((offset-1) % repeating_length) - 1; //Which set of 4 voltages within the CMU
maxv008 17:94dd9a0d3870 97 int CMU_number = (offset-1) / repeating_length;
maxv008 17:94dd9a0d3870 98 for(int i = 0; i < 4; i++)
maxv008 17:94dd9a0d3870 99 {
maxv008 17:94dd9a0d3870 100 readings[CMU_number].voltages[cellsubset*4 + i] = voltData.get_u16(i);
maxv008 17:94dd9a0d3870 101 }
maxv008 18:521ffdd724f3 102 return true;
maxv008 17:94dd9a0d3870 103 }
lcockerton62 0:0a5f554d2a16 104
lcockerton62 0:0a5f554d2a16 105 CANMessage createPackSOC(float SOC, float percentageCharge)
lcockerton62 0:0a5f554d2a16 106 {
lcockerton62 0:0a5f554d2a16 107 CANMessage msg;
lcockerton62 0:0a5f554d2a16 108 msg.len = 8;
maxv008 23:a1af4439c1fc 109 msg.id = BMS_BASE_ID + BATTERY_SOC_ID; //0x6F4
lcockerton62 0:0a5f554d2a16 110 CAN_Data data;
lcockerton62 0:0a5f554d2a16 111 data.setLowerFloat(SOC);
lcockerton62 0:0a5f554d2a16 112 data.setUpperFloat(percentageCharge);
lcockerton62 1:51477fe4851b 113 for(int i=0; i<8; i++) {
lcockerton62 0:0a5f554d2a16 114 msg.data[i] = data.get_u8(i);
lcockerton62 0:0a5f554d2a16 115 }
lcockerton62 1:51477fe4851b 116
lcockerton62 0:0a5f554d2a16 117 return msg;
lcockerton62 0:0a5f554d2a16 118 }
lcockerton62 0:0a5f554d2a16 119
maxv008 23:a1af4439c1fc 120 /**
maxv008 23:a1af4439c1fc 121 * decodePackSOC and decodePackSOCPercentage can be used with both of the SOC msg types
maxv008 23:a1af4439c1fc 122 */
maxv008 23:a1af4439c1fc 123 float decodePackSOC(CANMessage msg)
maxv008 23:a1af4439c1fc 124 {
maxv008 23:a1af4439c1fc 125 CAN_Data data;
maxv008 23:a1af4439c1fc 126 data.importCANData(msg);
maxv008 23:a1af4439c1fc 127 return data.getLowerFloat();
maxv008 23:a1af4439c1fc 128 }
maxv008 23:a1af4439c1fc 129
maxv008 23:a1af4439c1fc 130 float decodePackSOCPercentage(CANMessage msg)
maxv008 23:a1af4439c1fc 131 {
maxv008 23:a1af4439c1fc 132 CAN_Data data;
maxv008 23:a1af4439c1fc 133 data.importCANData(msg);
maxv008 23:a1af4439c1fc 134 return data.getUpperFloat();
maxv008 23:a1af4439c1fc 135 }
maxv008 23:a1af4439c1fc 136
lcockerton62 1:51477fe4851b 137 CANMessage createCellVoltageMAXMIN(pack_voltage_extremes max_voltage, pack_voltage_extremes min_voltage)
lcockerton62 0:0a5f554d2a16 138 {
lcockerton62 0:0a5f554d2a16 139 CANMessage msg;
lcockerton62 0:0a5f554d2a16 140 msg.len = 8;
lcockerton62 1:51477fe4851b 141 msg.id = BMS_BASE_ID + MAX_MIN_VOLTAGE;
lcockerton62 1:51477fe4851b 142
lcockerton62 0:0a5f554d2a16 143 CAN_Data data;
lcockerton62 1:51477fe4851b 144 data.set_u16(0,min_voltage.voltage); //Min voltage
lcockerton62 1:51477fe4851b 145 data.set_u16(1,max_voltage.voltage); //Max voltage
lcockerton62 1:51477fe4851b 146 data.set_u8(4,min_voltage.CMU_number); //CMU number of lowest cell
lcockerton62 1:51477fe4851b 147 data.set_u8(5,min_voltage.cell_number); //Cell number in CMU with lowest voltage
maxv008 31:888b2602aab2 148 data.set_u8(6,max_voltage.CMU_number); //CMU number of maxiumum cell
maxv008 31:888b2602aab2 149 data.set_u8(7,max_voltage.cell_number); //Cell number in CMU with highest voltage
lcockerton62 1:51477fe4851b 150
lcockerton62 1:51477fe4851b 151 for(int i=0; i<8; i++) {
lcockerton62 0:0a5f554d2a16 152 msg.data[i] = data.get_u8(i);
lcockerton62 0:0a5f554d2a16 153 }
lcockerton62 1:51477fe4851b 154
lcockerton62 0:0a5f554d2a16 155 return msg;
lcockerton62 0:0a5f554d2a16 156 }
lcockerton62 0:0a5f554d2a16 157
maxv008 23:a1af4439c1fc 158 void decodeCellVoltageMAXMIN(CANMessage msg, pack_voltage_extremes &min, pack_voltage_extremes &max)
maxv008 23:a1af4439c1fc 159 {
maxv008 23:a1af4439c1fc 160 CAN_Data decode;
maxv008 23:a1af4439c1fc 161 decode.importCANData(msg);
maxv008 23:a1af4439c1fc 162 min.voltage = decode.get_u16(0);
maxv008 23:a1af4439c1fc 163 max.voltage = decode.get_u16(1);
maxv008 23:a1af4439c1fc 164 min.CMU_number = decode.get_u8(4);
maxv008 23:a1af4439c1fc 165 min.cell_number = decode.get_u8(5);
maxv008 23:a1af4439c1fc 166 max.CMU_number = decode.get_u8(6);
maxv008 23:a1af4439c1fc 167 max.cell_number = decode.get_u8(7);
maxv008 23:a1af4439c1fc 168 }
maxv008 23:a1af4439c1fc 169
maxv008 23:a1af4439c1fc 170 //Since each CAN message can only support 1 ID, need to send 2 using this function
maxv008 23:a1af4439c1fc 171 //Use bool isMin to say if its a minimum or maximum
maxv008 23:a1af4439c1fc 172 CANMessage createCellTemperatureMAXMIN(pack_temperature_extremes ex_temperature, bool isMin)
lcockerton62 0:0a5f554d2a16 173 {
lcockerton62 0:0a5f554d2a16 174 CANMessage msg;
lcockerton62 0:0a5f554d2a16 175 msg.len = 8;
maxv008 23:a1af4439c1fc 176 msg.id = BMS_BASE_ID + (isMin ? MIN_TEMPERATURE : MAX_TEMPERATURE) ;
maxv008 23:a1af4439c1fc 177 //TODO, CHANGE CMU NUMBER TO ROMID
lcockerton62 0:0a5f554d2a16 178 CAN_Data data;
maxv008 23:a1af4439c1fc 179 data.set_u16(3,ex_temperature.temperature); //Extreme temperature
maxv008 23:a1af4439c1fc 180
maxv008 23:a1af4439c1fc 181 for(int i = 1; i <= 6; i++) //ID portion of ROM array
maxv008 23:a1af4439c1fc 182 {
maxv008 23:a1af4439c1fc 183 data.set_u8(i - 1, ex_temperature.ROMID[i]);
maxv008 23:a1af4439c1fc 184 }
lcockerton62 1:51477fe4851b 185
lcockerton62 1:51477fe4851b 186 for(int i=0; i<8; i++) {
lcockerton62 0:0a5f554d2a16 187 msg.data[i] = data.get_u8(i);
lcockerton62 0:0a5f554d2a16 188 }
lcockerton62 1:51477fe4851b 189
lcockerton62 0:0a5f554d2a16 190 return msg;
lcockerton62 0:0a5f554d2a16 191 }
lcockerton62 0:0a5f554d2a16 192
maxv008 23:a1af4439c1fc 193 //It is up to function caller to decide by msg.ID if it is minimum or not
maxv008 23:a1af4439c1fc 194 pack_temperature_extremes decodeCellTemperatureMAXMIN(CANMessage msg)
maxv008 23:a1af4439c1fc 195 {
maxv008 23:a1af4439c1fc 196 pack_temperature_extremes result;
maxv008 23:a1af4439c1fc 197 CAN_Data decode;
maxv008 23:a1af4439c1fc 198 unsigned long fullID = 0;
maxv008 23:a1af4439c1fc 199
maxv008 23:a1af4439c1fc 200 decode.importCANData(msg);
maxv008 53:4277cdcff69b 201 result.temperature = ((float) decode.get_16(3))/100;
maxv008 23:a1af4439c1fc 202 for(int i = 1; i <=6; i++)
maxv008 23:a1af4439c1fc 203 {
maxv008 23:a1af4439c1fc 204 result.ROMID[i] = decode.get_u8(i-1);
maxv008 23:a1af4439c1fc 205 fullID += (result.ROMID[i] << (8 * (i-1))); //Bit order not particularly important
maxv008 23:a1af4439c1fc 206 }
maxv008 23:a1af4439c1fc 207 result.ID = fullID;
maxv008 23:a1af4439c1fc 208 return result;
maxv008 23:a1af4439c1fc 209 }
maxv008 23:a1af4439c1fc 210
maxv008 31:888b2602aab2 211 CANMessage createBatteryVI(uint32_t batteryVoltage, float batteryCurrent)
lcockerton62 0:0a5f554d2a16 212 {
lcockerton62 0:0a5f554d2a16 213 CANMessage msg;
lcockerton62 0:0a5f554d2a16 214 msg.len = 8;
lcockerton62 1:51477fe4851b 215 msg.id = BMS_BASE_ID + BATTERY_VI_ID;
lcockerton62 1:51477fe4851b 216
lcockerton62 0:0a5f554d2a16 217 CAN_Data data;
lcockerton62 3:527790e4965a 218 data.setLower_uLong(batteryVoltage);
maxv008 31:888b2602aab2 219 data.setUpperFloat(batteryCurrent);
lcockerton62 1:51477fe4851b 220
lcockerton62 1:51477fe4851b 221 for(int i=0; i<8; i++) {
lcockerton62 0:0a5f554d2a16 222 msg.data[i] = data.get_u8(i);
lcockerton62 0:0a5f554d2a16 223 }
lcockerton62 0:0a5f554d2a16 224 return msg;
lcockerton62 0:0a5f554d2a16 225 }
lcockerton62 0:0a5f554d2a16 226
DasSidG 66:c884fba9eaea 227 CANMessage createBatteryPower(uint32_t batteryVoltage, float batteryCurrent)
DasSidG 66:c884fba9eaea 228 {
DasSidG 66:c884fba9eaea 229 CANMessage msg;
DasSidG 66:c884fba9eaea 230 msg.len = 8;
DasSidG 66:c884fba9eaea 231 msg.id = BMS_BASE_ID + BATTERY_POWER_ID;
DasSidG 66:c884fba9eaea 232
DasSidG 66:c884fba9eaea 233 CAN_Data data;
DasSidG 66:c884fba9eaea 234 data.setLowerFloat(batteryVoltage*batteryCurrent);
DasSidG 66:c884fba9eaea 235
DasSidG 66:c884fba9eaea 236 for(int i=0; i<8; i++) {
DasSidG 66:c884fba9eaea 237 msg.data[i] = data.get_u8(i);
DasSidG 66:c884fba9eaea 238 }
DasSidG 66:c884fba9eaea 239 return msg;
DasSidG 66:c884fba9eaea 240 }
DasSidG 66:c884fba9eaea 241
maxv008 31:888b2602aab2 242 uint32_t decodeBatteryVoltage(CANMessage msg)
maxv008 31:888b2602aab2 243 {
maxv008 31:888b2602aab2 244 uint32_t result = 0;
maxv008 31:888b2602aab2 245 CAN_Data decode;
maxv008 31:888b2602aab2 246 decode.importCANData(msg);
maxv008 31:888b2602aab2 247 result = decode.getLower_uLong();
maxv008 31:888b2602aab2 248 return result;
maxv008 31:888b2602aab2 249 }
maxv008 31:888b2602aab2 250
maxv008 31:888b2602aab2 251 float decodeBatteryCurrent(CANMessage msg)
maxv008 31:888b2602aab2 252 {
maxv008 48:5c3f42c44036 253 float result = 0;
maxv008 31:888b2602aab2 254 CAN_Data decode;
maxv008 31:888b2602aab2 255 decode.importCANData(msg);
maxv008 31:888b2602aab2 256 result = decode.getUpperFloat();
maxv008 31:888b2602aab2 257 return result;
maxv008 45:c288d7cbdb4a 258 }
maxv008 45:c288d7cbdb4a 259
maxv008 45:c288d7cbdb4a 260 CANMessage createIVTACurrent(int32_t current)
maxv008 45:c288d7cbdb4a 261 {
maxv008 45:c288d7cbdb4a 262 CANMessage msg;
maxv008 45:c288d7cbdb4a 263 msg.len = 8;
maxv008 45:c288d7cbdb4a 264 msg.id = BMS_BASE_ID + IVTA_ID;
maxv008 45:c288d7cbdb4a 265
maxv008 45:c288d7cbdb4a 266 CAN_Data data;
maxv008 45:c288d7cbdb4a 267 data.setLower_Long(current);
maxv008 45:c288d7cbdb4a 268 data.setHigher_Long(0);
maxv008 45:c288d7cbdb4a 269
maxv008 45:c288d7cbdb4a 270 for(int i=0; i<8; i++) {
maxv008 45:c288d7cbdb4a 271 msg.data[i] = data.get_u8(i);
maxv008 45:c288d7cbdb4a 272 }
maxv008 45:c288d7cbdb4a 273 return msg;
maxv008 31:888b2602aab2 274 }
maxv008 31:888b2602aab2 275
maxv008 45:c288d7cbdb4a 276 int32_t decodeIVTACurrent(CANMessage msg)
maxv008 45:c288d7cbdb4a 277 {
maxv008 45:c288d7cbdb4a 278 int32_t result = 0;
maxv008 45:c288d7cbdb4a 279 CAN_Data decode;
maxv008 45:c288d7cbdb4a 280 decode.importCANData(msg);
maxv008 45:c288d7cbdb4a 281 result = decode.getLower_Long();
maxv008 45:c288d7cbdb4a 282 return result;
maxv008 45:c288d7cbdb4a 283 }
maxv008 45:c288d7cbdb4a 284
DasSidG 66:c884fba9eaea 285 CANMessage createBatteryPackStatus(uint32_t status)
lcockerton62 0:0a5f554d2a16 286 {
lcockerton62 1:51477fe4851b 287 CANMessage msg;
lcockerton62 1:51477fe4851b 288 msg.len = 8;
lcockerton62 1:51477fe4851b 289 msg.id = BMS_BASE_ID + BATTERY_STATUS_ID;
lcockerton62 1:51477fe4851b 290
lcockerton62 1:51477fe4851b 291 CAN_Data data;
lcockerton62 1:51477fe4851b 292 data.setLower_uLong(status); //@TODO see the data sheet for this
lcockerton62 1:51477fe4851b 293 data.set_u8(4,0x00);//Hardware version random data @TODO check this
lcockerton62 1:51477fe4851b 294 data.set_u8(5,0x00);//Model ID @TODO check this
lcockerton62 1:51477fe4851b 295 data.set_u16(3,0x00); // Unused
lcockerton62 1:51477fe4851b 296
lcockerton62 1:51477fe4851b 297 for(int i=0; i<8; i++) {
lcockerton62 1:51477fe4851b 298 msg.data[i] = data.get_u8(i);
lcockerton62 1:51477fe4851b 299 }
lcockerton62 1:51477fe4851b 300 return msg;
lcockerton62 0:0a5f554d2a16 301 }
lcockerton62 0:0a5f554d2a16 302
DasSidG 66:c884fba9eaea 303 uint32_t decodeBatteryPackStatus(CANMessage msg)
maxv008 23:a1af4439c1fc 304 {
maxv008 23:a1af4439c1fc 305 CAN_Data decode;
maxv008 23:a1af4439c1fc 306 decode.importCANData(msg);
maxv008 23:a1af4439c1fc 307 return decode.getLower_uLong();
maxv008 23:a1af4439c1fc 308 }
maxv008 23:a1af4439c1fc 309
maxv008 31:888b2602aab2 310 //Values here don't matter, added just in case.
maxv008 31:888b2602aab2 311 CANMessage createBMSHeartbeat(uint32_t val1, uint32_t val2)
maxv008 31:888b2602aab2 312 {
maxv008 31:888b2602aab2 313 CANMessage msg;
maxv008 31:888b2602aab2 314 msg.len = 8;
maxv008 31:888b2602aab2 315 msg.id = BMS_BASE_ID;
maxv008 31:888b2602aab2 316
maxv008 31:888b2602aab2 317 CAN_Data data;
maxv008 31:888b2602aab2 318 data.setLower_uLong(val1);
maxv008 31:888b2602aab2 319 data.setHigher_uLong(val2);
maxv008 31:888b2602aab2 320
maxv008 31:888b2602aab2 321 for(int i=0; i<8; i++) {
maxv008 31:888b2602aab2 322 msg.data[i] = data.get_u8(i);
maxv008 31:888b2602aab2 323 }
maxv008 31:888b2602aab2 324 return msg;
maxv008 31:888b2602aab2 325 }
maxv008 31:888b2602aab2 326
maxv008 48:5c3f42c44036 327 CANMessage createEEPROMReset(float init_SOC, float init_SOC_Percent) //TODO: Ensure ID doesn't conflict more carefully (It should be fine)
maxv008 48:5c3f42c44036 328 {
maxv008 48:5c3f42c44036 329 CANMessage msg;
maxv008 48:5c3f42c44036 330 msg.len = 8;
maxv008 48:5c3f42c44036 331 msg.id = BMS_BASE_ID + EEPROM_RESET_ID;
maxv008 48:5c3f42c44036 332
maxv008 48:5c3f42c44036 333 CAN_Data data;
maxv008 48:5c3f42c44036 334 data.setLowerFloat(init_SOC);
maxv008 48:5c3f42c44036 335 data.setUpperFloat(init_SOC_Percent);
maxv008 48:5c3f42c44036 336
maxv008 48:5c3f42c44036 337 for(int i=0; i<8; i++) {
maxv008 48:5c3f42c44036 338 msg.data[i] = data.get_u8(i);
maxv008 48:5c3f42c44036 339 }
maxv008 48:5c3f42c44036 340 return msg;
maxv008 48:5c3f42c44036 341 }
maxv008 48:5c3f42c44036 342
maxv008 48:5c3f42c44036 343 float decodeEEPROMSOC(CANMessage msg)
maxv008 48:5c3f42c44036 344 {
maxv008 48:5c3f42c44036 345 float result = 0;
maxv008 48:5c3f42c44036 346 CAN_Data decode;
maxv008 48:5c3f42c44036 347 decode.importCANData(msg);
maxv008 48:5c3f42c44036 348 result = decode.getLowerFloat();
maxv008 48:5c3f42c44036 349 return result;
maxv008 48:5c3f42c44036 350 }
maxv008 48:5c3f42c44036 351
maxv008 48:5c3f42c44036 352 float decodeEEPROMSOCPercentage(CANMessage msg)
maxv008 48:5c3f42c44036 353 {
maxv008 48:5c3f42c44036 354 float result = 0;
maxv008 48:5c3f42c44036 355 CAN_Data decode;
maxv008 48:5c3f42c44036 356 decode.importCANData(msg);
maxv008 48:5c3f42c44036 357 result = decode.getUpperFloat();
maxv008 48:5c3f42c44036 358 return result;
maxv008 48:5c3f42c44036 359 }
maxv008 48:5c3f42c44036 360
lcockerton62 0:0a5f554d2a16 361 void convertFloatFloat(float lower, float upper, CANMessage& msg, bool littleEndian)
lcockerton62 0:0a5f554d2a16 362 {
lcockerton62 0:0a5f554d2a16 363 // Code taken from driver_controls
lcockerton62 0:0a5f554d2a16 364 //two converters for lower and higher float
lcockerton62 0:0a5f554d2a16 365 float2byte convL;
lcockerton62 0:0a5f554d2a16 366 float2byte convH;
lcockerton62 0:0a5f554d2a16 367 convL.f = lower;
lcockerton62 0:0a5f554d2a16 368 convH.f = upper;
lcockerton62 0:0a5f554d2a16 369 if(littleEndian) {
lcockerton62 0:0a5f554d2a16 370 for(int i=0; i<4; i++) {
lcockerton62 0:0a5f554d2a16 371 msg.data[i] = convL.b[i];
lcockerton62 0:0a5f554d2a16 372 //offset for upper float
lcockerton62 0:0a5f554d2a16 373 msg.data[i+4]=convH.b[i];
lcockerton62 0:0a5f554d2a16 374 }
lcockerton62 0:0a5f554d2a16 375 } else {
lcockerton62 0:0a5f554d2a16 376 for(int i=0; i<4; i++) {
lcockerton62 0:0a5f554d2a16 377 /*
lcockerton62 0:0a5f554d2a16 378 * Subtract because output data is Big Endian
lcockerton62 0:0a5f554d2a16 379 * i.e. convL/H is LSB --> MSB
lcockerton62 0:0a5f554d2a16 380 * output is MSB --> LSB
lcockerton62 0:0a5f554d2a16 381 */
lcockerton62 1:51477fe4851b 382
lcockerton62 0:0a5f554d2a16 383 msg.data[4-i] = convL.b[i];
lcockerton62 0:0a5f554d2a16 384 msg.data[7-i] = convH.b[i];
lcockerton62 0:0a5f554d2a16 385 }
lcockerton62 0:0a5f554d2a16 386 }
lcockerton62 0:0a5f554d2a16 387 }
lcockerton62 0:0a5f554d2a16 388