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