Cell voltages fork (SoC)

Dependencies:   CUER_CAN CUER_DS1820 LTC2943 LTC6804 mbed PowerControl

Committer:
maxv008
Date:
Sun Jul 02 13:35:18 2017 +0000
Revision:
17:94dd9a0d3870
Parent:
16:b2ef68c9a4fd
Child:
18:521ffdd724f3
added decoder for voltage messages

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
lcockerton62 0:0a5f554d2a16 13 using namespace CAN_IDs;
lcockerton62 0:0a5f554d2a16 14
lcockerton62 0:0a5f554d2a16 15 // Function definitions
lcockerton62 1:51477fe4851b 16 void transmit_data(BMU_data measurements,uint32_t status);
lcockerton62 1:51477fe4851b 17 void read_temperature_sensors(BMU_data &measurements);
lcockerton62 0:0a5f554d2a16 18 void update_SOC();
lcockerton62 0:0a5f554d2a16 19 void init();
maxv008 14:e0e88a009f4c 20 void interruptHandler();
maxv008 14:e0e88a009f4c 21 void CANDataSentCallback();
lcockerton62 1:51477fe4851b 22 void write_SOC_EEPROM(BMU_data &measurements,uint16_t start_address);
lcockerton62 1:51477fe4851b 23 uint16_t read_EEPROM_startup(BMU_data &measurements);
lcockerton62 1:51477fe4851b 24 uint32_t check_measurements(BMU_data &measurements);
lcockerton62 1:51477fe4851b 25 void take_measurements(BMU_data &measurements);
maxv008 14:e0e88a009f4c 26 void test_read_CAN_buffer();
DasSidG 12:fa9b1a459e47 27 bool test_read_voltage_CAN(uint16_t readings[], int can_ids[]);
maxv008 10:1079f8e52d65 28 void test_CAN_send();
maxv008 10:1079f8e52d65 29 void test_CAN_read();
lcockerton62 0:0a5f554d2a16 30
lcockerton62 0:0a5f554d2a16 31 CAN can(CAN_READ_PIN, CAN_WRITE_PIN); //Create a CAN object to handle CAN comms
maxv008 14:e0e88a009f4c 32 CANMessage buffer[CAN_BUFFER_SIZE]; //CAN receive buffer
maxv008 14:e0e88a009f4c 33 bool safe_to_write[CAN_BUFFER_SIZE]; //Semaphore bit indicating that it's safe to write to the software buffer
maxv008 14:e0e88a009f4c 34 bool CAN_data_sent = false;
maxv008 14:e0e88a009f4c 35
maxv008 17:94dd9a0d3870 36 //Global array to store most recently obtained voltage measurement:
maxv008 17:94dd9a0d3870 37 CMU_voltage voltage_readings[NO_CMUS];
maxv008 17:94dd9a0d3870 38
DasSidG 4:9050c5d6925e 39 uint16_t eeprom_start_address; //the initial address where we store/read SoC values
lcockerton62 0:0a5f554d2a16 40
lcockerton62 1:51477fe4851b 41 Timeout loop_delay;
lcockerton62 1:51477fe4851b 42 bool delay_finished = false;
lcockerton62 2:94716229ecc3 43
maxv008 14:e0e88a009f4c 44
lcockerton62 2:94716229ecc3 45 void loop_delay_callback(void)
lcockerton62 2:94716229ecc3 46 {
lcockerton62 1:51477fe4851b 47 delay_finished = true;
lcockerton62 1:51477fe4851b 48 }
lcockerton62 1:51477fe4851b 49
lcockerton62 0:0a5f554d2a16 50 int main()
DasSidG 11:cf2db05cfa56 51 {
lcockerton62 1:51477fe4851b 52 BMU_data measurements;
lcockerton62 1:51477fe4851b 53 uint16_t current_EEPROM_address;
lcockerton62 1:51477fe4851b 54 uint32_t status;
DasSidG 12:fa9b1a459e47 55 uint16_t volt_readings[36];
DasSidG 12:fa9b1a459e47 56 int can_ids[9];
maxv008 10:1079f8e52d65 57
DasSidG 12:fa9b1a459e47 58
maxv008 14:e0e88a009f4c 59 /**while(true)
maxv008 10:1079f8e52d65 60 {
DasSidG 12:fa9b1a459e47 61 for (int i = 0; i < 9; ++i) {
DasSidG 12:fa9b1a459e47 62 while(!test_read_voltage_CAN(&volt_readings[(i*4)], &can_ids[i]));
DasSidG 12:fa9b1a459e47 63 }
DasSidG 12:fa9b1a459e47 64
DasSidG 12:fa9b1a459e47 65 for (int i = 0; i < 36; ++i) {
DasSidG 12:fa9b1a459e47 66 printf("Cellvoltage %d = %d, CAN ID is %d \r\n", i, volt_readings[i], can_ids[i/4]);
DasSidG 12:fa9b1a459e47 67 volt_readings[i] = -1;
DasSidG 12:fa9b1a459e47 68 can_ids[i/4] = 0;
DasSidG 12:fa9b1a459e47 69 }
DasSidG 12:fa9b1a459e47 70 printf("\r\n");
DasSidG 12:fa9b1a459e47 71
maxv008 14:e0e88a009f4c 72 } */
lcockerton62 0:0a5f554d2a16 73 init();
maxv008 10:1079f8e52d65 74
DasSidG 11:cf2db05cfa56 75
DasSidG 11:cf2db05cfa56 76
DasSidG 11:cf2db05cfa56 77 //current_EEPROM_address = read_EEPROM_startup(measurements); // Read from the eeprom at startup to fill in the values of SoC
DasSidG 11:cf2db05cfa56 78 //ltc2943.accumulatedCharge(measurements.percentage_SOC); // Initialise the LTC2943 with the current state of charge
DasSidG 4:9050c5d6925e 79
lcockerton62 1:51477fe4851b 80 while (true) {
DasSidG 11:cf2db05cfa56 81
lcockerton62 1:51477fe4851b 82 // Take measurements from the sensors
lcockerton62 1:51477fe4851b 83 take_measurements(measurements);
DasSidG 11:cf2db05cfa56 84 /*// Dont want to read the temperature sensors during each iteration of the loop
lcockerton62 1:51477fe4851b 85 if (c == 0) {
lcockerton62 1:51477fe4851b 86 read_temperature_sensors(measurements);
lcockerton62 1:51477fe4851b 87 } else if(c >= 4) {
lcockerton62 0:0a5f554d2a16 88 c = -1;
lcockerton62 0:0a5f554d2a16 89 }
lcockerton62 0:0a5f554d2a16 90 c++;
lcockerton62 0:0a5f554d2a16 91
lcockerton62 1:51477fe4851b 92 // Check data for errors
lcockerton62 1:51477fe4851b 93 status = check_measurements(measurements);
lcockerton62 1:51477fe4851b 94
lcockerton62 0:0a5f554d2a16 95 // Update the SOC
lcockerton62 0:0a5f554d2a16 96 update_SOC();
lcockerton62 0:0a5f554d2a16 97
lcockerton62 1:51477fe4851b 98 //Store data in the eeprom
lcockerton62 1:51477fe4851b 99 write_SOC_EEPROM(measurements, current_EEPROM_address);
DasSidG 11:cf2db05cfa56 100 */
lcockerton62 0:0a5f554d2a16 101
lcockerton62 5:793afeef45dc 102 // CAN bus
maxv008 14:e0e88a009f4c 103 CAN_data_sent = false;//Currently does nothing, adding this line in more places then using
maxv008 14:e0e88a009f4c 104 //while(!CAN_data_sent); in order to ensure sending completes
DasSidG 15:e901aff1f5b3 105 transmit_data(measurements,status);
DasSidG 15:e901aff1f5b3 106 //test_read_CAN_buffer();
DasSidG 11:cf2db05cfa56 107
DasSidG 11:cf2db05cfa56 108 /*
lcockerton62 0:0a5f554d2a16 109 // Conserve power - enter a low powered mode
lcockerton62 2:94716229ecc3 110 delay_finished = false;
lcockerton62 1:51477fe4851b 111 loop_delay.attach(loop_delay_callback, LOOP_DELAY_S);
lcockerton62 1:51477fe4851b 112 while (!delay_finished) sleep();
DasSidG 11:cf2db05cfa56 113 */
DasSidG 11:cf2db05cfa56 114
DasSidG 11:cf2db05cfa56 115 wait(1);
maxv008 10:1079f8e52d65 116 }
lcockerton62 0:0a5f554d2a16 117 }
lcockerton62 0:0a5f554d2a16 118
lcockerton62 1:51477fe4851b 119 void transmit_data(BMU_data measurements, uint32_t status)
lcockerton62 0:0a5f554d2a16 120 {
msharma97 9:82ba050a7e13 121 CANMessage msg;
lcockerton62 0:0a5f554d2a16 122 /*
lcockerton62 0:0a5f554d2a16 123 Place all of the collected data onto the CAN bus
lcockerton62 0:0a5f554d2a16 124 */
lcockerton62 5:793afeef45dc 125 // Send cell voltages
maxv008 13:7b42af989cd1 126 //voltages sent in sets of 4 + one cmu data set
msharma97 9:82ba050a7e13 127 int repeating_unit_length = NO_READINGS_PER_CMU /4 + 1;
maxv008 10:1079f8e52d65 128 for(uint16_t i= 0; i < NO_CMUS; i++) {
msharma97 9:82ba050a7e13 129 //input id is offset, data structure is info, voltage, voltage, ......
maxv008 10:1079f8e52d65 130 //This is a slightly modified version of the Tritium BMS datasheet, to add an extra voltage reading set.
maxv008 10:1079f8e52d65 131 msg = createVoltageTelemetry(repeating_unit_length*i+2, measurements.cell_voltages[i].voltages);
msharma97 9:82ba050a7e13 132 can.write(msg);
maxv008 17:94dd9a0d3870 133 printf("Voltage Message id: %d \r\n", msg.id);
DasSidG 11:cf2db05cfa56 134 wait(0.1);
maxv008 17:94dd9a0d3870 135 //+4 - 4 cell voltages sent per measurement, simple pointer arithmetic
maxv008 10:1079f8e52d65 136 msg = createVoltageTelemetry(repeating_unit_length*i+3, measurements.cell_voltages[i].voltages + 4);
msharma97 9:82ba050a7e13 137 can.write(msg);
maxv008 17:94dd9a0d3870 138 printf("Voltage Message id: %d \r\n", msg.id);
DasSidG 11:cf2db05cfa56 139 wait(0.1);
maxv008 10:1079f8e52d65 140 msg = createVoltageTelemetry(repeating_unit_length*i+4, measurements.cell_voltages[i].voltages + 8);
msharma97 9:82ba050a7e13 141 can.write(msg);
maxv008 17:94dd9a0d3870 142 printf("Voltage Message id: %d \r\n", msg.id);
DasSidG 11:cf2db05cfa56 143 wait(0.1);
lcockerton62 1:51477fe4851b 144 }
maxv008 13:7b42af989cd1 145
maxv008 13:7b42af989cd1 146 //Transmitting all of the individual probes:
maxv008 17:94dd9a0d3870 147 for(uint8_t i = 0; i < devices_found; i++)
maxv008 13:7b42af989cd1 148 {
maxv008 14:e0e88a009f4c 149 individual_temperature tempreading = measurements.temperature_measurements[i];
maxv008 14:e0e88a009f4c 150 msg = createTemperatureTelemetry(i, &tempreading.ROMID[0], tempreading.measurement);
maxv008 17:94dd9a0d3870 151 individual_temperature testOut = decodeTemperatureTelemetry(msg);
maxv008 17:94dd9a0d3870 152 printf("Temperature reading sent (CAN ID = %d): (%f,%d) \r\n", msg.id, testOut.measurement, testOut.ID);
DasSidG 16:b2ef68c9a4fd 153 can.write(msg);
maxv008 13:7b42af989cd1 154 wait(0.1);
maxv008 13:7b42af989cd1 155 }
lcockerton62 1:51477fe4851b 156
lcockerton62 1:51477fe4851b 157 // Create SOC CAN message
lcockerton62 1:51477fe4851b 158 createPackSOC(measurements.SOC, measurements.percentage_SOC);
lcockerton62 0:0a5f554d2a16 159
lcockerton62 1:51477fe4851b 160 // Min/max cell voltages
lcockerton62 1:51477fe4851b 161 createCellVoltageMAXMIN(measurements.max_cell_voltage, measurements.min_cell_voltage);
lcockerton62 2:94716229ecc3 162
maxv008 13:7b42af989cd1 163 // Min/Max cell temperature, Currently the meaning of temp max/min is a ambiguous
maxv008 13:7b42af989cd1 164 // due to changes to Temperature reading (namely the CMU ID portion of it), @TODO change MAXMIN
lcockerton62 1:51477fe4851b 165 createCellTemperatureMAXMIN(measurements.min_cell_temp,measurements.max_cell_temp);
lcockerton62 2:94716229ecc3 166
lcockerton62 2:94716229ecc3 167 // Battery voltage and current
lcockerton62 5:793afeef45dc 168 // @TODO add the voltage
lcockerton62 1:51477fe4851b 169 createBatteryVI(measurements.battery_voltage,measurements.battery_current);
lcockerton62 2:94716229ecc3 170
lcockerton62 1:51477fe4851b 171 //Extended battery pack status
lcockerton62 1:51477fe4851b 172 createExtendedBatteryPackStatus(status);
lcockerton62 2:94716229ecc3 173
lcockerton62 0:0a5f554d2a16 174 }
lcockerton62 0:0a5f554d2a16 175
maxv008 10:1079f8e52d65 176
lcockerton62 1:51477fe4851b 177 uint16_t read_EEPROM_startup(BMU_data &measurements)
lcockerton62 0:0a5f554d2a16 178 {
lcockerton62 1:51477fe4851b 179 /* The first page of the EEPROM, specifically the first 2 addresses store a
lcockerton62 1:51477fe4851b 180 pointer of the first memory location of measurement data. The EEPROM only has a finite number of
lcockerton62 1:51477fe4851b 181 read/write cycles which is why we aren't writing to the same location throughout
lcockerton62 1:51477fe4851b 182 */
lcockerton62 5:793afeef45dc 183
lcockerton62 1:51477fe4851b 184 uint16_t start_address;
lcockerton62 1:51477fe4851b 185 char start_address_array[2];
lcockerton62 1:51477fe4851b 186 char SOC_out[8]; // 4 bytes for the 2 floats one is SOC and the other % charge
lcockerton62 1:51477fe4851b 187 float *fp1,*fp2; // temporary storage for float conversion
lcockerton62 1:51477fe4851b 188
lcockerton62 1:51477fe4851b 189 // Get a pointer to the start address for the data stored in the eeprom
lcockerton62 1:51477fe4851b 190 i2c_page_read(0x0000,2,start_address_array);
lcockerton62 1:51477fe4851b 191
lcockerton62 1:51477fe4851b 192 // Read the data from this address
lcockerton62 1:51477fe4851b 193 start_address = (start_address_array[1]<< 8) | start_address_array[0]; // mbed little endian follow this convention
lcockerton62 1:51477fe4851b 194 i2c_page_read(start_address, 8,SOC_out);
lcockerton62 0:0a5f554d2a16 195
lcockerton62 1:51477fe4851b 196 // Convert the SOC_out values back into floats
lcockerton62 1:51477fe4851b 197 fp1 = (float*)(&SOC_out[0]);
lcockerton62 1:51477fe4851b 198 fp2 = (float*)(&SOC_out[4]);
lcockerton62 1:51477fe4851b 199 measurements.SOC = *fp1;
lcockerton62 1:51477fe4851b 200 measurements.percentage_SOC = *fp2;
lcockerton62 1:51477fe4851b 201
lcockerton62 1:51477fe4851b 202 // Select the next address to write to
lcockerton62 1:51477fe4851b 203 start_address += 0x0040;
lcockerton62 1:51477fe4851b 204 if(start_address > MAX_WRITE_ADDRESS) {
lcockerton62 5:793afeef45dc 205 start_address = START_WRITE_ADDRESS; // Loop to the start of the eeprom
lcockerton62 1:51477fe4851b 206 }
lcockerton62 1:51477fe4851b 207
lcockerton62 5:793afeef45dc 208 /*@TODO need to include a CRC check for the address pointer for the scenario
lcockerton62 5:793afeef45dc 209 when power is removed and we are writing to the eeprom*/
lcockerton62 1:51477fe4851b 210 // write the new address to location 0x0000
lcockerton62 1:51477fe4851b 211 start_address_array[0] = start_address | 0x00FF;
lcockerton62 1:51477fe4851b 212 start_address_array[1] = start_address >> 8;
lcockerton62 1:51477fe4851b 213 i2c_page_write(0x0000, 2, start_address_array);
lcockerton62 1:51477fe4851b 214
lcockerton62 1:51477fe4851b 215 return start_address;
lcockerton62 0:0a5f554d2a16 216 }
lcockerton62 0:0a5f554d2a16 217
lcockerton62 1:51477fe4851b 218 void write_SOC_EEPROM(BMU_data &measurements,uint16_t start_address)
lcockerton62 0:0a5f554d2a16 219 {
lcockerton62 1:51477fe4851b 220 char data_out[8];
lcockerton62 1:51477fe4851b 221 float *fp1,*fp2;
lcockerton62 1:51477fe4851b 222
lcockerton62 1:51477fe4851b 223 fp1 = (float*)(&measurements.SOC);
lcockerton62 1:51477fe4851b 224 fp2 = (float*)(&measurements.percentage_SOC);
lcockerton62 0:0a5f554d2a16 225
lcockerton62 1:51477fe4851b 226 for(int i = 0; i < 4; i++ ) {
lcockerton62 1:51477fe4851b 227 data_out[i] = *fp1;
lcockerton62 1:51477fe4851b 228 fp1++;
lcockerton62 1:51477fe4851b 229 }
lcockerton62 1:51477fe4851b 230 for(int j = 4; j < 7; j++ ) {
lcockerton62 1:51477fe4851b 231 data_out[j] = *fp2;
lcockerton62 1:51477fe4851b 232 fp2++;
lcockerton62 1:51477fe4851b 233 }
lcockerton62 1:51477fe4851b 234 i2c_page_write(start_address, 8,data_out);
lcockerton62 0:0a5f554d2a16 235 }
lcockerton62 0:0a5f554d2a16 236
lcockerton62 1:51477fe4851b 237 void read_temperature_sensors(BMU_data &measurements)
lcockerton62 0:0a5f554d2a16 238 {
lcockerton62 1:51477fe4851b 239 float min_temperature;
lcockerton62 1:51477fe4851b 240 float max_temperature;
DasSidG 12:fa9b1a459e47 241 DigitalOut isotherm_12V_pin(ISOTHERM_12V_PIN);
DasSidG 12:fa9b1a459e47 242 isotherm_12V_pin = 1;
lcockerton62 1:51477fe4851b 243 probe[0]->convert_temperature(DS1820::all_devices);
DasSidG 12:fa9b1a459e47 244 isotherm_12V_pin = 0;
lcockerton62 1:51477fe4851b 245 min_temperature = probe[0]->temperature('C');
lcockerton62 1:51477fe4851b 246 max_temperature = min_temperature; // Initially set the max and min temperature equal
DasSidG 16:b2ef68c9a4fd 247 for (int i=0; i<devices_found; i++) {
maxv008 14:e0e88a009f4c 248 for(int j = 0; j < 7; j++)
maxv008 14:e0e88a009f4c 249 measurements.temperature_measurements[i].ROMID[j] = probe[i]->ROM[j];
lcockerton62 1:51477fe4851b 250 measurements.temperature_measurements[i].measurement = probe[i] ->temperature('C');
maxv008 14:e0e88a009f4c 251
maxv008 14:e0e88a009f4c 252
maxv008 14:e0e88a009f4c 253
lcockerton62 1:51477fe4851b 254 if(measurements.temperature_measurements[i].measurement > max_temperature) {
lcockerton62 1:51477fe4851b 255 max_temperature = measurements.temperature_measurements[i].measurement;
lcockerton62 2:94716229ecc3 256 } else if (measurements.temperature_measurements[i].measurement < min_temperature) {
lcockerton62 1:51477fe4851b 257 min_temperature = measurements.temperature_measurements[i].measurement;
lcockerton62 1:51477fe4851b 258 }
DasSidG 12:fa9b1a459e47 259
DasSidG 12:fa9b1a459e47 260 printf("Device %d temperature is %3.3f degrees Celcius.\r\n",i+1 ,probe[i]->temperature('C'));
lcockerton62 1:51477fe4851b 261 }
maxv008 14:e0e88a009f4c 262
maxv008 13:7b42af989cd1 263 //There is also a CMU # component of this struct, currently unfilled, perhaps not needed at all.
lcockerton62 1:51477fe4851b 264 measurements.max_cell_temp.temperature = max_temperature;
lcockerton62 1:51477fe4851b 265 measurements.min_cell_temp.temperature = min_temperature;
lcockerton62 0:0a5f554d2a16 266 }
lcockerton62 0:0a5f554d2a16 267
lcockerton62 0:0a5f554d2a16 268 void update_SOC()
lcockerton62 0:0a5f554d2a16 269 {
lcockerton62 1:51477fe4851b 270 // Update the SOC value
lcockerton62 0:0a5f554d2a16 271 }
lcockerton62 0:0a5f554d2a16 272
lcockerton62 0:0a5f554d2a16 273
lcockerton62 1:51477fe4851b 274 uint32_t check_measurements(BMU_data &measurements)
lcockerton62 1:51477fe4851b 275 {
lcockerton62 1:51477fe4851b 276 uint32_t status;
lcockerton62 2:94716229ecc3 277
lcockerton62 2:94716229ecc3 278 if(measurements.max_cell_voltage.voltage > MAX_CELL_VOLTAGE) {
lcockerton62 2:94716229ecc3 279 status = status | CELL_OVER_VOLTAGE;
lcockerton62 2:94716229ecc3 280 } else if (measurements.min_cell_voltage.voltage < MIN_CELL_VOLTAGE) {
lcockerton62 1:51477fe4851b 281 status = status | CELL_UNDER_VOLTAGE;
lcockerton62 2:94716229ecc3 282 } else if (measurements.max_cell_temp.temperature > MAX_CELL_TEMPERATURE) {
lcockerton62 1:51477fe4851b 283 status = status | CELL_OVER_TEMPERATURE;
lcockerton62 1:51477fe4851b 284 }
lcockerton62 2:94716229ecc3 285
lcockerton62 1:51477fe4851b 286 /*
lcockerton62 1:51477fe4851b 287 @TODO also include errors for:
lcockerton62 1:51477fe4851b 288 *untrusted measurement
lcockerton62 1:51477fe4851b 289 *CMU timeout
lcockerton62 1:51477fe4851b 290 *SOC not valid
lcockerton62 1:51477fe4851b 291 */
lcockerton62 1:51477fe4851b 292 return status;
lcockerton62 1:51477fe4851b 293 }
lcockerton62 1:51477fe4851b 294
lcockerton62 1:51477fe4851b 295 void take_measurements(BMU_data &measurements)
lcockerton62 1:51477fe4851b 296 {
maxv008 6:b567fcb604aa 297 uint16_t cellvoltages[NO_CMUS][12];
DasSidG 16:b2ef68c9a4fd 298 //Use LTC6804_acquireVoltage to fill this array, and then properly format
maxv008 6:b567fcb604aa 299 //it to be sent over CAN
maxv008 6:b567fcb604aa 300
DasSidG 16:b2ef68c9a4fd 301 LTC6804_acquireVoltage(cellvoltages);
DasSidG 16:b2ef68c9a4fd 302
maxv008 10:1079f8e52d65 303 for(int i=0; i<NO_CMUS; i++){
maxv008 17:94dd9a0d3870 304 for(int j=0; j < NO_READINGS_PER_CMU; j++){
DasSidG 16:b2ef68c9a4fd 305 measurements.cell_voltages[i].voltages[j] = cellvoltages[i][j]/ 10; //To get units of mV
maxv008 17:94dd9a0d3870 306 measurements.cell_voltages[i].CMU_number = i;
DasSidG 16:b2ef68c9a4fd 307 //printf("Cellvoltage[%d][%d] = %d \r\n",i,j,cellvoltages[i][j])/10);
maxv008 10:1079f8e52d65 308 }
maxv008 14:e0e88a009f4c 309 }
DasSidG 4:9050c5d6925e 310
maxv008 13:7b42af989cd1 311 //Add code to take all temperature measurements and add it to measurements struct.
maxv008 13:7b42af989cd1 312 read_temperature_sensors(measurements);
maxv008 13:7b42af989cd1 313
DasSidG 4:9050c5d6925e 314 //Current, SoC
maxv008 13:7b42af989cd1 315 measurements.battery_current = (uint32_t) ltc2943.current()*1000; //*1000 to convert to mA
DasSidG 4:9050c5d6925e 316 measurements.percentage_SOC = ltc2943.accumulatedCharge();
DasSidG 4:9050c5d6925e 317 measurements.SOC = (measurements.percentage_SOC /100) * BATTERY_CAPACITY;
lcockerton62 1:51477fe4851b 318 }
lcockerton62 1:51477fe4851b 319
lcockerton62 0:0a5f554d2a16 320 void init()
lcockerton62 0:0a5f554d2a16 321 {
lcockerton62 1:51477fe4851b 322 temperature_init(); // Initialise the temperature sensors
DasSidG 4:9050c5d6925e 323 LTC2943_initialise(); //Initialises the fixed parameters of the LTC2943
DasSidG 15:e901aff1f5b3 324 LTC6804_init(MD_FAST, DCP_DISABLED, CELL_CH_ALL, AUX_CH_VREF2); //Initialises the LTC6804s
maxv008 14:e0e88a009f4c 325
maxv008 14:e0e88a009f4c 326 for(int i=0; i<CAN_BUFFER_SIZE; i++)
maxv008 14:e0e88a009f4c 327 {
maxv008 14:e0e88a009f4c 328 buffer[i].id = BLANK_ID;
maxv008 14:e0e88a009f4c 329 //("%d",buffer[i].id);
maxv008 14:e0e88a009f4c 330 safe_to_write[i]= true;
maxv008 14:e0e88a009f4c 331 }
maxv008 14:e0e88a009f4c 332
maxv008 14:e0e88a009f4c 333 //Initialise CAN stuff, attach CAN interrupt handlers
maxv008 14:e0e88a009f4c 334 can.frequency(CAN_BIT_RATE); //set transmission rate to agreed bit rate (ELEC-006)
maxv008 14:e0e88a009f4c 335 can.reset(); // (FUNC-018)
maxv008 14:e0e88a009f4c 336 can.attach(&interruptHandler, CAN::RxIrq); //receive interrupt handler
maxv008 14:e0e88a009f4c 337 can.attach(&CANDataSentCallback, CAN::TxIrq); //send interrupt handler
maxv008 17:94dd9a0d3870 338
maxv008 17:94dd9a0d3870 339 //Initialize voltage array
maxv008 17:94dd9a0d3870 340 for(int i = 0; i < NO_CMUS; i++)
maxv008 17:94dd9a0d3870 341 {
maxv008 17:94dd9a0d3870 342 for(int j = 0; j < NO_READINGS_PER_CMU; j++)
maxv008 17:94dd9a0d3870 343 {
maxv008 17:94dd9a0d3870 344 voltage_readings[i].voltages[j] = 0;
maxv008 17:94dd9a0d3870 345 }
maxv008 17:94dd9a0d3870 346 }
maxv008 14:e0e88a009f4c 347 }
maxv008 14:e0e88a009f4c 348
maxv008 14:e0e88a009f4c 349 void CANDataSentCallback(void) {
maxv008 14:e0e88a009f4c 350 CAN_data_sent = true;
lcockerton62 0:0a5f554d2a16 351 }
lcockerton62 0:0a5f554d2a16 352
maxv008 14:e0e88a009f4c 353 void interruptHandler()
maxv008 14:e0e88a009f4c 354 {
maxv008 14:e0e88a009f4c 355 CANMessage msg;
DasSidG 16:b2ef68c9a4fd 356 can.read(msg);
maxv008 14:e0e88a009f4c 357 for(int i=0; i<CAN_BUFFER_SIZE; i++) {
maxv008 14:e0e88a009f4c 358 if((buffer[i].id == msg.id || buffer[i].id==BLANK_ID) && safe_to_write[i]) {
maxv008 14:e0e88a009f4c 359 //("id %d added to buffer \r\n", msg.id);
maxv008 14:e0e88a009f4c 360 buffer[i] = msg;
maxv008 14:e0e88a009f4c 361 //return required so that only first blank buffer entry is converted to incoming message ID each time new message ID is encountered
maxv008 14:e0e88a009f4c 362 return;
maxv008 14:e0e88a009f4c 363 }
maxv008 14:e0e88a009f4c 364 }
maxv008 14:e0e88a009f4c 365 }
maxv008 14:e0e88a009f4c 366
maxv008 14:e0e88a009f4c 367 void test_read_CAN_buffer()
maxv008 14:e0e88a009f4c 368 {
maxv008 14:e0e88a009f4c 369 //Import the data from the buffer into a non-volatile, more usable format
maxv008 14:e0e88a009f4c 370 CAN_Data can_data[CAN_BUFFER_SIZE]; //container for all of the raw data
maxv008 17:94dd9a0d3870 371 CANMessage msgArray[CAN_BUFFER_SIZE]; //Same as above but some functions take message as their parameter
maxv008 14:e0e88a009f4c 372 int received_CAN_IDs[CAN_BUFFER_SIZE]; //needed to keep track of which IDs we've received so far
maxv008 14:e0e88a009f4c 373 for (int i = 0; i<CAN_BUFFER_SIZE; ++i)
maxv008 14:e0e88a009f4c 374 {
maxv008 14:e0e88a009f4c 375 safe_to_write[i] = false;
maxv008 14:e0e88a009f4c 376 can_data[i].importCANData(buffer[i]);
maxv008 14:e0e88a009f4c 377 received_CAN_IDs[i] = buffer[i].id;
maxv008 17:94dd9a0d3870 378 msgArray[i] = buffer[i];
maxv008 14:e0e88a009f4c 379 safe_to_write[i] = true;
maxv008 14:e0e88a009f4c 380 }
maxv008 17:94dd9a0d3870 381
maxv008 17:94dd9a0d3870 382 //voltage readings:
maxv008 17:94dd9a0d3870 383
maxv008 17:94dd9a0d3870 384
maxv008 14:e0e88a009f4c 385 }
DasSidG 12:fa9b1a459e47 386 bool test_read_voltage_CAN(uint16_t readings[], int can_ids[])
maxv008 10:1079f8e52d65 387 {
maxv008 10:1079f8e52d65 388 CANMessage msg;
maxv008 10:1079f8e52d65 389 int can_id;
maxv008 10:1079f8e52d65 390 int offset;
maxv008 10:1079f8e52d65 391 int first_index;
maxv008 10:1079f8e52d65 392 int second_index;
maxv008 10:1079f8e52d65 393
maxv008 10:1079f8e52d65 394 if(can.read(msg))
maxv008 10:1079f8e52d65 395 {
maxv008 10:1079f8e52d65 396 for(int i =0; i < 4; i++)
maxv008 10:1079f8e52d65 397 {
maxv008 10:1079f8e52d65 398 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 399 }
DasSidG 12:fa9b1a459e47 400 can_id = msg.id;
DasSidG 12:fa9b1a459e47 401 can_ids[0] = msg.id;
DasSidG 12:fa9b1a459e47 402
DasSidG 11:cf2db05cfa56 403 offset = can_id - 1536; //1536 = 0x600
maxv008 10:1079f8e52d65 404 first_index = (offset - 1)/4; //offset of 2,3,4 is CMU 1; 6,7,8, is CMU 2; etc.
DasSidG 11:cf2db05cfa56 405 second_index = ((offset - 1) % 4) - 1; //Makes it so 0,1,2 represent each voltage set //SID: subtracted 1 to make it work
DasSidG 12:fa9b1a459e47 406
DasSidG 12:fa9b1a459e47 407 return true;
maxv008 10:1079f8e52d65 408 }
maxv008 10:1079f8e52d65 409 else
DasSidG 12:fa9b1a459e47 410 return false;
maxv008 10:1079f8e52d65 411 }
maxv008 10:1079f8e52d65 412
maxv008 10:1079f8e52d65 413 void test_CAN_send()
maxv008 10:1079f8e52d65 414 {
maxv008 10:1079f8e52d65 415 CANMessage msg;
DasSidG 11:cf2db05cfa56 416 char value = 142;
maxv008 10:1079f8e52d65 417 msg = CANMessage(1, &value,1);
maxv008 10:1079f8e52d65 418 if(can.write(msg))
maxv008 10:1079f8e52d65 419 printf("Succesfully sent %d \r\n", value);
maxv008 10:1079f8e52d65 420 else
maxv008 10:1079f8e52d65 421 printf("Sending Failed \r\n");
maxv008 10:1079f8e52d65 422 }
maxv008 10:1079f8e52d65 423
maxv008 10:1079f8e52d65 424 void test_CAN_read()
maxv008 10:1079f8e52d65 425 {
maxv008 10:1079f8e52d65 426 CANMessage msg;
maxv008 10:1079f8e52d65 427 if(can.read(msg))
maxv008 10:1079f8e52d65 428 printf("Successfully recieved %d \r\n", msg.data[0]);
maxv008 10:1079f8e52d65 429 else
maxv008 10:1079f8e52d65 430 printf("Reading Failed \r\n");
maxv008 10:1079f8e52d65 431 }