-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathController.cpp
More file actions
681 lines (613 loc) · 24.6 KB
/
Controller.cpp
File metadata and controls
681 lines (613 loc) · 24.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
#include "FlexCAN.h"
#include "Controller.hpp"
/////////////////////////////////////////////////
/// \brief When instantiated, the controller is in the init state ensuring that all the signal pins are set properly.
/////////////////////////////////////////////////
Controller::Controller()
: faultModuleLoop(String("ModuleLoop"), String("A"), true, true, String("One or more BMS modules have asserted the fault loop!\n"), String("All modules have deasserted the fault loop\n")),
faultBatMon(String("BatMon"), String("B"), false, true, String("The battery monitor asserted a fault!\n"), String("The battery monitor deasserted a fault\n")),
faultBMSSerialComms(String("BMSSerialComms"), String("C"), true, true, String("Serial communication with battery modules lost!\n"), String("Serial communication with battery modules re-established!\n")),
faultBMSOV(String("BMSOV"), String("D"), true, true, String("A cell reached a voltage higher than the OV threshold\n"), String("All cells are back under OV threshold\n")),
faultBMSUV(String("BMSUV"), String("E"), false, true, String("A cell reached a voltage lower than the UV threshold\n"), String("All cells are back over UV threshold\n")),
faultBMSOT(String("BMSOT"), String("F"), true, true, String("A module reached a temp higher than the OT threshold\n"), String("All modules are back under OT threshold\n")),
faultBMSUT(String("BMSUT"), String("G"), true, true, String("A module reached a temp lower than the UT threshold\n"), String("All modules are back over UT threshold\n")),
fault12VBatOV(String("12VBatOV"), String("H"), false, false, String("12V battery reached a voltage higher than the OV threshold\n"), String("12V battery back under the OV threshold\n")),
fault12VBatUV(String("12VBatUV"), String("I"), false, true, String("12V battery reached a voltage lower than the UV threshold\n"), String("12V battery back over the UV threshold\n")),
faultWatSen1(String("WatSen1"), String("J"), true, true, String("The battery water sensor 1 is reporting water!\n"), String("The battery water sensor 1 is reporting dry.\n")),
faultWatSen2(String("WatSen2"), String("K"), true, true, String("The battery water sensor 2 is reporting water!\n"), String("The battery water sensor 2 is reporting dry.\n")),
faultIncorectModuleCount(String("IncorectModuleCount"), String("L"), true, true, String("Found a different ammount of modules than configured!\n"), String("Found all modules as configured!\n")),
bms(&settings) {
state = INIT;
msg.ext = 1;
msg.id = BMS_EVCC_STATUS_IND;
msg.len = 5;
msg.buf[0] = 0;
msg.buf[1] = 0;
msg.buf[2] = 0;
msg.buf[3] = 0;
msg.buf[4] = 0;
msgStatusIns.bBMSStatusFlags = 0;
msgStatusIns.bBmsId = 0;
msgStatusIns.bBMSFault = 0;
msgStatusIns.bReserved2 = 0;
msgStatusIns.bReserved3 = 0;
//try to load object
settings.loadAllSettingsFromEEPROM(0);
if (!((settings.magic_bytes.valueMatchDefault()) && (settings.eeprom_version.valueMatchDefault()))) {
Serial.print("EEPROM does not match magicbytes and version\n");
(void)reloadDefaultSettings();
Serial.print("Default values reloaded\n");
} else {
Serial.printf("Loaded config from EEPROM|| magicbytes: 0x%X, version = %d\n", settings.magic_bytes.getVal(), settings.eeprom_version.getVal());
}
faults.push_back(&faultModuleLoop);
faults.push_back(&faultBatMon);
faults.push_back(&faultBMSSerialComms);
faults.push_back(&faultBMSOV);
faults.push_back(&faultBMSUV);
faults.push_back(&faultBMSOT);
faults.push_back(&faultBMSUT);
faults.push_back(&fault12VBatOV);
faults.push_back(&fault12VBatUV);
faults.push_back(&faultWatSen1);
faults.push_back(&faultWatSen2);
faults.push_back(&faultIncorectModuleCount);
//Serial.print("Controller faults created\n");
}
int32_t Controller::saveSettings() {
settings.saveAllSettingsToEEPROM(0);
return 0;
}
int32_t Controller::reloadDefaultSettings() {
settings.reloadDefaultSettings();
return saveSettings();
}
/////////////////////////////////////////////////
/// \brief Orchestrates the activities within the BMS via a state machine.
/////////////////////////////////////////////////
void Controller::doController() {
//Serial.print("doController\n");
static int ticks = 0;
static unsigned int bat12Vcyclestart = 0;
static int standbyTicks = 1; //1 because ticks slow down
const int stateticks = 4;
msgStatusIns.bBMSStatusFlags = 0;
msgStatusIns.bBMSFault = 0;
if (canOn == false && settings.canbus_to_EVCC.getVal() == 1) {
Can0.begin(settings.canbus_speed.getVal());
canOn = true;
}
if (canOn == true && settings.canbus_to_EVCC.getVal() == 0) {
Can0.end();
canOn = false;
}
//Serial.print("doController can done\n");
bat12vVoltage = (float)analogRead(INA_12V_BAT) / settings.bat12v_scaling_divisor.getVal();
if (state != INIT) syncModuleDataObjects();
//Serial.print("doController syncModuleDataObjects\n");
//figure out state transition
switch (state) {
/**************** ****************/
case INIT:
if (ticks >= stateticks) {
ticks = 0;
state = STANDBY;
lastResetTimeStamp = now();
LOG_INFO("Transition to STANDBY\n");
}
break;
/**************** ****************/
case STANDBY:
#ifdef STATECYCLING
if (ticks >= stateticks) {
ticks = 0;
state = PRE_CHARGE;
LOG_INFO("Transition to PRE_CHARGE\n");
}
#else
if (dc2dcON_H == 0 && bat12vVoltage < settings.dc2dc_cycle_v_setpoint.getVal()) {
bat12Vcyclestart = (millis() / 1000);
dc2dcON_H = 1;
} else if (dc2dcON_H == 1 && ((millis() / 1000) > bat12Vcyclestart + settings.dc2dc_cycle_time_s.getVal())) {
// I am ignoring the time counter loop around as it will simply result in a short charge cycle
// followed with a proper charging cycle.
dc2dcON_H = 0;
}
if (digitalRead(INH_RUN) == HIGH) {
ticks = 0;
state = RUN;
LOG_INFO("Transition to RUN\n");
} else if (digitalRead(INH_CHARGING) == HIGH) {
ticks = 0;
state = CHARGING;
LOG_INFO("Transition to CHARGING\n");
} else if (bms.getHighCellVolt() < settings.charger_cycle_v_setpoint.getVal() && bms.getHighCellVolt() < settings.max_charge_v_setpoint.getVal() && ticks >= standbyTicks) {
ticks = 0;
state = PRE_CHARGE;
LOG_INFO("Transition to PRE_CHARGE\n");
}
#endif
break;
/**************** ****************/
case PRE_CHARGE:
#ifdef STATECYCLING
if (ticks >= stateticks) {
ticks = 0;
state = CHARGING;
LOG_INFO("Transition to CHARGING\n");
}
#else
if (ticks >= 10 && ticks <= 100) { //adjust to give time to the EVCC to properly boot (5 ticks == 1 seconds)
if ((digitalRead(INL_EVSE_DISC) == LOW)) {
ticks = 0;
state = STANDBY;
LOG_INFO("Transition to STANDBY\n");
} else if (digitalRead(INH_CHARGING) == HIGH) {
ticks = 0;
state = CHARGING;
LOG_INFO("Transition to CHARGING\n");
}
} else if (ticks > 100) {
ticks = 0;
state = STANDBY;
LOG_ERR("charger did not start!!!\n");
LOG_INFO("Transition to STANDBY\n");
}
#endif
break;
/**************** ****************/
case CHARGING:
#ifdef STATECYCLING
if (ticks >= stateticks) {
ticks = 0;
state = TOP_BALANCING;
LOG_INFO("Transition to TOP_BALANCING\n");
}
#else
if (ticks >= 5 && (digitalRead(INL_EVSE_DISC) == LOW || digitalRead(INH_CHARGING) == LOW)) {
ticks = 0;
state = POST_CHARGE;
LOG_INFO("Transition to POST_CHARGE\n");
} else if (digitalRead(INL_EVSE_DISC) == LOW || digitalRead(INH_CHARGING) == LOW) {
//debounce error by letting ticks go up to 5.
//LOG_INFO("INL_EVSE_DISC == LOW || INH_CHARGING == LOW\n");
if (digitalRead(INL_EVSE_DISC) == LOW) {
LOG_INFO("INL_EVSE_DISC == LOW\n");
} else if (digitalRead(INH_CHARGING) == LOW) {
LOG_INFO("INH_CHARGING == LOW\n");
}
} else if (bms.getHighCellVolt() >= settings.top_balance_v_setpoint.getVal()) {
ticks = 0;
state = TOP_BALANCING;
LOG_INFO("Transition to TOP_BALANCING\n");
} else {
ticks = 0;
}
#endif
break;
/**************** ****************/
case TOP_BALANCING:
#ifdef STATECYCLING
if (ticks >= stateticks) {
ticks = 0;
state = POST_CHARGE;
LOG_INFO("Transition to POST_CHARGE\n");
}
#else
if (ticks >= 5 && (digitalRead(INL_EVSE_DISC) == LOW || digitalRead(INH_CHARGING) == LOW)) {
ticks = 0;
state = POST_CHARGE;
LOG_INFO("Transition to POST_CHARGE\n");
} else if (digitalRead(INL_EVSE_DISC) == LOW || digitalRead(INH_CHARGING) == LOW) {
//debounce error by letting ticks go up to 5.
//msgStatusIns.bBMSStatusFlags |= BMS_STATUS_CELL_BVC_FLAG;
LOG_INFO("INL_EVSE_DISC == LOW || INH_CHARGING == LOW\n");
} else {
//msgStatusIns.bBMSStatusFlags |= BMS_STATUS_CELL_BVC_FLAG;
ticks = 0;
}
#endif
break;
/**************** ****************/
case POST_CHARGE:
#ifdef STATECYCLING
if (ticks >= stateticks) {
ticks = 0;
state = RUN;
LOG_INFO("Transition to RUN\n");
}
#else
//adjust to give time to the EVCC to properly go to sleep (5 ticks == 1 seconds)
//This will happen if the loop evcc input (fault line) is asserted.
if (ticks >= 50 && digitalRead(INH_CHARGING) == LOW) {
ticks = 0;
state = STANDBY;
LOG_INFO("Transition to STANDBY\n");
}
#endif
break;
/**************** ****************/
case RUN:
#ifdef STATECYCLING
if (ticks >= stateticks) {
ticks = 0;
state = INIT;
LOG_INFO("Transition to INIT\n");
}
#else
if (digitalRead(INH_RUN) == LOW) {
ticks = 0;
state = STANDBY;
LOG_INFO("Transition to STANDBY\n");
}
#endif
break;
/**************** ****************/
default:
break;
}
//execute state
switch (state) {
case INIT:
period = LOOP_PERIOD_ACTIVE_MS;
init();
break;
case STANDBY:
//prevents sleeping if the console is connected or if within 10 minutes of a hard reset.
//The teensy wont let reprogram if it slept once so this allows reprograming within 10 minutes.
if (SERIALCONSOLE || millis() < settings.time_before_first_sleep.getVal()) {
period = LOOP_PERIOD_ACTIVE_MS;
standbyTicks = 10;
} else {
period = LOOP_PERIOD_STANDBY_MS;
standbyTicks = 1;
}
standby();
break;
case PRE_CHARGE:
period = LOOP_PERIOD_ACTIVE_MS;
pre_charge();
break;
case CHARGING:
period = LOOP_PERIOD_ACTIVE_MS;
charging();
break;
case TOP_BALANCING:
period = LOOP_PERIOD_ACTIVE_MS;
top_balancing();
break;
case POST_CHARGE:
period = LOOP_PERIOD_ACTIVE_MS;
post_charge();
break;
case RUN:
period = LOOP_PERIOD_ACTIVE_MS;
run();
break;
default:
period = LOOP_PERIOD_ACTIVE_MS;
break;
}
ticks++;
//set outputs
setOutput(OUTL_EVCC_ON, outL_evcc_on_buffer);
setOutput(OUTH_FAULT, outH_fault_buffer);
setOutput(OUTL_12V_BAT_CHRG, outL_12V_bat_chrg_buffer);
analogWrite(OUTPWM_PUMP, outpwm_pump_buffer);
//can bus message to evcc
msg.buf[0] = msgStatusIns.bBMSStatusFlags;
msg.buf[2] = msgStatusIns.bBMSFault;
if (settings.canbus_to_EVCC.getVal() == 1) {
Can0.write(msg);
}
}
/////////////////////////////////////////////////
/// \brief gather all the data from the boards and check for any faults.
/////////////////////////////////////////////////
void Controller::syncModuleDataObjects() {
float bat12vVoltage;
bms.wakeBoards();
if (bms.getAllVoltTemp() < settings.module_count.getVal()) {
faultIncorectModuleCount.countFault(settings.fault_debounce_count.getVal());
} else {
faultIncorectModuleCount.resetFault();
}
if (bms.getLineFault()) {
faultBMSSerialComms.countFault(settings.fault_debounce_count.getVal());
} else {
faultBMSSerialComms.resetFault();
}
if (digitalRead(INL_BAT_PACK_FAULT) == LOW) {
faultModuleLoop.countFault(settings.fault_debounce_count.getVal());
} else {
faultModuleLoop.resetFault();
}
if (digitalRead(INL_BAT_MON_FAULT) == LOW) {
faultBatMon.countFault(settings.fault_debounce_count.getVal());
} else {
faultBatMon.resetFault();
}
if (digitalRead(INL_WATER_SENS1) == LOW) {
faultWatSen1.countFault(settings.fault_debounce_count.getVal());
} else {
faultWatSen1.resetFault();
}
if (digitalRead(INL_WATER_SENS2) == LOW) {
faultWatSen2.countFault(settings.fault_debounce_count.getVal());
} else {
faultWatSen2.resetFault();
}
if (bms.getHighCellVolt() > settings.over_v_setpoint.getVal()) {
faultBMSOV.countFault(settings.fault_debounce_count.getVal());
} else {
faultBMSOV.resetFault();
}
if (bms.getLowCellVolt() < settings.under_v_setpoint.getVal()) {
faultBMSUV.countFault(settings.fault_debounce_count.getVal());
} else {
faultBMSUV.resetFault();
}
if (bms.getHighTemperature() > settings.over_t_setpoint.getVal()) {
faultBMSOT.countFault(settings.fault_debounce_count.getVal());
} else {
faultBMSOT.resetFault();
}
if (bms.getLowTemperature() < settings.under_t_setpoint.getVal()) {
faultBMSUT.countFault(settings.fault_debounce_count.getVal());
} else {
faultBMSUT.resetFault();
}
bat12vVoltage = (float)analogRead(INA_12V_BAT) / settings.bat12v_scaling_divisor.getVal();
if (bat12vVoltage > settings.bat12v_over_v_setpoint.getVal()) {
fault12VBatOV.countFault(settings.fault_debounce_count.getVal());
} else {
fault12VBatOV.resetFault();
}
if (bat12vVoltage < settings.bat12v_under_v_setpoint.getVal()) {
fault12VBatUV.countFault(settings.fault_debounce_count.getVal());
} else {
fault12VBatUV.resetFault();
}
chargerInhibit = false;
powerLimiter = false;
isFaulted = false;
for (auto i = faults.begin(); i != faults.end(); i++) {
if ((*i)->getChargeFault()) chargerInhibit |= (*i)->getFault();
if ((*i)->getRunFault()) powerLimiter |= (*i)->getFault();
isFaulted |= (*i)->getFault();
}
if (faultBMSUV.getFault()) {
msgStatusIns.bBMSStatusFlags |= BMS_STATUS_CELL_LVC_FLAG;
}
if (faultBMSOT.getFault()) {
msgStatusIns.bBMSFault |= BMS_FAULT_OVERTEMP_FLAG;
}
powerLimiter |= bms.getHighCellVolt() >= settings.max_charge_v_setpoint.getVal(); //added in case charging in run mode like my dad likes to do.
if (bms.getHighCellVolt() >= settings.max_charge_v_setpoint.getVal()) {
chargerInhibit |= bms.getHighCellVolt() >= settings.max_charge_v_setpoint.getVal();
LOG_INFO("bms.getHighCellVolt()[%.2f] >= settings.max_charge_v_setpoint.getVal()", bms.getHighCellVolt());
msgStatusIns.bBMSStatusFlags = BMS_STATUS_CELL_HVC_FLAG;
}
if (chargerInhibit) LOG_INFO("chargerInhibit (fault) line asserted!\n");
if (powerLimiter) LOG_INFO("powerLimiter (fault) line asserted!\n");
stickyFaulted |= isFaulted;
bms.clearFaults();
//bms.sleepBoards();
}
/////////////////////////////////////////////////
/// \brief balances the cells according to BALANCE_CELL_V_OFFSET threshold in the CONFIG.h file
/////////////////////////////////////////////////
void Controller::balanceCells() {
//balance for 1 second given that the controller wakes up every second.
if (bms.getHighCellVolt() > settings.precision_balance_v_setpoint.getVal()) {
LOG_INFO("precision balance\n");
bms.balanceCells(5, settings.precision_balance_cell_v_offset.getVal());
} else if (bms.getHighCellVolt() > settings.rough_balance_v_setpoint.getVal()) {
LOG_INFO("rough balance\n");
bms.balanceCells(5, settings.rough_balance_cell_v_offset.getVal());
}
}
/////////////////////////////////////////////////
/// \brief computes the duty cycle required for the pwm controlling the coolant pump.
///
/// Returns a float from 0.0 - 1.0 that must be adjusted to the PWM range (0-255).
///
/// @param the temparature in C
/////////////////////////////////////////////////
//pwd = a*temp + b
#define COOLING_A (1.0 - settings.floor_duty_coolant_pump.getVal()) / (settings.cooling_hight_setpoint.getVal() - settings.cooling_lowt_setpoint.getVal())
#define COOLING_B settings.floor_duty_coolant_pump.getVal() - COOLING_A* settings.cooling_lowt_setpoint.getVal()
float Controller::getCoolingPumpDuty(float temp) {
//LOG_INFO("Cooling Pump Set To Max duty\n");
//return 1.0; //always fully on.
if (temp < settings.cooling_lowt_setpoint.getVal()) {
return settings.floor_duty_coolant_pump.getVal();
} else if (temp > settings.cooling_hight_setpoint.getVal()) {
return 1.0;
} else {
return COOLING_A * temp + COOLING_B;
}
}
/////////////////////////////////////////////////
/// \brief reset all boards, assign address to each board and configure their thresholds
/////////////////////////////////////////////////
void Controller::init() {
pinMode(OUTL_12V_BAT_CHRG, INPUT);
pinMode(OUTPWM_PUMP, OUTPUT); //PWM use analogWrite(OUTPWM_PUMP, 0-255);
pinMode(INL_BAT_PACK_FAULT, INPUT_PULLUP);
pinMode(INL_BAT_MON_FAULT, INPUT_PULLUP);
pinMode(INL_EVSE_DISC, INPUT_PULLUP);
pinMode(INH_RUN, INPUT_PULLDOWN);
pinMode(INH_CHARGING, INPUT_PULLDOWN);
pinMode(INA_12V_BAT, INPUT); // [0-1023] = analogRead(INA_12V_BAT)
pinMode(OUTL_EVCC_ON, OUTPUT);
pinMode(OUTH_FAULT, OUTPUT);
pinMode(INL_WATER_SENS1, INPUT_PULLUP);
pinMode(INL_WATER_SENS2, INPUT_PULLUP);
isFaulted = false;
stickyFaulted = false;
chargerInhibit = false;
powerLimiter = false;
dc2dcON_H = false;
period = LOOP_PERIOD_ACTIVE_MS;
outL_12V_bat_chrg_buffer = 1;
outpwm_pump_buffer = 0;
outL_evcc_on_buffer = 1;
outH_fault_buffer = 0;
bms.renumberBoardIDs();
bms.clearFaults();
}
/////////////////////////////////////////////////
/// \brief This helper function allows mimicking an open collector output (floating or ground).
/////////////////////////////////////////////////
void Controller::setOutput(int pin, int state) {
if (state == 1) {
pinMode(pin, INPUT);
} else {
pinMode(pin, OUTPUT);
digitalWrite(pin, state);
}
}
/////////////////////////////////////////////////
/// \brief standby state is when the boat is not charging and not in run state.
/////////////////////////////////////////////////
void Controller::standby() {
balanceCells();
outL_evcc_on_buffer = HIGH;
outH_fault_buffer = chargerInhibit;
outL_12V_bat_chrg_buffer = !dc2dcON_H;
outpwm_pump_buffer = 0;
}
/////////////////////////////////////////////////
/// \brief pre_charge state is turning on the EVCC to charge the battery. If the EVSE is disconnected,
/// it goes back to STANDBY.
/////////////////////////////////////////////////
void Controller::pre_charge() {
balanceCells();
outL_evcc_on_buffer = LOW;
outH_fault_buffer = chargerInhibit;
outL_12V_bat_chrg_buffer = !dc2dcON_H;
outpwm_pump_buffer = 0;
msgStatusIns.bBMSStatusFlags = 0;
}
/////////////////////////////////////////////////
/// \brief charging state is when the boat *is connected*
/// to a EVSE and is actively charging until EVCC shuts itself down.
/////////////////////////////////////////////////
void Controller::charging() {
balanceCells();
outL_evcc_on_buffer = LOW;
outH_fault_buffer = chargerInhibit;
outL_12V_bat_chrg_buffer = LOW;
outpwm_pump_buffer = (uint8_t)(getCoolingPumpDuty(bms.getHighTemperature()) * 255);
msgStatusIns.bBMSStatusFlags = 0;
}
/////////////////////////////////////////////////
/// \brief top_balancing state is when the boat *is connected*
/// to a EVSE and is finishing up charging until EVCC shuts itself down.
/// During this phase, we turn off the DC2DC so as not to confuse the EVCC
/// as it tries to measure a current drop in the charging.
/////////////////////////////////////////////////
void Controller::top_balancing() {
balanceCells();
msgStatusIns.bBMSStatusFlags |= BMS_STATUS_CELL_BVC_FLAG;
outL_evcc_on_buffer = LOW;
outH_fault_buffer = chargerInhibit;
outL_12V_bat_chrg_buffer = HIGH; //stop DC2DC
outpwm_pump_buffer = (uint8_t)(getCoolingPumpDuty(bms.getHighTemperature()) * 255);
msgStatusIns.bBMSStatusFlags = BMS_STATUS_CELL_BVC_FLAG;
}
/////////////////////////////////////////////////
/// \brief post_charging state is when the boat is finishing a charge
/// to to let the EVCC go back to sleep.
/////////////////////////////////////////////////
void Controller::post_charge() {
balanceCells();
outL_evcc_on_buffer = HIGH; // de-assert key switch ignition KSI to the EVCC
outH_fault_buffer = HIGH; //keep asserting the loop to the EVCC until it goes to sleep
outL_12V_bat_chrg_buffer = HIGH; //stop DC2DC
outpwm_pump_buffer = (uint8_t)(getCoolingPumpDuty(bms.getHighTemperature()) * 255);
msgStatusIns.bBMSStatusFlags = BMS_STATUS_CELL_HVC_FLAG;
}
/////////////////////////////////////////////////
/// \brief run state is turned on and ready to operate.
/////////////////////////////////////////////////
void Controller::run() {
outL_evcc_on_buffer = LOW; //required so that the EVSE_DISC is valid (will inhibit the motor controller if EVSE is connected)
outH_fault_buffer = powerLimiter;
outL_12V_bat_chrg_buffer = LOW;
outpwm_pump_buffer = (uint8_t)(getCoolingPumpDuty(bms.getHighTemperature()) * 255);
}
/////////////////////////////////////////////////
/// \brief returns the current state the controller is in.
/////////////////////////////////////////////////
Controller::ControllerState Controller::getState() {
return state;
}
/////////////////////////////////////////////////
/// \brief returns the Settings instance to allow access to its members.
/////////////////////////////////////////////////
Settings* Controller::getSettingsPtr() {
return &settings;
}
/////////////////////////////////////////////////
/// \brief returns the BMS instance to allow access to its members for reporting purposes.
/////////////////////////////////////////////////
BMSModuleManager* Controller::getBMSPtr() {
return &bms;
}
/////////////////////////////////////////////////
/// \brief returns the main loop period the controller is expecting.
/////////////////////////////////////////////////
uint32_t Controller::getPeriodMillis() {
return period;
}
void Controller::printControllerState() {
uint32_t seconds = millis() / 1000;
LOG_CONSOLE("OUTL_EVCC_ON: %d\n", outL_evcc_on_buffer);
LOG_CONSOLE("OUTH_FAULT: %d\n", outH_fault_buffer);
LOG_CONSOLE("OUTL_12V_BAT_CHRG: %d\n", outL_12V_bat_chrg_buffer);
LOG_CONSOLE("OUTPWM_PUMP: %d\n", outpwm_pump_buffer);
LOG_CONSOLE("====================================================================================\n");
LOG_CONSOLE("= BMS Controller registered faults =\n");
switch (state) {
case INIT:
LOG_CONSOLE("= state: INIT =\n");
break;
case STANDBY:
LOG_CONSOLE("= state: STANDBY =\n");
break;
case PRE_CHARGE:
LOG_CONSOLE("= state: PRE_CHARGE =\n");
break;
case CHARGING:
LOG_CONSOLE("= state: CHARGING =\n");
break;
case TOP_BALANCING:
LOG_CONSOLE("= state: TOP_BALANCING =\n");
break;
case POST_CHARGE:
LOG_CONSOLE("= state: POST_CHARGE =\n");
break;
case RUN:
LOG_CONSOLE("= state: RUN =\n");
break;
}
LOG_CONSOLE("= Time since last reset:%-3d days, %02d:%02d:%02d =\n",
seconds / 86400, (seconds % 86400) / 3600, (seconds % 3600) / 60, (seconds % 60));
LOG_CONSOLE("= Time of last reset: ");
LOG_TIMESTAMP(lastResetTimeStamp);
LOG_CONSOLE(" =\n");
LOG_CONSOLE("= Time: ");
LOG_TIMESTAMP(now());
LOG_CONSOLE(" =\n");
LOG_CONSOLE("====================================================================================\n");
LOG_CONSOLE("%-22s last fault time\n", "Fault Name");
LOG_CONSOLE("---------------------- -----------------------------------------------------------\n");
for (auto i = faults.begin(); i != faults.end(); i++) {
if ((*i)->getSFault()) {
LOG_CONSOLE("%-22s @ ", (*i)->getName().c_str());
LOG_TIMESTAMP_LN((*i)->getTimeStamp());
}
}
}