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:
maxv008
Date:
Wed Jun 28 16:56:33 2017 +0000
Revision:
13:7b42af989cd1
Parent:
9:82ba050a7e13
Child:
14:e0e88a009f4c
Added function to create CAN Messages based on temperature readings from individual probes, and a function to parse msg back into readings. Implemented this into the transmit data function.

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"
lcockerton62 0:0a5f554d2a16 5
lcockerton62 1:51477fe4851b 6
lcockerton62 0:0a5f554d2a16 7 using namespace CAN_IDs;
lcockerton62 0:0a5f554d2a16 8
maxv008 13:7b42af989cd1 9 /**
maxv008 13:7b42af989cd1 10 * This function is rewritten to give readings for individual probes rather than
maxv008 13:7b42af989cd1 11 * for specific CMU. As a consequence, 0x800 onwards is being used for these probes,
maxv008 13:7b42af989cd1 12 * as everything above about 0x700 is unused by the Tritium standard. Additionally,
maxv008 13:7b42af989cd1 13 * instead of using uint16, floats will be used since only one temperature reading
maxv008 13:7b42af989cd1 14 * is needed per message, allowing 32 bits per reading, and float is the natural
maxv008 13:7b42af989cd1 15 * type obtained by the sensor.
maxv008 13:7b42af989cd1 16 */
maxv008 13:7b42af989cd1 17 CANMessage createTemperatureTelemetry(uint8_t offset, uint32_t ProbeID, 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 13:7b42af989cd1 24 data.setLower_uLong(ProbeID);
maxv008 13:7b42af989cd1 25 data.setUpperFloat(Temperature);
lcockerton62 0:0a5f554d2a16 26
lcockerton62 0:0a5f554d2a16 27 for (int i = 0; i<8; i++) {
lcockerton62 0:0a5f554d2a16 28 msg.data[i] = data.get_u8(i);
lcockerton62 0:0a5f554d2a16 29 }
lcockerton62 0:0a5f554d2a16 30
lcockerton62 0:0a5f554d2a16 31 return msg;
lcockerton62 0:0a5f554d2a16 32 }
maxv008 13:7b42af989cd1 33 /**
maxv008 13:7b42af989cd1 34 * Takes a CANMessage with precondition that it stores temperature of an individual
maxv008 13:7b42af989cd1 35 * probe and returns an individual_temperature object containing ID and reading.
maxv008 13:7b42af989cd1 36 */
maxv008 13:7b42af989cd1 37 individual_temperature decodeTemperatureTelemetry(CANMessage msg)
maxv008 13:7b42af989cd1 38 {
maxv008 13:7b42af989cd1 39 individual_temperature probe_reading;
maxv008 13:7b42af989cd1 40 CAN_Data decode;
maxv008 13:7b42af989cd1 41
maxv008 13:7b42af989cd1 42 decode.importCANData(msg);
maxv008 13:7b42af989cd1 43 probe_reading.measurement = decode.getUpperFloat();
maxv008 13:7b42af989cd1 44 probe_reading.ID = decode.getLower_uLong();
maxv008 13:7b42af989cd1 45
maxv008 13:7b42af989cd1 46 return probe_reading;
maxv008 13:7b42af989cd1 47 }
lcockerton62 0:0a5f554d2a16 48
msharma97 9:82ba050a7e13 49 CANMessage createVoltageTelemetry(int offset_id, uint16_t voltage[])
lcockerton62 0:0a5f554d2a16 50 {
lcockerton62 0:0a5f554d2a16 51 CANMessage msg;
lcockerton62 0:0a5f554d2a16 52 msg.len = 8;
msharma97 9:82ba050a7e13 53 msg.id = BMS_BASE_ID + offset_id; // for voltage 0x601 - 0x6EF @TODO
lcockerton62 0:0a5f554d2a16 54 CAN_Data data;
lcockerton62 0:0a5f554d2a16 55
lcockerton62 0:0a5f554d2a16 56 data.set_u16(0, voltage[0]);
lcockerton62 0:0a5f554d2a16 57 data.set_u16(1, voltage[1]);
lcockerton62 0:0a5f554d2a16 58 data.set_u16(2, voltage[2]);
lcockerton62 0:0a5f554d2a16 59 data.set_u16(3, voltage[3]);
lcockerton62 0:0a5f554d2a16 60
lcockerton62 0:0a5f554d2a16 61 for (int i = 0; i<8; i++) {
lcockerton62 0:0a5f554d2a16 62 msg.data[i] = data.get_u8(i);
lcockerton62 0:0a5f554d2a16 63 }
lcockerton62 0:0a5f554d2a16 64
lcockerton62 0:0a5f554d2a16 65 return msg;
lcockerton62 0:0a5f554d2a16 66 }
lcockerton62 0:0a5f554d2a16 67
lcockerton62 0:0a5f554d2a16 68 CANMessage createPackSOC(float SOC, float percentageCharge)
lcockerton62 0:0a5f554d2a16 69 {
lcockerton62 0:0a5f554d2a16 70 CANMessage msg;
lcockerton62 0:0a5f554d2a16 71 msg.len = 8;
lcockerton62 0:0a5f554d2a16 72 msg.id = BMS_BASE_ID + BATTERY_SOC_ID;
lcockerton62 0:0a5f554d2a16 73 CAN_Data data;
lcockerton62 0:0a5f554d2a16 74 data.setLowerFloat(SOC);
lcockerton62 0:0a5f554d2a16 75 data.setUpperFloat(percentageCharge);
lcockerton62 1:51477fe4851b 76 for(int i=0; i<8; i++) {
lcockerton62 0:0a5f554d2a16 77 msg.data[i] = data.get_u8(i);
lcockerton62 0:0a5f554d2a16 78 }
lcockerton62 1:51477fe4851b 79
lcockerton62 0:0a5f554d2a16 80 return msg;
lcockerton62 0:0a5f554d2a16 81 }
lcockerton62 0:0a5f554d2a16 82
lcockerton62 0:0a5f554d2a16 83 CANMessage createPackBalanceSOC(float SOC, float percentageCharge)
lcockerton62 0:0a5f554d2a16 84 {
lcockerton62 0:0a5f554d2a16 85 // @TODO - check is this being used?? section 5.4 trituim BMU CAN data sheet
lcockerton62 0:0a5f554d2a16 86 CANMessage msg;
lcockerton62 0:0a5f554d2a16 87 msg.len = 8;
lcockerton62 1:51477fe4851b 88 msg.id = BMS_BASE_ID + BATTERY_SOC_BASE_ID;
lcockerton62 1:51477fe4851b 89
lcockerton62 0:0a5f554d2a16 90 CAN_Data data;
lcockerton62 0:0a5f554d2a16 91 data.setLowerFloat(SOC);
lcockerton62 0:0a5f554d2a16 92 data.setUpperFloat(percentageCharge);
lcockerton62 1:51477fe4851b 93 for(int i=0; i<8; i++) {
lcockerton62 0:0a5f554d2a16 94 msg.data[i] = data.get_u8(i);
lcockerton62 0:0a5f554d2a16 95 }
lcockerton62 1:51477fe4851b 96
lcockerton62 0:0a5f554d2a16 97 return msg;
lcockerton62 0:0a5f554d2a16 98 }
lcockerton62 0:0a5f554d2a16 99
lcockerton62 1:51477fe4851b 100 CANMessage createCellVoltageMAXMIN(pack_voltage_extremes max_voltage, pack_voltage_extremes min_voltage)
lcockerton62 0:0a5f554d2a16 101 {
lcockerton62 0:0a5f554d2a16 102 CANMessage msg;
lcockerton62 0:0a5f554d2a16 103 msg.len = 8;
lcockerton62 1:51477fe4851b 104 msg.id = BMS_BASE_ID + MAX_MIN_VOLTAGE;
lcockerton62 1:51477fe4851b 105
lcockerton62 0:0a5f554d2a16 106 CAN_Data data;
lcockerton62 1:51477fe4851b 107 data.set_u16(0,min_voltage.voltage); //Min voltage
lcockerton62 1:51477fe4851b 108 data.set_u16(1,max_voltage.voltage); //Max voltage
lcockerton62 1:51477fe4851b 109 data.set_u8(4,min_voltage.CMU_number); //CMU number of lowest cell
lcockerton62 1:51477fe4851b 110 data.set_u8(5,min_voltage.cell_number); //Cell number in CMU with lowest voltage
lcockerton62 1:51477fe4851b 111 data.set_u8(6,min_voltage.CMU_number); //CMU number of maxiumum cell
lcockerton62 1:51477fe4851b 112 data.set_u8(7,min_voltage.cell_number); //Cell number in CMU with highest voltage
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 1:51477fe4851b 121 CANMessage createCellTemperatureMAXMIN(pack_temperature_extremes min_temperature, pack_temperature_extremes max_temperature)
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 + MAX_MIN_TEMPERATURE;
lcockerton62 1:51477fe4851b 126
lcockerton62 0:0a5f554d2a16 127 CAN_Data data;
lcockerton62 1:51477fe4851b 128 data.set_u16(0,min_temperature.temperature); //Min temperature
lcockerton62 1:51477fe4851b 129 data.set_u16(1,max_temperature.temperature); //Max temperature
lcockerton62 1:51477fe4851b 130 data.set_u8(4,min_temperature.CMU_number); //CMU number of lowest temperature cell
lcockerton62 0:0a5f554d2a16 131 data.set_u8(5,BLANK_DATA); //Dummy data
lcockerton62 1:51477fe4851b 132 data.set_u8(6,max_temperature.CMU_number); //CMU number of maxiumum temperature cell
lcockerton62 1:51477fe4851b 133 data.set_u8(7,BLANK_DATA); //Dummy data
lcockerton62 1:51477fe4851b 134
lcockerton62 1:51477fe4851b 135 for(int i=0; i<8; i++) {
lcockerton62 0:0a5f554d2a16 136 msg.data[i] = data.get_u8(i);
lcockerton62 0:0a5f554d2a16 137 }
lcockerton62 1:51477fe4851b 138
lcockerton62 0:0a5f554d2a16 139 return msg;
lcockerton62 0:0a5f554d2a16 140 }
lcockerton62 0:0a5f554d2a16 141
lcockerton62 0:0a5f554d2a16 142 CANMessage createBatteryVI(uint32_t batteryVoltage,uint32_t batteryCurrent)
lcockerton62 0:0a5f554d2a16 143 {
lcockerton62 0:0a5f554d2a16 144 CANMessage msg;
lcockerton62 0:0a5f554d2a16 145 msg.len = 8;
lcockerton62 1:51477fe4851b 146 msg.id = BMS_BASE_ID + BATTERY_VI_ID;
lcockerton62 1:51477fe4851b 147
lcockerton62 0:0a5f554d2a16 148 CAN_Data data;
lcockerton62 3:527790e4965a 149 data.setLower_uLong(batteryVoltage);
lcockerton62 3:527790e4965a 150 data.setHigher_uLong(batteryCurrent);
lcockerton62 1:51477fe4851b 151
lcockerton62 1:51477fe4851b 152 for(int i=0; i<8; i++) {
lcockerton62 0:0a5f554d2a16 153 msg.data[i] = data.get_u8(i);
lcockerton62 0:0a5f554d2a16 154 }
lcockerton62 0:0a5f554d2a16 155 return msg;
lcockerton62 0:0a5f554d2a16 156 }
lcockerton62 0:0a5f554d2a16 157
lcockerton62 0:0a5f554d2a16 158 CANMessage createBatteryPackStatus(uint16_t voltageThreshold[], uint8_t statusFlag,uint8_t BMS_CMU_Count,uint16_t BMS_Firmware_Build)
lcockerton62 0:0a5f554d2a16 159 {
lcockerton62 1:51477fe4851b 160 CANMessage msg;
lcockerton62 1:51477fe4851b 161 msg.len = 8;
lcockerton62 1:51477fe4851b 162 msg.id = BMS_BASE_ID + BATTERY_PACK_STATUS_ID;
lcockerton62 1:51477fe4851b 163
lcockerton62 1:51477fe4851b 164 CAN_Data data;
lcockerton62 1:51477fe4851b 165 data.set_u16(0,voltageThreshold[0]);
lcockerton62 1:51477fe4851b 166 data.set_u16(1,voltageThreshold[1]);
lcockerton62 1:51477fe4851b 167 data.set_16(3,BMS_Firmware_Build);
lcockerton62 1:51477fe4851b 168 data.set_u8(4,statusFlag);
lcockerton62 1:51477fe4851b 169 data.set_u8(5,BMS_CMU_Count);
lcockerton62 1:51477fe4851b 170
lcockerton62 1:51477fe4851b 171 for(int i=0; i<8; i++) {
lcockerton62 1:51477fe4851b 172 msg.data[i] = data.get_u8(i);
lcockerton62 1:51477fe4851b 173 }
lcockerton62 1:51477fe4851b 174 return msg;
lcockerton62 0:0a5f554d2a16 175 }
lcockerton62 0:0a5f554d2a16 176
lcockerton62 0:0a5f554d2a16 177 CANMessage createExtendedBatteryPackStatus(uint32_t status)
lcockerton62 0:0a5f554d2a16 178 {
lcockerton62 1:51477fe4851b 179 CANMessage msg;
lcockerton62 1:51477fe4851b 180 msg.len = 8;
lcockerton62 1:51477fe4851b 181 msg.id = BMS_BASE_ID + BATTERY_STATUS_ID;
lcockerton62 1:51477fe4851b 182
lcockerton62 1:51477fe4851b 183 CAN_Data data;
lcockerton62 1:51477fe4851b 184 data.setLower_uLong(status); //@TODO see the data sheet for this
lcockerton62 1:51477fe4851b 185 data.set_u8(4,0x00);//Hardware version random data @TODO check this
lcockerton62 1:51477fe4851b 186 data.set_u8(5,0x00);//Model ID @TODO check this
lcockerton62 1:51477fe4851b 187 data.set_u16(3,0x00); // Unused
lcockerton62 1:51477fe4851b 188
lcockerton62 1:51477fe4851b 189 for(int i=0; i<8; i++) {
lcockerton62 1:51477fe4851b 190 msg.data[i] = data.get_u8(i);
lcockerton62 1:51477fe4851b 191 }
lcockerton62 1:51477fe4851b 192 return msg;
lcockerton62 0:0a5f554d2a16 193 }
lcockerton62 0:0a5f554d2a16 194
lcockerton62 0:0a5f554d2a16 195 void convertFloatFloat(float lower, float upper, CANMessage& msg, bool littleEndian)
lcockerton62 0:0a5f554d2a16 196 {
lcockerton62 0:0a5f554d2a16 197 // Code taken from driver_controls
lcockerton62 0:0a5f554d2a16 198 //two converters for lower and higher float
lcockerton62 0:0a5f554d2a16 199 float2byte convL;
lcockerton62 0:0a5f554d2a16 200 float2byte convH;
lcockerton62 0:0a5f554d2a16 201 convL.f = lower;
lcockerton62 0:0a5f554d2a16 202 convH.f = upper;
lcockerton62 0:0a5f554d2a16 203 if(littleEndian) {
lcockerton62 0:0a5f554d2a16 204 for(int i=0; i<4; i++) {
lcockerton62 0:0a5f554d2a16 205 msg.data[i] = convL.b[i];
lcockerton62 0:0a5f554d2a16 206 //offset for upper float
lcockerton62 0:0a5f554d2a16 207 msg.data[i+4]=convH.b[i];
lcockerton62 0:0a5f554d2a16 208 }
lcockerton62 0:0a5f554d2a16 209 } else {
lcockerton62 0:0a5f554d2a16 210 for(int i=0; i<4; i++) {
lcockerton62 0:0a5f554d2a16 211 /*
lcockerton62 0:0a5f554d2a16 212 * Subtract because output data is Big Endian
lcockerton62 0:0a5f554d2a16 213 * i.e. convL/H is LSB --> MSB
lcockerton62 0:0a5f554d2a16 214 * output is MSB --> LSB
lcockerton62 0:0a5f554d2a16 215 */
lcockerton62 1:51477fe4851b 216
lcockerton62 0:0a5f554d2a16 217 msg.data[4-i] = convL.b[i];
lcockerton62 0:0a5f554d2a16 218 msg.data[7-i] = convH.b[i];
lcockerton62 0:0a5f554d2a16 219 }
lcockerton62 0:0a5f554d2a16 220 }
lcockerton62 1:51477fe4851b 221
lcockerton62 0:0a5f554d2a16 222 }
lcockerton62 0:0a5f554d2a16 223