CUER / Mbed 2 deprecated BMS_BMUCore_Max

Dependencies:   CUER_CAN CUER_DS1820 LTC2943 LTC6804 mbed PowerControl

Revision:
28:f1f882bd1653
Parent:
25:1fe8a42f8a6d
Child:
29:44924d2b1293
--- a/main.cpp	Sun Jul 09 14:07:23 2017 +0000
+++ b/main.cpp	Sun Jul 09 16:58:32 2017 +0000
@@ -37,12 +37,21 @@
 //Global array to store most recently obtained voltage and temp measurement:
 CMU_voltage voltage_readings[NO_CMUS];
 individual_temperature templist[NO_TEMPERATURE_SENSORS];
+uint32_t status;
+//LTC2943 ltc2943(i2c_sda, i2c_scl, alcc_pin, &dummyfunction, R_SENSE, BATTERY_CAPACITY);
 
 uint16_t eeprom_start_address; //the initial address where we store/read SoC values
 
 Timeout loop_delay;
 bool delay_finished = false;
 
+//The following is to initialize reading tests, can be removed when needed
+float packSOC;
+float packSOCPercentage;
+pack_voltage_extremes minVolt;
+pack_voltage_extremes maxVolt;
+pack_temperature_extremes minTemp;
+pack_temperature_extremes maxTemp;
 
 void loop_delay_callback(void)
 {
@@ -53,12 +62,9 @@
 { 
     BMU_data measurements;
     uint16_t current_EEPROM_address;
-    uint32_t status;
     uint16_t volt_readings[36];
     int can_ids[9];
     
-    LTC2943 ltc2943(i2c_sda, i2c_scl, alcc_pin, &dummyfunction, R_SENSE, BATTERY_CAPACITY);
-    
     init();
     
     //current_EEPROM_address = read_EEPROM_startup(measurements); // Read from the eeprom at startup to fill in the values of SoC
@@ -66,24 +72,18 @@
     
     while (true) {
         
-        status = take_measurements(measurements);
+        //status = take_measurements(measurements);
         /*// Dont want to read the temperature sensors during each iteration of the loop
-        if (c == 0) {
-            read_temperature_sensors(measurements);
-        } else if(c >= 4) {
-            c = -1;
-        }
-        c++;
 
         //Store data in the eeprom
         write_SOC_EEPROM(measurements, current_EEPROM_address);
         */
-
+        printf("Beginning of loop \r\n");
         // CAN bus
         CAN_data_sent = false;//Currently does nothing, adding this line in more places then using
         //while(!CAN_data_sent); in order to ensure sending completes
-        transmit_data(measurements,status);
-        //test_read_CAN_buffer();
+        //transmit_data(measurements,status);
+        test_read_CAN_buffer();
         
         /*
         // Conserve power - enter a low powered mode
@@ -91,8 +91,9 @@
         loop_delay.attach(loop_delay_callback, LOOP_DELAY_S);
         while (!delay_finished) sleep();
         */
-        
+        printf("BEFORE THE WAIT \r\n");
         wait(1);
+        printf("AFTER THE WAIT \r\n");
     } 
 }
 
@@ -135,7 +136,7 @@
     // Create SOC CAN message
     msg = createPackSOC(measurements.SOC, measurements.percentage_SOC);
     can.write(msg);
-    printf("SOC is %f and percentage SOC is %f \r\n", measurements.SOC, measurements.percentage_SOC);
+    printf("SOC is %f and percentage SOC is %f and id is %d \r\n", measurements.SOC, measurements.percentage_SOC, msg.id);
     
     // Min/max cell voltages
     msg = createCellVoltageMAXMIN(measurements.max_cell_voltage, measurements.min_cell_voltage);
@@ -266,7 +267,9 @@
     measurements.max_cell_temp.temperature = max_temperature;
     std::memcpy(measurements.max_cell_temp.ROMID, max_id, sizeof(char)*8);
     measurements.min_cell_temp.temperature = min_temperature;
-    std::memcpy(measurements.min_cell_temp.ROMID, max_id, sizeof(char)*8);
+    std::memcpy(measurements.min_cell_temp.ROMID, min_id, sizeof(char)*8);
+    delete max_id;
+    delete min_id;
 }
 
 void update_SOC()
@@ -337,8 +340,7 @@
 
     // Update the SOC and take relevant measurements
     update_SOC();
-    measurements.battery_current = (uint32_t) ltc2943.current()*1000; //*1000 to convert to mA
-    printf("Battery Current is %d \r\n", measurements.battery_current);
+    measurements.battery_current = (uint32_t) ltc2943.current() * 1000; //*1000 to convert to mA
     measurements.percentage_SOC = ltc2943.accumulatedCharge();
     measurements.SOC = (measurements.percentage_SOC /100) * BATTERY_CAPACITY;
     
@@ -349,10 +351,10 @@
 void init()
 {
     //Comment out measurement stuff with BCU testing
-    temperature_init(); // Initialise the temperature sensors
+    /*temperature_init(); // Initialise the temperature sensors
     LTC2943_initialise(); //Initialises the fixed parameters of the LTC2943
     LTC6804_init(MD_FAST, DCP_DISABLED, CELL_CH_ALL, AUX_CH_VREF2); //Initialises the LTC6804s
-    
+    */
     for(int i=0; i<CAN_BUFFER_SIZE; i++) 
     {
         buffer[i].id = BLANK_ID;
@@ -376,9 +378,18 @@
     //Initialize Temperature Array
     for(int i = 0; i < NO_TEMPERATURE_SENSORS; i++)
     {
-        templist[i].measurement = -INFINITY;
+        templist[i].measurement = INFINITY;
         templist[i].ID = 0;
     }
+    //initialize stuff used in reading test:
+    packSOC = INFINITY;
+    packSOCPercentage = INFINITY;
+    
+    minVolt.voltage = 0;
+    maxVolt.voltage = 0;
+    
+    minTemp.temperature = 0; minTemp.ID = 0;
+    maxTemp.temperature = 0; maxTemp.ID = 0;
 }
 
 void CANDataSentCallback(void) {
@@ -415,24 +426,13 @@
     }
     
     //voltage and Temp and SOC readings:
-    float packSOC = INFINITY;
-    float packSOCPercentage = INFINITY;
-    
-    pack_voltage_extremes minVolt;
-    pack_voltage_extremes maxVolt;
-    minVolt.voltage = 0;
-    maxVolt.voltage = 0;
-    
-    pack_temperature_extremes minTemp;
-    pack_temperature_extremes maxTemp;
-    minTemp.temperature = 0; minTemp.ID = 0;
-    maxTemp.temperature = 0; maxTemp.ID = 0;
     for(int i = 0; i < CAN_BUFFER_SIZE; i++)
     {
         //voltage
-        if(decodeVoltageTelemetry(msgArray[i], &voltage_readings[0]))
-            continue;
-        //temperature
+        if(decodeVoltageTelemetry(msgArray[i], voltage_readings))
+            printf("we made it inside the if \r\n");//continue;
+        //temperature 
+        printf("We made it past the if statement \r\n");
         if(msgArray[i].id >= 0x700)
         {
             individual_temperature dataPoint = decodeTemperatureTelemetry(msgArray[i]);
@@ -456,21 +456,21 @@
         {
             packSOC = decodePackSOC(msgArray[i]);
             packSOCPercentage = decodePackSOCPercentage(msgArray[i]);
-        }
+        } 
         
         if(msgArray[i].id == BMS_BASE_ID + MIN_TEMPERATURE)
             minTemp = decodeCellTemperatureMAXMIN(msgArray[i]);
         if(msgArray[i].id == BMS_BASE_ID + MAX_TEMPERATURE)
             maxTemp = decodeCellTemperatureMAXMIN(msgArray[i]);
-            
+        
         if(msgArray[i].id == BMS_BASE_ID + MAX_MIN_VOLTAGE)
         {
             decodeCellVoltageMAXMIN(msgArray[i], minVolt, maxVolt);
         }
         
         if(msgArray[i].id == BMS_BASE_ID + BATTERY_STATUS_ID)
-            status = decodeExtendedBatteryPackStatus(msgArray[i]);
-    }
+            status = decodeExtendedBatteryPackStatus(msgArray[i]); 
+    } 
     //Print obtained Readings:
     for(int i = 0; i < NO_CMUS; i++)
         for(int j = 0; j < 12; j++)
@@ -483,7 +483,7 @@
     
     printf("Voltage (Max,Min) = (%d,%d) \r\n", maxVolt.voltage, minVolt.voltage);
     
-    printf("(Temperature, ID): Maximum = (%d,%d). Minimum = (%d,%d) \r\n",
+    printf("(Temperature, ID): Minimum = (%d,%d). Maximum = (%d,%d) \r\n",
         minTemp.temperature,minTemp.ID,maxTemp.temperature,maxTemp.ID);  
         
     printf("Status value is: %d \r\n", status);