Skip to content

Commit 0839241

Browse files
committed
Working bms temp sense, prepping for CAN
1 parent 73e8857 commit 0839241

File tree

8 files changed

+219
-90
lines changed

8 files changed

+219
-90
lines changed

bms/.mbed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
ROOT=.
22
TARGET=NUCLEO_L432KC
33
TARGET_CODE=0770
4+
TARGET_SERIAL=0671FF555185754867150637

bms/src/BmsConfig.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,23 @@ extern DigitalOut* chargerControl;
7979
//
8080
// Units: millivolts
8181
#ifndef BMS_BALANCE_THRESHOLD
82-
#define BMS_BALANCE_THRESHOLD 3700
82+
#define BMS_BALANCE_THRESHOLD 3900
8383
#endif
8484

85+
// highest power allowed
86+
#ifndef CAR_MAX_POWER
87+
#define CAR_MAX_POWER 80000
88+
#endif
89+
90+
// percent of highest power allowed (for tolerance)
91+
#ifndef CAR_POWER_PERCENT
92+
#define CAR_POWER_PERCENT 0.95
93+
#endif
8594

95+
// percent of precharge needed to consider precharging done (and close the +AIR)
96+
#ifndef PRECHARGE_PERCENT
97+
#define PRECHARGE_PERCENT 0.95
98+
#endif
8699

87100

88101
// BMS Cell lookup

bms/src/BmsThread.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "LTC681xBus.h"
44
#include "LTC681xCommand.h"
55
#include "ThisThread.h"
6+
#include <algorithm>
67
#include <cstdint>
78
#include <cstdio>
89

@@ -90,6 +91,8 @@ void BMSThread::threadWorker() {
9091
std::array<uint16_t, BMS_BANK_COUNT * BMS_BANK_CELL_COUNT> allVoltages;
9192
std::array<int8_t, BMS_BANK_COUNT * BMS_BANK_TEMP_COUNT> allTemps;
9293
while (true) {
94+
95+
bool isBalancing = false;
9396

9497
while(!mainToBMSMailbox->empty()) {
9598
MainToBMSEvent *mainToBMSEvent;
@@ -210,7 +213,7 @@ void BMSThread::threadWorker() {
210213

211214
int8_t temp = convertTemp(tempVoltage / 10);
212215
// printf("%d: T: %d\n", j, temp);
213-
allTemps[j] = temp;
216+
allTemps[(BMS_BANK_CELL_COUNT * i) + j] = temp;
214217
}
215218

216219
for (int j = 0; j < 12; j++) {
@@ -235,7 +238,7 @@ void BMSThread::threadWorker() {
235238
maxVoltage = allVoltages[i];
236239
}
237240
}
238-
241+
239242
int8_t minTemp = allTemps[0];
240243
int8_t maxTemp = 0;
241244
for (int i = 0; i < BMS_BANK_COUNT * BMS_BANK_TEMP_COUNT; i++) {
@@ -245,7 +248,11 @@ void BMSThread::threadWorker() {
245248
maxTemp = allTemps[i];
246249
}
247250
}
248-
printf("min temp: %d, max temp: %d\nmin volt: %d, max volt %d\n", minTemp, maxTemp, minVoltage, maxVoltage);
251+
// printf("0 Temps: %d, %d, %d, %d, %d, %d, %d\n", allTemps[0], allTemps[1], allTemps[2], allTemps[3], allTemps[4], allTemps[5], allTemps[6]);
252+
// printf("1 Temps: %d, %d, %d, %d, %d, %d, %d\n", allTemps[7], allTemps[8], allTemps[9], allTemps[10], allTemps[11], allTemps[12], allTemps[13]);
253+
// printf("2 Temps: %d, %d, %d, %d, %d, %d, %d\n", allTemps[14], allTemps[15], allTemps[16], allTemps[17], allTemps[18], allTemps[19], allTemps[20]);
254+
// printf("3 Temps: %d, %d, %d, %d, %d, %d, %d\n\n", allTemps[21], allTemps[22], allTemps[23], allTemps[24], allTemps[25], allTemps[26], allTemps[27]);
255+
// printf("min temp: %d, max temp: %d\nmin volt: %d, max volt %d\n", minTemp, maxTemp, minVoltage, maxVoltage);
249256

250257
if (minVoltage <= BMS_FAULT_VOLTAGE_THRESHOLD_LOW ||
251258
maxVoltage >= BMS_FAULT_VOLTAGE_THRESHOLD_HIGH ||
@@ -271,6 +278,7 @@ void BMSThread::threadWorker() {
271278
cellVoltage >= minVoltage + BMS_DISCHARGE_THRESHOLD) {
272279
// printf("Balancing cell %d\?n", cellNum);
273280
dischargeValue |= (0x1 << j);
281+
isBalancing = true;
274282
}
275283
}
276284

@@ -304,6 +312,11 @@ void BMSThread::threadWorker() {
304312
msg->temperatureValues[i] = allTemps[i];
305313
}
306314
msg->bmsState = bmsState;
315+
msg->isBalancing = isBalancing;
316+
msg->minVolt = (uint8_t)(minVoltage*50/1000.0);
317+
msg->maxVolt = (uint8_t)(maxVoltage*50/1000.0);
318+
msg->minTemp = minTemp;
319+
msg->maxTemp = maxTemp;
307320
bmsEventMailbox->put((BmsEvent *)msg);
308321
}
309322

@@ -317,7 +330,5 @@ void BMSThread::threadWorker() {
317330
}
318331

319332
void BMSThread::throwBmsFault() {
320-
// bmsState = BMSThreadState::BMSFault;
321-
// palClearLine(LINE_BMS_FLT);
322-
// palSetLine(LINE_CHARGER_CONTROL);
333+
bmsState = BMSThreadState::BMSFault;
323334
}

bms/src/Can.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include "Can.h"
2+
#include <cstdint>
3+
4+
CANMessage accBoardBootup () {
5+
const char * startupMessage = 0x00;
6+
return CANMessage(kNMT_ACC_HEARTBEAT, startupMessage);
7+
}
8+
9+
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) {
10+
uint8_t data[8];
11+
data[0] = glvVoltage;
12+
data[1] = tsVoltage;
13+
data[2] = tsVoltage >> 8;
14+
data[3] = bmsFault + (bmsBalancing << 1) + (prechargeDone << 2) + (charging << 3) + (fansOn << 4) + (shutdownClosed << 5) + (unused_A << 6) +(unused_B << 7);
15+
data[4] = minCellVoltage;
16+
data[5] = maxCellVoltage;
17+
data[6] = tsCurrent;
18+
data[7] = tsCurrent >> 8;
19+
return CANMessage(kTPDO_ACC_BOARD_State, data);
20+
}
21+
22+
CANMessage accBoardTemp(uint8_t segment, int8_t *temps) {
23+
uint8_t data[8];
24+
data[0] = segment;
25+
for (int i = 0; i < 7; i++) {
26+
data[i+1] = (uint8_t)(temps[i]+40);
27+
}
28+
29+
return CANMessage(kTPDO_ACC_BOARD_Temp, data);
30+
}
31+
32+
CANMessage accBoardVolt(uint8_t segment, uint16_t *volts) {
33+
uint8_t data[8];
34+
data[0] = segment;
35+
for (int i = 0; i < 7; i++) {
36+
data[i+1] = (uint8_t) (50.0*volts[i]/1000.0);
37+
}
38+
39+
return CANMessage(kTPDO_ACC_BOARD_Volt, data);
40+
}
41+
42+
CANMessage motorControllerCurrentLim(uint16_t chargeCurLim, uint16_t dischargeCurLim) {
43+
uint8_t data[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
44+
data[0] = chargeCurLim;
45+
data[1] = chargeCurLim >> 8;
46+
data[2] = dischargeCurLim;
47+
data[3] = dischargeCurLim >> 8;
48+
return CANMessage(kRPDO_MAX_CURRENTS, data);
49+
}

bms/src/Can.h

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,50 +10,24 @@
1010

1111
// SIDs From Accumulator
1212
constexpr uint32_t kTPDO_ACC_BOARD_State = 0x182;
13-
constexpr uint32_t kTPDO_ACC_BOARD_Temp_0 = 0x282;
14-
constexpr uint32_t kTPDO_ACC_BOARD_Temp_1 = 0x382;
15-
constexpr uint32_t kTPDO_ACC_BOARD_Temp_2 = 0x482;
16-
constexpr uint32_t kTPDO_ACC_BOARD_Temp_3 = 0x582;
13+
constexpr uint32_t kTPDO_ACC_BOARD_Temp = 0x282;
14+
constexpr uint32_t kTPDO_ACC_BOARD_Volt = 0x382;
1715
constexpr uint32_t kNMT_ACC_HEARTBEAT = 0x702;
16+
constexpr uint32_t kRPDO_MAX_CURRENTS = 0x286;
1817

19-
CANMessage AccBoardBootup () {
20-
const char * startupMessage = 0x00;
21-
return CANMessage(kNMT_ACC_HEARTBEAT, startupMessage);
22-
}
23-
24-
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) {
25-
uint8_t data[8];
26-
data[0] = glvVoltage;
27-
data[1] = tsVoltage;
28-
data[2] = tsVoltage >> 8;
29-
data[3] = bmsFault + (bmsBalancing << 1) + (prechargeDone << 2) + (charging << 3) + (fansOn << 4) + (shutdownClosed << 5) + (unused_A << 6) +(unused_B << 7);
30-
data[4] = minCellVoltage;
31-
data[5] = maxCellVoltage;
32-
data[6] = tsCurrent;
33-
data[7] = tsCurrent >> 8;
34-
return CANMessage(kTPDO_ACC_BOARD_State, data);
35-
}
36-
37-
CANMessage AccBoardTemp(int segment, uint8_t *temps) {
38-
uint8_t data[8];
39-
for (int i = 0; i < 7; i++) {
40-
data[i] = temps[i];
41-
}
42-
int canID;
43-
switch (segment) {
44-
case 0:
45-
return kTPDO_ACC_BOARD_Temp_0;
46-
case 1:
47-
return kTPDO_ACC_BOARD_Temp_1;
48-
case 2:
49-
return kTPDO_ACC_BOARD_Temp_2;
50-
case 3:
51-
return kTPDO_ACC_BOARD_Temp_3;
52-
default:
53-
return NULL;
54-
break;
55-
}
56-
return CANMessage(canID, data);
57-
}
18+
/* Bootup message */
19+
CANMessage accBoardBootup ();
20+
21+
/* TPDO that sends various states and information about the accumulator */
22+
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);
23+
24+
/* TPDO that sends all temperatures for one segment */
25+
CANMessage accBoardTemp(uint8_t segment, int8_t *temps);
26+
27+
/* TPDO that sends all voltages for one segment */
28+
CANMessage accBoardVolt(uint8_t segment, uint16_t *voltages);
29+
30+
/* RPDO for limiting the current to the Motor Controller (AC-X1) */
31+
CANMessage motorControllerCurrentLim(uint16_t chargeCurLim, uint16_t dischargeCurLim);
5832

5933
#endif // _FS_BMS_SRC_CAN_H_

bms/src/Event.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ class BmsEvent {
1515
public:
1616
uint16_t voltageValues[BMS_BANK_COUNT * BMS_BANK_CELL_COUNT];
1717
int8_t temperatureValues[BMS_BANK_COUNT * BMS_BANK_TEMP_COUNT];
18+
uint8_t minVolt;
19+
uint8_t maxVolt;
20+
int8_t minTemp;
21+
int8_t maxTemp;
22+
bool isBalancing;
1823
BMSThreadState bmsState;
1924
};
2025

bms/src/Main.cpp

Lines changed: 79 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include "LTC681xParallelBus.h"
1111
#include "BmsThread.h"
1212

13+
#include "Can.h"
14+
1315

1416
CAN* canBus;
1517

@@ -40,13 +42,20 @@ AnalogIn glv_voltage_pin(ACC_GLV_VOLTAGE);
4042
bool prechargeDone = false;
4143
bool hasBmsFault = false;
4244
bool isCharging = false;
45+
bool hasFansOn = false;
46+
bool isBalancing = false;
4347

44-
uint32_t dcBusVoltage;
45-
uint32_t tsVoltage;
48+
uint16_t dcBusVoltage;
49+
uint16_t tsVoltage;
50+
uint8_t glvVoltage;
51+
uint16_t tsCurrent;
4652

4753
uint16_t allVoltages[BMS_BANK_COUNT*BMS_BANK_CELL_COUNT];
4854
int8_t allTemps[BMS_BANK_COUNT*BMS_BANK_TEMP_COUNT];
4955

56+
int8_t minCellVolt; // factor of 0.02 so 255 is 5.1 volts
57+
int8_t maxCellVolt; // factor of 0.02 so 255 is 5.1 volts
58+
5059
int main() {
5160

5261
printf("main\n");
@@ -74,7 +83,7 @@ int main() {
7483
Timer t;
7584
t.start();
7685
while (1) {
77-
int glv_voltage = glv_voltage_pin * 18530; // in mV
86+
glvVoltage = glv_voltage_pin * 18530; // in mV
7887
//printf("GLV voltage: %d mV\n", glv_voltage);
7988

8089
while (!bmsMailbox->empty()) {
@@ -93,6 +102,10 @@ int main() {
93102
case BMSThreadState::BMSIdle:
94103
hasBmsFault = false;
95104

105+
minCellVolt = bmsEvent->minVolt;
106+
maxCellVolt = bmsEvent->maxVolt;
107+
isBalancing = bmsEvent->isBalancing;
108+
96109
tsVoltage = 0;
97110

98111
for (int i = 0; i < BMS_BANK_COUNT*BMS_BANK_CELL_COUNT; i++) {
@@ -140,21 +153,28 @@ int main() {
140153
}
141154

142155

143-
if (dcBusVoltage >= tsVoltage * 0.95) {
156+
if (dcBusVoltage >= tsVoltage * PRECHARGE_PERCENT) {
144157
prechargeDone = true;
145158
}
146159

147160
precharge_control_pin = prechargeDone;
148161
bms_fault_pin = hasBmsFault;
149162

150-
if (prechargeDone || charge_state_pin) {
151-
fan_control_pin = true;
152-
}
163+
hasFansOn = (prechargeDone || charge_state_pin);
164+
153165

154166
isCharging = charge_state_pin;
155167
// printf("charge state: %x\n", isCharging);
156168

157169
charge_enable_pin = isCharging && !hasBmsFault && shutdown_measure_pin;
170+
fan_control_pin = hasFansOn;
171+
172+
173+
// times 1.5 to change from 3.3v to 5v
174+
// divided by 0.625 for how the current sensor works :/
175+
// divided by 300 because that's the nominal current reading of the sensor (ie baseline)
176+
// multiplied by 10 and cast to a uint16 for 1 decimal place
177+
tsCurrent = (uint16_t)(((current_sense_pin-current_vref_pin)/125.0)*10);
158178

159179

160180
ThisThread::sleep_for(50 - (t.read_ms()%50));
@@ -181,6 +201,56 @@ void canRX() {
181201
}
182202
}
183203

184-
void canTX() {
185-
204+
void canBootupTX() {
205+
canBus->write(accBoardBootup());
206+
}
207+
208+
void canBoardStateTX() {
209+
canBus->write(accBoardState(
210+
glvVoltage,
211+
tsVoltage,
212+
hasBmsFault,
213+
isBalancing,
214+
prechargeDone,
215+
isCharging,
216+
hasFansOn,
217+
shutdown_measure_pin,
218+
false,
219+
false,
220+
minCellVolt,
221+
maxCellVolt,
222+
tsCurrent
223+
));
224+
}
225+
226+
void canBoardTempTX(uint8_t segment) {
227+
int8_t temps[7] = {
228+
allTemps[(segment * BMS_BANK_CELL_COUNT)],
229+
allTemps[(segment * BMS_BANK_CELL_COUNT) + 1],
230+
allTemps[(segment * BMS_BANK_CELL_COUNT) + 2],
231+
allTemps[(segment * BMS_BANK_CELL_COUNT) + 3],
232+
allTemps[(segment * BMS_BANK_CELL_COUNT) + 4],
233+
allTemps[(segment * BMS_BANK_CELL_COUNT) + 5],
234+
allTemps[(segment * BMS_BANK_CELL_COUNT) + 6]
235+
};
236+
canBus->write(accBoardTemp(segment, temps));
237+
}
238+
239+
void canBoardVoltTX(uint8_t segment) {
240+
uint16_t volts[7] = {
241+
allVoltages[(segment * BMS_BANK_CELL_COUNT)],
242+
allVoltages[(segment * BMS_BANK_CELL_COUNT) + 1],
243+
allVoltages[(segment * BMS_BANK_CELL_COUNT) + 2],
244+
allVoltages[(segment * BMS_BANK_CELL_COUNT) + 3],
245+
allVoltages[(segment * BMS_BANK_CELL_COUNT) + 4],
246+
allVoltages[(segment * BMS_BANK_CELL_COUNT) + 5],
247+
allVoltages[(segment * BMS_BANK_CELL_COUNT) + 6]
248+
};
249+
canBus->write(accBoardVolt(segment, volts));
250+
}
251+
252+
void canCurrentLimTX() {
253+
uint16_t chargeCurrentLimit = 0x0000;
254+
uint16_t dischargeCurrentLimit = (uint16_t)(CAR_MAX_POWER/tsVoltage)*CAR_POWER_PERCENT;
255+
canBus->write(motorControllerCurrentLim(chargeCurrentLimit, dischargeCurrentLimit));
186256
}

0 commit comments

Comments
 (0)