Cell voltages fork (SoC)

Dependencies:   CUER_CAN CUER_DS1820 LTC2943 LTC6804 mbed PowerControl

Revision:
50:89f54b7a4c1b
Parent:
49:419985e0b2b1
Parent:
48:5c3f42c44036
Child:
51:95a55958904d
diff -r 419985e0b2b1 -r 89f54b7a4c1b main.cpp
--- a/main.cpp	Mon Jul 24 19:00:57 2017 +0000
+++ b/main.cpp	Mon Jul 24 19:19:53 2017 +0000
@@ -55,7 +55,7 @@
 int temperature_counter = TEMPERATURE_MEASUREMENT_FREQ;
 
 
-uint16_t eeprom_start_address; //the initial address where we store/read SoC values
+uint16_t eeprom_start_address; //the current address where we store/read SoC values
 
 Timeout loop_delay;
 bool delay_finished = false;
@@ -73,19 +73,18 @@
 pack_temperature_extremes maxTemp;
 float batteryCurrent; uint32_t batteryVoltage;
 
+BMU_data measurements; //Put as global variable so interrupt can see it, otherwise treated like it is local to main
 int main()
-{ 
-    BMU_data measurements;
-    uint16_t current_EEPROM_address;
+{   
     //uint16_t volt_readings[36];
     //int can_ids[9];
     
     init();
     
-    //current_EEPROM_address = 0x0040; //reset has no way of setting the current address for rest of code.
+    //eeprom_start_address = 0x0040; //reset has no way of setting the current address for rest of code.
     //reset_EEPROM(1,100); //Used to completely initialize EEPROM as if it has never been touched
-    current_EEPROM_address = read_EEPROM_startup(measurements); // Read from the eeprom at startup to fill in the values of SoC
-    if (DEBUG) printf("Current EEPROM Address %d \r\n", current_EEPROM_address);
+    eeprom_start_address = read_EEPROM_startup(measurements); // Read from the eeprom at startup to fill in the values of SoC
+    if (DEBUG) printf("Current EEPROM Address %d \r\n", eeprom_start_address);
     if (DEBUG) printf("SOC is %f and SOC Percentage is %f \r\n", measurements.SOC, measurements.percentage_SOC);
     ltc2943.accumulatedCharge(measurements.percentage_SOC); // Initialise the LTC2943 with the current state of charge
     
@@ -98,7 +97,7 @@
         // Dont want to read the temperature sensors during each iteration of the loop
 
         //Store data in the eeprom
-        if(TRANSMIT_MODE) write_SOC_EEPROM(measurements, current_EEPROM_address);
+        if(TRANSMIT_MODE) write_SOC_EEPROM(measurements, eeprom_start_address);
         
         // CAN bus
         CAN_data_sent = false;//Currently does nothing, adding this line in more places then using
@@ -125,7 +124,7 @@
     /*
     Place all of the collected data onto the CAN bus
     */
-    // Send cell voltages
+    //Send cell voltages
     //voltages sent in sets of 4 + one cmu data set
     int repeating_unit_length = NO_READINGS_PER_CMU /4 + 1;
     for(uint16_t i= 0; i < NO_CMUS; i++) {
@@ -154,8 +153,6 @@
             wait(0.01);
             individual_temperature tempreading = measurements.temperature_measurements[i];
             msg = createTemperatureTelemetry(i, &tempreading.ROMID[0], tempreading.measurement);
-            individual_temperature testOut = decodeTemperatureTelemetry(msg);
-            if (DEBUG) printf("Temperature reading sent (CAN ID = %d): (%f,%d) \r\n", msg.id, testOut.measurement, testOut.ID); 
             if(can.write(msg));
             else
                 if (DEBUG) printf("Sending Temperature Failed for some reason \r\n");
@@ -311,6 +308,7 @@
     second_address[2] = (char) (pec_address2 >> 8);
     second_address[3] = (char) (pec_address2);
     
+    wait_ms(10);
     i2c_page_write(0x0000, 4, first_address);
     wait_ms(10);
     i2c_page_write(0x0004, 4, second_address);   //This initializes addresses
@@ -349,6 +347,7 @@
     if (DEBUG) printf("Start address 1 is  (%x,%x) \r\n \r\n", start_address_array1[0], start_address_array1[1]);
     if (DEBUG) printf("Start address 2 is  (%x,%x) \r\n \r\n", start_address_array2[0], start_address_array2[1]);
     wait_ms(10);
+    //TODO: Figure out of this can be removed, unecessary reading for testing purposes. (Hide behind if(DEBUG)?)
     i2c_page_read(address1,10,test_float_array);
     /*for (int i = 0; i < 10; ++i) {
        printf("test_float array %d is %d \r\n", i, test_float_array[i]); 
@@ -743,6 +742,15 @@
            return;
         }
     }
+    if(TRANSMIT_MODE) //It may be better to do this outside of an interrupt, it is fairly long. Requires a global variable
+    {
+        if(msg.id == BMS_BASE_ID + EEPROM_RESET_ID)
+        {
+            reset_EEPROM(decodeEEPROMSOC(msg), decodeEEPROMSOCPercentage(msg));    
+            eeprom_start_address = read_EEPROM_startup(measurements); // Read newly written data back from EEPROM (can be done faster but this is more safe)
+            ltc2943.accumulatedCharge(measurements.percentage_SOC); // Initialise the LTC2943 with the current state of charge
+        }
+    }
 }
 
 void test_read_CAN_buffer()