Cell voltages fork (SoC)

Dependencies:   CUER_CAN CUER_DS1820 LTC2943 LTC6804 mbed PowerControl

Files at this revision

API Documentation at this revision

Comitter:
DasSidG
Date:
Sat Sep 16 01:10:01 2017 +0000
Parent:
65:95f21910cf9d
Commit message:
Added more flags to the BMU status; Removed some unused functions, re-ordered some functions slightly; Tidied up some bits that were generating warnings

Changed in this revision

CANParserBMU.cpp Show annotated file Show diff for this revision Revisions of this file
CANParserBMU.h Show annotated file Show diff for this revision Revisions of this file
CUER_CAN.lib Show annotated file Show diff for this revision Revisions of this file
Data_Types_BMU.h Show annotated file Show diff for this revision Revisions of this file
IVTA.cpp Show annotated file Show diff for this revision Revisions of this file
IVTA.h Show annotated file Show diff for this revision Revisions of this file
LTC6804.lib Show annotated file Show diff for this revision Revisions of this file
Temperature.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 95f21910cf9d -r c884fba9eaea CANParserBMU.cpp
--- a/CANParserBMU.cpp	Tue Sep 12 01:29:12 2017 +0000
+++ b/CANParserBMU.cpp	Sat Sep 16 01:10:01 2017 +0000
@@ -117,23 +117,6 @@
     return msg;
 }
 
-CANMessage createPackBalanceSOC(float SOC, float percentageCharge)
-{
-    // @TODO - check is this being used?? section 5.4 trituim BMU CAN data sheet
-    CANMessage msg;
-    msg.len = 8;
-    msg.id = BMS_BASE_ID + BATTERY_SOC_BASE_ID;
-
-    CAN_Data data;
-    data.setLowerFloat(SOC);
-    data.setUpperFloat(percentageCharge);
-    for(int i=0; i<8; i++) {
-        msg.data[i] = data.get_u8(i);
-    }
-
-    return msg;
-}
-
 /**
 * decodePackSOC and decodePackSOCPercentage can be used with both of the SOC msg types
 */
@@ -241,6 +224,21 @@
     return msg;
 }
 
+CANMessage createBatteryPower(uint32_t batteryVoltage, float batteryCurrent)
+{
+    CANMessage msg;
+    msg.len = 8;
+    msg.id = BMS_BASE_ID + BATTERY_POWER_ID;
+
+    CAN_Data data;
+    data.setLowerFloat(batteryVoltage*batteryCurrent);
+
+    for(int i=0; i<8; i++) {
+        msg.data[i] = data.get_u8(i);
+    }
+    return msg;
+}
+
 uint32_t decodeBatteryVoltage(CANMessage msg)
 {
     uint32_t result = 0;
@@ -284,26 +282,7 @@
     return result;
 }
 
-CANMessage createBatteryPackStatus(uint16_t voltageThreshold[], uint8_t statusFlag,uint8_t BMS_CMU_Count,uint16_t BMS_Firmware_Build)
-{
-    CANMessage msg;
-    msg.len = 8;
-    msg.id = BMS_BASE_ID + BATTERY_PACK_STATUS_ID;
-
-    CAN_Data data;
-    data.set_u16(0,voltageThreshold[0]);
-    data.set_u16(1,voltageThreshold[1]);
-    data.set_16(3,BMS_Firmware_Build);
-    data.set_u8(4,statusFlag);
-    data.set_u8(5,BMS_CMU_Count);
-
-    for(int i=0; i<8; i++) {
-        msg.data[i] = data.get_u8(i);
-    }
-    return msg;
-}
-
-CANMessage createExtendedBatteryPackStatus(uint32_t status)
+CANMessage createBatteryPackStatus(uint32_t status)
 {
     CANMessage msg;
     msg.len = 8;
@@ -321,7 +300,7 @@
     return msg;
 }
 
-uint32_t decodeExtendedBatteryPackStatus(CANMessage msg)
+uint32_t decodeBatteryPackStatus(CANMessage msg)
 {
     CAN_Data decode;
     decode.importCANData(msg);
diff -r 95f21910cf9d -r c884fba9eaea CANParserBMU.h
--- a/CANParserBMU.h	Tue Sep 12 01:29:12 2017 +0000
+++ b/CANParserBMU.h	Sat Sep 16 01:10:01 2017 +0000
@@ -5,15 +5,6 @@
 #ifndef CANParserBMU_H
 #define CANParserBMU_H
 
-//@TODO Move these definitions elsewhere ideally into CAN_IDS.h
-#define BATTERY_SOC_BASE_ID 0xF5
-#define MAX_MIN_VOLTAGE 0xF8
-#define BLANK_DATA 0x00
-#define MAX_MIN_TEMPERATURE 0xF9
-#define BATTERY_PACK_STATUS_ID 0xFA
-
-//@TODO add some comments
-//@TODO Heartbeat
 CANMessage createTemperatureTelemetry(uint8_t offset, char * ROMID, float Temperature);
 
 individual_temperature decodeTemperatureTelemetry(CANMessage msg); //working
@@ -28,31 +19,29 @@
 
 float decodePackSOCPercentage(CANMessage msg);//working
 
-CANMessage createPackBalanceSOC(float SOC, float percentageCharge); //unused (and untested)
+CANMessage createCellVoltageMAXMIN(pack_voltage_extremes max_voltage, pack_voltage_extremes min_voltage);
 
-CANMessage createCellVoltageMAXMIN(pack_voltage_extremes max_voltage, pack_voltage_extremes min_voltage); //working
-
-void decodeCellVoltageMAXMIN(CANMessage msg, pack_voltage_extremes &min, pack_voltage_extremes &max); //working
+void decodeCellVoltageMAXMIN(CANMessage msg, pack_voltage_extremes &min, pack_voltage_extremes &max);
 
-CANMessage createCellTemperatureMAXMIN(pack_temperature_extremes ex_temperature, bool isMin); //working
+CANMessage createCellTemperatureMAXMIN(pack_temperature_extremes ex_temperature, bool isMin);
 
-pack_temperature_extremes decodeCellTemperatureMAXMIN(CANMessage msg); //working
+pack_temperature_extremes decodeCellTemperatureMAXMIN(CANMessage msg);
 
-CANMessage createBatteryVI(uint32_t batteryVoltage,float batteryCurrent); //Working, with a delay between sends
+CANMessage createBatteryVI(uint32_t batteryVoltage,float batteryCurrent);
 
 uint32_t decodeBatteryVoltage(CANMessage msg); //Works
 
 float decodeBatteryCurrent(CANMessage msg); //Works
 
+CANMessage createBatteryPower(uint32_t batteryVoltage, float batteryCurrent);
+
 CANMessage createIVTACurrent(int32_t current);
 
 int32_t decodeIVTACurrent(CANMessage msg);
 
-CANMessage createBatteryPackStatus(uint16_t voltageThreshold[], uint8_t statusFlag,uint8_t BMS_CMU_Count,uint16_t BMS_Firmware_Build); //unused (i think?)
+CANMessage createBatteryPackStatus(uint32_t status);
 
-CANMessage createExtendedBatteryPackStatus(uint32_t status); //Unsure if its working, seems good, check if values make sense
-
-uint32_t decodeExtendedBatteryPackStatus(CANMessage msg); //Unsure if its working
+uint32_t decodeBatteryPackStatus(CANMessage msg);
 
 CANMessage createBMSHeartbeat(uint32_t val1, uint32_t val2);
 
diff -r 95f21910cf9d -r c884fba9eaea CUER_CAN.lib
--- a/CUER_CAN.lib	Tue Sep 12 01:29:12 2017 +0000
+++ b/CUER_CAN.lib	Sat Sep 16 01:10:01 2017 +0000
@@ -1,1 +1,1 @@
-https://developer.mbed.org/teams/CUER/code/CUER_CAN/#3af5e77fdef3
+https://developer.mbed.org/teams/CUER/code/CUER_CAN/#f7720793ffa8
diff -r 95f21910cf9d -r c884fba9eaea Data_Types_BMU.h
--- a/Data_Types_BMU.h	Tue Sep 12 01:29:12 2017 +0000
+++ b/Data_Types_BMU.h	Sat Sep 16 01:10:01 2017 +0000
@@ -28,41 +28,45 @@
 #define SECOND_ADDRESS_OFFSET 20
 #define MAX_WRITE_ADDRESS 0x7FC0
 
-// Error states
-#define NONE = 0x00000000
-#define CELL_OVER_VOLTAGE 0x00000001
-#define CELL_UNDER_VOLTAGE 0x00000002
-#define CELL_OVER_CHARGE_TEMPERATURE 0x00000004
-#define CELL_OVER_DISCHARGE_TEMPERATURE 0x00000008
-#define CELL_UNDER_CHARGE_TEMPERATURE 0x00000010
-#define CELL_UNDER_DISCHARGE_TEMPERATURE 0x00000020
-#define MAX_TEMPERATURE_DIFFERENCE_ERROR 0x00000040
-#define OVERCURRENT_ERROR 0x00000080
-#define CELL_OVER_CHARGE_VOLTAGE 0x00000100
+// Limit values
+
+//Note: there are three categories of limiting values, for which the vehicle and driver response will differ:
+
+//Dangerous errors, which will trigger a car shutdown and tell the driver to leave immediately
+
+#define MAX_CELL_SAFE_VOLTAGE 4300 //mV
+#define MIN_CELL_SAFE_VOLTAGE 2300 //mV
+#define MAX_CELL_SAFE_TEMPERATURE 70
+#define MAX_TEMPERATURE_DIFFERENCE 5 //this threshold also indicates a a dangerous situation i.e. suspected battery fire
+
+//Moderate errors, which will trigger a car shutdown, but the driver can remain in the vehicle
+
+#define MAX_CELL_VOLTAGE 4200 //mV
+#define MIN_CELL_VOLTAGE 2500 //mV
+#define MAX_CELL_DISCHARGE_TEMPERATURE 55
+#define MIN_CELL_DISCHARGE_TEMPERATURE -20
+#define OVERCURRENT_THRESHOLD 60000 //mA
 
-//Legacy error states from Tritium BMS
-/*
-#define CMU_COMMUNICATION_TIMEOUT 0x00000010
-#define VEHICLE_COMMUNICATIONS_TIMEOUT 0x00000020
-#define BMU_SETUP_MODE 0x00000040
-#define CMU_CAN_POWER_STATUS 0x00000080
-#define PACK_ISOLATION_TEST_FAILURE 0x00000100
-#define SOC_MEASUREMENT_NOT_VALID = 0x00000200
-#define CAN_12V_LOW = 0x00000400
-#define CONTACTOR_NOT_ENGAGED = 0x00000800
-#define CMU_EXTRA_CELL = 0x00001000
-*/ 
+//Low priority errors, for which the vehicle can continue driving, but some functionality may be reduced (e.g. array may be disabled)
+
+#define MAX_CELL_CHARGE_VOLTAGE 4150 //mV
+#define MAX_CELL_CHARGE_TEMPERATURE 45
+#define MIN_CELL_CHARGE_TEMPERATURE 10
 
-// Limit values
-#define MAX_CELL_VOLTAGE 4200 //mV
-#define MAX_CELL_CHARGE_VOLTAGE 4150 //mV
-#define MIN_CELL_VOLTAGE 2500 //mV
-#define MAX_CELL_CHARGE_TEMPERATURE 45
-#define MAX_CELL_DISCHARGE_TEMPERATURE 55
-#define MIN_CELL_CHARGE_TEMPERATURE 10
-#define MIN_CELL_DISCHARGE_TEMPERATURE -20
-#define MAX_TEMPERATURE_DIFFERENCE 5
-#define OVERCURRENT_THRESHOLD 60000 //mA
+// Error states
+#define NONE                                        0x00000000  
+#define CELL_OVER_VOLTAGE                           0x00000001  //Moderate error
+#define CELL_UNDER_VOLTAGE                          0x00000002  //Moderate error
+#define CELL_OVER_CHARGE_TEMPERATURE                0x00000004  //Low priority error
+#define CELL_OVER_DISCHARGE_TEMPERATURE             0x00000008  //Moderate error
+#define CELL_UNDER_CHARGE_TEMPERATURE               0x00000010  //Low priority error
+#define CELL_UNDER_DISCHARGE_TEMPERATURE            0x00000020  //Moderate error
+#define MAX_TEMPERATURE_DIFFERENCE_ERROR            0x00000040  //Dangerous error
+#define OVERCURRENT_ERROR                           0x00000080  //Moderate error
+#define CELL_OVER_CHARGE_VOLTAGE                    0x00000100  //Low priority error
+#define CELL_DANGEROUSLY_OVER_VOLTAGE               0x00000200  //Dangerous error
+#define CELL_DANGEROUSLY_UNDER_VOLTAGE              0x00000400  //Dangerous error
+#define CELL_DANGEROUSLY_OVER_TEMPERATURE           0x00000800  //Dangerous error
 
 //CAN Buffer
 #define CAN_BUFFER_SIZE 255 //Setting this to be quite large, should be equal to max # of ids expected to recieve
@@ -128,10 +132,4 @@
     int32_t ivta_current;
 };
 
-
-// @TODO are both the battery status and extended status used or just one of them
-enum BMU_CAN_flags{
-    // Think this is used for legacy software
-};
-
 #endif
\ No newline at end of file
diff -r 95f21910cf9d -r c884fba9eaea IVTA.cpp
--- a/IVTA.cpp	Tue Sep 12 01:29:12 2017 +0000
+++ b/IVTA.cpp	Sat Sep 16 01:10:01 2017 +0000
@@ -1,4 +1,4 @@
-#include "IVTA.h";
+#include "IVTA.h"
 
 DigitalOut IVTA_SS(p11);
 
@@ -7,7 +7,6 @@
 int ivta_transfer(uint8_t * txrx)
 {
     char i;// j;
-    uint16_t crc;
     
     //pc.printf("*** Into a transfer***  \r \n");
     
diff -r 95f21910cf9d -r c884fba9eaea IVTA.h
--- a/IVTA.h	Tue Sep 12 01:29:12 2017 +0000
+++ b/IVTA.h	Sat Sep 16 01:10:01 2017 +0000
@@ -1,7 +1,7 @@
 #ifndef IVTA_H
 #define IVTA_H
 
-#include "mbed.h";
+#include "mbed.h"
 
 #define DEBUG 0
 
diff -r 95f21910cf9d -r c884fba9eaea LTC6804.lib
--- a/LTC6804.lib	Tue Sep 12 01:29:12 2017 +0000
+++ b/LTC6804.lib	Sat Sep 16 01:10:01 2017 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/teams/CUER/code/LTC6804/#e3a5393dfbac
+http://developer.mbed.org/teams/CUER/code/LTC6804/#1c94bbb97eaa
diff -r 95f21910cf9d -r c884fba9eaea Temperature.cpp
--- a/Temperature.cpp	Tue Sep 12 01:29:12 2017 +0000
+++ b/Temperature.cpp	Sat Sep 16 01:10:01 2017 +0000
@@ -1,7 +1,7 @@
 /*
 * Code for identifying address and temperature of DS1820 1-Wire Thermometers by Maxim
 * Uses the DS1820 library written by Michael Hagberg and Fernando Caamaño, with some slight modifications
-* Currently tested using DS18S20 1-Wire Thermometers in an array, parasite power yet to be tested
+* NOTE: THIS IS NOT CURRENTLY BEING USED IN THE BMU CODE AS TEMPERATURE MEASUREMENTS ARE CURRENTLY BEING COLLATED BY A SEPARATE PCB
 */
 #include "Temperature.h"
 #define DEBUG 0
diff -r 95f21910cf9d -r c884fba9eaea main.cpp
--- a/main.cpp	Tue Sep 12 01:29:12 2017 +0000
+++ b/main.cpp	Sat Sep 16 01:10:01 2017 +0000
@@ -204,8 +204,11 @@
     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);
+    msg = createBatteryPower(measurements.battery_voltage,measurements.battery_current);
+    can_send(msg);
+    
+    //Battery pack status
+    msg = createBatteryPackStatus(status);
     can_send(msg);
     if (DEBUG) printf("Sent battery pack status with value %d \r\n", status);
     
@@ -236,7 +239,7 @@
 
     // Get a pointer to the start address for the data stored in the eeprom
     i2c_page_read(0x0000, 4, start_address_array1);
-    if (DEBUG) printf("\r\n\ Start address (%d,%d) \r\n \r\n", start_address_array1[0], start_address_array1[1]);
+    if (DEBUG) printf("\r\n Start address (%d,%d) \r\n \r\n", start_address_array1[0], start_address_array1[1]);
     wait_ms(10);
     i2c_page_read(0x0004, 4, start_address_array2);
     
@@ -507,12 +510,8 @@
     if(measurements.max_cell_voltage.voltage > MAX_CELL_VOLTAGE) {
         status = status | CELL_OVER_VOLTAGE;
     }
-    if(measurements.max_cell_voltage.voltage > MAX_CELL_CHARGE_VOLTAGE) {
-        status = status | CELL_OVER_CHARGE_VOLTAGE;
-    }
     if (measurements.min_cell_voltage.voltage < MIN_CELL_VOLTAGE) {
         status = status | CELL_UNDER_VOLTAGE;
-        //printf(" \r\n \r\n \r\n Min cell in check MEASUREMENTS is voltage is %d \r\n", measurements.min_cell_voltage.voltage);
     }
     if (maxTemp.temperature > MAX_CELL_CHARGE_TEMPERATURE && (temp_measurements_timer.read() > initial_temperature_delay || temperature_measurements_received)) {
         status = status | CELL_OVER_CHARGE_TEMPERATURE;
@@ -532,6 +531,18 @@
     if (fabs(measurements.battery_current) >  OVERCURRENT_THRESHOLD) {
         status = status | OVERCURRENT_ERROR;
     }
+    if(measurements.max_cell_voltage.voltage > MAX_CELL_CHARGE_VOLTAGE) {
+        status = status | CELL_OVER_CHARGE_VOLTAGE;
+    }
+    if(measurements.max_cell_voltage.voltage > MAX_CELL_SAFE_VOLTAGE) {
+        status = status | CELL_DANGEROUSLY_OVER_VOLTAGE;
+    }
+    if (measurements.min_cell_voltage.voltage < MIN_CELL_VOLTAGE) {
+        status = status | CELL_DANGEROUSLY_UNDER_VOLTAGE;
+    }
+    if (maxTemp.temperature > MAX_CELL_SAFE_TEMPERATURE && (temp_measurements_timer.read() > initial_temperature_delay || temperature_measurements_received)) {
+        status = status | CELL_DANGEROUSLY_OVER_TEMPERATURE;
+    }
 
     /*
     @TODO also include errors for:
@@ -725,12 +736,10 @@
     //Import the data from the buffer into a non-volatile, more usable format
     CAN_Data can_data[CAN_BUFFER_SIZE]; //container for all of the raw data
     CANMessage msgArray[CAN_BUFFER_SIZE]; //Same as above but some functions take message as their parameter
-    int received_CAN_IDs[CAN_BUFFER_SIZE]; //needed to keep track of which IDs we've received so far
     for (int i = 0; i<CAN_BUFFER_SIZE; ++i) 
     {
         safe_to_write[i] = false;
         can_data[i].importCANData(buffer[i]);
-        received_CAN_IDs[i] = buffer[i].id;
         msgArray[i] = buffer[i];
         safe_to_write[i] = true;
         //printf("Id recieved %d \r\n", buffer[i].id);
@@ -782,6 +791,16 @@
         minTemp.temperature,minTemp.ID,maxTemp.temperature,maxTemp.ID);
 }
 
+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;
+}
+
 /*void test_read_CAN_buffer()
 {
     //Import the data from the buffer into a non-volatile, more usable format
@@ -849,7 +868,7 @@
         }
         
         if(msgArray[i].id == BMS_BASE_ID + BATTERY_STATUS_ID)
-            status = decodeExtendedBatteryPackStatus(msgArray[i]); 
+            status = decodeBatteryPackStatus(msgArray[i]); 
             
         if(msgArray[i].id == BMS_BASE_ID)
             if (DEBUG) printf("BMS Heartbeat Recieved \r\n");
@@ -877,34 +896,6 @@
     if (DEBUG) printf("Status value is: %d \r\n", status);
 } */
 
-void test_CAN_send()
-{
-    CANMessage msg;
-    char value = 142;
-    msg = CANMessage(1, &value,1);
-    if(can_send(msg))
-        if (DEBUG) printf("Succesfully sent %d \r\n", value);
-    else
-        if (DEBUG) printf("Sending Failed \r\n");   
-}
-
-void test_CAN_read()
-{
-    CANMessage msg;
-    if(can.read(msg))
-        if (DEBUG) printf("Successfully recieved %d \r\n", msg.data[0]);  
-    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;
-}
 
 
+