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 CUER

Revision:
10:1079f8e52d65
Parent:
9:82ba050a7e13
Child:
11:cf2db05cfa56
--- a/main.cpp	Sat Feb 11 16:09:20 2017 +0000
+++ b/main.cpp	Mon Jun 19 12:00:03 2017 +0000
@@ -6,7 +6,7 @@
 #include "EEPROM_I2C.h"
 #include "Temperature.h"
 #include "LTC2943_Read.h"
-#include "State_Of_Charge.h"
+#include "Cell_Voltage.h"
 #include "SPI_I2C_Parser.h"
 
 
@@ -22,6 +22,9 @@
 uint16_t read_EEPROM_startup(BMU_data &measurements);
 uint32_t check_measurements(BMU_data &measurements);
 void take_measurements(BMU_data &measurements);
+void test_read_voltage_CAN();
+void test_CAN_send();
+void test_CAN_read();
 
 CAN can(CAN_READ_PIN, CAN_WRITE_PIN); //Create a CAN object to handle CAN comms
 uint16_t eeprom_start_address; //the initial address where we store/read SoC values
@@ -40,7 +43,14 @@
     uint16_t current_EEPROM_address;
     uint32_t status;
     int c = 0;
+    
+    /*while(true)
+    {
+        wait(0.05);
+        test_read_voltage_CAN();
+    }*/
     init();
+    
     current_EEPROM_address = read_EEPROM_startup(measurements); // Read from the eeprom at startup to fill in the values of SoC
     ltc2943.accumulatedCharge(measurements.percentage_SOC); // Initialise the LTC2943 with the current state of charge
     
@@ -72,7 +82,7 @@
         delay_finished = false;
         loop_delay.attach(loop_delay_callback, LOOP_DELAY_S);
         while (!delay_finished) sleep();
-    }
+    } 
 }
 
 void transmit_data(BMU_data measurements, uint32_t status)
@@ -84,20 +94,18 @@
     // Send cell voltages
     //voltages sent in sets of 4 + one cmy data set
     int repeating_unit_length = NO_READINGS_PER_CMU /4 + 1;
-    for(int i= 0; i < NO_CMUS; i++) {
-        //createVoltageTelemetry(i + 2 , measurements.cell_voltages[i].first_cell_voltages);
-        //createVoltageTelemetry(i + 3, measurements.cell_voltages[i].last_cell_voltages);
+    for(uint16_t i= 0; i < NO_CMUS; i++) {
         //input id is offset, data structure is info, voltage, voltage, ......
-        //
-        msg = createVoltageTelemetry(repeating_unit_length*i+2, measures.cell_voltages[i].voltages); 
+        //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);
+        //CONSIDER WAITS JUST IN CASE
         //+4 - 4 cell voltages sent per measurement
-        msg = createVoltageTelemetry(repeating_unit_length*i+3, measures.cell_voltages[i].voltages + 4); 
+        msg = createVoltageTelemetry(repeating_unit_length*i+3, measurements.cell_voltages[i].voltages + 4); 
         can.write(msg);
-        msg = createVoltageTelemetry(repeating_unit_length*i+4, measures.cell_voltages[i].voltages + 8); 
+        msg = createVoltageTelemetry(repeating_unit_length*i+4, measurements.cell_voltages[i].voltages + 8); 
         can.write(msg);
-        
-    
+        printf("Message id: %d \r\n", msg.id);
     }
 
     // Create SOC CAN message
@@ -118,6 +126,7 @@
 
 }
 
+
 uint16_t read_EEPROM_startup(BMU_data &measurements)
 {
     /* The first page of the EEPROM, specifically the first 2 addresses store a
@@ -236,24 +245,17 @@
     
     LTC6804_acquireVoltage(cellvoltages);
     
-    
-    // Here collect all measured data from the sensors
-    /*
-    * TODO Cell voltages
-    */
+    for(int i=0; i<NO_CMUS; i++){
+       for(int j=0; j<12; j++){
+             measurements.cell_voltages[i].voltages[j] =  cellvoltages[i][j] / 10;
+             printf("Cellvoltage[%d][%d] = %d \r\n",i,j,cellvoltages[i][j] /10);   
+       }   
+    }
     
     //Current, SoC
     measurements.battery_current = (uint32_t) ltc2943.current()*1000; //*1000 to converet to mA
     measurements.percentage_SOC = ltc2943.accumulatedCharge();
     measurements.SOC = (measurements.percentage_SOC /100) * BATTERY_CAPACITY;
-    
-    for(int i=0; i<NO_CMUS; i++){
-        for(int j=0; j<12; j++){
-             measurements.cell_voltages[i][j] =  cellvoltages[i][j] / 10;   
-        }   
-    }
-    
-    
 }
 
 void init()
@@ -262,3 +264,51 @@
     LTC2943_initialise(); //Initialises the fixed parameters of the LTC2943
 }
 
+void test_read_voltage_CAN()
+{
+    CANMessage msg;
+    uint16_t readings[4];
+    int can_id;
+    int offset;
+    int first_index;
+    int second_index;
+    
+    if(can.read(msg))
+    {
+        for(int i =0; i < 4; i++)
+        {
+            readings[i] = (msg.data[2 * i]) + (msg.data[2*i+1] << 8); //Since data is 8 8bit ints not 4 16 bit ones
+        }
+        can_id = msg.id; 
+        printf("CAN ID: %d \r\n", can_id);
+        offset = can_id - 1537; //1537 = 0x600
+        first_index = (offset - 1)/4; //offset of 2,3,4 is CMU 1; 6,7,8, is CMU 2; etc.
+        second_index = ((offset - 1) % 4); //Makes it so 0,1,2 represent each voltage set
+        for(int i = 0; i < 4; i++)
+        {
+            printf("Cell_Voltage[%d][%d] = %d \r\n", first_index, second_index *4 + i, readings[i]);  
+        } 
+    }
+    else
+        printf("Reading Failed \r\n");       
+}
+
+void test_CAN_send()
+{
+    CANMessage msg;
+    char value = 87;
+    msg = CANMessage(1, &value,1);
+    if(can.write(msg))
+        printf("Succesfully sent %d \r\n", value);
+    else
+        printf("Sending Failed \r\n");   
+}
+
+void test_CAN_read()
+{
+    CANMessage msg;
+    if(can.read(msg))
+        printf("Successfully recieved %d \r\n", msg.data[0]);  
+    else
+        printf("Reading Failed \r\n"); 
+}