-
Notifications
You must be signed in to change notification settings - Fork 33
/
TeslaBMSV2.ino
3990 lines (3637 loc) · 107 KB
/
TeslaBMSV2.ino
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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2019 Simp ECO Engineering
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "BMSModuleManager.h"
#include <Arduino.h>
#include "config.h"
#include "SerialConsole.h"
#include "Logger.h"
#include <ADC.h> //https://github.com/pedvide/ADC
#include <EEPROM.h>
#include <FlexCAN.h> //https://github.com/collin80/FlexCAN_Library
#include <SPI.h>
#include <Filters.h>//https://github.com/JonHub/Filters
#include "Serial_CAN_Module_TeensyS2.h" //https://github.com/tomdebree/Serial_CAN_Teensy
/*
#define CPU_REBOOT (_reboot_Teensyduino_());
*/
#define RESTART_ADDR 0xE000ED0C
#define READ_RESTART() (*(volatile uint32_t *)RESTART_ADDR)
#define WRITE_RESTART(val) ((*(volatile uint32_t *)RESTART_ADDR) = (val))
#define CPU_REBOOT WRITE_RESTART(0x5FA0004)
Serial_CAN can;
BMSModuleManager bms;
SerialConsole console;
EEPROMSettings settings;
/////Version Identifier/////////
int firmver = 230719; //Year Month Day
//Curent filter//
float filterFrequency = 5.0 ;
FilterOnePole lowpassFilter( LOWPASS, filterFrequency );
//Simple BMS V2 wiring//
const int ACUR2 = A0; // current 1
const int ACUR1 = A1; // current 2
const int IN1 = 17; // input 1 - high active
const int IN2 = 16; // input 2- high active
const int IN3 = 18; // input 1 - high active
const int IN4 = 19; // input 2- high active
const int OUT1 = 11;// output 1 - high active
const int OUT2 = 12;// output 2 - high active
const int OUT3 = 20;// output 3 - high active
const int OUT4 = 21;// output 4 - high active
const int OUT5 = 22;// output 5 - Low active
const int OUT6 = 23;// output 6 - Low active
const int OUT7 = 5;// output 7 - Low active
const int OUT8 = 6;// output 8 - Low active
const int led = 13;
const int BMBfault = 11;
byte bmsstatus = 0;
//bms status values
#define Boot 0
#define Ready 1
#define Drive 2
#define Charge 3
#define Precharge 4
#define Error 5
//
//Current sensor values
#define Undefined 0
#define Analoguedual 1
#define Canbus 2
#define Analoguesing 3
// Can current sensor values
#define LemCAB300 1
#define IsaScale 3
#define VictronLynx 4
#define LemCAB500 2
#define CurCanMax 4 // max value
//
//Charger Types
#define NoCharger 0
#define BrusaNLG5 1
#define ChevyVolt 2
#define Eltek 3
#define Elcon 4
#define Victron 5
#define Coda 6
#define VictronHV 7
//
int Discharge;
//variables for output control
int pulltime = 100;
int contctrl, contstat = 0; //1 = out 5 high 2 = out 6 high 3 = both high
unsigned long conttimer1, conttimer2, conttimer3, Pretimer, Pretimer1, overtriptimer, undertriptimer, mainconttimer = 0;
uint16_t pwmfreq = 18000;//pwm frequency
int pwmcurmax = 50;//Max current to be shown with pwm
int pwmcurmid = 50;//Mid point for pwm dutycycle based on current
int16_t pwmcurmin = 0;//DONOT fill in, calculated later based on other values
bool OutputEnable = 0;
bool CanOnReq = false;
bool CanOnRev = false;
//variables for VE can
uint16_t chargevoltage = 49100; //max charge voltage in mv
uint16_t chargecurrent, tempchargecurrent = 0;
uint16_t disvoltage = 42000; // max discharge voltage in mv
uint16_t discurrent = 0;
int batvcal = 0;
uint16_t SOH = 100; // SOH place holder
unsigned char alarm[4] = {0, 0, 0, 0};
unsigned char warning[4] = {0, 0, 0, 0};
unsigned char mes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
unsigned char bmsname[8] = {'S', 'I', 'M', 'P', ' ', 'B', 'M', 'S'};
unsigned char bmsmanu[8] = {'S', 'I', 'M', 'P', ' ', 'E', 'C', 'O'};
long unsigned int rxId;
unsigned char len = 0;
byte rxBuf[8];
char msgString[128]; // Array to store serial string
uint32_t inbox;
signed long CANmilliamps;//mV
signed long voltage1, voltage2, voltage3 = 0; //mV only with ISAscale sensor
//struct can_frame canMsg;
//MCP2515 CAN1(10); //set CS pin for can controlelr
//variables for current calulation
int value;
float currentact, RawCur;
float ampsecond;
unsigned long lasttime;
unsigned long looptime, looptime1, UnderTimer, OverTime, cleartime, baltimer, CanOntimeout = 0; //ms
int currentsense = 14;
int sensor = 1;
//Variables for SOC calc
int SOC = 100; //State of Charge
int SOCset = 0;
int SOCtest = 0;
int SOCmem = 0;
int SOCreset = 0;
///charger variables
int maxac1 = 16; //Shore power 16A per charger
int maxac2 = 10; //Generator Charging
int chargerid1 = 0x618; //bulk chargers
int chargerid2 = 0x638; //finishing charger
float chargerendbulk = 0; //V before Charge Voltage to turn off the bulk charger/s
float chargerend = 0; //V before Charge Voltage to turn off the finishing charger/s
int chargertoggle = 0;
int ncharger = 1; // number of chargers
bool chargecurrentlimit = 0;
//serial canbus expansion
unsigned long id = 0;
unsigned char dta[8];
//AC current control
volatile uint32_t pilottimer = 0;
volatile uint16_t timehigh, duration = 0;
volatile uint16_t accurlim = 0;
volatile int dutycycle = 0;
uint16_t chargerpower = 0;
bool CPdebug = 0;
//variables
int outputstate = 0;
int incomingByte = 0;
int x = 0;
int storagemode = 0;
int cellspresent = 0;
int dashused = 1;
int Charged = 0;
int renum = 0;
//Debugging modes//////////////////
int debug = 1;
int inputcheck = 0; //read digital inputs
int outputcheck = 0; //check outputs
int candebug = 0; //view can frames
int gaugedebug = 0;
int debugCur = 0;
int CSVdebug = 0;
int delim = 0;
int menuload = 0;
int balancecells;
int debugdigits = 2; //amount of digits behind decimal for voltage reading
int testcount = 0;
ADC *adc = new ADC(); // adc object
void loadSettings()
{
Logger::console("Resetting to factory defaults");
settings.version = EEPROM_VERSION;
settings.checksum = 2;
settings.canSpeed = 500000;
settings.batteryID = 0x01; //in the future should be 0xFF to force it to ask for an address
settings.OverVSetpoint = 4.2f;
settings.UnderVSetpoint = 3.0f;
settings.ChargeVsetpoint = 4.1f;
settings.ChargeHys = 0.2f; // voltage drop required for charger to kick back on
settings.WarnOff = 0.1f; //voltage offset to raise a warning
settings.DischVsetpoint = 3.2f;
settings.DischHys = 0.2f; // Discharge voltage offset
settings.CellGap = 0.2f; //max delta between high and low cell
settings.OverTSetpoint = 65.0f;
settings.UnderTSetpoint = -10.0f;
settings.ChargeTSetpoint = 0.0f;
settings.triptime = 500;//mS of delay before counting over or undervoltage
settings.DisTSetpoint = 40.0f;
settings.WarnToff = 5.0f; //temp offset before raising warning
settings.IgnoreTemp = 0; // 0 - use both sensors, 1 or 2 only use that sensor
settings.IgnoreVolt = 0.5;//
settings.balanceVoltage = 3.9f;
settings.balanceHyst = 0.04f;
settings.balanceDuty = 50;
settings.logLevel = 2;
settings.CAP = 100; //battery size in Ah
settings.Pstrings = 1; // strings in parallel used to divide voltage of pack
settings.Scells = 12;//Cells in series
settings.StoreVsetpoint = 3.8; // V storage mode charge max
settings.discurrentmax = 300; // max discharge current in 0.1A
settings.DisTaper = 0.3f; //V offset to bring in discharge taper to Zero Amps at settings.DischVsetpoint
settings.chargecurrentmax = 300; //max charge current in 0.1A
settings.chargecurrent2max = 150; //max charge current in 0.1A
settings.chargecurrentend = 50; //end charge current in 0.1A
settings.PulseCh = 600; //Peak Charge current in 0.1A
settings.PulseChDur = 5000; //Ms of discharge pulse derating
settings.PulseDi = 600; //Peak Charge current in 0.1A
settings.PulseDiDur = 5000; //Ms of discharge pulse derating
settings.socvolt[0] = 3100; //Voltage and SOC curve for voltage based SOC calc
settings.socvolt[1] = 10; //Voltage and SOC curve for voltage based SOC calc
settings.socvolt[2] = 4100; //Voltage and SOC curve for voltage based SOC calc
settings.socvolt[3] = 90; //Voltage and SOC curve for voltage based SOC calc
settings.invertcur = 0; //Invert current sensor direction
settings.cursens = 2;
settings.curcan = LemCAB300;
settings.voltsoc = 0; //SOC purely voltage based
settings.Pretime = 5000; //ms of precharge time
settings.conthold = 50; //holding duty cycle for contactor 0-255
settings.Precurrent = 1000; //ma before closing main contator
settings.convhigh = 580; // mV/A current sensor high range channel
settings.convlow = 6430; // mV/A current sensor low range channel
settings.offset1 = 1750; //mV mid point of channel 1
settings.offset2 = 1750;//mV mid point of channel 2
settings.changecur = 20000;//mA change overpoint
settings.gaugelow = 50; //empty fuel gauge pwm
settings.gaugehigh = 255; //full fuel gauge pwm
settings.ESSmode = 0; //activate ESS mode
settings.ncur = 1; //number of multiples to use for current measurement
settings.chargertype = 2; // 1 - Brusa NLG5xx 2 - Volt charger 0 -No Charger
settings.chargerspd = 100; //ms per message
settings.chargereff = 85; //% effiecency of charger
settings.chargerACv = 240;// AC input voltage into Charger
settings.UnderDur = 5000; //ms of allowed undervoltage before throwing open stopping discharge.
settings.CurDead = 5;// mV of dead band on current sensor
settings.ExpMess = 0; //send alternate victron info
settings.SerialCan = 0; //Serial canbus or display: 0-display 1- canbus expansion
settings.tripcont = 1; //in ESSmode 1 - Main contactor function, 0 - Trip function
}
CAN_message_t msg;
CAN_message_t inMsg;
uint32_t lastUpdate;
void setup()
{
SERIALBMS.begin(612500); //Tesla serial bus
delay(2000); //just for easy debugging. It takes a few seconds for USB to come up properly on most OS's
//pinMode(ACUR1, INPUT);//Not required for Analogue Pins
//pinMode(ACUR2, INPUT);//Not required for Analogue Pins
pinMode(IN1, INPUT);
pinMode(IN2, INPUT);
pinMode(IN3, INPUT);
pinMode(IN4, INPUT);
pinMode(OUT1, OUTPUT); // drive contactor
pinMode(OUT2, OUTPUT); // precharge
pinMode(OUT3, OUTPUT); // charge relay
pinMode(OUT4, OUTPUT); // Negative contactor
pinMode(OUT5, OUTPUT); // pwm driver output
pinMode(OUT6, OUTPUT); // pwm driver output
pinMode(OUT7, OUTPUT); // pwm driver output
pinMode(OUT8, OUTPUT); // pwm driver output
pinMode(led, OUTPUT);
analogWriteFrequency(OUT5, pwmfreq);
analogWriteFrequency(OUT6, pwmfreq);
analogWriteFrequency(OUT7, pwmfreq);
analogWriteFrequency(OUT8, pwmfreq);
EEPROM.get(0, settings);
if (settings.version != EEPROM_VERSION)
{
loadSettings();
}
Can0.begin(settings.canSpeed);
CAN_filter_t allPassFilter; // Enables extended addresses
allPassFilter.id = 0;
allPassFilter.ext = 1;
allPassFilter.rtr = 0;
for (int filterNum = 4; filterNum < 16; filterNum++) {
Can0.setFilter(allPassFilter, filterNum);
}
//if using enable pins on a transceiver they need to be set on
adc->adc0->setAveraging(16); // set number of averages
adc->adc0->setResolution(16); // set bits of resolution
adc->adc0->setConversionSpeed(ADC_CONVERSION_SPEED::HIGH_SPEED);
adc->adc0->setSamplingSpeed(ADC_SAMPLING_SPEED::LOW_SPEED);
adc->adc0->startContinuous(ACUR1);
SERIALCONSOLE.begin(115200);
SERIALCONSOLE.println("Starting up!");
SERIALCONSOLE.println("SimpBMS V2 Tesla");
Serial2.begin(115200); //display and can adpater canbus
// Display reason the Teensy was last reset
Serial.println();
Serial.println("Reason for last Reset: ");
if (RCM_SRS1 & RCM_SRS1_SACKERR) Serial.println("Stop Mode Acknowledge Error Reset");
if (RCM_SRS1 & RCM_SRS1_MDM_AP) Serial.println("MDM-AP Reset");
if (RCM_SRS1 & RCM_SRS1_SW) Serial.println("Software Reset"); // reboot with SCB_AIRCR = 0x05FA0004
if (RCM_SRS1 & RCM_SRS1_LOCKUP) Serial.println("Core Lockup Event Reset");
if (RCM_SRS0 & RCM_SRS0_POR) Serial.println("Power-on Reset"); // removed / applied power
if (RCM_SRS0 & RCM_SRS0_PIN) Serial.println("External Pin Reset"); // Reboot with software download
if (RCM_SRS0 & RCM_SRS0_WDOG) Serial.println("Watchdog(COP) Reset"); // WDT timed out
if (RCM_SRS0 & RCM_SRS0_LOC) Serial.println("Loss of External Clock Reset");
if (RCM_SRS0 & RCM_SRS0_LOL) Serial.println("Loss of Lock in PLL Reset");
if (RCM_SRS0 & RCM_SRS0_LVD) Serial.println("Low-voltage Detect Reset");
Serial.println();
///////////////////
// enable WDT
noInterrupts(); // don't allow interrupts while setting up WDOG
WDOG_UNLOCK = WDOG_UNLOCK_SEQ1; // unlock access to WDOG registers
WDOG_UNLOCK = WDOG_UNLOCK_SEQ2;
delayMicroseconds(1); // Need to wait a bit..
WDOG_TOVALH = 0x1000;
WDOG_TOVALL = 0x0000;
WDOG_PRESC = 0;
WDOG_STCTRLH |= WDOG_STCTRLH_ALLOWUPDATE |
WDOG_STCTRLH_WDOGEN | WDOG_STCTRLH_WAITEN |
WDOG_STCTRLH_STOPEN | WDOG_STCTRLH_CLKSRC;
interrupts();
/////////////////
//VE.begin(19200); //Victron VE direct bus
#if defined (__arm__) && defined (__SAM3X8E__)
serialSpecialInit(USART0, 612500); //required for Due based boards as the stock core files don't support 612500 baud.
#endif
SERIALCONSOLE.println("Started serial interface to BMS.");
/*
EEPROM.get(0, settings);
if (settings.version != EEPROM_VERSION)
{
loadSettings();
}
*/
bms.renumberBoardIDs();
Logger::setLoglevel(Logger::Off); //Debug = 0, Info = 1, Warn = 2, Error = 3, Off = 4
lastUpdate = 0;
bms.findBoards();
digitalWrite(led, HIGH);
bms.setPstrings(settings.Pstrings);
bms.setSensors(settings.IgnoreTemp, settings.IgnoreVolt);
//SOC recovery//
SOC = (EEPROM.read(1000));
if (settings.voltsoc == 1)
{
SOCmem = 0;
}
else
{
if (SOC > 100)
{
SOCmem = 0;
}
else if (SOC > 1)
{
//SOCmem = 1;
}
}
SERIALCONSOLE.println("Recovery SOC: ");
SERIALCONSOLE.print(SOC);
////Calculate fixed numbers
pwmcurmin = (pwmcurmid / 50 * pwmcurmax * -1);
////
bms.clearFaults();
///precharge timer kickers
Pretimer = millis();
Pretimer1 = millis();
// setup interrupts
//RISING/HIGH/CHANGE/LOW/FALLING
attachInterrupt (IN4, isrCP , CHANGE); // attach BUTTON 1 interrupt handler [ pin# 7 ]
PMC_LVDSC1 = PMC_LVDSC1_LVDV(1); // enable hi v
PMC_LVDSC2 = PMC_LVDSC2_LVWIE | PMC_LVDSC2_LVWV(3); // 2.92-3.08v
attachInterruptVector(IRQ_LOW_VOLTAGE, low_voltage_isr);
NVIC_ENABLE_IRQ(IRQ_LOW_VOLTAGE);
bmsstatus = Boot;
}
void loop()
{
while (Can0.available())
{
canread();
}
if (SERIALCONSOLE.available() > 0)
{
menu();
}
if (outputcheck != 1)
{
contcon();
if (settings.ESSmode == 1)
{
if (settings.ChargerDirect == 1)
{
OutputEnable = 1;
}
else
{
if (digitalRead(IN2) == HIGH || CanOnReq == true)
{
OutputEnable = 1;
//Serial.println(CanOnReq);
}
else
{
OutputEnable = 0;
}
}
if (bmsstatus != Error && bmsstatus != Boot && OutputEnable == 1)
{
contctrl = contctrl | 4; //turn on negative contactor
if (settings.tripcont != 0)
{
if (bms.getLowCellVolt() > settings.UnderVSetpoint && bms.getHighCellVolt() < settings.OverVSetpoint)
{
if (digitalRead(OUT2) == LOW && digitalRead(OUT4) == LOW)
{
mainconttimer = millis();
digitalWrite(OUT4, HIGH);//Precharge start
Serial.println();
Serial.println("Precharge!!!");
Serial.println(mainconttimer);
Serial.println();
}
if (mainconttimer + settings.Pretime < millis() && digitalRead(OUT2) == LOW && abs(currentact) < settings.Precurrent)
{
digitalWrite(OUT2, HIGH);//turn on contactor
contctrl = contctrl | 2; //turn on contactor
Serial.println();
Serial.println("Main On!!!");
Serial.println();
mainconttimer = millis() + settings.Pretime;
}
if (mainconttimer + settings.Pretime + 1000 < millis() )
{
digitalWrite(OUT4, LOW);//ensure precharge is low
}
}
else
{
digitalWrite(OUT4, LOW);//ensure precharge is low
mainconttimer = 0;
}
}
if (digitalRead(IN1) == LOW)//Key OFF
{
if (storagemode == 1)
{
storagemode = 0;
}
}
else
{
if (storagemode == 0)
{
storagemode = 1;
}
}
if (bms.getHighCellVolt() > settings.balanceVoltage && bms.getHighCellVolt() > bms.getLowCellVolt() + settings.balanceHyst)
{
balancecells = 1;
}
else
{
balancecells = 0;
}
//Pretimer + settings.Pretime > millis();
if (storagemode == 1)
{
if (bms.getHighCellVolt() > settings.StoreVsetpoint || chargecurrent == 0)
{
digitalWrite(OUT3, LOW);//turn off charger
// contctrl = contctrl & 253;
// Pretimer = millis();
Charged = 1;
SOCcharged(2);
}
else
{
if (Charged == 1)
{
if (bms.getHighCellVolt() < (settings.StoreVsetpoint - settings.ChargeHys))
{
Charged = 0;
digitalWrite(OUT3, HIGH);//turn on charger
/*
if (Pretimer + settings.Pretime < millis())
{
contctrl = contctrl | 2;
Pretimer = 0;
}
*/
}
}
else
{
digitalWrite(OUT3, HIGH);//turn on charger
/*
if (Pretimer + settings.Pretime < millis())
{
contctrl = contctrl | 2;
Pretimer = 0;
}
*/
}
}
}
else
{
if (bms.getHighCellVolt() > settings.OverVSetpoint || bms.getHighCellVolt() > settings.ChargeVsetpoint || chargecurrent == 0)
{
if ((millis() - overtriptimer) > settings.triptime)
{
if (digitalRead(OUT3) == 1)
{
Serial.println();
Serial.println("Over Voltage Trip");
digitalWrite(OUT3, LOW);//turn off charger
// contctrl = contctrl & 253;
//Pretimer = millis();
Charged = 1;
SOCcharged(2);
}
}
}
else
{
overtriptimer = millis();
if (Charged == 1)
{
if (bms.getHighCellVolt() < (settings.ChargeVsetpoint - settings.ChargeHys))
{
if (digitalRead(OUT3) == 0)
{
Serial.println();
Serial.println("Reset Over Voltage Trip Not Charged");
Charged = 0;
digitalWrite(OUT3, HIGH);//turn on charger
}
/*
if (Pretimer + settings.Pretime < millis())
{
// Serial.println();
//Serial.print(Pretimer);
contctrl = contctrl | 2;
}*/
}
}
else
{
if (digitalRead(OUT3) == 0)
{
Serial.println();
Serial.println("Reset Over Voltage Trip Not Charged");
digitalWrite(OUT3, HIGH);//turn on charger
}
/*
if (Pretimer + settings.Pretime < millis())
{
// Serial.println();
//Serial.print(Pretimer);
contctrl = contctrl | 2;
}*/
}
}
}
if (bms.getLowCellVolt() < settings.UnderVSetpoint || bms.getLowCellVolt() < settings.DischVsetpoint)
{
if (digitalRead(OUT1) == 1)
{
if ((millis() - undertriptimer) > settings.triptime)
{
Serial.println();
Serial.println("Under Voltage Trip");
digitalWrite(OUT1, LOW);//turn off discharge
// contctrl = contctrl & 254;
// Pretimer1 = millis();
}
}
}
else
{
undertriptimer = millis();
if (bms.getLowCellVolt() > settings.DischVsetpoint + settings.DischHys)
{
if (digitalRead(OUT1) == 0)
{
Serial.println();
Serial.println("Reset Under Voltage Trip");
digitalWrite(OUT1, HIGH);//turn on discharge
}
/*
if (Pretimer1 + settings.Pretime < millis())
{
contctrl = contctrl | 1;
}*/
}
}
if (SOCset == 1)
{
if (settings.tripcont == 0)
{
if (bms.getLowCellVolt() < settings.UnderVSetpoint || bms.getHighCellVolt() > settings.OverVSetpoint || bms.getHighTemperature() > settings.OverTSetpoint)
{
digitalWrite(OUT2, HIGH);//trip breaker
bmsstatus = Error;
}
else
{
digitalWrite(OUT2, LOW);//trip breaker
}
}
else
{
if (bms.getLowCellVolt() < settings.UnderVSetpoint || bms.getHighCellVolt() > settings.OverVSetpoint || bms.getHighTemperature() > settings.OverTSetpoint)
{
digitalWrite(OUT2, LOW);//turn off contactor
contctrl = contctrl & 253; //turn off contactor
digitalWrite(OUT4, LOW);//ensure precharge is low
bmsstatus = Error;
}
}
}
}
else
{
//digitalWrite(OUT2, HIGH);//trip breaker
Discharge = 0;
digitalWrite(OUT4, LOW);
digitalWrite(OUT3, LOW);//turn off charger
digitalWrite(OUT2, LOW);
digitalWrite(OUT1, LOW);//turn off discharge
contctrl = 0; //turn off out 5 and 6
if (SOCset == 1)
{
if (settings.tripcont == 0)
{
digitalWrite(OUT2, HIGH);//trip breaker
}
else
{
digitalWrite(OUT2, LOW);//turn off contactor
digitalWrite(OUT4, LOW);//ensure precharge is low
}
if (bms.getLowCellVolt() > settings.UnderVSetpoint && bms.getHighCellVolt() < settings.OverVSetpoint && bms.getHighTemperature() < settings.OverTSetpoint && cellspresent == bms.seriescells() && cellspresent == (settings.Scells * settings.Pstrings))
{
bmsstatus = Ready;
}
}
}
//pwmcomms();
}
else
{
switch (bmsstatus)
{
case (Boot):
Discharge = 0;
digitalWrite(OUT4, LOW);
digitalWrite(OUT3, LOW);//turn off charger
digitalWrite(OUT2, LOW);
digitalWrite(OUT1, LOW);//turn off discharge
contctrl = 0;
bmsstatus = Ready;
break;
case (Ready):
Discharge = 0;
digitalWrite(OUT4, LOW);
digitalWrite(OUT3, LOW);//turn off charger
digitalWrite(OUT2, LOW);
digitalWrite(OUT1, LOW);//turn off discharge
contctrl = 0; //turn off out 5 and 6
accurlim = 0;
if (bms.getHighCellVolt() > settings.balanceVoltage && bms.getHighCellVolt() > bms.getLowCellVolt() + settings.balanceHyst)
{
//bms.balanceCells();
balancecells = 1;
}
else
{
balancecells = 0;
}
if (digitalRead(IN3) == HIGH && (bms.getHighCellVolt() < (settings.ChargeVsetpoint - settings.ChargeHys)) && bms.getHighTemperature() < (settings.OverTSetpoint - settings.WarnToff)) //detect AC present for charging and check not balancing
{
if (settings.ChargerDirect == 1)
{
bmsstatus = Charge;
}
else
{
bmsstatus = Precharge;
Pretimer = millis();
}
}
if (digitalRead(IN1) == HIGH && bms.getLowCellVolt() > settings.DischVsetpoint) //detect Key ON
{
bmsstatus = Precharge;
Pretimer = millis();
}
break;
case (Precharge):
Discharge = 0;
Prechargecon();
break;
case (Drive):
Discharge = 1;
accurlim = 0;
if (digitalRead(IN1) == LOW)//Key OFF
{
bmsstatus = Ready;
}
if (digitalRead(IN3) == HIGH && (bms.getHighCellVolt() < (settings.ChargeVsetpoint - settings.ChargeHys)) && bms.getHighTemperature() < (settings.OverTSetpoint - settings.WarnToff)) //detect AC present for charging and check not balancing
{
bmsstatus = Charge;
}
break;
case (Charge):
if (settings.ChargerDirect > 0)
{
Discharge = 0;
digitalWrite(OUT4, LOW);
digitalWrite(OUT2, LOW);
digitalWrite(OUT1, LOW);//turn off discharge
contctrl = 0; //turn off out 5 and 6
}
Discharge = 0;
if (digitalRead(IN2) == HIGH)
{
chargecurrentlimit = true;
}
else
{
chargecurrentlimit = false;
}
digitalWrite(OUT3, HIGH);//enable charger
if (bms.getHighCellVolt() > settings.balanceVoltage)
{
//bms.balanceCells();
balancecells = 1;
}
else
{
balancecells = 0;
}
if (bms.getHighCellVolt() > settings.ChargeVsetpoint || bms.getHighTemperature() > settings.OverTSetpoint)
{
if (bms.getAvgCellVolt() > (settings.ChargeVsetpoint - settings.ChargeHys))
{
SOCcharged(2);
}
else
{
SOCcharged(1);
}
digitalWrite(OUT3, LOW);//turn off charger
bmsstatus = Ready;
}
if (digitalRead(IN3) == LOW)//detect AC not present for charging
{
bmsstatus = Ready;
}
break;
case (Error):
Discharge = 0;
digitalWrite(OUT4, LOW);
digitalWrite(OUT3, LOW);//turn off charger
digitalWrite(OUT2, LOW);
digitalWrite(OUT1, LOW);//turn off discharge
contctrl = 0; //turn off out 5 and 6
/*
if (digitalRead(IN3) == HIGH) //detect AC present for charging
{
bmsstatus = Charge;
}
*/
if (bms.getLowCellVolt() >= settings.UnderVSetpoint && bms.getHighCellVolt() <= settings.OverVSetpoint && digitalRead(IN1) == LOW)
{
bmsstatus = Ready;
}
break;
}
}
if ( settings.cursens == Analoguedual || settings.cursens == Analoguesing)
{
getcurrent();
}
}
if (millis() - looptime > 500)
{
looptime = millis();
bms.getAllVoltTemp();
//UV check
if (settings.ESSmode == 1)
{
if (bms.getLowCellVolt() < settings.UnderVSetpoint || bms.getHighCellVolt() < settings.UnderVSetpoint)
{
if (undertriptimer > millis()) //check is last time not undervoltage is longer thatn UnderDur ago
{
bmsstatus = Error;
}
}
else
{
undertriptimer = millis() + settings.triptime;
}
if (bms.getLowCellVolt() > settings.OverVSetpoint || bms.getHighCellVolt() > settings.OverVSetpoint)
{
if (overtriptimer > millis()) //check is last time not undervoltage is longer thatn UnderDur ago
{
bmsstatus = Error;
}
}
else
{
overtriptimer = millis() + settings.triptime;
}
}
else //In 'vehicle' mode
{
if (bms.getLowCellVolt() < settings.UnderVSetpoint)
{
if (UnderTimer < millis()) //check is last time not undervoltage is longer thatn UnderDur ago
{
bmsstatus = Error;
}
}
else
{
UnderTimer = millis() + settings.triptime;
}
if (bms.getHighCellVolt() < settings.UnderVSetpoint || bms.getHighTemperature() > settings.OverTSetpoint)
{
bmsstatus = Error;
}
if (bms.getHighCellVolt() > settings.OverVSetpoint)
{
if (OverTime < millis()) //check is last time not undervoltage is longer thatn UnderDur ago
{
bmsstatus = Error;
}
}
else
{
OverTime = millis() + settings.triptime;
}
}
balancing();
if (debug != 0)
{
printbmsstat();
bms.printPackDetails(debugdigits);
}
if (CSVdebug != 0)
{
bms.printAllCSV(millis(), currentact, SOC, delim);
}
if (inputcheck != 0)
{
inputdebug();
}
if (outputcheck != 0)
{
outputdebug();
}
else
{
gaugeupdate();
}
updateSOC();
currentlimit();
if (settings.ESSmode == 1 && settings.ChargerDirect == 0 && CanOnRev == true)
{
if ((millis() - CanOntimeout) > 5000)
{
Serial.println();
Serial.println("0x309 Can On Request Missing");
CanOnReq = false;
}
}
if (cellspresent == 0 && SOCset == 1)
{
cellspresent = bms.seriescells();
bms.setSensors(settings.IgnoreTemp, settings.IgnoreVolt);
}
else
{
if (cellspresent != bms.seriescells() || cellspresent != (settings.Scells * settings.Pstrings)) //detect a fault in cells detected
{
if (debug != 0)
{
SERIALCONSOLE.println(" ");
SERIALCONSOLE.print(" !!! Series Cells Fault !!!");