Skip to content

Commit

Permalink
More CAN changes; temp not working over CAN
Browse files Browse the repository at this point in the history
  • Loading branch information
Mayamelon committed May 20, 2024
1 parent 94de02b commit d277335
Show file tree
Hide file tree
Showing 8 changed files with 538 additions and 68 deletions.
1 change: 1 addition & 0 deletions bms/.mbed
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ROOT=.
TARGET=NUCLEO_L432KC
TARGET_CODE=0770
TARGET_SERIAL=0671FF555185754867150637
4 changes: 4 additions & 0 deletions bms/src/BmsConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ extern DigitalOut* chargerControl;
#ifndef CAR_POWER_PERCENT
#define CAR_POWER_PERCENT 0.95
#endif
// maximum allowed current draw by the motor controller
#ifndef CAR_CURRENT_MAX
#define CAR_CURRENT_MAX 700
#endif

// percent of precharge needed to consider precharging done (and close the +AIR)
#ifndef PRECHARGE_PERCENT
Expand Down
45 changes: 37 additions & 8 deletions bms/src/Can.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Can.h"
#include <cstdint>
#include <cstdio>

CANMessage accBoardBootup() {
uint8_t startupMessage[8];
Expand All @@ -20,23 +21,51 @@ CANMessage accBoardState(uint8_t glvVoltage, uint16_t tsVoltage, bool bmsFault,
}

CANMessage accBoardTemp(uint8_t segment, int8_t *temps) {
uint8_t data[8];
data[0] = segment;
uint8_t data[7];
uint32_t id;
switch (segment) {
case 0:
id = kTPDO_ACC_BOARD_Temp_0;
break;
case 1:
id = kTPDO_ACC_BOARD_Temp_1;
break;
case 2:
id = kTPDO_ACC_BOARD_Temp_2;
break;
case 3:
id = kTPDO_ACC_BOARD_Temp_3;
break;
}
for (int i = 0; i < 7; i++) {
data[i+1] = (uint8_t)(temps[i]+40);
data[i] = (uint8_t)(temps[i]);
}

return CANMessage(kTPDO_ACC_BOARD_Temp, data);
return CANMessage(id, data);
}

CANMessage accBoardVolt(uint8_t segment, uint16_t *volts) {
uint8_t data[8];
data[0] = segment;
uint8_t data[7];
uint32_t id;
switch (segment) {
case 0:
id = kTPDO_ACC_BOARD_Volt_0;
break;
case 1:
id = kTPDO_ACC_BOARD_Volt_1;
break;
case 2:
id = kTPDO_ACC_BOARD_Volt_2;
break;
case 3:
id = kTPDO_ACC_BOARD_Volt_3;
break;
}
for (int i = 0; i < 7; i++) {
data[i+1] = (uint8_t) (50.0*volts[i]/1000.0);
data[i] = (uint8_t) (50.0*volts[i]/1000.0);
}

return CANMessage(kTPDO_ACC_BOARD_Volt, data);
return CANMessage(id, data);
}

CANMessage motorControllerCurrentLim(uint16_t chargeCurLim, uint16_t dischargeCurLim) {
Expand Down
22 changes: 18 additions & 4 deletions bms/src/Can.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,29 @@

// SIDs From Accumulator
constexpr uint32_t kTPDO_ACC_BOARD_State = 0x182;
constexpr uint32_t kTPDO_ACC_BOARD_Temp = 0x282;
constexpr uint32_t kTPDO_ACC_BOARD_Volt = 0x382;

constexpr uint32_t kTPDO_ACC_BOARD_Temp_0 = 0x189;
constexpr uint32_t kTPDO_ACC_BOARD_Temp_1 = 0x289;
constexpr uint32_t kTPDO_ACC_BOARD_Temp_2 = 0x389;
constexpr uint32_t kTPDO_ACC_BOARD_Temp_3 = 0x489;

constexpr uint32_t kTPDO_ACC_BOARD_Volt_0 = 0x188;
constexpr uint32_t kTPDO_ACC_BOARD_Volt_1 = 0x288;
constexpr uint32_t kTPDO_ACC_BOARD_Volt_2 = 0x388;
constexpr uint32_t kTPDO_ACC_BOARD_Volt_3 = 0x488;

constexpr uint32_t kNMT_ACC_HEARTBEAT = 0x702;
constexpr uint32_t kRPDO_MAX_CURRENTS = 0x286;

/* Bootup message */
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);
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);

/* TPDO that sends all temperatures for one segment */
CANMessage accBoardTemp(uint8_t segment, int8_t *temps);
Expand All @@ -28,6 +41,7 @@ CANMessage accBoardTemp(uint8_t segment, int8_t *temps);
CANMessage accBoardVolt(uint8_t segment, uint16_t *voltages);

/* RPDO for limiting the current to the Motor Controller (AC-X1) */
CANMessage motorControllerCurrentLim(uint16_t chargeCurLim, uint16_t dischargeCurLim);
CANMessage motorControllerCurrentLim(uint16_t chargeCurLim,
uint16_t dischargeCurLim);

#endif // _FS_BMS_SRC_CAN_H_
60 changes: 32 additions & 28 deletions bms/src/Main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <algorithm>
#include <array>
#include <cstdint>
#include <cstdio>
Expand Down Expand Up @@ -35,7 +36,7 @@ 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
EventQueue queue(32*EVENTS_EVENT_SIZE); // creates an eventqueue which is thread and ISR safe. EVENTS_EVENT_SIZE is the size of the buffer allocated



Expand Down Expand Up @@ -66,7 +67,8 @@ bool hasFansOn = false;
bool isBalancing = false;

uint16_t dcBusVoltage;
uint16_t tsVoltage;
uint32_t tsVoltagemV;
//uint16_t tsVoltage;
uint8_t glvVoltage;
uint16_t tsCurrent;

Expand Down Expand Up @@ -103,8 +105,8 @@ int main() {
Timer t;
t.start();
while (1) {
glvVoltage = glv_voltage_pin * 18530; // in mV
//printf("GLV voltage: %d mV\n", glv_voltage);
glvVoltage = (uint8_t)(glv_voltage_pin * 185.3); // in mV
//printf("GLV voltage: %d mV\n", glvVoltage * 100);

while (!bmsMailbox->empty()) {
BmsEvent *bmsEvent;
Expand All @@ -126,16 +128,16 @@ int main() {
maxCellVolt = bmsEvent->maxVolt;
isBalancing = bmsEvent->isBalancing;

tsVoltage = 0;
tsVoltagemV = 0;

for (int i = 0; i < BMS_BANK_COUNT*BMS_BANK_CELL_COUNT; i++) {
allVoltages[i] = bmsEvent->voltageValues[i];
tsVoltage += allVoltages[i];
tsVoltagemV += allVoltages[i];
//printf("%d, V: %d\n", i, allVoltages[i]);
}
for (int i = 0; i < BMS_BANK_COUNT*BMS_BANK_TEMP_COUNT; i++) {
allTemps[i] = bmsEvent->temperatureValues[i];
//printf("%d, V: %d\n", i, allTemps[i]);
// printf("%d, T: %d\n", i, allTemps[i]);
}

break;
Expand Down Expand Up @@ -173,7 +175,7 @@ int main() {
}


if (dcBusVoltage >= tsVoltage * PRECHARGE_PERCENT) {
if (dcBusVoltage >= (uint16_t)(tsVoltagemV/100.0) * PRECHARGE_PERCENT) {
prechargeDone = true;
}

Expand Down Expand Up @@ -205,21 +207,19 @@ 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.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);
queue.call_every(100ms, &canBoardStateTX);
queue.call_every( 40ms, &canCurrentLimTX);
queue.call_every(200ms, &canVoltTX0);
queue.call_every(200ms, &canVoltTX1);
queue.call_every(200ms, &canVoltTX2);
queue.call_every(200ms, &canVoltTX3);
queue.call_every(200ms, &canTempTX0);
queue.call_every(200ms, &canTempTX1);
queue.call_every(200ms, &canTempTX2);
queue.call_every(200ms, &canTempTX3);


fan_control_pin = 0; // turn fans off at start
Expand All @@ -244,7 +244,7 @@ void canBootupTX() {
void canBoardStateTX() {
canBus->write(accBoardState(
glvVoltage,
tsVoltage,
(uint16_t)(tsVoltagemV/100.0),
hasBmsFault,
isBalancing,
prechargeDone,
Expand All @@ -257,6 +257,7 @@ void canBoardStateTX() {
maxCellVolt,
tsCurrent
));
ThisThread::sleep_for(1ms);
}

void canTempTX(uint8_t segment) {
Expand All @@ -270,6 +271,7 @@ void canTempTX(uint8_t segment) {
allTemps[(segment * BMS_BANK_CELL_COUNT) + 6]
};
canBus->write(accBoardTemp(segment, temps));
ThisThread::sleep_for(1ms);
}

void canVoltTX(uint8_t segment) {
Expand All @@ -283,6 +285,14 @@ void canVoltTX(uint8_t segment) {
allVoltages[(segment * BMS_BANK_CELL_COUNT) + 6]
};
canBus->write(accBoardVolt(segment, volts));
ThisThread::sleep_for(1ms);
}

void canCurrentLimTX() {
uint16_t chargeCurrentLimit = 0x0000;
uint16_t dischargeCurrentLimit = (uint16_t)(((CAR_MAX_POWER/(tsVoltagemV/1000.0))*CAR_POWER_PERCENT < CAR_CURRENT_MAX) ? (CAR_MAX_POWER/(tsVoltagemV/1000.0)*CAR_POWER_PERCENT) : CAR_CURRENT_MAX);
canBus->write(motorControllerCurrentLim(chargeCurrentLimit, dischargeCurrentLimit));
ThisThread::sleep_for(1ms);
}

void canVoltTX0() {
Expand Down Expand Up @@ -316,9 +326,3 @@ void canTempTX2() {
void canTempTX3() {
canTempTX(3);
}

void canCurrentLimTX() {
uint16_t chargeCurrentLimit = 0x0000;
uint16_t dischargeCurrentLimit = (uint16_t)(CAR_MAX_POWER/117.6)*CAR_POWER_PERCENT;
canBus->write(motorControllerCurrentLim(chargeCurrentLimit, dischargeCurrentLimit));
}
Loading

0 comments on commit d277335

Please sign in to comment.