Has base BMU code but sends dummy temperature and voltage readings to test CAN
Dependencies: CUER_CAN DS1820 LTC2943 LTC6804 mbed
Fork of BMS_BMUCore_Max by
main.cpp@7:d00f4433cea9, 2017-02-11 (annotated)
- Committer:
- maxv008
- Date:
- Sat Feb 11 14:57:22 2017 +0000
- Revision:
- 7:d00f4433cea9
- Parent:
- 6:b567fcb604aa
- Child:
- 9:82ba050a7e13
ffdf
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 | 7:d00f4433cea9 | 9 | #include "State_Of_Charge.h" |
maxv008 | 7:d00f4433cea9 | 10 | #include "SPI_I2C_Parser.h" |
lcockerton62 | 0:0a5f554d2a16 | 11 | |
lcockerton62 | 0:0a5f554d2a16 | 12 | using namespace CAN_IDs; |
lcockerton62 | 0:0a5f554d2a16 | 13 | |
lcockerton62 | 0:0a5f554d2a16 | 14 | // Function definitions |
lcockerton62 | 1:51477fe4851b | 15 | void transmit_data(BMU_data measurements,uint32_t status); |
lcockerton62 | 1:51477fe4851b | 16 | void read_temperature_sensors(BMU_data &measurements); |
lcockerton62 | 0:0a5f554d2a16 | 17 | void update_SOC(); |
lcockerton62 | 0:0a5f554d2a16 | 18 | void init(); |
lcockerton62 | 1:51477fe4851b | 19 | void write_SOC_EEPROM(BMU_data &measurements,uint16_t start_address); |
lcockerton62 | 1:51477fe4851b | 20 | uint16_t read_EEPROM_startup(BMU_data &measurements); |
lcockerton62 | 1:51477fe4851b | 21 | uint32_t check_measurements(BMU_data &measurements); |
lcockerton62 | 1:51477fe4851b | 22 | void take_measurements(BMU_data &measurements); |
lcockerton62 | 0:0a5f554d2a16 | 23 | |
lcockerton62 | 0:0a5f554d2a16 | 24 | CAN can(CAN_READ_PIN, CAN_WRITE_PIN); //Create a CAN object to handle CAN comms |
DasSidG | 4:9050c5d6925e | 25 | uint16_t eeprom_start_address; //the initial address where we store/read SoC values |
lcockerton62 | 0:0a5f554d2a16 | 26 | |
lcockerton62 | 1:51477fe4851b | 27 | Timeout loop_delay; |
lcockerton62 | 1:51477fe4851b | 28 | bool delay_finished = false; |
lcockerton62 | 2:94716229ecc3 | 29 | |
lcockerton62 | 2:94716229ecc3 | 30 | void loop_delay_callback(void) |
lcockerton62 | 2:94716229ecc3 | 31 | { |
lcockerton62 | 1:51477fe4851b | 32 | delay_finished = true; |
lcockerton62 | 1:51477fe4851b | 33 | } |
lcockerton62 | 1:51477fe4851b | 34 | |
lcockerton62 | 0:0a5f554d2a16 | 35 | int main() |
lcockerton62 | 0:0a5f554d2a16 | 36 | { |
lcockerton62 | 1:51477fe4851b | 37 | BMU_data measurements; |
lcockerton62 | 1:51477fe4851b | 38 | uint16_t current_EEPROM_address; |
lcockerton62 | 1:51477fe4851b | 39 | uint32_t status; |
lcockerton62 | 0:0a5f554d2a16 | 40 | int c = 0; |
lcockerton62 | 0:0a5f554d2a16 | 41 | init(); |
DasSidG | 4:9050c5d6925e | 42 | current_EEPROM_address = read_EEPROM_startup(measurements); // Read from the eeprom at startup to fill in the values of SoC |
DasSidG | 4:9050c5d6925e | 43 | ltc2943.accumulatedCharge(measurements.percentage_SOC); // Initialise the LTC2943 with the current state of charge |
DasSidG | 4:9050c5d6925e | 44 | |
lcockerton62 | 1:51477fe4851b | 45 | while (true) { |
lcockerton62 | 2:94716229ecc3 | 46 | |
lcockerton62 | 1:51477fe4851b | 47 | // Take measurements from the sensors |
lcockerton62 | 1:51477fe4851b | 48 | take_measurements(measurements); |
lcockerton62 | 0:0a5f554d2a16 | 49 | // Dont want to read the temperature sensors during each iteration of the loop |
lcockerton62 | 1:51477fe4851b | 50 | if (c == 0) { |
lcockerton62 | 1:51477fe4851b | 51 | read_temperature_sensors(measurements); |
lcockerton62 | 1:51477fe4851b | 52 | } else if(c >= 4) { |
lcockerton62 | 0:0a5f554d2a16 | 53 | c = -1; |
lcockerton62 | 0:0a5f554d2a16 | 54 | } |
lcockerton62 | 0:0a5f554d2a16 | 55 | c++; |
lcockerton62 | 0:0a5f554d2a16 | 56 | |
lcockerton62 | 1:51477fe4851b | 57 | // Check data for errors |
lcockerton62 | 1:51477fe4851b | 58 | status = check_measurements(measurements); |
lcockerton62 | 1:51477fe4851b | 59 | |
lcockerton62 | 0:0a5f554d2a16 | 60 | // Update the SOC |
lcockerton62 | 0:0a5f554d2a16 | 61 | update_SOC(); |
lcockerton62 | 0:0a5f554d2a16 | 62 | |
lcockerton62 | 1:51477fe4851b | 63 | //Store data in the eeprom |
lcockerton62 | 1:51477fe4851b | 64 | write_SOC_EEPROM(measurements, current_EEPROM_address); |
lcockerton62 | 0:0a5f554d2a16 | 65 | |
lcockerton62 | 5:793afeef45dc | 66 | // CAN bus |
lcockerton62 | 1:51477fe4851b | 67 | transmit_data(measurements,status); |
lcockerton62 | 0:0a5f554d2a16 | 68 | |
lcockerton62 | 0:0a5f554d2a16 | 69 | // Conserve power - enter a low powered mode |
lcockerton62 | 2:94716229ecc3 | 70 | delay_finished = false; |
lcockerton62 | 1:51477fe4851b | 71 | loop_delay.attach(loop_delay_callback, LOOP_DELAY_S); |
lcockerton62 | 1:51477fe4851b | 72 | while (!delay_finished) sleep(); |
lcockerton62 | 0:0a5f554d2a16 | 73 | } |
lcockerton62 | 0:0a5f554d2a16 | 74 | } |
lcockerton62 | 0:0a5f554d2a16 | 75 | |
lcockerton62 | 1:51477fe4851b | 76 | void transmit_data(BMU_data measurements, uint32_t status) |
lcockerton62 | 0:0a5f554d2a16 | 77 | { |
lcockerton62 | 0:0a5f554d2a16 | 78 | /* |
lcockerton62 | 0:0a5f554d2a16 | 79 | Place all of the collected data onto the CAN bus |
lcockerton62 | 0:0a5f554d2a16 | 80 | */ |
lcockerton62 | 5:793afeef45dc | 81 | // Send cell voltages |
lcockerton62 | 1:51477fe4851b | 82 | for(int i= 0; i < NO_CMUS; i++) { |
lcockerton62 | 1:51477fe4851b | 83 | createVoltageTelemetry(i + 2 , measurements.cell_voltages[i].first_cell_voltages); |
lcockerton62 | 1:51477fe4851b | 84 | createVoltageTelemetry(i + 3, measurements.cell_voltages[i].last_cell_voltages); |
lcockerton62 | 1:51477fe4851b | 85 | } |
lcockerton62 | 1:51477fe4851b | 86 | |
lcockerton62 | 1:51477fe4851b | 87 | // Create SOC CAN message |
lcockerton62 | 1:51477fe4851b | 88 | createPackSOC(measurements.SOC, measurements.percentage_SOC); |
lcockerton62 | 0:0a5f554d2a16 | 89 | |
lcockerton62 | 1:51477fe4851b | 90 | // Min/max cell voltages |
lcockerton62 | 1:51477fe4851b | 91 | createCellVoltageMAXMIN(measurements.max_cell_voltage, measurements.min_cell_voltage); |
lcockerton62 | 2:94716229ecc3 | 92 | |
lcockerton62 | 1:51477fe4851b | 93 | // Min/Max cell temperature |
lcockerton62 | 1:51477fe4851b | 94 | createCellTemperatureMAXMIN(measurements.min_cell_temp,measurements.max_cell_temp); |
lcockerton62 | 2:94716229ecc3 | 95 | |
lcockerton62 | 2:94716229ecc3 | 96 | // Battery voltage and current |
lcockerton62 | 5:793afeef45dc | 97 | // @TODO add the voltage |
lcockerton62 | 1:51477fe4851b | 98 | createBatteryVI(measurements.battery_voltage,measurements.battery_current); |
lcockerton62 | 2:94716229ecc3 | 99 | |
lcockerton62 | 1:51477fe4851b | 100 | //Extended battery pack status |
lcockerton62 | 1:51477fe4851b | 101 | createExtendedBatteryPackStatus(status); |
lcockerton62 | 2:94716229ecc3 | 102 | |
lcockerton62 | 0:0a5f554d2a16 | 103 | } |
lcockerton62 | 0:0a5f554d2a16 | 104 | |
lcockerton62 | 1:51477fe4851b | 105 | uint16_t read_EEPROM_startup(BMU_data &measurements) |
lcockerton62 | 0:0a5f554d2a16 | 106 | { |
lcockerton62 | 1:51477fe4851b | 107 | /* The first page of the EEPROM, specifically the first 2 addresses store a |
lcockerton62 | 1:51477fe4851b | 108 | pointer of the first memory location of measurement data. The EEPROM only has a finite number of |
lcockerton62 | 1:51477fe4851b | 109 | read/write cycles which is why we aren't writing to the same location throughout |
lcockerton62 | 1:51477fe4851b | 110 | */ |
lcockerton62 | 5:793afeef45dc | 111 | |
lcockerton62 | 1:51477fe4851b | 112 | uint16_t start_address; |
lcockerton62 | 1:51477fe4851b | 113 | char start_address_array[2]; |
lcockerton62 | 1:51477fe4851b | 114 | char SOC_out[8]; // 4 bytes for the 2 floats one is SOC and the other % charge |
lcockerton62 | 1:51477fe4851b | 115 | float *fp1,*fp2; // temporary storage for float conversion |
lcockerton62 | 1:51477fe4851b | 116 | |
lcockerton62 | 1:51477fe4851b | 117 | // Get a pointer to the start address for the data stored in the eeprom |
lcockerton62 | 1:51477fe4851b | 118 | i2c_page_read(0x0000,2,start_address_array); |
lcockerton62 | 1:51477fe4851b | 119 | |
lcockerton62 | 1:51477fe4851b | 120 | // Read the data from this address |
lcockerton62 | 1:51477fe4851b | 121 | start_address = (start_address_array[1]<< 8) | start_address_array[0]; // mbed little endian follow this convention |
lcockerton62 | 1:51477fe4851b | 122 | i2c_page_read(start_address, 8,SOC_out); |
lcockerton62 | 0:0a5f554d2a16 | 123 | |
lcockerton62 | 1:51477fe4851b | 124 | // Convert the SOC_out values back into floats |
lcockerton62 | 1:51477fe4851b | 125 | fp1 = (float*)(&SOC_out[0]); |
lcockerton62 | 1:51477fe4851b | 126 | fp2 = (float*)(&SOC_out[4]); |
lcockerton62 | 1:51477fe4851b | 127 | measurements.SOC = *fp1; |
lcockerton62 | 1:51477fe4851b | 128 | measurements.percentage_SOC = *fp2; |
lcockerton62 | 1:51477fe4851b | 129 | |
lcockerton62 | 1:51477fe4851b | 130 | // Select the next address to write to |
lcockerton62 | 1:51477fe4851b | 131 | start_address += 0x0040; |
lcockerton62 | 1:51477fe4851b | 132 | if(start_address > MAX_WRITE_ADDRESS) { |
lcockerton62 | 5:793afeef45dc | 133 | start_address = START_WRITE_ADDRESS; // Loop to the start of the eeprom |
lcockerton62 | 1:51477fe4851b | 134 | } |
lcockerton62 | 1:51477fe4851b | 135 | |
lcockerton62 | 5:793afeef45dc | 136 | /*@TODO need to include a CRC check for the address pointer for the scenario |
lcockerton62 | 5:793afeef45dc | 137 | when power is removed and we are writing to the eeprom*/ |
lcockerton62 | 1:51477fe4851b | 138 | // write the new address to location 0x0000 |
lcockerton62 | 1:51477fe4851b | 139 | start_address_array[0] = start_address | 0x00FF; |
lcockerton62 | 1:51477fe4851b | 140 | start_address_array[1] = start_address >> 8; |
lcockerton62 | 1:51477fe4851b | 141 | i2c_page_write(0x0000, 2, start_address_array); |
lcockerton62 | 1:51477fe4851b | 142 | |
lcockerton62 | 1:51477fe4851b | 143 | return start_address; |
lcockerton62 | 0:0a5f554d2a16 | 144 | } |
lcockerton62 | 0:0a5f554d2a16 | 145 | |
lcockerton62 | 1:51477fe4851b | 146 | void write_SOC_EEPROM(BMU_data &measurements,uint16_t start_address) |
lcockerton62 | 0:0a5f554d2a16 | 147 | { |
lcockerton62 | 1:51477fe4851b | 148 | char data_out[8]; |
lcockerton62 | 1:51477fe4851b | 149 | float *fp1,*fp2; |
lcockerton62 | 1:51477fe4851b | 150 | |
lcockerton62 | 1:51477fe4851b | 151 | fp1 = (float*)(&measurements.SOC); |
lcockerton62 | 1:51477fe4851b | 152 | fp2 = (float*)(&measurements.percentage_SOC); |
lcockerton62 | 0:0a5f554d2a16 | 153 | |
lcockerton62 | 1:51477fe4851b | 154 | for(int i = 0; i < 4; i++ ) { |
lcockerton62 | 1:51477fe4851b | 155 | data_out[i] = *fp1; |
lcockerton62 | 1:51477fe4851b | 156 | fp1++; |
lcockerton62 | 1:51477fe4851b | 157 | } |
lcockerton62 | 1:51477fe4851b | 158 | for(int j = 4; j < 7; j++ ) { |
lcockerton62 | 1:51477fe4851b | 159 | data_out[j] = *fp2; |
lcockerton62 | 1:51477fe4851b | 160 | fp2++; |
lcockerton62 | 1:51477fe4851b | 161 | } |
lcockerton62 | 1:51477fe4851b | 162 | i2c_page_write(start_address, 8,data_out); |
lcockerton62 | 0:0a5f554d2a16 | 163 | } |
lcockerton62 | 0:0a5f554d2a16 | 164 | |
lcockerton62 | 1:51477fe4851b | 165 | void read_temperature_sensors(BMU_data &measurements) |
lcockerton62 | 0:0a5f554d2a16 | 166 | { |
lcockerton62 | 1:51477fe4851b | 167 | float min_temperature; |
lcockerton62 | 1:51477fe4851b | 168 | float max_temperature; |
lcockerton62 | 1:51477fe4851b | 169 | |
lcockerton62 | 1:51477fe4851b | 170 | probe[0]->convert_temperature(DS1820::all_devices); |
lcockerton62 | 1:51477fe4851b | 171 | min_temperature = probe[0]->temperature('C'); |
lcockerton62 | 1:51477fe4851b | 172 | max_temperature = min_temperature; // Initially set the max and min temperature equal |
lcockerton62 | 1:51477fe4851b | 173 | for (int i=1; i<devices_found; i++) { |
lcockerton62 | 2:94716229ecc3 | 174 | |
lcockerton62 | 1:51477fe4851b | 175 | measurements.temperature_measurements[i].ID = i; |
lcockerton62 | 1:51477fe4851b | 176 | measurements.temperature_measurements[i].measurement = probe[i] ->temperature('C'); |
lcockerton62 | 2:94716229ecc3 | 177 | |
lcockerton62 | 1:51477fe4851b | 178 | if(measurements.temperature_measurements[i].measurement > max_temperature) { |
lcockerton62 | 1:51477fe4851b | 179 | max_temperature = measurements.temperature_measurements[i].measurement; |
lcockerton62 | 2:94716229ecc3 | 180 | } else if (measurements.temperature_measurements[i].measurement < min_temperature) { |
lcockerton62 | 1:51477fe4851b | 181 | min_temperature = measurements.temperature_measurements[i].measurement; |
lcockerton62 | 1:51477fe4851b | 182 | } |
lcockerton62 | 1:51477fe4851b | 183 | } |
lcockerton62 | 1:51477fe4851b | 184 | measurements.max_cell_temp.temperature = max_temperature; |
lcockerton62 | 1:51477fe4851b | 185 | measurements.min_cell_temp.temperature = min_temperature; |
lcockerton62 | 0:0a5f554d2a16 | 186 | } |
lcockerton62 | 0:0a5f554d2a16 | 187 | |
lcockerton62 | 0:0a5f554d2a16 | 188 | void update_SOC() |
lcockerton62 | 0:0a5f554d2a16 | 189 | { |
lcockerton62 | 1:51477fe4851b | 190 | // Update the SOC value |
lcockerton62 | 0:0a5f554d2a16 | 191 | } |
lcockerton62 | 0:0a5f554d2a16 | 192 | |
lcockerton62 | 0:0a5f554d2a16 | 193 | |
lcockerton62 | 1:51477fe4851b | 194 | uint32_t check_measurements(BMU_data &measurements) |
lcockerton62 | 1:51477fe4851b | 195 | { |
lcockerton62 | 1:51477fe4851b | 196 | uint32_t status; |
lcockerton62 | 2:94716229ecc3 | 197 | |
lcockerton62 | 2:94716229ecc3 | 198 | if(measurements.max_cell_voltage.voltage > MAX_CELL_VOLTAGE) { |
lcockerton62 | 2:94716229ecc3 | 199 | status = status | CELL_OVER_VOLTAGE; |
lcockerton62 | 2:94716229ecc3 | 200 | } else if (measurements.min_cell_voltage.voltage < MIN_CELL_VOLTAGE) { |
lcockerton62 | 1:51477fe4851b | 201 | status = status | CELL_UNDER_VOLTAGE; |
lcockerton62 | 2:94716229ecc3 | 202 | } else if (measurements.max_cell_temp.temperature > MAX_CELL_TEMPERATURE) { |
lcockerton62 | 1:51477fe4851b | 203 | status = status | CELL_OVER_TEMPERATURE; |
lcockerton62 | 1:51477fe4851b | 204 | } |
lcockerton62 | 2:94716229ecc3 | 205 | |
lcockerton62 | 1:51477fe4851b | 206 | /* |
lcockerton62 | 1:51477fe4851b | 207 | @TODO also include errors for: |
lcockerton62 | 1:51477fe4851b | 208 | *untrusted measurement |
lcockerton62 | 1:51477fe4851b | 209 | *CMU timeout |
lcockerton62 | 1:51477fe4851b | 210 | *SOC not valid |
lcockerton62 | 1:51477fe4851b | 211 | */ |
lcockerton62 | 1:51477fe4851b | 212 | return status; |
lcockerton62 | 1:51477fe4851b | 213 | } |
lcockerton62 | 1:51477fe4851b | 214 | |
lcockerton62 | 1:51477fe4851b | 215 | void take_measurements(BMU_data &measurements) |
lcockerton62 | 1:51477fe4851b | 216 | { |
maxv008 | 6:b567fcb604aa | 217 | uint16_t cellvoltages[NO_CMUS][12]; |
maxv008 | 6:b567fcb604aa | 218 | //TODO Use LTC6804_acquireVoltage to fill this array, and then properly format |
maxv008 | 6:b567fcb604aa | 219 | //it to be sent over CAN |
maxv008 | 6:b567fcb604aa | 220 | |
maxv008 | 7:d00f4433cea9 | 221 | LTC6804_acquireVoltage(cellvoltages); |
maxv008 | 7:d00f4433cea9 | 222 | |
maxv008 | 7:d00f4433cea9 | 223 | |
lcockerton62 | 2:94716229ecc3 | 224 | // Here collect all measured data from the sensors |
lcockerton62 | 1:51477fe4851b | 225 | /* |
lcockerton62 | 5:793afeef45dc | 226 | * TODO Cell voltages |
lcockerton62 | 1:51477fe4851b | 227 | */ |
DasSidG | 4:9050c5d6925e | 228 | |
DasSidG | 4:9050c5d6925e | 229 | //Current, SoC |
DasSidG | 4:9050c5d6925e | 230 | measurements.battery_current = (uint32_t) ltc2943.current()*1000; //*1000 to converet to mA |
DasSidG | 4:9050c5d6925e | 231 | measurements.percentage_SOC = ltc2943.accumulatedCharge(); |
DasSidG | 4:9050c5d6925e | 232 | measurements.SOC = (measurements.percentage_SOC /100) * BATTERY_CAPACITY; |
DasSidG | 4:9050c5d6925e | 233 | |
lcockerton62 | 1:51477fe4851b | 234 | } |
lcockerton62 | 1:51477fe4851b | 235 | |
lcockerton62 | 0:0a5f554d2a16 | 236 | void init() |
lcockerton62 | 0:0a5f554d2a16 | 237 | { |
lcockerton62 | 1:51477fe4851b | 238 | temperature_init(); // Initialise the temperature sensors |
DasSidG | 4:9050c5d6925e | 239 | LTC2943_initialise(); //Initialises the fixed parameters of the LTC2943 |
lcockerton62 | 0:0a5f554d2a16 | 240 | } |
lcockerton62 | 0:0a5f554d2a16 | 241 |