1
+ #include < algorithm>
1
2
#include < array>
2
3
#include < cstdint>
3
4
#include < cstdio>
@@ -35,7 +36,7 @@ void canCurrentLimTX();
35
36
36
37
37
38
38
- 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
39
+ 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
39
40
40
41
41
42
@@ -66,7 +67,8 @@ bool hasFansOn = false;
66
67
bool isBalancing = false ;
67
68
68
69
uint16_t dcBusVoltage;
69
- uint16_t tsVoltage;
70
+ uint32_t tsVoltagemV;
71
+ // uint16_t tsVoltage;
70
72
uint8_t glvVoltage;
71
73
uint16_t tsCurrent;
72
74
@@ -103,8 +105,8 @@ int main() {
103
105
Timer t;
104
106
t.start ();
105
107
while (1 ) {
106
- glvVoltage = glv_voltage_pin * 18530 ; // in mV
107
- // printf("GLV voltage: %d mV\n", glv_voltage );
108
+ glvVoltage = ( uint8_t )( glv_voltage_pin * 185.3 ) ; // in mV
109
+ // printf("GLV voltage: %d mV\n", glvVoltage * 100 );
108
110
109
111
while (!bmsMailbox->empty ()) {
110
112
BmsEvent *bmsEvent;
@@ -126,16 +128,16 @@ int main() {
126
128
maxCellVolt = bmsEvent->maxVolt ;
127
129
isBalancing = bmsEvent->isBalancing ;
128
130
129
- tsVoltage = 0 ;
131
+ tsVoltagemV = 0 ;
130
132
131
133
for (int i = 0 ; i < BMS_BANK_COUNT*BMS_BANK_CELL_COUNT; i++) {
132
134
allVoltages[i] = bmsEvent->voltageValues [i];
133
- tsVoltage += allVoltages[i];
135
+ tsVoltagemV += allVoltages[i];
134
136
// printf("%d, V: %d\n", i, allVoltages[i]);
135
137
}
136
138
for (int i = 0 ; i < BMS_BANK_COUNT*BMS_BANK_TEMP_COUNT; i++) {
137
139
allTemps[i] = bmsEvent->temperatureValues [i];
138
- // printf("%d, V : %d\n", i, allTemps[i]);
140
+ // printf("%d, T : %d\n", i, allTemps[i]);
139
141
}
140
142
141
143
break ;
@@ -173,7 +175,7 @@ int main() {
173
175
}
174
176
175
177
176
- if (dcBusVoltage >= tsVoltage * PRECHARGE_PERCENT) {
178
+ if (dcBusVoltage >= ( uint16_t )(tsVoltagemV/ 100.0 ) * PRECHARGE_PERCENT) {
177
179
prechargeDone = true ;
178
180
}
179
181
@@ -205,21 +207,19 @@ void initIO() {
205
207
canBus = new CAN (BMS_PIN_CAN_RX, BMS_PIN_CAN_TX, BMS_CAN_FREQUENCY);
206
208
canBus->attach (canRX);
207
209
208
- // canBus->write(accBoardBootup());
209
-
210
- int canBootupUD = queue.call (&canBootupTX);
210
+ queue.call (&canBootupTX);
211
211
queue.dispatch_once ();
212
212
213
- int canBoardStateID = queue.call_every (100ms, &canBoardStateTX);
214
- int canCurrentLimID = queue.call_every (40ms, &canCurrentLimTX);
215
- int canVoltID0 = queue.call_every (200ms, &canVoltTX0);
216
- int canVoltID1 = queue.call_every (200ms, &canVoltTX1);
217
- int canVoltID2 = queue.call_every (200ms, &canVoltTX2);
218
- int canVoltID3 = queue.call_every (200ms, &canVoltTX3);
219
- int canTempID0 = queue.call_every (200ms, &canTempTX0);
220
- int canTempID1 = queue.call_every (200ms, &canTempTX1);
221
- int canTempID2 = queue.call_every (200ms, &canTempTX2);
222
- int canTempID3 = queue.call_every (200ms, &canTempTX3);
213
+ queue.call_every (100ms, &canBoardStateTX);
214
+ queue.call_every ( 40ms, &canCurrentLimTX);
215
+ queue.call_every (200ms, &canVoltTX0);
216
+ queue.call_every (200ms, &canVoltTX1);
217
+ queue.call_every (200ms, &canVoltTX2);
218
+ queue.call_every (200ms, &canVoltTX3);
219
+ queue.call_every (200ms, &canTempTX0);
220
+ queue.call_every (200ms, &canTempTX1);
221
+ queue.call_every (200ms, &canTempTX2);
222
+ queue.call_every (200ms, &canTempTX3);
223
223
224
224
225
225
fan_control_pin = 0 ; // turn fans off at start
@@ -244,7 +244,7 @@ void canBootupTX() {
244
244
void canBoardStateTX () {
245
245
canBus->write (accBoardState (
246
246
glvVoltage,
247
- tsVoltage ,
247
+ ( uint16_t )(tsVoltagemV/ 100.0 ) ,
248
248
hasBmsFault,
249
249
isBalancing,
250
250
prechargeDone,
@@ -257,6 +257,7 @@ void canBoardStateTX() {
257
257
maxCellVolt,
258
258
tsCurrent
259
259
));
260
+ ThisThread::sleep_for (1ms);
260
261
}
261
262
262
263
void canTempTX (uint8_t segment) {
@@ -270,6 +271,7 @@ void canTempTX(uint8_t segment) {
270
271
allTemps[(segment * BMS_BANK_CELL_COUNT) + 6 ]
271
272
};
272
273
canBus->write (accBoardTemp (segment, temps));
274
+ ThisThread::sleep_for (1ms);
273
275
}
274
276
275
277
void canVoltTX (uint8_t segment) {
@@ -283,6 +285,14 @@ void canVoltTX(uint8_t segment) {
283
285
allVoltages[(segment * BMS_BANK_CELL_COUNT) + 6 ]
284
286
};
285
287
canBus->write (accBoardVolt (segment, volts));
288
+ ThisThread::sleep_for (1ms);
289
+ }
290
+
291
+ void canCurrentLimTX () {
292
+ uint16_t chargeCurrentLimit = 0x0000 ;
293
+ 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);
294
+ canBus->write (motorControllerCurrentLim (chargeCurrentLimit, dischargeCurrentLimit));
295
+ ThisThread::sleep_for (1ms);
286
296
}
287
297
288
298
void canVoltTX0 () {
@@ -316,9 +326,3 @@ void canTempTX2() {
316
326
void canTempTX3 () {
317
327
canTempTX (3 );
318
328
}
319
-
320
- void canCurrentLimTX () {
321
- uint16_t chargeCurrentLimit = 0x0000 ;
322
- uint16_t dischargeCurrentLimit = (uint16_t )(CAR_MAX_POWER/117.6 )*CAR_POWER_PERCENT;
323
- canBus->write (motorControllerCurrentLim (chargeCurrentLimit, dischargeCurrentLimit));
324
- }
0 commit comments