Skip to content

Commit

Permalink
Can changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mayamelon committed May 20, 2024
1 parent 0839241 commit 94de02b
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 29 deletions.
1 change: 0 additions & 1 deletion bms/.mbed
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
ROOT=.
TARGET=NUCLEO_L432KC
TARGET_CODE=0770
TARGET_SERIAL=0671FF555185754867150637
2 changes: 2 additions & 0 deletions bms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ target_sources(${APP_TARGET}
src/BmsThread.cpp
src/EnergusTempSensor.cpp
src/LTC6811.cpp
src/Can.cpp
)

target_link_libraries(${APP_TARGET}
PRIVATE
mbed-os
mbed-events
lib-mbed-ltc681x
)

Expand Down
4 changes: 2 additions & 2 deletions bms/src/Can.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "Can.h"
#include <cstdint>

CANMessage accBoardBootup () {
const char * startupMessage = 0x00;
CANMessage accBoardBootup() {
uint8_t startupMessage[8];
return CANMessage(kNMT_ACC_HEARTBEAT, startupMessage);
}

Expand Down
2 changes: 1 addition & 1 deletion bms/src/Can.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ constexpr uint32_t kNMT_ACC_HEARTBEAT = 0x702;
constexpr uint32_t kRPDO_MAX_CURRENTS = 0x286;

/* Bootup message */
CANMessage accBoardBootup ();
CANMessage accBoardBootup();

/* TPDO that sends various states and information about the accumulator */
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);
Expand Down
80 changes: 74 additions & 6 deletions bms/src/Main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <array>
#include <cstdint>
#include <cstdio>
#include <string>
#include <memory>
#include <vector>
#include <iostream>
Expand All @@ -17,7 +18,26 @@ CAN* canBus;

void initIO();
void canRX();
void canTX();

void canBootupTX();
void canBoardStateTX();
void canTempTX(uint8_t seg);
void canTempTX0();
void canTempTX1();
void canTempTX2();
void canTempTX3();
void canVoltTX(uint8_t seg);
void canVoltTX0();
void canVoltTX1();
void canVoltTX2();
void canVoltTX3();
void canCurrentLimTX();



EventQueue queue(4*EVENTS_EVENT_SIZE); // creates an eventqueue which is thread and ISR safe. EVENTS_EVENT_SIZE is the size of the buffer allocated



CircularBuffer<CANMessage, 32> canqueue;

Expand Down Expand Up @@ -174,9 +194,9 @@ int main() {
// divided by 0.625 for how the current sensor works :/
// divided by 300 because that's the nominal current reading of the sensor (ie baseline)
// multiplied by 10 and cast to a uint16 for 1 decimal place
tsCurrent = (uint16_t)(((current_sense_pin-current_vref_pin)/125.0)*10);

tsCurrent = ((uint16_t)((current_sense_pin-current_vref_pin)/125.0))*10;

queue.dispatch_once();
ThisThread::sleep_for(50 - (t.read_ms()%50));
}
}
Expand All @@ -185,6 +205,22 @@ void initIO() {
canBus = new CAN(BMS_PIN_CAN_RX, BMS_PIN_CAN_TX, BMS_CAN_FREQUENCY);
canBus->attach(canRX);

// canBus->write(accBoardBootup());

int canBootupUD = queue.call(&canBootupTX);
queue.dispatch_once();

int canBoardStateID = queue.call_every(100ms, &canBoardStateTX);
int canCurrentLimID = queue.call_every(40ms, &canCurrentLimTX);
int canVoltID0 = queue.call_every(200ms, &canVoltTX0);
int canVoltID1 = queue.call_every(200ms, &canVoltTX1);
int canVoltID2 = queue.call_every(200ms, &canVoltTX2);
int canVoltID3 = queue.call_every(200ms, &canVoltTX3);
int canTempID0 = queue.call_every(200ms, &canTempTX0);
int canTempID1 = queue.call_every(200ms, &canTempTX1);
int canTempID2 = queue.call_every(200ms, &canTempTX2);
int canTempID3 = queue.call_every(200ms, &canTempTX3);


fan_control_pin = 0; // turn fans off at start
charge_enable_pin = 0; // charge not allowed at start
Expand Down Expand Up @@ -223,7 +259,7 @@ void canBoardStateTX() {
));
}

void canBoardTempTX(uint8_t segment) {
void canTempTX(uint8_t segment) {
int8_t temps[7] = {
allTemps[(segment * BMS_BANK_CELL_COUNT)],
allTemps[(segment * BMS_BANK_CELL_COUNT) + 1],
Expand All @@ -236,7 +272,7 @@ void canBoardTempTX(uint8_t segment) {
canBus->write(accBoardTemp(segment, temps));
}

void canBoardVoltTX(uint8_t segment) {
void canVoltTX(uint8_t segment) {
uint16_t volts[7] = {
allVoltages[(segment * BMS_BANK_CELL_COUNT)],
allVoltages[(segment * BMS_BANK_CELL_COUNT) + 1],
Expand All @@ -249,8 +285,40 @@ void canBoardVoltTX(uint8_t segment) {
canBus->write(accBoardVolt(segment, volts));
}

void canVoltTX0() {
canVoltTX(0);
}

void canVoltTX1() {
canVoltTX(1);
}

void canVoltTX2() {
canVoltTX(2);
}

void canVoltTX3() {
canVoltTX(3);
}

void canTempTX0() {
canTempTX(0);
}

void canTempTX1() {
canTempTX(1);
}

void canTempTX2() {
canTempTX(2);
}

void canTempTX3() {
canTempTX(3);
}

void canCurrentLimTX() {
uint16_t chargeCurrentLimit = 0x0000;
uint16_t dischargeCurrentLimit = (uint16_t)(CAR_MAX_POWER/tsVoltage)*CAR_POWER_PERCENT;
uint16_t dischargeCurrentLimit = (uint16_t)(CAR_MAX_POWER/117.6)*CAR_POWER_PERCENT;
canBus->write(motorControllerCurrentLim(chargeCurrentLimit, dischargeCurrentLimit));
}
91 changes: 72 additions & 19 deletions mainCANbus.dbc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ BO_ 3221225472 VECTOR__INDEPENDENT_SIG_MSG: 0 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
SG_ Volt_Segment : 0|8@1+ (1,0) [0|0] "" Vector__XXX
SG_ Temp_Segment : 0|8@1+ (1,0) [0|255] "" 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
Expand All @@ -76,15 +78,41 @@ BO_ 386 TPDO_ACC_BOARD_State: 8 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: 8 Vector__XXX
SG_ TEMP_0 : 8|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ TEMP_1 : 16|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ TEMP_2 : 24|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ TEMP_3 : 32|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ TEMP_4 : 40|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ TEMP_5 : 48|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ TEMP_6 : 56|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ Temp_Segment : 0|8@1+ (1,0) [0|255] "" Vector__XXX
BO_ 393 TPDO_ACC_BOARD_Temp_0: 8 Vector__XXX
SG_ Seg0_TEMP_0 : 0|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ Seg0_TEMP_1 : 8|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ Seg0_TEMP_2 : 16|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ Seg0_TEMP_3 : 24|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ Seg0_TEMP_4 : 32|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ Seg0_TEMP_5 : 40|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ Seg0_TEMP_6 : 48|8@1+ (1,-40) [-40|215] "" Vector__XXX

BO_ 649 TPDO_ACC_BOARD_Temp_1: 8 Vector__XXX
SG_ Seg1_TEMP_0 : 0|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ Seg1_TEMP_1 : 8|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ Seg1_TEMP_2 : 16|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ Seg1_TEMP_3 : 24|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ Seg1_TEMP_4 : 32|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ Seg1_TEMP_5 : 40|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ Seg1_TEMP_6 : 48|8@1+ (1,-40) [-40|215] "" Vector__XXX

BO_ 905 TPDO_ACC_BOARD_Temp_2: 8 Vector__XXX
SG_ Seg2_TEMP_0 : 0|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ Seg2_TEMP_1 : 8|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ Seg2_TEMP_2 : 16|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ Seg2_TEMP_3 : 24|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ Seg2_TEMP_4 : 32|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ Seg2_TEMP_5 : 40|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ Seg2_TEMP_6 : 48|8@1+ (1,-40) [-40|215] "" Vector__XXX

BO_ 1161 TPDO_ACC_BOARD_Temp_3: 8 Vector__XXX
SG_ Seg3_TEMP_0 : 0|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ Seg3_TEMP_1 : 8|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ Seg3_TEMP_2 : 16|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ Seg3_TEMP_3 : 24|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ Seg3_TEMP_4 : 32|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ Seg3_TEMP_5 : 40|8@1+ (1,-40) [-40|215] "" Vector__XXX
SG_ Seg3_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
Expand All @@ -97,15 +125,41 @@ BO_ 644 TPDO_BSPD_BOARD_Brake: 8 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

BO_ 899 TPDO_ACC_BOARD_Volt: 8 Vector__XXX
SG_ Volt_Segment : 0|8@1+ (1,0) [0|0] "" Vector__XXX
SG_ VOLT_0 : 8|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX
SG_ VOLT_1 : 16|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX
SG_ VOLT_2 : 24|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX
SG_ VOLT_3 : 32|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX
SG_ VOLT_4 : 40|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX
SG_ VOLT_5 : 48|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX
SG_ VOLT_6 : 56|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX
BO_ 392 TPDO_ACC_BOARD_Volt_0: 8 Vector__XXX
SG_ Seg0_VOLT_0 : 0|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX
SG_ Seg0_VOLT_1 : 8|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX
SG_ Seg0_VOLT_2 : 16|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX
SG_ Seg0_VOLT_3 : 24|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX
SG_ Seg0_VOLT_4 : 32|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX
SG_ Seg0_VOLT_5 : 40|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX
SG_ Seg0_VOLT_6 : 48|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX

BO_ 648 TPDO_ACC_BOARD_Volt_1: 8 Vector__XXX
SG_ Seg1_VOLT_0 : 0|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX
SG_ Seg1_VOLT_1 : 8|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX
SG_ Seg1_VOLT_2 : 16|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX
SG_ Seg1_VOLT_3 : 24|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX
SG_ Seg1_VOLT_4 : 32|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX
SG_ Seg1_VOLT_5 : 40|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX
SG_ Seg1_VOLT_6 : 48|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX

BO_ 904 TPDO_ACC_BOARD_Volt_2: 8 Vector__XXX
SG_ Seg2_VOLT_0 : 0|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX
SG_ Seg2_VOLT_1 : 8|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX
SG_ Seg2_VOLT_2 : 16|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX
SG_ Seg2_VOLT_3 : 24|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX
SG_ Seg2_VOLT_4 : 32|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX
SG_ Seg2_VOLT_5 : 40|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX
SG_ Seg2_VOLT_6 : 48|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX

BO_ 1160 TPDO_ACC_BOARD_Volt_3: 8 Vector__XXX
SG_ Seg3_VOLT_0 : 0|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX
SG_ Seg3_VOLT_1 : 8|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX
SG_ Seg3_VOLT_2 : 16|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX
SG_ Seg3_VOLT_3 : 24|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX
SG_ Seg3_VOLT_4 : 32|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX
SG_ Seg3_VOLT_5 : 40|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX
SG_ Seg3_VOLT_6 : 48|8@1+ (0.02,0) [0|5.1] "V" Vector__XXX



Expand All @@ -125,6 +179,5 @@ BA_DEF_DEF_ "BusType" "";
BA_DEF_DEF_ "ECU" "";
BA_DEF_DEF_ "VFrameFormat" "StandardCAN";
BA_ "BusType" "CAN FD";
VAL_ 642 Temp_Segment 1 "Segment 0" 2 "Segment 1" 3 "Segment 2" 4 "Segment 3" ;
VAL_ 1794 Status 0 "Boot up" 4 "Stopped" 5 "Operational" 127 "Pre-operational" ;

0 comments on commit 94de02b

Please sign in to comment.