Cell voltages fork (SoC)

Dependencies:   CUER_CAN CUER_DS1820 LTC2943 LTC6804 mbed PowerControl

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