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

CANParserBMU.cpp

Committer:
lcockerton62
Date:
2016-12-22
Revision:
0:0a5f554d2a16
Child:
1:51477fe4851b

File content as of revision 0:0a5f554d2a16:

// Here are the functions to generate the CAN messages
#include "CANParserBMU.h"
#include "mbed.h"

using namespace CAN_IDs;

CANMessage createTemperatureTelemetry(uint8_t ID, uint32_t CMUSerialNumber, uint16_t PCBTemperature, uint16_t cellTemperature)
{
    CANMessage msg;
    msg.len = 8;
    msg.id = BMS_BASE_ID + ID; // for voltage 0x601 - 0x6EF  
    CAN_Data data;

    data.setLower_uLong(CMUSerialNumber); 
    data.set_u16(2, PCBTemperature);
    data.set_u16(3, cellTemperature);

    for (int i = 0; i<8; i++) {
        msg.data[i] = data.get_u8(i);
    }

    return msg;
}

CANMessage createVoltageTelemetry(int ID, uint16_t voltage[])
{
    CANMessage msg;
    msg.len = 8;
    msg.id = BMS_BASE_ID + ID; // for voltage 0x601 - 0x6EF @TODO 
    CAN_Data data;

    data.set_u16(0, voltage[0]);
    data.set_u16(1, voltage[1]);
    data.set_u16(2, voltage[2]);
    data.set_u16(3, voltage[3]);

    for (int i = 0; i<8; i++) {
        msg.data[i] = data.get_u8(i);
    }

    return msg;
}


CANMessage createPackSOC(float SOC, float percentageCharge)
{
    CANMessage msg;
    msg.len = 8;
    msg.id = BMS_BASE_ID + BATTERY_SOC_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;
}

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;
}

CANMessage createCellVoltageMAXMIN(uint16_t MAXMINVoltage[], uint8_t cellVoltageInformation[])
{
    //@TODO create a structure to store this data , so that the data is stored in the correct order
    CANMessage msg;
    msg.len = 8;
    msg.id = BMS_BASE_ID + MAX_MIN_VOLTAGE; 
    
    CAN_Data data;
    data.set_u16(0,MAXMINVoltage[0]);   //Min voltage
    data.set_u16(1,MAXMINVoltage[1]);   //Max voltage
    data.set_u8(4,cellVoltageInformation[0]);  //CMU number of lowest cell
    data.set_u8(5,cellVoltageInformation[1]);  //Cell number in CMU with lowest voltage
    data.set_u8(6,cellVoltageInformation[2]);  //CMU number of maxiumum cell
    data.set_u8(7,cellVoltageInformation[3]);  //Cell number in CMU with highest voltage   
    
    for(int i=0; i<8; i++){
        msg.data[i] = data.get_u8(i);
    }
    
    return msg;
}

CANMessage createCellTemperatureMAXMIN(uint16_t MAXMINTemperature[], uint8_t cellTemperatureInformation[])
{
    //@TODO create a structure to store this data , so that the data is stored in the correct order
    CANMessage msg;
    msg.len = 8;
    msg.id = BMS_BASE_ID + MAX_MIN_TEMPERATURE; 
    
    CAN_Data data;
    data.set_u16(0,MAXMINTemperature[0]);   //Min temperature
    data.set_u16(1,MAXMINTemperature[1]);   //Max temperature
    data.set_u8(4,cellTemperatureInformation[0]);  //CMU number of lowest temperature cell
    data.set_u8(5,BLANK_DATA);  //Dummy data
    data.set_u8(6,cellTemperatureInformation[1]);  //CMU number of maxiumum temperature cell
    data.set_u8(7,BLANK_DATA);  //Dummy data   
    
    for(int i=0; i<8; i++){
        msg.data[i] = data.get_u8(i);
    }
    
    return msg;
}

CANMessage createBatteryVI(uint32_t batteryVoltage,uint32_t batteryCurrent)
{
    CANMessage msg;
    msg.len = 8;
    msg.id = BMS_BASE_ID + BATTERY_VI_ID;    
       
    CAN_Data data;
    //TODO check the below lines
   // data.set_u32(0,batteryVoltage);
    //data.set_u32(1,batteryCurrent);
    
        for(int i=0; i<8; i++){
        msg.data[i] = data.get_u8(i);
    }
    return msg;
}

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 msg;
  msg.len = 8;
  msg.id = BMS_BASE_ID + BATTERY_STATUS_ID;  
  
  CAN_Data data;
  data.setLower_uLong(status); //@TODO see the data sheet for this 
  data.set_u8(4,0x00);//Hardware version random data @TODO check this
  data.set_u8(5,0x00);//Model ID @TODO check this
  data.set_u16(3,0x00); // Unused
  
  for(int i=0; i<8; i++){
    msg.data[i] = data.get_u8(i);
  }
  return msg;
}

void convertFloatFloat(float lower, float upper, CANMessage& msg, bool littleEndian)
{
    // Code taken from driver_controls
    //two converters for lower and higher float
    float2byte convL;
    float2byte convH;
    convL.f = lower;
    convH.f = upper;
    if(littleEndian) {
        for(int i=0; i<4; i++) {
            msg.data[i] = convL.b[i];
            //offset for upper float
            msg.data[i+4]=convH.b[i];
        }
    } else {
        for(int i=0; i<4; i++) {
            /*
            * Subtract because output data is Big Endian
            * i.e. convL/H is LSB --> MSB
            * output is       MSB --> LSB
            */
            
            msg.data[4-i] = convL.b[i];
            msg.data[7-i] = convH.b[i];
        }
    }
    
}