Cell voltages fork (SoC)

Dependencies:   CUER_CAN CUER_DS1820 LTC2943 LTC6804 mbed PowerControl

Revision:
51:95a55958904d
Parent:
50:89f54b7a4c1b
Child:
52:63e84c6a9cfd
--- a/main.cpp	Mon Jul 24 19:19:53 2017 +0000
+++ b/main.cpp	Mon Jul 24 20:49:37 2017 +0000
@@ -12,9 +12,8 @@
 #include "PowerControl/EthernetPowerControl.h"
 
 #define DEBUG 1
-#define ACTIVE 0
-#define INACTIVE 1
-#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
+#define TRANSMIT_MODE 0 //Useful to allow testing CAN read on BCU. Leave as 1 for BMS (and CAN write) 0 for BCU read-mode
+#define CAN_TIMEOUT_MS 100 
 
 using namespace CAN_IDs;
 
@@ -25,6 +24,7 @@
 void init();
 void interruptHandler();
 void CANDataSentCallback();
+bool can_send(CANMessage msg);
 void write_SOC_EEPROM(BMU_data &measurements,uint16_t start_address);
 uint16_t read_EEPROM_startup(BMU_data &measurements);
 void reset_EEPROM(float init_SOC, float init_SOC_Percent);
@@ -100,7 +100,6 @@
         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
         //while(!CAN_data_sent); in order to ensure sending completes
         if(TRANSMIT_MODE)
             transmit_data(measurements,status);
@@ -131,17 +130,14 @@
         //input id is offset, data structure is info, voltage, voltage, ......
         //This is a slightly modified version of the Tritium BMS datasheet, to add an extra voltage reading set.
         msg = createVoltageTelemetry(repeating_unit_length*i+2, measurements.cell_voltages[i].voltages); 
-        can.write(msg);
-        wait(0.01);
+        can_send(msg);
         if (DEBUG) printf("Voltage Message id: %d \r\n", msg.id);
         //+4 - 4 cell voltages sent per measurement, simple pointer arithmetic
         msg = createVoltageTelemetry(repeating_unit_length*i+3, measurements.cell_voltages[i].voltages + 4); 
-        can.write(msg);
-        wait(0.01);
+        can_send(msg);
         if (DEBUG) printf("Voltage Message id: %d \r\n", msg.id);
         msg = createVoltageTelemetry(repeating_unit_length*i+4, measurements.cell_voltages[i].voltages + 8); 
-        can.write(msg);
-        wait(0.01);
+        can_send(msg);
         if (DEBUG) printf("Voltage Message id: %d \r\n", msg.id);
     }
     
@@ -150,10 +146,10 @@
     if (temperature_counter == TEMPERATURE_MEASUREMENT_FREQ) {
         for(uint8_t i = 0; i < devices_found; i++)
         {
-            wait(0.01);
             individual_temperature tempreading = measurements.temperature_measurements[i];
             msg = createTemperatureTelemetry(i, &tempreading.ROMID[0], tempreading.measurement);
-            if(can.write(msg));
+            if(can_send(msg)) {
+            }
             else
                 if (DEBUG) printf("Sending Temperature Failed for some reason \r\n");
         }
@@ -161,40 +157,32 @@
 
     // Create SOC CAN message
     msg = createPackSOC(measurements.SOC, measurements.percentage_SOC);
-    can.write(msg);
-    wait(0.01);
+    can_send(msg);
     if (DEBUG) 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);
-    can.write(msg);
-    wait(0.01);
+    can_send(msg);
     
     // Min/Max cell temperatures
     msg = createCellTemperatureMAXMIN(measurements.min_cell_temp, true);
-    can.write(msg);
-    wait(0.01);
+    can_send(msg);
     msg = createCellTemperatureMAXMIN(measurements.max_cell_temp, false);
-    can.write(msg);
-    wait(0.01); //WAITS ABSOLUTELY NECESSARY! values may be changed. Limit to how fast msg can be sent
+    can_send(msg);
     // Battery voltage and current
     msg = createBatteryVI(measurements.battery_voltage,measurements.battery_current);
-    can.write(msg);
-    wait(0.01);
+    can_send(msg);
     if (DEBUG) printf("Sent Battery voltage %d and current %f with id %d \r\n",decodeBatteryVoltage(msg),decodeBatteryCurrent(msg),msg.id);
     
     //Extended battery pack status
     msg = createExtendedBatteryPackStatus(status);
-    can.write(msg);
-    wait(0.01);
+    can_send(msg);
     if (DEBUG) printf("Sent battery pack status with value %d \r\n", status);
     
     msg = createBMSHeartbeat(0, 0);
-    can.write(msg);
-    wait(0.01);
+    can_send(msg);
     msg = createIVTACurrent(measurements.ivta_current);
-    can.write(msg);
-    wait(0.01);
+    can_send(msg);
 }
 
 
@@ -438,7 +426,7 @@
     //pc.printf("*** Into a transfer***  \r \n");
     
     // Activate IVT-A and send packet.
-    IVTA_SS = ACTIVE;
+    IVTA_SS = 0;
     //pc.printf("Data sent");
     wait_us(3);
     for(i = 0; i < 9; i++)
@@ -447,7 +435,7 @@
         wait_us(3);
     }
     wait_us(3);
-    IVTA_SS = INACTIVE;
+    IVTA_SS = 1;
     /*
        Note: we ignore the packet sent by the IVT-A
        during this communication phase. Similarly,
@@ -456,7 +444,7 @@
     */
 
     wait_us(500);                           // Wait between packets
-    IVTA_SS = ACTIVE;                       // Activate IVT-A and receive packet.
+    IVTA_SS = 0;                       // Activate IVT-A and receive packet.
     wait_us(3);
     for(i = 0; i < 9; i++)
     {
@@ -464,7 +452,7 @@
         wait_us(3);
     }
     wait_us(3);
-    IVTA_SS = INACTIVE;
+    IVTA_SS = 1;
     
     return 0;
 }
@@ -851,7 +839,7 @@
     CANMessage msg;
     char value = 142;
     msg = CANMessage(1, &value,1);
-    if(can.write(msg))
+    if(can_send(msg))
         if (DEBUG) printf("Succesfully sent %d \r\n", value);
     else
         if (DEBUG) printf("Sending Failed \r\n");   
@@ -865,3 +853,13 @@
     else
         if (DEBUG) printf("Reading Failed \r\n"); 
 }
+
+bool can_send(CANMessage msg) {
+    Timer t;
+    CAN_data_sent = false;
+    t.start();
+    can.write(msg);
+    while(!CAN_data_sent && t.read_ms() < CAN_TIMEOUT_MS);
+    if (t.read_ms() > CAN_TIMEOUT_MS) return false;
+    else return true;
+}