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:
Mon Jun 19 12:00:03 2017 +0000
Revision:
10:1079f8e52d65
Parent:
9:82ba050a7e13
Child:
11:cf2db05cfa56
Includes Can test functions and working (at some point anyways) voltage telemetry sending over CAN

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lcockerton62 0:0a5f554d2a16 1 #include "mbed.h"
lcockerton62 0:0a5f554d2a16 2 #include "CANParserBMU.h"
lcockerton62 0:0a5f554d2a16 3 #include "Data_Types_BMU.h"
lcockerton62 0:0a5f554d2a16 4 #include "CAN_Data.h"
lcockerton62 0:0a5f554d2a16 5 #include "CAN_IDs.h"
lcockerton62 1:51477fe4851b 6 #include "EEPROM_I2C.h"
lcockerton62 1:51477fe4851b 7 #include "Temperature.h"
DasSidG 4:9050c5d6925e 8 #include "LTC2943_Read.h"
maxv008 10:1079f8e52d65 9 #include "Cell_Voltage.h"
maxv008 7:d00f4433cea9 10 #include "SPI_I2C_Parser.h"
lcockerton62 0:0a5f554d2a16 11
msharma97 9:82ba050a7e13 12
msharma97 9:82ba050a7e13 13
lcockerton62 0:0a5f554d2a16 14 using namespace CAN_IDs;
lcockerton62 0:0a5f554d2a16 15
lcockerton62 0:0a5f554d2a16 16 // Function definitions
lcockerton62 1:51477fe4851b 17 void transmit_data(BMU_data measurements,uint32_t status);
lcockerton62 1:51477fe4851b 18 void read_temperature_sensors(BMU_data &measurements);
lcockerton62 0:0a5f554d2a16 19 void update_SOC();
lcockerton62 0:0a5f554d2a16 20 void init();
lcockerton62 1:51477fe4851b 21 void write_SOC_EEPROM(BMU_data &measurements,uint16_t start_address);
lcockerton62 1:51477fe4851b 22 uint16_t read_EEPROM_startup(BMU_data &measurements);
lcockerton62 1:51477fe4851b 23 uint32_t check_measurements(BMU_data &measurements);
lcockerton62 1:51477fe4851b 24 void take_measurements(BMU_data &measurements);
maxv008 10:1079f8e52d65 25 void test_read_voltage_CAN();
maxv008 10:1079f8e52d65 26 void test_CAN_send();
maxv008 10:1079f8e52d65 27 void test_CAN_read();
lcockerton62 0:0a5f554d2a16 28
lcockerton62 0:0a5f554d2a16 29 CAN can(CAN_READ_PIN, CAN_WRITE_PIN); //Create a CAN object to handle CAN comms
DasSidG 4:9050c5d6925e 30 uint16_t eeprom_start_address; //the initial address where we store/read SoC values
lcockerton62 0:0a5f554d2a16 31
lcockerton62 1:51477fe4851b 32 Timeout loop_delay;
lcockerton62 1:51477fe4851b 33 bool delay_finished = false;
lcockerton62 2:94716229ecc3 34
lcockerton62 2:94716229ecc3 35 void loop_delay_callback(void)
lcockerton62 2:94716229ecc3 36 {
lcockerton62 1:51477fe4851b 37 delay_finished = true;
lcockerton62 1:51477fe4851b 38 }
lcockerton62 1:51477fe4851b 39
lcockerton62 0:0a5f554d2a16 40 int main()
lcockerton62 0:0a5f554d2a16 41 {
lcockerton62 1:51477fe4851b 42 BMU_data measurements;
lcockerton62 1:51477fe4851b 43 uint16_t current_EEPROM_address;
lcockerton62 1:51477fe4851b 44 uint32_t status;
lcockerton62 0:0a5f554d2a16 45 int c = 0;
maxv008 10:1079f8e52d65 46
maxv008 10:1079f8e52d65 47 /*while(true)
maxv008 10:1079f8e52d65 48 {
maxv008 10:1079f8e52d65 49 wait(0.05);
maxv008 10:1079f8e52d65 50 test_read_voltage_CAN();
maxv008 10:1079f8e52d65 51 }*/
lcockerton62 0:0a5f554d2a16 52 init();
maxv008 10:1079f8e52d65 53
DasSidG 4:9050c5d6925e 54 current_EEPROM_address = read_EEPROM_startup(measurements); // Read from the eeprom at startup to fill in the values of SoC
DasSidG 4:9050c5d6925e 55 ltc2943.accumulatedCharge(measurements.percentage_SOC); // Initialise the LTC2943 with the current state of charge
DasSidG 4:9050c5d6925e 56
lcockerton62 1:51477fe4851b 57 while (true) {
lcockerton62 2:94716229ecc3 58
lcockerton62 1:51477fe4851b 59 // Take measurements from the sensors
lcockerton62 1:51477fe4851b 60 take_measurements(measurements);
lcockerton62 0:0a5f554d2a16 61 // Dont want to read the temperature sensors during each iteration of the loop
lcockerton62 1:51477fe4851b 62 if (c == 0) {
lcockerton62 1:51477fe4851b 63 read_temperature_sensors(measurements);
lcockerton62 1:51477fe4851b 64 } else if(c >= 4) {
lcockerton62 0:0a5f554d2a16 65 c = -1;
lcockerton62 0:0a5f554d2a16 66 }
lcockerton62 0:0a5f554d2a16 67 c++;
lcockerton62 0:0a5f554d2a16 68
lcockerton62 1:51477fe4851b 69 // Check data for errors
lcockerton62 1:51477fe4851b 70 status = check_measurements(measurements);
lcockerton62 1:51477fe4851b 71
lcockerton62 0:0a5f554d2a16 72 // Update the SOC
lcockerton62 0:0a5f554d2a16 73 update_SOC();
lcockerton62 0:0a5f554d2a16 74
lcockerton62 1:51477fe4851b 75 //Store data in the eeprom
lcockerton62 1:51477fe4851b 76 write_SOC_EEPROM(measurements, current_EEPROM_address);
lcockerton62 0:0a5f554d2a16 77
lcockerton62 5:793afeef45dc 78 // CAN bus
lcockerton62 1:51477fe4851b 79 transmit_data(measurements,status);
lcockerton62 0:0a5f554d2a16 80
lcockerton62 0:0a5f554d2a16 81 // Conserve power - enter a low powered mode
lcockerton62 2:94716229ecc3 82 delay_finished = false;
lcockerton62 1:51477fe4851b 83 loop_delay.attach(loop_delay_callback, LOOP_DELAY_S);
lcockerton62 1:51477fe4851b 84 while (!delay_finished) sleep();
maxv008 10:1079f8e52d65 85 }
lcockerton62 0:0a5f554d2a16 86 }
lcockerton62 0:0a5f554d2a16 87
lcockerton62 1:51477fe4851b 88 void transmit_data(BMU_data measurements, uint32_t status)
lcockerton62 0:0a5f554d2a16 89 {
msharma97 9:82ba050a7e13 90 CANMessage msg;
lcockerton62 0:0a5f554d2a16 91 /*
lcockerton62 0:0a5f554d2a16 92 Place all of the collected data onto the CAN bus
lcockerton62 0:0a5f554d2a16 93 */
lcockerton62 5:793afeef45dc 94 // Send cell voltages
msharma97 9:82ba050a7e13 95 //voltages sent in sets of 4 + one cmy data set
msharma97 9:82ba050a7e13 96 int repeating_unit_length = NO_READINGS_PER_CMU /4 + 1;
maxv008 10:1079f8e52d65 97 for(uint16_t i= 0; i < NO_CMUS; i++) {
msharma97 9:82ba050a7e13 98 //input id is offset, data structure is info, voltage, voltage, ......
maxv008 10:1079f8e52d65 99 //This is a slightly modified version of the Tritium BMS datasheet, to add an extra voltage reading set.
maxv008 10:1079f8e52d65 100 msg = createVoltageTelemetry(repeating_unit_length*i+2, measurements.cell_voltages[i].voltages);
msharma97 9:82ba050a7e13 101 can.write(msg);
maxv008 10:1079f8e52d65 102 //CONSIDER WAITS JUST IN CASE
msharma97 9:82ba050a7e13 103 //+4 - 4 cell voltages sent per measurement
maxv008 10:1079f8e52d65 104 msg = createVoltageTelemetry(repeating_unit_length*i+3, measurements.cell_voltages[i].voltages + 4);
msharma97 9:82ba050a7e13 105 can.write(msg);
maxv008 10:1079f8e52d65 106 msg = createVoltageTelemetry(repeating_unit_length*i+4, measurements.cell_voltages[i].voltages + 8);
msharma97 9:82ba050a7e13 107 can.write(msg);
maxv008 10:1079f8e52d65 108 printf("Message id: %d \r\n", msg.id);
lcockerton62 1:51477fe4851b 109 }
lcockerton62 1:51477fe4851b 110
lcockerton62 1:51477fe4851b 111 // Create SOC CAN message
lcockerton62 1:51477fe4851b 112 createPackSOC(measurements.SOC, measurements.percentage_SOC);
lcockerton62 0:0a5f554d2a16 113
lcockerton62 1:51477fe4851b 114 // Min/max cell voltages
lcockerton62 1:51477fe4851b 115 createCellVoltageMAXMIN(measurements.max_cell_voltage, measurements.min_cell_voltage);
lcockerton62 2:94716229ecc3 116
lcockerton62 1:51477fe4851b 117 // Min/Max cell temperature
lcockerton62 1:51477fe4851b 118 createCellTemperatureMAXMIN(measurements.min_cell_temp,measurements.max_cell_temp);
lcockerton62 2:94716229ecc3 119
lcockerton62 2:94716229ecc3 120 // Battery voltage and current
lcockerton62 5:793afeef45dc 121 // @TODO add the voltage
lcockerton62 1:51477fe4851b 122 createBatteryVI(measurements.battery_voltage,measurements.battery_current);
lcockerton62 2:94716229ecc3 123
lcockerton62 1:51477fe4851b 124 //Extended battery pack status
lcockerton62 1:51477fe4851b 125 createExtendedBatteryPackStatus(status);
lcockerton62 2:94716229ecc3 126
lcockerton62 0:0a5f554d2a16 127 }
lcockerton62 0:0a5f554d2a16 128
maxv008 10:1079f8e52d65 129
lcockerton62 1:51477fe4851b 130 uint16_t read_EEPROM_startup(BMU_data &measurements)
lcockerton62 0:0a5f554d2a16 131 {
lcockerton62 1:51477fe4851b 132 /* The first page of the EEPROM, specifically the first 2 addresses store a
lcockerton62 1:51477fe4851b 133 pointer of the first memory location of measurement data. The EEPROM only has a finite number of
lcockerton62 1:51477fe4851b 134 read/write cycles which is why we aren't writing to the same location throughout
lcockerton62 1:51477fe4851b 135 */
lcockerton62 5:793afeef45dc 136
lcockerton62 1:51477fe4851b 137 uint16_t start_address;
lcockerton62 1:51477fe4851b 138 char start_address_array[2];
lcockerton62 1:51477fe4851b 139 char SOC_out[8]; // 4 bytes for the 2 floats one is SOC and the other % charge
lcockerton62 1:51477fe4851b 140 float *fp1,*fp2; // temporary storage for float conversion
lcockerton62 1:51477fe4851b 141
lcockerton62 1:51477fe4851b 142 // Get a pointer to the start address for the data stored in the eeprom
lcockerton62 1:51477fe4851b 143 i2c_page_read(0x0000,2,start_address_array);
lcockerton62 1:51477fe4851b 144
lcockerton62 1:51477fe4851b 145 // Read the data from this address
lcockerton62 1:51477fe4851b 146 start_address = (start_address_array[1]<< 8) | start_address_array[0]; // mbed little endian follow this convention
lcockerton62 1:51477fe4851b 147 i2c_page_read(start_address, 8,SOC_out);
lcockerton62 0:0a5f554d2a16 148
lcockerton62 1:51477fe4851b 149 // Convert the SOC_out values back into floats
lcockerton62 1:51477fe4851b 150 fp1 = (float*)(&SOC_out[0]);
lcockerton62 1:51477fe4851b 151 fp2 = (float*)(&SOC_out[4]);
lcockerton62 1:51477fe4851b 152 measurements.SOC = *fp1;
lcockerton62 1:51477fe4851b 153 measurements.percentage_SOC = *fp2;
lcockerton62 1:51477fe4851b 154
lcockerton62 1:51477fe4851b 155 // Select the next address to write to
lcockerton62 1:51477fe4851b 156 start_address += 0x0040;
lcockerton62 1:51477fe4851b 157 if(start_address > MAX_WRITE_ADDRESS) {
lcockerton62 5:793afeef45dc 158 start_address = START_WRITE_ADDRESS; // Loop to the start of the eeprom
lcockerton62 1:51477fe4851b 159 }
lcockerton62 1:51477fe4851b 160
lcockerton62 5:793afeef45dc 161 /*@TODO need to include a CRC check for the address pointer for the scenario
lcockerton62 5:793afeef45dc 162 when power is removed and we are writing to the eeprom*/
lcockerton62 1:51477fe4851b 163 // write the new address to location 0x0000
lcockerton62 1:51477fe4851b 164 start_address_array[0] = start_address | 0x00FF;
lcockerton62 1:51477fe4851b 165 start_address_array[1] = start_address >> 8;
lcockerton62 1:51477fe4851b 166 i2c_page_write(0x0000, 2, start_address_array);
lcockerton62 1:51477fe4851b 167
lcockerton62 1:51477fe4851b 168 return start_address;
lcockerton62 0:0a5f554d2a16 169 }
lcockerton62 0:0a5f554d2a16 170
lcockerton62 1:51477fe4851b 171 void write_SOC_EEPROM(BMU_data &measurements,uint16_t start_address)
lcockerton62 0:0a5f554d2a16 172 {
lcockerton62 1:51477fe4851b 173 char data_out[8];
lcockerton62 1:51477fe4851b 174 float *fp1,*fp2;
lcockerton62 1:51477fe4851b 175
lcockerton62 1:51477fe4851b 176 fp1 = (float*)(&measurements.SOC);
lcockerton62 1:51477fe4851b 177 fp2 = (float*)(&measurements.percentage_SOC);
lcockerton62 0:0a5f554d2a16 178
lcockerton62 1:51477fe4851b 179 for(int i = 0; i < 4; i++ ) {
lcockerton62 1:51477fe4851b 180 data_out[i] = *fp1;
lcockerton62 1:51477fe4851b 181 fp1++;
lcockerton62 1:51477fe4851b 182 }
lcockerton62 1:51477fe4851b 183 for(int j = 4; j < 7; j++ ) {
lcockerton62 1:51477fe4851b 184 data_out[j] = *fp2;
lcockerton62 1:51477fe4851b 185 fp2++;
lcockerton62 1:51477fe4851b 186 }
lcockerton62 1:51477fe4851b 187 i2c_page_write(start_address, 8,data_out);
lcockerton62 0:0a5f554d2a16 188 }
lcockerton62 0:0a5f554d2a16 189
lcockerton62 1:51477fe4851b 190 void read_temperature_sensors(BMU_data &measurements)
lcockerton62 0:0a5f554d2a16 191 {
lcockerton62 1:51477fe4851b 192 float min_temperature;
lcockerton62 1:51477fe4851b 193 float max_temperature;
lcockerton62 1:51477fe4851b 194
lcockerton62 1:51477fe4851b 195 probe[0]->convert_temperature(DS1820::all_devices);
lcockerton62 1:51477fe4851b 196 min_temperature = probe[0]->temperature('C');
lcockerton62 1:51477fe4851b 197 max_temperature = min_temperature; // Initially set the max and min temperature equal
lcockerton62 1:51477fe4851b 198 for (int i=1; i<devices_found; i++) {
lcockerton62 2:94716229ecc3 199
lcockerton62 1:51477fe4851b 200 measurements.temperature_measurements[i].ID = i;
lcockerton62 1:51477fe4851b 201 measurements.temperature_measurements[i].measurement = probe[i] ->temperature('C');
lcockerton62 2:94716229ecc3 202
lcockerton62 1:51477fe4851b 203 if(measurements.temperature_measurements[i].measurement > max_temperature) {
lcockerton62 1:51477fe4851b 204 max_temperature = measurements.temperature_measurements[i].measurement;
lcockerton62 2:94716229ecc3 205 } else if (measurements.temperature_measurements[i].measurement < min_temperature) {
lcockerton62 1:51477fe4851b 206 min_temperature = measurements.temperature_measurements[i].measurement;
lcockerton62 1:51477fe4851b 207 }
lcockerton62 1:51477fe4851b 208 }
lcockerton62 1:51477fe4851b 209 measurements.max_cell_temp.temperature = max_temperature;
lcockerton62 1:51477fe4851b 210 measurements.min_cell_temp.temperature = min_temperature;
lcockerton62 0:0a5f554d2a16 211 }
lcockerton62 0:0a5f554d2a16 212
lcockerton62 0:0a5f554d2a16 213 void update_SOC()
lcockerton62 0:0a5f554d2a16 214 {
lcockerton62 1:51477fe4851b 215 // Update the SOC value
lcockerton62 0:0a5f554d2a16 216 }
lcockerton62 0:0a5f554d2a16 217
lcockerton62 0:0a5f554d2a16 218
lcockerton62 1:51477fe4851b 219 uint32_t check_measurements(BMU_data &measurements)
lcockerton62 1:51477fe4851b 220 {
lcockerton62 1:51477fe4851b 221 uint32_t status;
lcockerton62 2:94716229ecc3 222
lcockerton62 2:94716229ecc3 223 if(measurements.max_cell_voltage.voltage > MAX_CELL_VOLTAGE) {
lcockerton62 2:94716229ecc3 224 status = status | CELL_OVER_VOLTAGE;
lcockerton62 2:94716229ecc3 225 } else if (measurements.min_cell_voltage.voltage < MIN_CELL_VOLTAGE) {
lcockerton62 1:51477fe4851b 226 status = status | CELL_UNDER_VOLTAGE;
lcockerton62 2:94716229ecc3 227 } else if (measurements.max_cell_temp.temperature > MAX_CELL_TEMPERATURE) {
lcockerton62 1:51477fe4851b 228 status = status | CELL_OVER_TEMPERATURE;
lcockerton62 1:51477fe4851b 229 }
lcockerton62 2:94716229ecc3 230
lcockerton62 1:51477fe4851b 231 /*
lcockerton62 1:51477fe4851b 232 @TODO also include errors for:
lcockerton62 1:51477fe4851b 233 *untrusted measurement
lcockerton62 1:51477fe4851b 234 *CMU timeout
lcockerton62 1:51477fe4851b 235 *SOC not valid
lcockerton62 1:51477fe4851b 236 */
lcockerton62 1:51477fe4851b 237 return status;
lcockerton62 1:51477fe4851b 238 }
lcockerton62 1:51477fe4851b 239
lcockerton62 1:51477fe4851b 240 void take_measurements(BMU_data &measurements)
lcockerton62 1:51477fe4851b 241 {
maxv008 6:b567fcb604aa 242 uint16_t cellvoltages[NO_CMUS][12];
maxv008 6:b567fcb604aa 243 //TODO Use LTC6804_acquireVoltage to fill this array, and then properly format
maxv008 6:b567fcb604aa 244 //it to be sent over CAN
maxv008 6:b567fcb604aa 245
maxv008 7:d00f4433cea9 246 LTC6804_acquireVoltage(cellvoltages);
maxv008 7:d00f4433cea9 247
maxv008 10:1079f8e52d65 248 for(int i=0; i<NO_CMUS; i++){
maxv008 10:1079f8e52d65 249 for(int j=0; j<12; j++){
maxv008 10:1079f8e52d65 250 measurements.cell_voltages[i].voltages[j] = cellvoltages[i][j] / 10;
maxv008 10:1079f8e52d65 251 printf("Cellvoltage[%d][%d] = %d \r\n",i,j,cellvoltages[i][j] /10);
maxv008 10:1079f8e52d65 252 }
maxv008 10:1079f8e52d65 253 }
DasSidG 4:9050c5d6925e 254
DasSidG 4:9050c5d6925e 255 //Current, SoC
DasSidG 4:9050c5d6925e 256 measurements.battery_current = (uint32_t) ltc2943.current()*1000; //*1000 to converet to mA
DasSidG 4:9050c5d6925e 257 measurements.percentage_SOC = ltc2943.accumulatedCharge();
DasSidG 4:9050c5d6925e 258 measurements.SOC = (measurements.percentage_SOC /100) * BATTERY_CAPACITY;
lcockerton62 1:51477fe4851b 259 }
lcockerton62 1:51477fe4851b 260
lcockerton62 0:0a5f554d2a16 261 void init()
lcockerton62 0:0a5f554d2a16 262 {
lcockerton62 1:51477fe4851b 263 temperature_init(); // Initialise the temperature sensors
DasSidG 4:9050c5d6925e 264 LTC2943_initialise(); //Initialises the fixed parameters of the LTC2943
lcockerton62 0:0a5f554d2a16 265 }
lcockerton62 0:0a5f554d2a16 266
maxv008 10:1079f8e52d65 267 void test_read_voltage_CAN()
maxv008 10:1079f8e52d65 268 {
maxv008 10:1079f8e52d65 269 CANMessage msg;
maxv008 10:1079f8e52d65 270 uint16_t readings[4];
maxv008 10:1079f8e52d65 271 int can_id;
maxv008 10:1079f8e52d65 272 int offset;
maxv008 10:1079f8e52d65 273 int first_index;
maxv008 10:1079f8e52d65 274 int second_index;
maxv008 10:1079f8e52d65 275
maxv008 10:1079f8e52d65 276 if(can.read(msg))
maxv008 10:1079f8e52d65 277 {
maxv008 10:1079f8e52d65 278 for(int i =0; i < 4; i++)
maxv008 10:1079f8e52d65 279 {
maxv008 10:1079f8e52d65 280 readings[i] = (msg.data[2 * i]) + (msg.data[2*i+1] << 8); //Since data is 8 8bit ints not 4 16 bit ones
maxv008 10:1079f8e52d65 281 }
maxv008 10:1079f8e52d65 282 can_id = msg.id;
maxv008 10:1079f8e52d65 283 printf("CAN ID: %d \r\n", can_id);
maxv008 10:1079f8e52d65 284 offset = can_id - 1537; //1537 = 0x600
maxv008 10:1079f8e52d65 285 first_index = (offset - 1)/4; //offset of 2,3,4 is CMU 1; 6,7,8, is CMU 2; etc.
maxv008 10:1079f8e52d65 286 second_index = ((offset - 1) % 4); //Makes it so 0,1,2 represent each voltage set
maxv008 10:1079f8e52d65 287 for(int i = 0; i < 4; i++)
maxv008 10:1079f8e52d65 288 {
maxv008 10:1079f8e52d65 289 printf("Cell_Voltage[%d][%d] = %d \r\n", first_index, second_index *4 + i, readings[i]);
maxv008 10:1079f8e52d65 290 }
maxv008 10:1079f8e52d65 291 }
maxv008 10:1079f8e52d65 292 else
maxv008 10:1079f8e52d65 293 printf("Reading Failed \r\n");
maxv008 10:1079f8e52d65 294 }
maxv008 10:1079f8e52d65 295
maxv008 10:1079f8e52d65 296 void test_CAN_send()
maxv008 10:1079f8e52d65 297 {
maxv008 10:1079f8e52d65 298 CANMessage msg;
maxv008 10:1079f8e52d65 299 char value = 87;
maxv008 10:1079f8e52d65 300 msg = CANMessage(1, &value,1);
maxv008 10:1079f8e52d65 301 if(can.write(msg))
maxv008 10:1079f8e52d65 302 printf("Succesfully sent %d \r\n", value);
maxv008 10:1079f8e52d65 303 else
maxv008 10:1079f8e52d65 304 printf("Sending Failed \r\n");
maxv008 10:1079f8e52d65 305 }
maxv008 10:1079f8e52d65 306
maxv008 10:1079f8e52d65 307 void test_CAN_read()
maxv008 10:1079f8e52d65 308 {
maxv008 10:1079f8e52d65 309 CANMessage msg;
maxv008 10:1079f8e52d65 310 if(can.read(msg))
maxv008 10:1079f8e52d65 311 printf("Successfully recieved %d \r\n", msg.data[0]);
maxv008 10:1079f8e52d65 312 else
maxv008 10:1079f8e52d65 313 printf("Reading Failed \r\n");
maxv008 10:1079f8e52d65 314 }