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