Cell voltages fork (SoC)

Dependencies:   CUER_CAN CUER_DS1820 LTC2943 LTC6804 mbed PowerControl

Committer:
DasSidG
Date:
Sun Jul 02 11:39:39 2017 +0000
Revision:
16:b2ef68c9a4fd
Parent:
15:e901aff1f5b3
Child:
17:94dd9a0d3870
Cleaned up Dummy data

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