Skip to content

Commit

Permalink
CAN dbc prep
Browse files Browse the repository at this point in the history
  • Loading branch information
Mayamelon committed May 15, 2024
1 parent 5750b42 commit 73e8857
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 58 deletions.
2 changes: 1 addition & 1 deletion bms/src/BmsConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extern DigitalOut* chargerControl;

// Number of LTC6811 battery banks to communicate with
#ifndef BMS_BANK_COUNT
#define BMS_BANK_COUNT 1
#define BMS_BANK_COUNT 4
#endif

// Number of cell voltage readings per LTC6811
Expand Down
14 changes: 8 additions & 6 deletions bms/src/BmsThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ void BMSThread::threadWorker() {
printf("Things are not okay. StartADC\n");
}

ThisThread::sleep_for(5ms);

// Read back values from all chips
for (int i = 0; i < BMS_BANK_COUNT; i++) {
if (m_bus.PollAdcCompletion(
Expand Down Expand Up @@ -219,7 +221,7 @@ void BMSThread::threadWorker() {
if (index != -1) {
allVoltages[(BMS_BANK_CELL_COUNT * i) + index] = voltage;

printf("%d: V: %d\n", index, voltage);
// printf("%d: V: %d\n", index, voltage);
}
}
}
Expand All @@ -243,7 +245,7 @@ void BMSThread::threadWorker() {
maxTemp = allTemps[i];
}
}
// printf("min temp: %d, max temp: %d\nmin volt: %d, max volt %d\n", minTemp, maxTemp, minVoltage, maxVoltage);
printf("min temp: %d, max temp: %d\nmin volt: %d, max volt %d\n", minTemp, maxTemp, minVoltage, maxVoltage);

if (minVoltage <= BMS_FAULT_VOLTAGE_THRESHOLD_LOW ||
maxVoltage >= BMS_FAULT_VOLTAGE_THRESHOLD_HIGH ||
Expand All @@ -267,7 +269,7 @@ void BMSThread::threadWorker() {
uint16_t cellVoltage = allVoltages[i * BMS_BANK_CELL_COUNT + cellNum];
if (cellVoltage >= BMS_BALANCE_THRESHOLD &&
cellVoltage >= minVoltage + BMS_DISCHARGE_THRESHOLD) {
printf("Balancing cell %d\n", cellNum);
// printf("Balancing cell %d\?n", cellNum);
dischargeValue |= (0x1 << j);
}
}
Expand Down Expand Up @@ -307,15 +309,15 @@ void BMSThread::threadWorker() {


if (charging) {
ThisThread::sleep_for(200ms); // longer duty cycle when charging
ThisThread::sleep_for(500ms); // longer duty cycle when charging, 500 default
} else {
ThisThread::sleep_for(100ms);
ThisThread::sleep_for(100ms); // 100 ms
}
}
}

void BMSThread::throwBmsFault() {
bmsState = BMSThreadState::BMSFault;
// bmsState = BMSThreadState::BMSFault;
// palClearLine(LINE_BMS_FLT);
// palSetLine(LINE_CHARGER_CONTROL);
}
92 changes: 42 additions & 50 deletions bms/src/Can.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,57 @@
#ifndef _FS_BMS_SRC_CAN_H_
#define _FS_BMS_SRC_CAN_H_

#include <cstdint>
#include <stdint.h>

#include "mbed.h"

// SIDs From Accumulator
constexpr uint32_t kFuncIdHeartBeatAcc = 0x701;
constexpr uint32_t kFuncIdCellStartup = 0x420;
constexpr uint32_t kFuncIdFaultStatus = 0x421;
constexpr uint32_t kFuncIdBmsStat = 0x422;
constexpr uint32_t kFuncIdCellVoltage[7] = {0x423, 0x424, 0x425, 0x426,
0x427, 0x428, 0x429};
constexpr uint32_t kFuncIdCellTempAdc[4] = {0x42a, 0x42b, 0x42c, 0x42d};

CANMessage BMSCellStartup () {
const char * startupMessage = "SPICYBOI";
return CANMessage(kFuncIdCellStartup, startupMessage);
}

CANMessage BMSFaultStatus (uint8_t contactorState, uint8_t faultState, uint8_t warningState, uint8_t SoC, uint16_t DoD, uint16_t totalCurrent) {
uint8_t data[8];
data[0] = contactorState;
data[1] = faultState;
data[2] = warningState;
data[3] = SoC;
data[4] = DoD >> 8;
data[5] = DoD;
data[6] = totalCurrent >> 8;
data[7] = totalCurrent;
return CANMessage(kFuncIdFaultStatus, data);
}

CANMessage BMSStatMessage (uint16_t totalVoltage, uint16_t maxVoltage, uint16_t minVoltage, uint8_t maxTemp, uint8_t minTemp) {
uint8_t data[8];
data[0] = totalVoltage >> 8;
data[1] = totalVoltage;
data[2] = maxVoltage >> 8;
data[3] = maxVoltage;
data[4] = minVoltage >> 8;
data[5] = minVoltage;
data[6] = maxTemp;
data[7] = minTemp;
return CANMessage(kFuncIdBmsStat, data);
constexpr uint32_t kTPDO_ACC_BOARD_State = 0x182;
constexpr uint32_t kTPDO_ACC_BOARD_Temp_0 = 0x282;
constexpr uint32_t kTPDO_ACC_BOARD_Temp_1 = 0x382;
constexpr uint32_t kTPDO_ACC_BOARD_Temp_2 = 0x482;
constexpr uint32_t kTPDO_ACC_BOARD_Temp_3 = 0x582;
constexpr uint32_t kNMT_ACC_HEARTBEAT = 0x702;

CANMessage AccBoardBootup () {
const char * startupMessage = 0x00;
return CANMessage(kNMT_ACC_HEARTBEAT, startupMessage);
}

CANMessage BMSVoltageMessage (uint8_t row, uint16_t * voltages) {
uint8_t data[8];
for (int i = 0; i < 8; i += 2) {
data[i] = voltages[i/2] >> 8;
data[i + 1] = voltages [i/2];
}
return CANMessage(kFuncIdCellVoltage[row], data);
CANMessage AccBoardState(uint8_t glvVoltage, uint16_t tsVoltage, bool bmsFault, bool bmsBalancing, bool prechargeDone, bool charging, bool fansOn, bool shutdownClosed, bool unused_A, bool unused_B, uint8_t minCellVoltage, uint8_t maxCellVoltage, int16_t tsCurrent) {
uint8_t data[8];
data[0] = glvVoltage;
data[1] = tsVoltage;
data[2] = tsVoltage >> 8;
data[3] = bmsFault + (bmsBalancing << 1) + (prechargeDone << 2) + (charging << 3) + (fansOn << 4) + (shutdownClosed << 5) + (unused_A << 6) +(unused_B << 7);
data[4] = minCellVoltage;
data[5] = maxCellVoltage;
data[6] = tsCurrent;
data[7] = tsCurrent >> 8;
return CANMessage(kTPDO_ACC_BOARD_State, data);
}

CANMessage BMSTempMessage (uint8_t row, uint8_t * temps) {
uint8_t data[7];
for (int i = 0; i < 7; i++) {
data[i] = temps[i];
}
return CANMessage(kFuncIdCellTempAdc[row], data, 7);
CANMessage AccBoardTemp(int segment, uint8_t *temps) {
uint8_t data[8];
for (int i = 0; i < 7; i++) {
data[i] = temps[i];
}
int canID;
switch (segment) {
case 0:
return kTPDO_ACC_BOARD_Temp_0;
case 1:
return kTPDO_ACC_BOARD_Temp_1;
case 2:
return kTPDO_ACC_BOARD_Temp_2;
case 3:
return kTPDO_ACC_BOARD_Temp_3;
default:
return NULL;
break;
}
return CANMessage(canID, data);
}

#endif // _FS_BMS_SRC_CAN_H_
2 changes: 1 addition & 1 deletion bms/src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,5 +182,5 @@ void canRX() {
}

void canTX() {

}
124 changes: 124 additions & 0 deletions mainCANbus.dbc
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
VERSION ""


NS_ :
NS_DESC_
CM_
BA_DEF_
BA_
VAL_
CAT_DEF_
CAT_
FILTER
BA_DEF_DEF_
EV_DATA_
ENVVAR_DATA_
SGTYPE_
SGTYPE_VAL_
BA_DEF_SGTYPE_
BA_SGTYPE_
SIG_TYPE_REF_
VAL_TABLE_
SIG_GROUP_
SIG_VALTYPE_
SIGTYPE_VALTYPE_
BO_TX_BU_
BA_DEF_REL_
BA_REL_
BA_DEF_DEF_REL_
BU_SG_REL_
BU_EV_REL_
BU_BO_REL_
SG_MUL_VAL_

BS_:

BU_:


BO_ 3221225472 VECTOR__INDEPENDENT_SIG_MSG: 0 Vector__XXX
SG_ Max_Cell_Temp : 56|8@1+ (1,-40) [-40|215] "C" Vector__XXX
SG_ NewSignal_0004 : 24|8@1+ (1,0) [0|0] "" Vector__XXX

BO_ 386 TPDO_ACC_BOARD_State: 8 Vector__XXX
SG_ GLV_Voltage : 0|8@1+ (0.1,0) [0|25.5] "Volts" Vector__XXX
SG_ TS_Voltage : 8|16@1+ (0.1,0) [0|6553.5] "Volts" Vector__XXX
SG_ BMS_Fault : 24|1@1+ (1,0) [0|0] "" Vector__XXX
SG_ BMS_Balancing : 25|1@1+ (1,0) [0|0] "" Vector__XXX
SG_ Precharge_Done : 26|1@1+ (1,0) [0|0] "" Vector__XXX
SG_ Charging : 27|1@1+ (1,0) [0|0] "" Vector__XXX
SG_ Fans_On : 28|1@1+ (1,0) [0|0] "" Vector__XXX
SG_ Shutdown_Closed : 29|1@1+ (1,0) [0|0] "" Vector__XXX
SG_ UNUSED_a : 30|1@1+ (1,0) [0|0] "" Vector__XXX
SG_ UNUSED_b : 31|1@1+ (1,0) [0|0] "" Vector__XXX
SG_ Min_Cell_Voltage : 32|8@1+ (0.02,0) [0|5.1] "Volts" Vector__XXX
SG_ Max_Cell_Voltage : 40|8@1+ (0.02,0) [0|5.1] "Volts" Vector__XXX
SG_ TS_Current : 48|16@1- (0.1,0) [-3276.8|3276.7] "Amps" Vector__XXX

BO_ 642 TPDO_ACC_BOARD_Temp_0: 8 Vector__XXX
SG_ TEMP_0 : 0|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ TEMP_1 : 8|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ TEMP_2 : 16|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ TEMP_3 : 24|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ TEMP_4 : 32|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ TEMP_5 : 40|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ TEMP_6 : 48|8@1+ (1,-40) [-40|215] "" Vector__XXX

BO_ 898 TPDO_ACC_BOARD_Temp_1: 8 Vector__XXX
SG_ TEMP_0 : 0|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ TEMP_1 : 8|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ TEMP_2 : 16|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ TEMP_3 : 24|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ TEMP_4 : 32|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ TEMP_5 : 40|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ TEMP_6 : 48|8@1+ (1,-40) [-40|215] "" Vector__XXX

BO_ 1154 TPDO_ACC_BOARD_Temp_2: 8 Vector__XXX
SG_ TEMP_0 : 0|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ TEMP_1 : 8|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ TEMP_2 : 16|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ TEMP_3 : 24|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ TEMP_4 : 32|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ TEMP_5 : 40|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ TEMP_6 : 48|8@1+ (1,-40) [-40|215] "" Vector__XXX

BO_ 1410 TPDO_ACC_BOARD_Temp_3: 8 Vector__XXX
SG_ TEMP_0 : 0|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ TEMP_1 : 8|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ TEMP_2 : 16|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ TEMP_3 : 24|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ TEMP_4 : 32|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ TEMP_5 : 40|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ TEMP_6 : 48|8@1+ (1,-40) [-40|215] "" Vector__XXX

BO_ 1794 NMT_ACC_HEARTBEAT: 8 Vector__XXX
SG_ Status : 0|8@1+ (1,0) [0|255] "" Vector__XXX

BO_ 1 TPDO_BSPD_BOARD_Brake: 8 Vector__XXX
SG_ Brake_Sensor_F : 0|8@1+ (0.5,0) [0|127.5] "percent" Vector__XXX
SG_ Brake_Sensor_R : 8|8@1+ (0.5,0) [0|127.5] "percent" Vector__XXX
SG_ Steering_Sensor : 16|8@1+ (1,-90) [-90|165] "degrees" Vector__XXX
SG_ Brakes_On : 24|1@1+ (1,0) [0|1] "" Vector__XXX
SG_ IMD_Fault : 25|1@1+ (1,0) [0|1] "" Vector__XXX
SG_ BSPD_Fault : 26|1@1+ (1,0) [0|1] "" Vector__XXX



CM_ SG_ 386 BMS_Fault "Fault High";
CM_ SG_ 386 BMS_Balancing "Balancing High";
CM_ SG_ 386 Precharge_Done "Done High";
CM_ SG_ 386 Charging "Charging High";
CM_ SG_ 386 Fans_On "On High";
CM_ SG_ 386 Shutdown_Closed "Closed High";
CM_ SG_ 1 Steering_Sensor "90 (left) to -90 (right)";
BA_DEF_ BO_ "CANFD_BRS" ENUM "0","1";
BA_DEF_ "BusType" STRING ;
BA_DEF_ BU_ "ECU" STRING ;
BA_DEF_ BO_ "VFrameFormat" ENUM "StandardCAN","ExtendedCAN","StandardCAN_FD","ExtendedCAN_FD";
BA_DEF_DEF_ "CANFD_BRS" "1";
BA_DEF_DEF_ "BusType" "";
BA_DEF_DEF_ "ECU" "";
BA_DEF_DEF_ "VFrameFormat" "StandardCAN";
BA_ "BusType" "CAN FD";
VAL_ 1794 Status 0 "Boot up" 4 "Stopped" 5 "Operational" 127 "Pre-operational" ;

0 comments on commit 73e8857

Please sign in to comment.