Cell voltages fork (SoC)

Dependencies:   CUER_CAN CUER_DS1820 LTC2943 LTC6804 mbed PowerControl

Committer:
maxv008
Date:
Sat Jul 22 17:47:14 2017 +0000
Revision:
45:c288d7cbdb4a
Parent:
41:9183c5616281
Child:
46:ac7065d52d6e
Added current measurement via IVTA

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"
maxv008 25:1fe8a42f8a6d 11 #include "LTC2943.h"
DasSidG 41:9183c5616281 12 #include "PowerControl/PowerControl.h"
DasSidG 41:9183c5616281 13 #include "PowerControl/EthernetPowerControl.h"
lcockerton62 0:0a5f554d2a16 14
maxv008 45:c288d7cbdb4a 15 #define DEBUG 1
maxv008 45:c288d7cbdb4a 16 #define ACTIVE 0
maxv008 45:c288d7cbdb4a 17 #define INACTIVE 1
maxv008 45:c288d7cbdb4a 18 #define TRANSMIT_MODE 1 //Useful to allow testing CAN read on BCU. Leave as 1 for BMS (and CAN write) 0 for BCU read-mode
msharma97 9:82ba050a7e13 19
lcockerton62 0:0a5f554d2a16 20 using namespace CAN_IDs;
lcockerton62 0:0a5f554d2a16 21
lcockerton62 0:0a5f554d2a16 22 // Function definitions
lcockerton62 1:51477fe4851b 23 void transmit_data(BMU_data measurements,uint32_t status);
lcockerton62 1:51477fe4851b 24 void read_temperature_sensors(BMU_data &measurements);
lcockerton62 0:0a5f554d2a16 25 void update_SOC();
lcockerton62 0:0a5f554d2a16 26 void init();
maxv008 14:e0e88a009f4c 27 void interruptHandler();
maxv008 14:e0e88a009f4c 28 void CANDataSentCallback();
lcockerton62 1:51477fe4851b 29 void write_SOC_EEPROM(BMU_data &measurements,uint16_t start_address);
lcockerton62 1:51477fe4851b 30 uint16_t read_EEPROM_startup(BMU_data &measurements);
maxv008 32:5b82679b2e6f 31 void reset_EEPROM(float init_SOC, float init_SOC_Percent);
lcockerton62 1:51477fe4851b 32 uint32_t check_measurements(BMU_data &measurements);
maxv008 23:a1af4439c1fc 33 uint32_t take_measurements(BMU_data &measurements);
maxv008 14:e0e88a009f4c 34 void test_read_CAN_buffer();
DasSidG 12:fa9b1a459e47 35 bool test_read_voltage_CAN(uint16_t readings[], int can_ids[]);
maxv008 10:1079f8e52d65 36 void test_CAN_send();
maxv008 10:1079f8e52d65 37 void test_CAN_read();
maxv008 35:be07fef5db72 38 bool check_EEPROM_PEC(char start_address_array[], char SOC_out[]);
maxv008 45:c288d7cbdb4a 39 int ivta_init(void);
maxv008 45:c288d7cbdb4a 40 int ivta_transfer(int * txrx);
maxv008 45:c288d7cbdb4a 41 int ivta_get_current(int measurements[]);
maxv008 45:c288d7cbdb4a 42 //IVTA stuff (sorry for the mess)
maxv008 45:c288d7cbdb4a 43 DigitalOut IVTA_SS(p11);
maxv008 45:c288d7cbdb4a 44 Serial pc(USBTX, USBRX);
maxv008 45:c288d7cbdb4a 45 SPI spi_ivta(p5, p6, p7); // mosi, miso, sclk
lcockerton62 0:0a5f554d2a16 46
lcockerton62 0:0a5f554d2a16 47 CAN can(CAN_READ_PIN, CAN_WRITE_PIN); //Create a CAN object to handle CAN comms
maxv008 14:e0e88a009f4c 48 CANMessage buffer[CAN_BUFFER_SIZE]; //CAN receive buffer
maxv008 14:e0e88a009f4c 49 bool safe_to_write[CAN_BUFFER_SIZE]; //Semaphore bit indicating that it's safe to write to the software buffer
maxv008 14:e0e88a009f4c 50 bool CAN_data_sent = false;
maxv008 14:e0e88a009f4c 51
maxv008 20:a1a1bfc938da 52 //Global array to store most recently obtained voltage and temp measurement:
maxv008 17:94dd9a0d3870 53 CMU_voltage voltage_readings[NO_CMUS];
maxv008 20:a1a1bfc938da 54 individual_temperature templist[NO_TEMPERATURE_SENSORS];
maxv008 28:f1f882bd1653 55 uint32_t status;
DasSidG 38:b1f5bfe38d70 56 int temperature_counter = TEMPERATURE_MEASUREMENT_FREQ;
DasSidG 38:b1f5bfe38d70 57
maxv008 17:94dd9a0d3870 58
DasSidG 4:9050c5d6925e 59 uint16_t eeprom_start_address; //the initial address where we store/read SoC values
lcockerton62 0:0a5f554d2a16 60
lcockerton62 1:51477fe4851b 61 Timeout loop_delay;
lcockerton62 1:51477fe4851b 62 bool delay_finished = false;
lcockerton62 2:94716229ecc3 63
DasSidG 39:34be1b8f46be 64 void loop_delay_callback(void) {
DasSidG 39:34be1b8f46be 65 delay_finished = true;
DasSidG 39:34be1b8f46be 66 }
DasSidG 39:34be1b8f46be 67
maxv008 28:f1f882bd1653 68 //The following is to initialize reading tests, can be removed when needed
maxv008 28:f1f882bd1653 69 float packSOC;
maxv008 28:f1f882bd1653 70 float packSOCPercentage;
maxv008 28:f1f882bd1653 71 pack_voltage_extremes minVolt;
maxv008 28:f1f882bd1653 72 pack_voltage_extremes maxVolt;
maxv008 28:f1f882bd1653 73 pack_temperature_extremes minTemp;
maxv008 28:f1f882bd1653 74 pack_temperature_extremes maxTemp;
maxv008 31:888b2602aab2 75 float batteryCurrent; uint32_t batteryVoltage;
maxv008 14:e0e88a009f4c 76
lcockerton62 0:0a5f554d2a16 77 int main()
DasSidG 11:cf2db05cfa56 78 {
lcockerton62 1:51477fe4851b 79 BMU_data measurements;
lcockerton62 1:51477fe4851b 80 uint16_t current_EEPROM_address;
DasSidG 12:fa9b1a459e47 81 uint16_t volt_readings[36];
DasSidG 12:fa9b1a459e47 82 int can_ids[9];
maxv008 10:1079f8e52d65 83
lcockerton62 0:0a5f554d2a16 84 init();
maxv008 10:1079f8e52d65 85
maxv008 35:be07fef5db72 86 //current_EEPROM_address = 0x0040; //reset has no way of setting the current address for rest of code.
maxv008 35:be07fef5db72 87 //reset_EEPROM(1,100); //Used to completely initialize EEPROM as if it has never been touched
maxv008 35:be07fef5db72 88 current_EEPROM_address = read_EEPROM_startup(measurements); // Read from the eeprom at startup to fill in the values of SoC
DasSidG 36:1b23c0692f54 89 if (DEBUG) printf("Current EEPROM Address %d \r\n", current_EEPROM_address);
DasSidG 36:1b23c0692f54 90 if (DEBUG) printf("SOC is %f and SOC Percentage is %f \r\n", measurements.SOC, measurements.percentage_SOC);
maxv008 35:be07fef5db72 91 ltc2943.accumulatedCharge(measurements.percentage_SOC); // Initialise the LTC2943 with the current state of charge
DasSidG 4:9050c5d6925e 92
lcockerton62 1:51477fe4851b 93 while (true) {
DasSidG 11:cf2db05cfa56 94
maxv008 35:be07fef5db72 95 Timer t;
maxv008 35:be07fef5db72 96 t.start();
maxv008 35:be07fef5db72 97
maxv008 45:c288d7cbdb4a 98 if(TRANSMIT_MODE) status = take_measurements(measurements);
maxv008 35:be07fef5db72 99 // Dont want to read the temperature sensors during each iteration of the loop
lcockerton62 0:0a5f554d2a16 100
lcockerton62 1:51477fe4851b 101 //Store data in the eeprom
maxv008 45:c288d7cbdb4a 102 if(TRANSMIT_MODE) write_SOC_EEPROM(measurements, current_EEPROM_address);
maxv008 35:be07fef5db72 103
lcockerton62 5:793afeef45dc 104 // CAN bus
maxv008 14:e0e88a009f4c 105 CAN_data_sent = false;//Currently does nothing, adding this line in more places then using
maxv008 14:e0e88a009f4c 106 //while(!CAN_data_sent); in order to ensure sending completes
maxv008 45:c288d7cbdb4a 107 if(TRANSMIT_MODE)
maxv008 45:c288d7cbdb4a 108 transmit_data(measurements,status);
maxv008 45:c288d7cbdb4a 109 else
maxv008 45:c288d7cbdb4a 110 test_read_CAN_buffer();
DasSidG 11:cf2db05cfa56 111
DasSidG 39:34be1b8f46be 112
lcockerton62 0:0a5f554d2a16 113 // Conserve power - enter a low powered mode
lcockerton62 2:94716229ecc3 114 delay_finished = false;
lcockerton62 1:51477fe4851b 115 loop_delay.attach(loop_delay_callback, LOOP_DELAY_S);
DasSidG 40:0753cbb8bc6a 116 //while (!delay_finished) sleep();
DasSidG 39:34be1b8f46be 117
DasSidG 40:0753cbb8bc6a 118 wait(1);
DasSidG 39:34be1b8f46be 119 if (debug) printf("Loop time is %d \r\n", t.read_ms());
maxv008 10:1079f8e52d65 120 }
lcockerton62 0:0a5f554d2a16 121 }
lcockerton62 0:0a5f554d2a16 122
lcockerton62 1:51477fe4851b 123 void transmit_data(BMU_data measurements, uint32_t status)
lcockerton62 0:0a5f554d2a16 124 {
msharma97 9:82ba050a7e13 125 CANMessage msg;
lcockerton62 0:0a5f554d2a16 126 /*
lcockerton62 0:0a5f554d2a16 127 Place all of the collected data onto the CAN bus
lcockerton62 0:0a5f554d2a16 128 */
lcockerton62 5:793afeef45dc 129 // Send cell voltages
maxv008 13:7b42af989cd1 130 //voltages sent in sets of 4 + one cmu data set
msharma97 9:82ba050a7e13 131 int repeating_unit_length = NO_READINGS_PER_CMU /4 + 1;
maxv008 10:1079f8e52d65 132 for(uint16_t i= 0; i < NO_CMUS; i++) {
msharma97 9:82ba050a7e13 133 //input id is offset, data structure is info, voltage, voltage, ......
maxv008 10:1079f8e52d65 134 //This is a slightly modified version of the Tritium BMS datasheet, to add an extra voltage reading set.
maxv008 10:1079f8e52d65 135 msg = createVoltageTelemetry(repeating_unit_length*i+2, measurements.cell_voltages[i].voltages);
msharma97 9:82ba050a7e13 136 can.write(msg);
DasSidG 36:1b23c0692f54 137 if (DEBUG) printf("Voltage Message id: %d \r\n", msg.id);
maxv008 17:94dd9a0d3870 138 //+4 - 4 cell voltages sent per measurement, simple pointer arithmetic
maxv008 10:1079f8e52d65 139 msg = createVoltageTelemetry(repeating_unit_length*i+3, measurements.cell_voltages[i].voltages + 4);
msharma97 9:82ba050a7e13 140 can.write(msg);
DasSidG 36:1b23c0692f54 141 if (DEBUG) printf("Voltage Message id: %d \r\n", msg.id);
maxv008 10:1079f8e52d65 142 msg = createVoltageTelemetry(repeating_unit_length*i+4, measurements.cell_voltages[i].voltages + 8);
msharma97 9:82ba050a7e13 143 can.write(msg);
DasSidG 36:1b23c0692f54 144 if (DEBUG) printf("Voltage Message id: %d \r\n", msg.id);
lcockerton62 1:51477fe4851b 145 }
maxv008 13:7b42af989cd1 146
maxv008 13:7b42af989cd1 147 //Transmitting all of the individual probes:
DasSidG 38:b1f5bfe38d70 148
DasSidG 38:b1f5bfe38d70 149 if (temperature_counter == TEMPERATURE_MEASUREMENT_FREQ) {
DasSidG 38:b1f5bfe38d70 150 for(uint8_t i = 0; i < devices_found; i++)
DasSidG 38:b1f5bfe38d70 151 {
DasSidG 38:b1f5bfe38d70 152 individual_temperature tempreading = measurements.temperature_measurements[i];
DasSidG 38:b1f5bfe38d70 153 msg = createTemperatureTelemetry(i, &tempreading.ROMID[0], tempreading.measurement);
DasSidG 38:b1f5bfe38d70 154 individual_temperature testOut = decodeTemperatureTelemetry(msg);
DasSidG 38:b1f5bfe38d70 155 if (DEBUG) printf("Temperature reading sent (CAN ID = %d): (%f,%d) \r\n", msg.id, testOut.measurement, testOut.ID);
DasSidG 38:b1f5bfe38d70 156 if(can.write(msg));
DasSidG 38:b1f5bfe38d70 157 else
DasSidG 38:b1f5bfe38d70 158 if (DEBUG) printf("Sending Temperature Failed for some reason \r\n");
DasSidG 38:b1f5bfe38d70 159 }
maxv008 13:7b42af989cd1 160 }
lcockerton62 1:51477fe4851b 161
lcockerton62 1:51477fe4851b 162 // Create SOC CAN message
maxv008 23:a1af4439c1fc 163 msg = createPackSOC(measurements.SOC, measurements.percentage_SOC);
maxv008 23:a1af4439c1fc 164 can.write(msg);
DasSidG 36:1b23c0692f54 165 if (DEBUG) printf("SOC is %f and percentage SOC is %f and id is %d \r\n", measurements.SOC, measurements.percentage_SOC, msg.id);
maxv008 23:a1af4439c1fc 166
lcockerton62 1:51477fe4851b 167 // Min/max cell voltages
maxv008 23:a1af4439c1fc 168 msg = createCellVoltageMAXMIN(measurements.max_cell_voltage, measurements.min_cell_voltage);
maxv008 23:a1af4439c1fc 169 can.write(msg);
maxv008 23:a1af4439c1fc 170
maxv008 23:a1af4439c1fc 171 // Min/Max cell temperatures
maxv008 23:a1af4439c1fc 172 msg = createCellTemperatureMAXMIN(measurements.min_cell_temp, true);
maxv008 23:a1af4439c1fc 173 can.write(msg);
maxv008 23:a1af4439c1fc 174 msg = createCellTemperatureMAXMIN(measurements.max_cell_temp, false);
maxv008 23:a1af4439c1fc 175 can.write(msg);
maxv008 31:888b2602aab2 176 wait(0.1); //WAITS ABSOLUTELY NECESSARY! values may be changed. Limit to how fast msg can be sent
lcockerton62 2:94716229ecc3 177 // Battery voltage and current
maxv008 23:a1af4439c1fc 178 msg = createBatteryVI(measurements.battery_voltage,measurements.battery_current);
maxv008 31:888b2602aab2 179 can.write(msg);
DasSidG 36:1b23c0692f54 180 if (DEBUG) printf("Sent Battery voltage %d and current %f with id %d \r\n",decodeBatteryVoltage(msg),decodeBatteryCurrent(msg),msg.id);
maxv008 23:a1af4439c1fc 181
lcockerton62 1:51477fe4851b 182 //Extended battery pack status
maxv008 23:a1af4439c1fc 183 msg = createExtendedBatteryPackStatus(status);
maxv008 23:a1af4439c1fc 184 can.write(msg);
maxv008 31:888b2602aab2 185
DasSidG 36:1b23c0692f54 186 if (DEBUG) printf("Sent battery pack status with value %d \r\n", status);
DasSidG 36:1b23c0692f54 187
maxv008 31:888b2602aab2 188 msg = createBMSHeartbeat(0, 0);
maxv008 31:888b2602aab2 189 can.write(msg);
maxv008 45:c288d7cbdb4a 190
maxv008 45:c288d7cbdb4a 191 msg = createIVTACurrent(measurements.ivta_current);
maxv008 45:c288d7cbdb4a 192 can.write(msg);
lcockerton62 0:0a5f554d2a16 193 }
lcockerton62 0:0a5f554d2a16 194
maxv008 10:1079f8e52d65 195
lcockerton62 1:51477fe4851b 196 uint16_t read_EEPROM_startup(BMU_data &measurements)
lcockerton62 0:0a5f554d2a16 197 {
lcockerton62 1:51477fe4851b 198 /* The first page of the EEPROM, specifically the first 2 addresses store a
lcockerton62 1:51477fe4851b 199 pointer of the first memory location of measurement data. The EEPROM only has a finite number of
lcockerton62 1:51477fe4851b 200 read/write cycles which is why we aren't writing to the same location throughout
lcockerton62 1:51477fe4851b 201 */
lcockerton62 30:d90895e96226 202 uint16_t start_address1;
lcockerton62 30:d90895e96226 203 uint16_t start_address2;
maxv008 35:be07fef5db72 204 char start_address_array1[4];
maxv008 35:be07fef5db72 205 char start_address_array2[4];
lcockerton62 22:2df45c818786 206 char SOC_out[10]; // 4 bytes for the 2 floats one is SOC and the other % charge
lcockerton62 30:d90895e96226 207 bool is_first_read_true = 0;
lcockerton62 30:d90895e96226 208 bool is_second_read_true = 0;
maxv008 35:be07fef5db72 209
maxv008 35:be07fef5db72 210 union float2bytes { float f; char b[sizeof(float)]; };
maxv008 35:be07fef5db72 211 float2bytes SOC_union;
maxv008 35:be07fef5db72 212 float2bytes SOC_Percent_union;
lcockerton62 30:d90895e96226 213
lcockerton62 30:d90895e96226 214 // Get a pointer to the start address for the data stored in the eeprom
maxv008 35:be07fef5db72 215 i2c_page_read(0x0000, 4, start_address_array1);
DasSidG 36:1b23c0692f54 216 if (DEBUG) printf("\r\n\ Start address (%d,%d) \r\n \r\n", start_address_array1[0], start_address_array1[1]);
maxv008 35:be07fef5db72 217 i2c_page_read(0x0004, 4, start_address_array2);
lcockerton62 30:d90895e96226 218
maxv008 35:be07fef5db72 219 is_first_read_true = check_EEPROM_PEC(start_address_array1, SOC_out);
lcockerton62 30:d90895e96226 220
lcockerton62 30:d90895e96226 221 if(is_first_read_true){
maxv008 35:be07fef5db72 222 for ( int i=0; i < sizeof(float); i++ ) {
maxv008 35:be07fef5db72 223 SOC_union.b[i] = SOC_out[i];
maxv008 35:be07fef5db72 224 }
maxv008 35:be07fef5db72 225
maxv008 35:be07fef5db72 226 for ( int i=0; i < sizeof(float); i++ ) {
maxv008 35:be07fef5db72 227 SOC_Percent_union.b[i] = SOC_out[i + sizeof(float)];
maxv008 35:be07fef5db72 228 }
maxv008 35:be07fef5db72 229 measurements.SOC = SOC_union.f;
maxv008 35:be07fef5db72 230 measurements.percentage_SOC = SOC_Percent_union.f;
lcockerton62 30:d90895e96226 231 }
lcockerton62 30:d90895e96226 232 else{
maxv008 35:be07fef5db72 233 is_second_read_true = check_EEPROM_PEC(start_address_array2, SOC_out);
lcockerton62 30:d90895e96226 234
lcockerton62 30:d90895e96226 235 if(is_second_read_true){
maxv008 35:be07fef5db72 236 for ( int i=0; i < sizeof(float); i++ ) {
maxv008 35:be07fef5db72 237 SOC_union.b[i] = SOC_out[i];
maxv008 35:be07fef5db72 238 }
maxv008 35:be07fef5db72 239
maxv008 35:be07fef5db72 240 for ( int i=0; i < sizeof(float); i++ ) {
maxv008 35:be07fef5db72 241 SOC_Percent_union.b[i] = SOC_out[i + sizeof(float)];
maxv008 35:be07fef5db72 242 }
maxv008 35:be07fef5db72 243 measurements.SOC = SOC_union.f;
maxv008 35:be07fef5db72 244 measurements.percentage_SOC = SOC_Percent_union.f;
lcockerton62 30:d90895e96226 245 }
lcockerton62 30:d90895e96226 246 }
lcockerton62 30:d90895e96226 247
lcockerton62 30:d90895e96226 248 if(is_second_read_true || is_first_read_true){
lcockerton62 30:d90895e96226 249 // Select the next address to write to
maxv008 35:be07fef5db72 250 start_address1 = (start_address_array1[1] << 8) | (start_address_array1[0]);
maxv008 35:be07fef5db72 251 start_address2 = (start_address_array2[1] << 8) | (start_address_array2[0]);
maxv008 35:be07fef5db72 252 start_address1 += 0x0040;
maxv008 32:5b82679b2e6f 253 start_address2 += 0x0040; //Also each SOC is taking 0xA space, so 0x15 should be sufficient offset
maxv008 35:be07fef5db72 254 if(start_address2 > MAX_WRITE_ADDRESS) { //Check second start address since it is the larger value.
DasSidG 36:1b23c0692f54 255 if (DEBUG) printf("Resetting start_address \r\n");
lcockerton62 30:d90895e96226 256 start_address1 = START_WRITE_ADDRESS; // Loop to the start of the eeprom
lcockerton62 30:d90895e96226 257 start_address2 = START_WRITE_ADDRESS + SECOND_ADDRESS_OFFSET; // Write this data SECOND_ADDRESS_OFFSET memory locations later than the first set // (this was chosen since only 10 bytes are written to memory
lcockerton62 30:d90895e96226 258 }
maxv008 35:be07fef5db72 259 start_address_array1[0] = start_address1 & 0x00FF;
maxv008 35:be07fef5db72 260 start_address_array1[1] = start_address1 >> 8;
maxv008 35:be07fef5db72 261 start_address_array2[0] = start_address2 & 0x00FF;
maxv008 32:5b82679b2e6f 262 start_address_array2[1] = start_address2 >> 8;
maxv008 35:be07fef5db72 263 //PEC for new address
maxv008 35:be07fef5db72 264 uint16_t pec_address1 = pec15_calc(2, (uint8_t*)start_address_array1);
maxv008 35:be07fef5db72 265 uint16_t pec_address2 = pec15_calc(2, (uint8_t*)start_address_array2);
maxv008 35:be07fef5db72 266 start_address_array1[2] = (char) (pec_address1 >> 8);
maxv008 35:be07fef5db72 267 start_address_array1[3] = (char) (pec_address1);
maxv008 35:be07fef5db72 268 start_address_array2[2] = (char) (pec_address2 >> 8);
maxv008 35:be07fef5db72 269 start_address_array2[3] = (char) (pec_address2);
lcockerton62 30:d90895e96226 270
lcockerton62 30:d90895e96226 271 // Write the new location of the address to memory
maxv008 35:be07fef5db72 272 wait_ms(10);
maxv008 35:be07fef5db72 273 i2c_page_write(0x0000, 4, start_address_array1);
maxv008 33:44b241c7b2c1 274 wait_ms(10);
maxv008 35:be07fef5db72 275 i2c_page_write(0x0004, 4, start_address_array2);
lcockerton62 30:d90895e96226 276
maxv008 35:be07fef5db72 277 write_SOC_EEPROM(measurements, start_address1); //Initializes new memory location to avoid PEC if reset without taking measurements.
lcockerton62 30:d90895e96226 278 return start_address1;
lcockerton62 30:d90895e96226 279 }
lcockerton62 30:d90895e96226 280 else{
DasSidG 36:1b23c0692f54 281 if (DEBUG) printf("PEC error"); //@TODO an error flag should be raised since both values have failed
maxv008 32:5b82679b2e6f 282
lcockerton62 30:d90895e96226 283 }
maxv008 32:5b82679b2e6f 284 return -1; //Will end up as maximum integer, just indicating an error.
maxv008 32:5b82679b2e6f 285 }
maxv008 32:5b82679b2e6f 286
maxv008 32:5b82679b2e6f 287 void reset_EEPROM(float init_SOC, float init_SOC_Percent)
maxv008 32:5b82679b2e6f 288 {
maxv008 32:5b82679b2e6f 289 char start_address_array1[2]; //Purely for testing
DasSidG 34:65fd6a72106f 290 char start_address_array2[2]; //Purely for testing
maxv008 33:44b241c7b2c1 291 char test_float_array[10];
maxv008 32:5b82679b2e6f 292 //Very first addresses to use
maxv008 35:be07fef5db72 293 char first_address[4] = {0x40,0,0,0}; //Address 0x0040, PEC section left blank to start
maxv008 35:be07fef5db72 294 char second_address[4] = {first_address[0] + SECOND_ADDRESS_OFFSET,0,0,0};
maxv008 32:5b82679b2e6f 295 uint16_t address1 = (first_address[1] << 8) | first_address[0];
maxv008 32:5b82679b2e6f 296 uint16_t address2 = (second_address[1] << 8) | second_address[0];
maxv008 35:be07fef5db72 297
maxv008 35:be07fef5db72 298 //PEC stuff for the addresses
maxv008 35:be07fef5db72 299 uint16_t pec_address1 = pec15_calc(2, (uint8_t*)first_address);
maxv008 35:be07fef5db72 300 uint16_t pec_address2 = pec15_calc(2, (uint8_t*)second_address);
maxv008 35:be07fef5db72 301 first_address[2] = (char) (pec_address1 >> 8);
maxv008 35:be07fef5db72 302 first_address[3] = (char) (pec_address1);
maxv008 35:be07fef5db72 303 second_address[2] = (char) (pec_address2 >> 8);
maxv008 35:be07fef5db72 304 second_address[3] = (char) (pec_address2);
maxv008 32:5b82679b2e6f 305
maxv008 35:be07fef5db72 306 i2c_page_write(0x0000, 4, first_address);
maxv008 33:44b241c7b2c1 307 wait_ms(10);
maxv008 35:be07fef5db72 308 i2c_page_write(0x0004, 4, second_address); //This initializes addresses
maxv008 32:5b82679b2e6f 309 //Next segment is for putting initial SOC in:
maxv008 33:44b241c7b2c1 310 wait_ms(10);
maxv008 32:5b82679b2e6f 311
maxv008 32:5b82679b2e6f 312 char data_out[10];
maxv008 32:5b82679b2e6f 313 uint16_t data_pec;
DasSidG 34:65fd6a72106f 314
DasSidG 34:65fd6a72106f 315 union float2bytes { float f; char b[sizeof(float)]; };
DasSidG 34:65fd6a72106f 316
DasSidG 34:65fd6a72106f 317 float2bytes init_SOC_union;
DasSidG 34:65fd6a72106f 318 float2bytes init_SOC_Percent_union;
DasSidG 34:65fd6a72106f 319
DasSidG 34:65fd6a72106f 320 init_SOC_union.f = init_SOC;
DasSidG 34:65fd6a72106f 321 for ( int i=0; i < sizeof(float); i++ ) {
DasSidG 34:65fd6a72106f 322 data_out[i] = init_SOC_union.b[i];
DasSidG 34:65fd6a72106f 323 }
DasSidG 34:65fd6a72106f 324
DasSidG 34:65fd6a72106f 325 init_SOC_Percent_union.f = init_SOC_Percent;
DasSidG 34:65fd6a72106f 326 for ( int i=0; i < sizeof(float); i++ ) {
DasSidG 34:65fd6a72106f 327 data_out[i+sizeof(float)] = init_SOC_Percent_union.b[i];
DasSidG 34:65fd6a72106f 328 }
maxv008 32:5b82679b2e6f 329
maxv008 32:5b82679b2e6f 330 data_pec = pec15_calc(8, ((uint8_t*)data_out)); // Calculate the pec and then write it to memory
maxv008 32:5b82679b2e6f 331 data_out[8] = (char)(data_pec >> 8);
maxv008 32:5b82679b2e6f 332 data_out[9] = (char)(data_pec);
DasSidG 34:65fd6a72106f 333
maxv008 32:5b82679b2e6f 334 i2c_page_write(address1, 10,data_out);
maxv008 33:44b241c7b2c1 335 wait_ms(10);
maxv008 32:5b82679b2e6f 336 i2c_page_write(address2, 10,data_out);
DasSidG 34:65fd6a72106f 337 wait_ms(10);
maxv008 35:be07fef5db72 338 i2c_page_read(0x0000,4,start_address_array1);
DasSidG 34:65fd6a72106f 339 wait_ms(10);
maxv008 35:be07fef5db72 340 i2c_page_read(0x0004,4,start_address_array2);
DasSidG 36:1b23c0692f54 341 if (DEBUG) printf("Start address 1 is (%x,%x) \r\n \r\n", start_address_array1[0], start_address_array1[1]);
DasSidG 36:1b23c0692f54 342 if (DEBUG) printf("Start address 2 is (%x,%x) \r\n \r\n", start_address_array2[0], start_address_array2[1]);
maxv008 33:44b241c7b2c1 343 wait_ms(10);
DasSidG 34:65fd6a72106f 344 i2c_page_read(address1,10,test_float_array);
maxv008 35:be07fef5db72 345 /*for (int i = 0; i < 10; ++i) {
DasSidG 34:65fd6a72106f 346 printf("test_float array %d is %d \r\n", i, test_float_array[i]);
maxv008 35:be07fef5db72 347 }*/
DasSidG 34:65fd6a72106f 348
DasSidG 34:65fd6a72106f 349 float2bytes rec_init_SOC_union;
DasSidG 34:65fd6a72106f 350 float2bytes rec_init_SOC_Percentage_union;
DasSidG 34:65fd6a72106f 351
DasSidG 34:65fd6a72106f 352
DasSidG 34:65fd6a72106f 353 for ( int i=0; i < sizeof(float); i++ ) {
DasSidG 34:65fd6a72106f 354 rec_init_SOC_union.b[i] = test_float_array[i];
DasSidG 34:65fd6a72106f 355 }
DasSidG 34:65fd6a72106f 356 float rec_init_SOC = rec_init_SOC_union.f;
DasSidG 34:65fd6a72106f 357
DasSidG 34:65fd6a72106f 358 for ( int i=0; i < sizeof(float); i++ ) {
DasSidG 34:65fd6a72106f 359 rec_init_SOC_Percentage_union.b[i] = test_float_array[i+4];
DasSidG 34:65fd6a72106f 360 }
DasSidG 34:65fd6a72106f 361 float rec_init_SOC_Percentage = rec_init_SOC_Percentage_union.f;
DasSidG 34:65fd6a72106f 362
DasSidG 36:1b23c0692f54 363 if (DEBUG) printf("init SOC %f \r\n \r\n", rec_init_SOC);
DasSidG 36:1b23c0692f54 364 if (DEBUG) printf("percentage SOC %f \r\n \r\n", rec_init_SOC_Percentage);
lcockerton62 30:d90895e96226 365 }
lcockerton62 30:d90895e96226 366
maxv008 35:be07fef5db72 367 bool check_EEPROM_PEC(char start_address_array[], char SOC_out[]){
lcockerton62 30:d90895e96226 368 // Helper method to check the PEC, returns 0 if the pec is wrong and 1 if the pec is correct
maxv008 35:be07fef5db72 369 uint16_t adr_recieved_pec;
maxv008 35:be07fef5db72 370 uint16_t adr_data_pec;
lcockerton62 22:2df45c818786 371 uint16_t received_pec;
lcockerton62 22:2df45c818786 372 uint16_t data_pec;
lcockerton62 30:d90895e96226 373
maxv008 35:be07fef5db72 374 //Check the PEC of the address itself
maxv008 35:be07fef5db72 375 adr_recieved_pec = (uint16_t)(start_address_array[2] << 8) + (uint16_t)start_address_array[3];
maxv008 35:be07fef5db72 376 adr_data_pec = pec15_calc(2, (uint8_t*)start_address_array);
maxv008 35:be07fef5db72 377 if(adr_recieved_pec != adr_data_pec){
DasSidG 36:1b23c0692f54 378 if (DEBUG) printf("PEC Error in address \r\n");
maxv008 35:be07fef5db72 379 return 0; //If they are equal, continue on to checking the data
maxv008 35:be07fef5db72 380 }
maxv008 35:be07fef5db72 381
lcockerton62 1:51477fe4851b 382 // Read the data from this address
maxv008 35:be07fef5db72 383 uint16_t start_address = (start_address_array[1]<< 8) | start_address_array[0]; // mbed little endian follow this convention
lcockerton62 22:2df45c818786 384 i2c_page_read(start_address, 10,SOC_out); // Reading will aquire 2 floats and a PEC for the data
lcockerton62 0:0a5f554d2a16 385
lcockerton62 22:2df45c818786 386 // Convert the SOC_out values back into floats and deal with the pec
lcockerton62 22:2df45c818786 387 received_pec = (uint16_t)(SOC_out[8]<<8) + (uint16_t)SOC_out[9];
lcockerton62 22:2df45c818786 388 data_pec = pec15_calc(8, (uint8_t*)SOC_out);
lcockerton62 22:2df45c818786 389 if(received_pec != data_pec) {
lcockerton62 30:d90895e96226 390 return 0;
lcockerton62 22:2df45c818786 391 }
lcockerton62 30:d90895e96226 392 else
lcockerton62 30:d90895e96226 393 return 1;
lcockerton62 0:0a5f554d2a16 394 }
lcockerton62 0:0a5f554d2a16 395
maxv008 35:be07fef5db72 396 //Note, this function does not check PEC of address, assumes correctness!
lcockerton62 1:51477fe4851b 397 void write_SOC_EEPROM(BMU_data &measurements,uint16_t start_address)
lcockerton62 0:0a5f554d2a16 398 {
lcockerton62 22:2df45c818786 399 char data_out[10];
maxv008 35:be07fef5db72 400 //float *fp1,*fp2;
lcockerton62 22:2df45c818786 401 uint16_t data_pec;
maxv008 35:be07fef5db72 402 union float2bytes { float f; char b[sizeof(float)]; };
maxv008 35:be07fef5db72 403 float2bytes SOC_union;
maxv008 35:be07fef5db72 404 float2bytes SOC_Percent_union;
lcockerton62 0:0a5f554d2a16 405
maxv008 35:be07fef5db72 406
maxv008 35:be07fef5db72 407 SOC_union.f = measurements.SOC;
maxv008 35:be07fef5db72 408 for ( int i=0; i < sizeof(float); i++ ) {
maxv008 35:be07fef5db72 409 data_out[i] = SOC_union.b[i];
lcockerton62 1:51477fe4851b 410 }
maxv008 35:be07fef5db72 411
maxv008 35:be07fef5db72 412 SOC_Percent_union.f = measurements.percentage_SOC;
maxv008 35:be07fef5db72 413 for ( int i=0; i < sizeof(float); i++ ) {
maxv008 35:be07fef5db72 414 data_out[i+sizeof(float)] = SOC_Percent_union.b[i];
lcockerton62 1:51477fe4851b 415 }
maxv008 35:be07fef5db72 416
lcockerton62 22:2df45c818786 417 data_pec = pec15_calc(8, ((uint8_t*)data_out)); // Calculate the pec and then write it to memory
lcockerton62 22:2df45c818786 418 data_out[8] = (char)(data_pec >> 8);
lcockerton62 22:2df45c818786 419 data_out[9] = (char)(data_pec);
maxv008 35:be07fef5db72 420 wait_ms(10); //Just in case function calling it doesnt put a wait before hand
lcockerton62 30:d90895e96226 421 i2c_page_write(start_address, 10,data_out);
maxv008 35:be07fef5db72 422 wait_ms(10);
lcockerton62 30:d90895e96226 423 i2c_page_write((start_address+SECOND_ADDRESS_OFFSET), 10,data_out); // Write the data to the backup memory location, SECOND_ADDRESS_OFFSET memory locations later
lcockerton62 0:0a5f554d2a16 424 }
lcockerton62 0:0a5f554d2a16 425
maxv008 45:c288d7cbdb4a 426 int ivta_transfer(int * txrx)
maxv008 45:c288d7cbdb4a 427 {
maxv008 45:c288d7cbdb4a 428 char i;// j;
maxv008 45:c288d7cbdb4a 429 uint16_t crc;
maxv008 45:c288d7cbdb4a 430
maxv008 45:c288d7cbdb4a 431 //pc.printf("*** Into a transfer*** \r \n");
maxv008 45:c288d7cbdb4a 432
maxv008 45:c288d7cbdb4a 433 // Activate IVT-A and send packet.
maxv008 45:c288d7cbdb4a 434 IVTA_SS = ACTIVE;
maxv008 45:c288d7cbdb4a 435 //pc.printf("Data sent");
maxv008 45:c288d7cbdb4a 436 wait_us(3);
maxv008 45:c288d7cbdb4a 437 for(i = 0; i < 9; i++)
maxv008 45:c288d7cbdb4a 438 {
maxv008 45:c288d7cbdb4a 439 spi_ivta.write(txrx[i]);
maxv008 45:c288d7cbdb4a 440 wait_us(3);
maxv008 45:c288d7cbdb4a 441 }
maxv008 45:c288d7cbdb4a 442 wait_us(3);
maxv008 45:c288d7cbdb4a 443 IVTA_SS = INACTIVE;
maxv008 45:c288d7cbdb4a 444 /*
maxv008 45:c288d7cbdb4a 445 Note: we ignore the packet sent by the IVT-A
maxv008 45:c288d7cbdb4a 446 during this communication phase. Similarly,
maxv008 45:c288d7cbdb4a 447 we only receive data in the next phase. Essentially
maxv008 45:c288d7cbdb4a 448 we run the IVT-A in half-duplex mode.
maxv008 45:c288d7cbdb4a 449 */
maxv008 45:c288d7cbdb4a 450
maxv008 45:c288d7cbdb4a 451 wait_us(500); // Wait between packets
maxv008 45:c288d7cbdb4a 452 IVTA_SS = ACTIVE; // Activate IVT-A and receive packet.
maxv008 45:c288d7cbdb4a 453 wait_us(3);
maxv008 45:c288d7cbdb4a 454 for(i = 0; i < 9; i++)
maxv008 45:c288d7cbdb4a 455 {
maxv008 45:c288d7cbdb4a 456 txrx[i] = spi_ivta.write(0x00); // Write some dummy data(half duplex)
maxv008 45:c288d7cbdb4a 457 wait_us(3);
maxv008 45:c288d7cbdb4a 458 }
maxv008 45:c288d7cbdb4a 459 wait_us(3);
maxv008 45:c288d7cbdb4a 460 IVTA_SS = INACTIVE;
maxv008 45:c288d7cbdb4a 461
maxv008 45:c288d7cbdb4a 462 return 0;
maxv008 45:c288d7cbdb4a 463 }
maxv008 45:c288d7cbdb4a 464
maxv008 45:c288d7cbdb4a 465 /*
maxv008 45:c288d7cbdb4a 466 Initialise IVT-A.
maxv008 45:c288d7cbdb4a 467 Write configuration:
maxv008 45:c288d7cbdb4a 468 + Current in 300A range measured on channel A.
maxv008 45:c288d7cbdb4a 469 + Temperature measured on channel B (currently unused).
maxv008 45:c288d7cbdb4a 470 + Average data over 10 samples.
maxv008 45:c288d7cbdb4a 471 */
maxv008 45:c288d7cbdb4a 472 int ivta_init(void)
maxv008 45:c288d7cbdb4a 473 {
maxv008 45:c288d7cbdb4a 474 spi_ivta.format(8,0); // The format works for 0 or 1, most of testing done with 0
maxv008 45:c288d7cbdb4a 475 spi_ivta.frequency(500000);
maxv008 45:c288d7cbdb4a 476 /* The last 2 bytes of the package are the CRC check, upper 4 bits in the
maxv008 45:c288d7cbdb4a 477 command are the command code */
maxv008 45:c288d7cbdb4a 478 int transfer;
maxv008 45:c288d7cbdb4a 479 int packet[9] = {0x90,1,5,200,0,0,0,0x78,0xB8};
maxv008 45:c288d7cbdb4a 480 transfer = ivta_transfer(packet);
maxv008 45:c288d7cbdb4a 481 return transfer;
maxv008 45:c288d7cbdb4a 482 }
maxv008 45:c288d7cbdb4a 483
maxv008 45:c288d7cbdb4a 484 /*
maxv008 45:c288d7cbdb4a 485 Fetch current from IVT-A. Return error codes
maxv008 45:c288d7cbdb4a 486 as defined in ivta.h. Supplied current pointer
maxv008 45:c288d7cbdb4a 487 must point to 3 chars.
maxv008 45:c288d7cbdb4a 488 */
maxv008 45:c288d7cbdb4a 489 int ivta_get_current(int measurements[])
maxv008 45:c288d7cbdb4a 490 {
maxv008 45:c288d7cbdb4a 491 //printf("In get current \r \n");
maxv008 45:c288d7cbdb4a 492 //int r_val;
maxv008 45:c288d7cbdb4a 493 int packet[9] = {0x10,1,0,0,0,0,0,0x0B,0x10}; // Command code 1.
maxv008 45:c288d7cbdb4a 494 //printf("In get current \r \n");
maxv008 45:c288d7cbdb4a 495 int r_val = ivta_transfer(packet);
maxv008 45:c288d7cbdb4a 496
maxv008 45:c288d7cbdb4a 497 /* printf("Packet1 : %d \r", packet[1]);
maxv008 45:c288d7cbdb4a 498 printf("Packet1 : %d \r", packet[2]);
maxv008 45:c288d7cbdb4a 499 printf("Packet1 : %d \r", packet[3]);
maxv008 45:c288d7cbdb4a 500 printf("Packet1 : %d \r", packet[4]);*/
maxv008 45:c288d7cbdb4a 501
maxv008 45:c288d7cbdb4a 502 measurements[0] = packet[1]; // Must shift the index of the packet
maxv008 45:c288d7cbdb4a 503 measurements[1] = packet[2];
maxv008 45:c288d7cbdb4a 504 measurements[2] = packet[3];
maxv008 45:c288d7cbdb4a 505 measurements[3] = packet[4];
maxv008 45:c288d7cbdb4a 506 measurements[4] = packet[5];
maxv008 45:c288d7cbdb4a 507 measurements[5] = packet[6];
maxv008 45:c288d7cbdb4a 508
maxv008 45:c288d7cbdb4a 509 return 0;
maxv008 45:c288d7cbdb4a 510 }
maxv008 45:c288d7cbdb4a 511
maxv008 45:c288d7cbdb4a 512
lcockerton62 1:51477fe4851b 513 void read_temperature_sensors(BMU_data &measurements)
lcockerton62 0:0a5f554d2a16 514 {
lcockerton62 1:51477fe4851b 515 float min_temperature;
maxv008 23:a1af4439c1fc 516 char min_id[8];
lcockerton62 1:51477fe4851b 517 float max_temperature;
maxv008 23:a1af4439c1fc 518 char max_id[8];
DasSidG 21:d461d58e70fc 519 isotherm_12V_pin = 1;
lcockerton62 1:51477fe4851b 520 probe[0]->convert_temperature(DS1820::all_devices);
DasSidG 21:d461d58e70fc 521
lcockerton62 1:51477fe4851b 522 min_temperature = probe[0]->temperature('C');
maxv008 23:a1af4439c1fc 523 std::memcpy(min_id, probe[0]->ROM, sizeof(char)*8); //invalid shallow copy: min_id = probe[0]->ROM;
lcockerton62 1:51477fe4851b 524 max_temperature = min_temperature; // Initially set the max and min temperature equal
maxv008 23:a1af4439c1fc 525 std::memcpy(max_id, probe[0]->ROM, sizeof(char)*8);
DasSidG 16:b2ef68c9a4fd 526 for (int i=0; i<devices_found; i++) {
maxv008 14:e0e88a009f4c 527 for(int j = 0; j < 7; j++)
maxv008 14:e0e88a009f4c 528 measurements.temperature_measurements[i].ROMID[j] = probe[i]->ROM[j];
lcockerton62 1:51477fe4851b 529 measurements.temperature_measurements[i].measurement = probe[i] ->temperature('C');
maxv008 14:e0e88a009f4c 530
lcockerton62 1:51477fe4851b 531 if(measurements.temperature_measurements[i].measurement > max_temperature) {
lcockerton62 1:51477fe4851b 532 max_temperature = measurements.temperature_measurements[i].measurement;
maxv008 23:a1af4439c1fc 533 std::memcpy(max_id, measurements.temperature_measurements[i].ROMID, sizeof(char)*8);
lcockerton62 2:94716229ecc3 534 } else if (measurements.temperature_measurements[i].measurement < min_temperature) {
lcockerton62 1:51477fe4851b 535 min_temperature = measurements.temperature_measurements[i].measurement;
maxv008 23:a1af4439c1fc 536 std::memcpy(min_id, measurements.temperature_measurements[i].ROMID, sizeof(char)*8);
lcockerton62 1:51477fe4851b 537 }
DasSidG 12:fa9b1a459e47 538
maxv008 18:521ffdd724f3 539 //printf("Device %d temperature is %3.3f degrees Celcius.\r\n",i+1 ,probe[i]->temperature('C'));
lcockerton62 1:51477fe4851b 540 }
DasSidG 21:d461d58e70fc 541 isotherm_12V_pin = 0;
maxv008 13:7b42af989cd1 542 //There is also a CMU # component of this struct, currently unfilled, perhaps not needed at all.
lcockerton62 1:51477fe4851b 543 measurements.max_cell_temp.temperature = max_temperature;
maxv008 23:a1af4439c1fc 544 std::memcpy(measurements.max_cell_temp.ROMID, max_id, sizeof(char)*8);
lcockerton62 1:51477fe4851b 545 measurements.min_cell_temp.temperature = min_temperature;
maxv008 28:f1f882bd1653 546 std::memcpy(measurements.min_cell_temp.ROMID, min_id, sizeof(char)*8);
maxv008 28:f1f882bd1653 547 delete max_id;
maxv008 28:f1f882bd1653 548 delete min_id;
lcockerton62 0:0a5f554d2a16 549 }
lcockerton62 0:0a5f554d2a16 550
lcockerton62 0:0a5f554d2a16 551 void update_SOC()
lcockerton62 0:0a5f554d2a16 552 {
lcockerton62 1:51477fe4851b 553 // Update the SOC value
maxv008 25:1fe8a42f8a6d 554 ltc2943.readAll();
lcockerton62 0:0a5f554d2a16 555 }
lcockerton62 0:0a5f554d2a16 556
lcockerton62 0:0a5f554d2a16 557
lcockerton62 1:51477fe4851b 558 uint32_t check_measurements(BMU_data &measurements)
lcockerton62 1:51477fe4851b 559 {
lcockerton62 1:51477fe4851b 560 uint32_t status;
lcockerton62 2:94716229ecc3 561
lcockerton62 2:94716229ecc3 562 if(measurements.max_cell_voltage.voltage > MAX_CELL_VOLTAGE) {
lcockerton62 2:94716229ecc3 563 status = status | CELL_OVER_VOLTAGE;
lcockerton62 2:94716229ecc3 564 } else if (measurements.min_cell_voltage.voltage < MIN_CELL_VOLTAGE) {
lcockerton62 1:51477fe4851b 565 status = status | CELL_UNDER_VOLTAGE;
lcockerton62 2:94716229ecc3 566 } else if (measurements.max_cell_temp.temperature > MAX_CELL_TEMPERATURE) {
lcockerton62 1:51477fe4851b 567 status = status | CELL_OVER_TEMPERATURE;
lcockerton62 1:51477fe4851b 568 }
lcockerton62 2:94716229ecc3 569
lcockerton62 1:51477fe4851b 570 /*
lcockerton62 1:51477fe4851b 571 @TODO also include errors for:
lcockerton62 1:51477fe4851b 572 *untrusted measurement
lcockerton62 1:51477fe4851b 573 *CMU timeout
lcockerton62 1:51477fe4851b 574 *SOC not valid
lcockerton62 1:51477fe4851b 575 */
lcockerton62 1:51477fe4851b 576 return status;
lcockerton62 1:51477fe4851b 577 }
lcockerton62 1:51477fe4851b 578
maxv008 23:a1af4439c1fc 579 //Returns the status variable
maxv008 23:a1af4439c1fc 580 uint32_t take_measurements(BMU_data &measurements)
lcockerton62 1:51477fe4851b 581 {
maxv008 6:b567fcb604aa 582 uint16_t cellvoltages[NO_CMUS][12];
DasSidG 16:b2ef68c9a4fd 583 //Use LTC6804_acquireVoltage to fill this array, and then properly format
maxv008 6:b567fcb604aa 584 //it to be sent over CAN
maxv008 6:b567fcb604aa 585
maxv008 45:c288d7cbdb4a 586 //LTC6804_acquireVoltage(cellvoltages);
maxv008 23:a1af4439c1fc 587 pack_voltage_extremes min_voltage;
DasSidG 37:fae62a2773a1 588 pack_voltage_extremes max_voltage;
maxv008 23:a1af4439c1fc 589 min_voltage.voltage = 65535; //largest 16 bit unsigned int
maxv008 23:a1af4439c1fc 590 max_voltage.voltage = 0;
maxv008 23:a1af4439c1fc 591
DasSidG 37:fae62a2773a1 592 bool last_CMU = false;
maxv008 23:a1af4439c1fc 593 //Sets voltage readings as well as max/min voltage values.
maxv008 10:1079f8e52d65 594 for(int i=0; i<NO_CMUS; i++){
DasSidG 37:fae62a2773a1 595 if (i == (NO_CMUS - 1)) last_CMU = true;
maxv008 17:94dd9a0d3870 596 for(int j=0; j < NO_READINGS_PER_CMU; j++){
DasSidG 16:b2ef68c9a4fd 597 measurements.cell_voltages[i].voltages[j] = cellvoltages[i][j]/ 10; //To get units of mV
maxv008 17:94dd9a0d3870 598 measurements.cell_voltages[i].CMU_number = i;
DasSidG 37:fae62a2773a1 599
DasSidG 37:fae62a2773a1 600 if(!(last_CMU && j >(NO_READINGS_PER_CMU - (NUM_MISSING_CELLS + 1))))
DasSidG 37:fae62a2773a1 601 //the condition above is to account for the missing cells (not a complete set of 12) on the top CMU
maxv008 23:a1af4439c1fc 602 {
DasSidG 37:fae62a2773a1 603 if(measurements.cell_voltages[i].voltages[j] < min_voltage.voltage)
DasSidG 37:fae62a2773a1 604 {
DasSidG 37:fae62a2773a1 605 min_voltage.voltage = measurements.cell_voltages[i].voltages[j];
DasSidG 37:fae62a2773a1 606 min_voltage.CMU_number = i;
DasSidG 37:fae62a2773a1 607 min_voltage.cell_number = j;
DasSidG 37:fae62a2773a1 608 }
DasSidG 37:fae62a2773a1 609 else if(measurements.cell_voltages[i].voltages[j] > max_voltage.voltage)
DasSidG 37:fae62a2773a1 610 {
DasSidG 37:fae62a2773a1 611 max_voltage.voltage = measurements.cell_voltages[i].voltages[j];
DasSidG 37:fae62a2773a1 612 max_voltage.CMU_number = i;
DasSidG 37:fae62a2773a1 613 max_voltage.cell_number = j;
DasSidG 37:fae62a2773a1 614 }
DasSidG 37:fae62a2773a1 615 }
maxv008 10:1079f8e52d65 616 }
maxv008 23:a1af4439c1fc 617 }
maxv008 23:a1af4439c1fc 618 measurements.max_cell_voltage = max_voltage;
DasSidG 36:1b23c0692f54 619 if (DEBUG) printf("Max Voltage is %d \r\n", max_voltage.voltage);
maxv008 23:a1af4439c1fc 620 measurements.min_cell_voltage = min_voltage;
DasSidG 36:1b23c0692f54 621 if (DEBUG) printf("Min Voltage is %d \r\n", min_voltage.voltage);
DasSidG 4:9050c5d6925e 622
DasSidG 38:b1f5bfe38d70 623 //Code to take all temperature measurements and add it to measurements struct.
DasSidG 38:b1f5bfe38d70 624 //Don't need to take temperature measurements every loop though
DasSidG 38:b1f5bfe38d70 625
DasSidG 38:b1f5bfe38d70 626 if (temperature_counter ==TEMPERATURE_MEASUREMENT_FREQ) {
DasSidG 38:b1f5bfe38d70 627 read_temperature_sensors(measurements);
DasSidG 38:b1f5bfe38d70 628 temperature_counter = 0;
DasSidG 38:b1f5bfe38d70 629 }
DasSidG 38:b1f5bfe38d70 630 temperature_counter++;
DasSidG 38:b1f5bfe38d70 631
maxv008 23:a1af4439c1fc 632 // Update the SOC and take relevant measurements
maxv008 23:a1af4439c1fc 633 update_SOC();
maxv008 31:888b2602aab2 634 measurements.battery_voltage = 0;
maxv008 31:888b2602aab2 635 for(int i = 0; i < NO_CMUS; i++)
maxv008 31:888b2602aab2 636 {
maxv008 31:888b2602aab2 637 for(int j = 0; j < NO_READINGS_PER_CMU; j++)
maxv008 31:888b2602aab2 638 {
maxv008 31:888b2602aab2 639 measurements.battery_voltage += measurements.cell_voltages[i].voltages[j];
maxv008 31:888b2602aab2 640 }
maxv008 31:888b2602aab2 641 }
maxv008 31:888b2602aab2 642 measurements.battery_current =ltc2943.current() * 1000; //*1000 to convert to mA
DasSidG 4:9050c5d6925e 643 measurements.percentage_SOC = ltc2943.accumulatedCharge();
DasSidG 4:9050c5d6925e 644 measurements.SOC = (measurements.percentage_SOC /100) * BATTERY_CAPACITY;
maxv008 23:a1af4439c1fc 645
maxv008 45:c288d7cbdb4a 646 //The following takes IVT-A measurement
maxv008 45:c288d7cbdb4a 647 IVTA_SS = 1;
maxv008 45:c288d7cbdb4a 648 int ivta_measurements[6];
maxv008 45:c288d7cbdb4a 649 uint32_t unsigned_current;
maxv008 45:c288d7cbdb4a 650 ivta_init();
maxv008 45:c288d7cbdb4a 651
maxv008 45:c288d7cbdb4a 652 ivta_get_current(ivta_measurements);
maxv008 45:c288d7cbdb4a 653 unsigned_current = ((uint32_t)ivta_measurements[0]);
maxv008 45:c288d7cbdb4a 654 unsigned_current = unsigned_current | (((uint32_t)ivta_measurements[1]<<8));
maxv008 45:c288d7cbdb4a 655 unsigned_current = unsigned_current | (((uint32_t)ivta_measurements[2]<<16));
maxv008 45:c288d7cbdb4a 656
maxv008 45:c288d7cbdb4a 657 if(ivta_measurements[2] & 0x80){ //For some reason converting using 2's complement ¯\_(ツ)_/¯
maxv008 45:c288d7cbdb4a 658 uint32_t convertedData = ((~unsigned_current) + 1) & 0x00FFFFFF;
maxv008 45:c288d7cbdb4a 659 int32_t convertedData2 = (~convertedData)+1;
maxv008 45:c288d7cbdb4a 660 measurements.ivta_current = convertedData2;
maxv008 45:c288d7cbdb4a 661 if(DEBUG) printf("*** Current measurement value: %d *** \r \n",convertedData2);
maxv008 45:c288d7cbdb4a 662 }
maxv008 45:c288d7cbdb4a 663 else{
maxv008 45:c288d7cbdb4a 664 measurements.ivta_current = unsigned_current;
maxv008 45:c288d7cbdb4a 665 if(DEBUG) pc.printf("*** Current measurement value: %d *** \r \n", (int32_t)unsigned_current);
maxv008 45:c288d7cbdb4a 666 }
maxv008 23:a1af4439c1fc 667 // Check data for errors
maxv008 23:a1af4439c1fc 668 return check_measurements(measurements);
lcockerton62 1:51477fe4851b 669 }
lcockerton62 1:51477fe4851b 670
lcockerton62 0:0a5f554d2a16 671 void init()
lcockerton62 0:0a5f554d2a16 672 {
DasSidG 41:9183c5616281 673 PHY_PowerDown();
DasSidG 41:9183c5616281 674
maxv008 45:c288d7cbdb4a 675 if(TRANSMIT_MODE)
maxv008 45:c288d7cbdb4a 676 {
maxv008 45:c288d7cbdb4a 677 temperature_init(); // Initialise the temperature sensors
maxv008 45:c288d7cbdb4a 678 LTC2943_initialise(); //Initialises the fixed parameters of the LTC2943
maxv008 45:c288d7cbdb4a 679 LTC6804_init(MD_FAST, DCP_DISABLED, CELL_CH_ALL, AUX_CH_VREF2); //Initialises the LTC6804s
maxv008 31:888b2602aab2 680
maxv008 45:c288d7cbdb4a 681 ivta_init();
maxv008 45:c288d7cbdb4a 682 }
maxv008 14:e0e88a009f4c 683 for(int i=0; i<CAN_BUFFER_SIZE; i++)
maxv008 14:e0e88a009f4c 684 {
maxv008 14:e0e88a009f4c 685 buffer[i].id = BLANK_ID;
maxv008 14:e0e88a009f4c 686 safe_to_write[i]= true;
maxv008 14:e0e88a009f4c 687 }
maxv008 14:e0e88a009f4c 688
maxv008 14:e0e88a009f4c 689 //Initialise CAN stuff, attach CAN interrupt handlers
maxv008 14:e0e88a009f4c 690 can.frequency(CAN_BIT_RATE); //set transmission rate to agreed bit rate (ELEC-006)
maxv008 14:e0e88a009f4c 691 can.reset(); // (FUNC-018)
maxv008 14:e0e88a009f4c 692 can.attach(&interruptHandler, CAN::RxIrq); //receive interrupt handler
maxv008 14:e0e88a009f4c 693 can.attach(&CANDataSentCallback, CAN::TxIrq); //send interrupt handler
maxv008 17:94dd9a0d3870 694
maxv008 17:94dd9a0d3870 695 //Initialize voltage array
maxv008 17:94dd9a0d3870 696 for(int i = 0; i < NO_CMUS; i++)
maxv008 17:94dd9a0d3870 697 {
maxv008 17:94dd9a0d3870 698 for(int j = 0; j < NO_READINGS_PER_CMU; j++)
maxv008 17:94dd9a0d3870 699 {
maxv008 17:94dd9a0d3870 700 voltage_readings[i].voltages[j] = 0;
maxv008 17:94dd9a0d3870 701 }
maxv008 17:94dd9a0d3870 702 }
maxv008 20:a1a1bfc938da 703 //Initialize Temperature Array
maxv008 20:a1a1bfc938da 704 for(int i = 0; i < NO_TEMPERATURE_SENSORS; i++)
maxv008 20:a1a1bfc938da 705 {
maxv008 28:f1f882bd1653 706 templist[i].measurement = INFINITY;
maxv008 20:a1a1bfc938da 707 templist[i].ID = 0;
maxv008 20:a1a1bfc938da 708 }
maxv008 28:f1f882bd1653 709 //initialize stuff used in reading test:
maxv008 28:f1f882bd1653 710 packSOC = INFINITY;
maxv008 28:f1f882bd1653 711 packSOCPercentage = INFINITY;
maxv008 28:f1f882bd1653 712
maxv008 28:f1f882bd1653 713 minVolt.voltage = 0;
maxv008 28:f1f882bd1653 714 maxVolt.voltage = 0;
maxv008 28:f1f882bd1653 715
maxv008 28:f1f882bd1653 716 minTemp.temperature = 0; minTemp.ID = 0;
maxv008 28:f1f882bd1653 717 maxTemp.temperature = 0; maxTemp.ID = 0;
maxv008 31:888b2602aab2 718
maxv008 31:888b2602aab2 719 batteryCurrent = INFINITY; batteryVoltage = 0;
maxv008 14:e0e88a009f4c 720 }
maxv008 14:e0e88a009f4c 721
maxv008 14:e0e88a009f4c 722 void CANDataSentCallback(void) {
maxv008 14:e0e88a009f4c 723 CAN_data_sent = true;
lcockerton62 0:0a5f554d2a16 724 }
lcockerton62 0:0a5f554d2a16 725
maxv008 14:e0e88a009f4c 726 void interruptHandler()
maxv008 14:e0e88a009f4c 727 {
maxv008 14:e0e88a009f4c 728 CANMessage msg;
DasSidG 16:b2ef68c9a4fd 729 can.read(msg);
maxv008 14:e0e88a009f4c 730 for(int i=0; i<CAN_BUFFER_SIZE; i++) {
maxv008 14:e0e88a009f4c 731 if((buffer[i].id == msg.id || buffer[i].id==BLANK_ID) && safe_to_write[i]) {
maxv008 14:e0e88a009f4c 732 //("id %d added to buffer \r\n", msg.id);
maxv008 14:e0e88a009f4c 733 buffer[i] = msg;
maxv008 14:e0e88a009f4c 734 //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 735 return;
maxv008 14:e0e88a009f4c 736 }
maxv008 14:e0e88a009f4c 737 }
maxv008 14:e0e88a009f4c 738 }
maxv008 14:e0e88a009f4c 739
maxv008 14:e0e88a009f4c 740 void test_read_CAN_buffer()
maxv008 14:e0e88a009f4c 741 {
maxv008 14:e0e88a009f4c 742 //Import the data from the buffer into a non-volatile, more usable format
maxv008 14:e0e88a009f4c 743 CAN_Data can_data[CAN_BUFFER_SIZE]; //container for all of the raw data
maxv008 17:94dd9a0d3870 744 CANMessage msgArray[CAN_BUFFER_SIZE]; //Same as above but some functions take message as their parameter
maxv008 14:e0e88a009f4c 745 int received_CAN_IDs[CAN_BUFFER_SIZE]; //needed to keep track of which IDs we've received so far
maxv008 14:e0e88a009f4c 746 for (int i = 0; i<CAN_BUFFER_SIZE; ++i)
maxv008 14:e0e88a009f4c 747 {
maxv008 14:e0e88a009f4c 748 safe_to_write[i] = false;
maxv008 14:e0e88a009f4c 749 can_data[i].importCANData(buffer[i]);
maxv008 14:e0e88a009f4c 750 received_CAN_IDs[i] = buffer[i].id;
maxv008 17:94dd9a0d3870 751 msgArray[i] = buffer[i];
maxv008 14:e0e88a009f4c 752 safe_to_write[i] = true;
maxv008 31:888b2602aab2 753 //printf("Id recieved %d \r\n", buffer[i].id);
maxv008 14:e0e88a009f4c 754 }
maxv008 17:94dd9a0d3870 755
maxv008 23:a1af4439c1fc 756 //voltage and Temp and SOC readings:
maxv008 18:521ffdd724f3 757 for(int i = 0; i < CAN_BUFFER_SIZE; i++)
maxv008 18:521ffdd724f3 758 {
maxv008 18:521ffdd724f3 759 //voltage
maxv008 28:f1f882bd1653 760 if(decodeVoltageTelemetry(msgArray[i], voltage_readings))
ItsJustZi 29:44924d2b1293 761 continue;
maxv008 28:f1f882bd1653 762 //temperature
maxv008 20:a1a1bfc938da 763 if(msgArray[i].id >= 0x700)
maxv008 18:521ffdd724f3 764 {
maxv008 20:a1a1bfc938da 765 individual_temperature dataPoint = decodeTemperatureTelemetry(msgArray[i]);
maxv008 20:a1a1bfc938da 766 for(int j = 0; j < NO_TEMPERATURE_SENSORS; j++)
maxv008 20:a1a1bfc938da 767 {
maxv008 20:a1a1bfc938da 768 if(dataPoint.ID == templist[j].ID)
maxv008 20:a1a1bfc938da 769 {
maxv008 20:a1a1bfc938da 770 templist[j] = dataPoint;
maxv008 20:a1a1bfc938da 771 break;
maxv008 20:a1a1bfc938da 772 }
maxv008 20:a1a1bfc938da 773 else if(templist[j].ID == 0)
maxv008 20:a1a1bfc938da 774 {
maxv008 20:a1a1bfc938da 775 templist[j] = dataPoint;
maxv008 20:a1a1bfc938da 776 break;
maxv008 20:a1a1bfc938da 777 }
maxv008 20:a1a1bfc938da 778 }
maxv008 20:a1a1bfc938da 779
maxv008 23:a1af4439c1fc 780 }
maxv008 23:a1af4439c1fc 781 //SOC
maxv008 23:a1af4439c1fc 782 if(msgArray[i].id == 0x6F4)
maxv008 23:a1af4439c1fc 783 {
maxv008 23:a1af4439c1fc 784 packSOC = decodePackSOC(msgArray[i]);
maxv008 23:a1af4439c1fc 785 packSOCPercentage = decodePackSOCPercentage(msgArray[i]);
maxv008 28:f1f882bd1653 786 }
maxv008 23:a1af4439c1fc 787
maxv008 23:a1af4439c1fc 788 if(msgArray[i].id == BMS_BASE_ID + MIN_TEMPERATURE)
maxv008 23:a1af4439c1fc 789 minTemp = decodeCellTemperatureMAXMIN(msgArray[i]);
maxv008 23:a1af4439c1fc 790 if(msgArray[i].id == BMS_BASE_ID + MAX_TEMPERATURE)
maxv008 23:a1af4439c1fc 791 maxTemp = decodeCellTemperatureMAXMIN(msgArray[i]);
maxv008 28:f1f882bd1653 792
maxv008 23:a1af4439c1fc 793 if(msgArray[i].id == BMS_BASE_ID + MAX_MIN_VOLTAGE)
maxv008 23:a1af4439c1fc 794 {
maxv008 23:a1af4439c1fc 795 decodeCellVoltageMAXMIN(msgArray[i], minVolt, maxVolt);
maxv008 23:a1af4439c1fc 796 }
maxv008 23:a1af4439c1fc 797
maxv008 31:888b2602aab2 798 if(msgArray[i].id == BMS_BASE_ID + BATTERY_VI_ID)
maxv008 31:888b2602aab2 799 {
maxv008 31:888b2602aab2 800 batteryVoltage = decodeBatteryVoltage(msgArray[i]);
maxv008 31:888b2602aab2 801 batteryCurrent = decodeBatteryCurrent(msgArray[i]);
maxv008 31:888b2602aab2 802 }
maxv008 31:888b2602aab2 803
maxv008 23:a1af4439c1fc 804 if(msgArray[i].id == BMS_BASE_ID + BATTERY_STATUS_ID)
maxv008 28:f1f882bd1653 805 status = decodeExtendedBatteryPackStatus(msgArray[i]);
maxv008 31:888b2602aab2 806
maxv008 31:888b2602aab2 807 if(msgArray[i].id == BMS_BASE_ID)
DasSidG 36:1b23c0692f54 808 if (DEBUG) printf("BMS Heartbeat Recieved \r\n");
maxv008 45:c288d7cbdb4a 809
maxv008 45:c288d7cbdb4a 810 if(msgArray[i].id == BMS_BASE_ID + IVTA_ID)
maxv008 45:c288d7cbdb4a 811 if (DEBUG) printf("IVTA Current is %d \r\n", decodeIVTACurrent(msgArray[i]));
maxv008 28:f1f882bd1653 812 }
maxv008 18:521ffdd724f3 813 //Print obtained Readings:
maxv008 18:521ffdd724f3 814 for(int i = 0; i < NO_CMUS; i++)
maxv008 18:521ffdd724f3 815 for(int j = 0; j < 12; j++)
DasSidG 36:1b23c0692f54 816 if (DEBUG) printf("Voltage number %d for CMU %d is %d \r\n", j, i, voltage_readings[i].voltages[j]);
maxv008 17:94dd9a0d3870 817
maxv008 18:521ffdd724f3 818 for(int i = 0; i < NO_TEMPERATURE_SENSORS; i++)
DasSidG 36:1b23c0692f54 819 if (DEBUG) printf("Temperature of Sensor with ID %d is %f \r\n", templist[i].ID, templist[i].measurement);
maxv008 23:a1af4439c1fc 820
DasSidG 36:1b23c0692f54 821 if (DEBUG) printf("SOC is %f and SOC Percentage is %f \r\n", packSOC, packSOCPercentage);
maxv008 23:a1af4439c1fc 822
DasSidG 36:1b23c0692f54 823 if (DEBUG) printf("Battery Current is %f and Battery Voltage is %d \r\n", batteryCurrent, batteryVoltage);
maxv008 31:888b2602aab2 824
DasSidG 36:1b23c0692f54 825 if (DEBUG) printf("Voltage (Max,Min),(Max_CMU,Max_num) = (%d,%d),(%d,%d) \r\n", maxVolt.voltage, minVolt.voltage, maxVolt.CMU_number, maxVolt.cell_number);
maxv008 23:a1af4439c1fc 826
DasSidG 36:1b23c0692f54 827 if (DEBUG) printf("(Temperature, ID): Minimum = (%d,%d). Maximum = (%d,%d) \r\n",
maxv008 23:a1af4439c1fc 828 minTemp.temperature,minTemp.ID,maxTemp.temperature,maxTemp.ID);
maxv008 23:a1af4439c1fc 829
DasSidG 36:1b23c0692f54 830 if (DEBUG) printf("Status value is: %d \r\n", status);
maxv008 14:e0e88a009f4c 831 }
maxv008 23:a1af4439c1fc 832
DasSidG 12:fa9b1a459e47 833 bool test_read_voltage_CAN(uint16_t readings[], int can_ids[])
maxv008 10:1079f8e52d65 834 {
maxv008 10:1079f8e52d65 835 CANMessage msg;
maxv008 10:1079f8e52d65 836 int can_id;
maxv008 10:1079f8e52d65 837 int offset;
maxv008 10:1079f8e52d65 838 int first_index;
maxv008 10:1079f8e52d65 839 int second_index;
maxv008 10:1079f8e52d65 840
maxv008 10:1079f8e52d65 841 if(can.read(msg))
maxv008 10:1079f8e52d65 842 {
maxv008 10:1079f8e52d65 843 for(int i =0; i < 4; i++)
maxv008 10:1079f8e52d65 844 {
maxv008 10:1079f8e52d65 845 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 846 }
DasSidG 12:fa9b1a459e47 847 can_id = msg.id;
DasSidG 12:fa9b1a459e47 848 can_ids[0] = msg.id;
DasSidG 12:fa9b1a459e47 849
DasSidG 11:cf2db05cfa56 850 offset = can_id - 1536; //1536 = 0x600
maxv008 10:1079f8e52d65 851 first_index = (offset - 1)/4; //offset of 2,3,4 is CMU 1; 6,7,8, is CMU 2; etc.
DasSidG 11:cf2db05cfa56 852 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 853
DasSidG 12:fa9b1a459e47 854 return true;
maxv008 10:1079f8e52d65 855 }
maxv008 10:1079f8e52d65 856 else
DasSidG 12:fa9b1a459e47 857 return false;
maxv008 10:1079f8e52d65 858 }
maxv008 10:1079f8e52d65 859
maxv008 10:1079f8e52d65 860 void test_CAN_send()
maxv008 10:1079f8e52d65 861 {
maxv008 10:1079f8e52d65 862 CANMessage msg;
DasSidG 11:cf2db05cfa56 863 char value = 142;
maxv008 10:1079f8e52d65 864 msg = CANMessage(1, &value,1);
maxv008 10:1079f8e52d65 865 if(can.write(msg))
DasSidG 36:1b23c0692f54 866 if (DEBUG) printf("Succesfully sent %d \r\n", value);
maxv008 10:1079f8e52d65 867 else
DasSidG 36:1b23c0692f54 868 if (DEBUG) printf("Sending Failed \r\n");
maxv008 10:1079f8e52d65 869 }
maxv008 10:1079f8e52d65 870
maxv008 10:1079f8e52d65 871 void test_CAN_read()
maxv008 10:1079f8e52d65 872 {
maxv008 10:1079f8e52d65 873 CANMessage msg;
maxv008 10:1079f8e52d65 874 if(can.read(msg))
DasSidG 36:1b23c0692f54 875 if (DEBUG) printf("Successfully recieved %d \r\n", msg.data[0]);
maxv008 10:1079f8e52d65 876 else
DasSidG 36:1b23c0692f54 877 if (DEBUG) printf("Reading Failed \r\n");
maxv008 10:1079f8e52d65 878 }