-
Notifications
You must be signed in to change notification settings - Fork 64
/
i3LIM.cpp
847 lines (712 loc) · 30.2 KB
/
i3LIM.cpp
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
#include <i3LIM.h>
enum class ChargeStatus : uint8_t
{
// no led
NotRdy = 0x0,
// dc ccs mode
Init = 0x1,
Rdy = 0x2
};
enum class ChargeRequest : uint8_t
{
EndCharge = 0x0,
Charge = 0x1
};
enum class ChargeReady : uint8_t
{
NotRdy = 0x0,
Rdy = 0x1
};
enum class ChargePhase : uint8_t
{
Standby = 0x0,
Initialisation = 0x1,
Subpoena = 0x2,
EnergyTransfer = 0x3,
Shutdown = 0x4,
CableTest = 0x9,
Reserved = 0xE,
InvalidSignal = 0xF
};
static uint8_t CP_Mode=0;
static ChargePhase Chg_Phase=ChargePhase::Standby;
static uint8_t lim_state=0;
static uint8_t lim_stateCnt=0;
static uint8_t ctr_1second=0;
static uint8_t ctr_5second=0;
static uint8_t ctr_20ms=0;
static uint8_t vin_ctr=0;
static uint8_t Timer_1Sec=0;
static uint8_t Timer_60Sec=0;
uint8_t ChargeType=0;
uint8_t CCS_Plim=0;//ccs power limit flag. 0=no,1=yes,3=invalid.
uint8_t CCS_Ilim=0;//ccs current limit flag. 0=no,1=yes,3=invalid.
uint8_t CCS_Vlim=0;//ccs voltage limit flag. 0=no,1=yes,3=invalid.
uint8_t CCS_Stat=0;//ccs charging status. 0=standby,1=charging,3=invalid.
uint8_t CCS_Malf=0;//ccs malfunction status. 0=normal,1=fail,3=invalid.
uint8_t CCS_Bmalf=0;//ccs battery malfunction status. 0=no,1=yes,3=invalid.
uint8_t CCS_Stop=0;//ccs chargeing stop status. 0=tracking,1=supression,3=invalid.
uint8_t CCS_Iso=0;//ccs isolation status. 0=invalid,1=valid,2=error,3=invalid signal.
uint8_t CCS_IntStat=0;//ccs charger internal status. 0=not ready,1=ready,2=switch off charger,3=interruption,4=pre charge,5=insulation monitor,6=estop,7=malfunction,0x13=reserved,0x14=reserved,0x15=invlaid signal.
static uint32_t sec_328=0;
static uint16_t Cont_Volts=0;
static uint16_t Bulk_SOCt=0;//Time to bulk soc target.
static uint16_t Full_SOCt=0;//Time to full SOC target.
static uint32_t CHG_Pwr=0; //calculated charge power. 12 bit value scale x25. Values based on 50kw DC fc and 1kw and 3kw ac logs. From bms???
static int16_t FC_Cur=0; //10 bit signed int with the ccs dc current command.scale of 1.
static uint8_t EOC_Time=0x00; //end of charge time in minutes.
static ChargeStatus CHG_Status=ChargeStatus::NotRdy; //observed values 0 when not charging , 1 and transition to 2 when commanded to charge. only 4 bits used.
//seems to control led colour.
static ChargeRequest CHG_Req=ChargeRequest::EndCharge; //observed values 0 when not charging , 1 when requested to charge. only 1 bit used in logs so far.
static ChargeReady CHG_Ready=ChargeReady::NotRdy; //indicator to the LIM that we are ready to charge. observed values 0 when not charging , 1 when commanded to charge. only 2 bits used.
static uint8_t CONT_Ctrl=0; //4 bits with DC ccs contactor command.
static uint8_t CCSI_Spnt=0;
static uint8_t LastSeenOpmode = 0xff;
void i3LIMClass::handle3B4(uint32_t data[2]) //Lim data
{
/*
0x3B4 D4 low nible: status pilot
0=no pilot
1=10-96%PWM not charge ready
2=10-96%PWM charge ready
3=error
4=5% not charge ready
5=5% charge ready
6=pilot static
*/
uint8_t* bytes = (uint8_t*)data;// arrgghhh this converts the two 32bit array into bytes. See comments are useful:)
uint8_t CP_Amps=bytes[0];
Param::SetInt(Param::PilotLim,CP_Amps);
uint8_t PP_Amps=bytes[1];
Param::SetInt(Param::CableLim,PP_Amps);
bool PP=(bytes[2]&0x1);
Param::SetInt(Param::PlugDet,PP);
CP_Mode=(bytes[4]&0x7);
Param::SetInt(Param::PilotTyp,CP_Mode);
Cont_Volts=bytes[7]*2;
// Cont_Volts=FP_MUL(Cont_Volts,2);
Param::SetInt(Param::CCS_V_Con,Cont_Volts);//voltage measured on the charger side of the hv ccs contactors in the car
ChargeType=bytes[6];
}
void i3LIMClass::handle29E(uint32_t data[2]) //Lim data. Available current and voltage from the ccs charger
{
uint8_t* bytes = (uint8_t*)data;// arrgghhh this converts the two 32bit array into bytes. See comments are useful:)
uint16_t V_Avail=((bytes[2]<<8)|(bytes[1]));
V_Avail=FP_TOINT(FP_DIV(V_Avail,10));
Param::SetInt(Param::CCS_V_Avail,V_Avail);//available voltage from ccs charger
uint16_t I_Avail=((bytes[4]<<8)|(bytes[3]));
I_Avail=FP_TOINT(FP_DIV(I_Avail,10));
Param::SetInt(Param::CCS_I_Avail,I_Avail);//available current from ccs charger
CCS_Iso = (bytes[0]>>6)&0x03;
CCS_IntStat = (bytes[0]>>2)&0x0f;
Param::SetInt(Param::CCS_COND,CCS_IntStat);//update evse condition on webui
}
void i3LIMClass::handle2B2(uint32_t data[2]) //Lim data. Current and Votage as measured by the ccs charger
{
uint8_t* bytes = (uint8_t*)data;// arrgghhh this converts the two 32bit array into bytes. See comments are useful:)
uint16_t CCS_Vmeas=((bytes[1]<<8)|(bytes[0]));
CCS_Vmeas=FP_TOINT(FP_DIV(CCS_Vmeas,10));
Param::SetInt(Param::CCS_V,CCS_Vmeas);//Voltage measurement from ccs charger
uint16_t CCS_Imeas=((bytes[3]<<8)|(bytes[2]));
CCS_Imeas=FP_TOINT(FP_DIV(CCS_Imeas,10));
Param::SetInt(Param::CCS_I,CCS_Imeas);//Current measurement from ccs charger
[[maybe_unused]] uint8_t Batt_Cmp=bytes[4]&0xc0; //battrery compatability flag from charger? upper two bits of byte 4.
CCS_Ilim = (bytes[5]>>4)&0x03;
CCS_Vlim = (bytes[5]>>6)&0x03;
CCS_Stat = bytes[4]&0x03;
CCS_Malf = (bytes[4]>>2)&0x03;
CCS_Bmalf = bytes[5]&0x03;
CCS_Stop = (bytes[5]>>2)&0x03;
}
void i3LIMClass::handle2EF(uint32_t data[2]) //Lim data. Min available voltage from the ccs charger.
{
uint8_t* bytes = (uint8_t*)data;// arrgghhh this converts the two 32bit array into bytes. See comments are useful:)
uint16_t minV_Avail=((bytes[1]<<8)|(bytes[0]));
minV_Avail=FP_TOINT(FP_DIV(minV_Avail,10));
Param::SetInt(Param::CCS_V_Min,minV_Avail);//minimum available voltage from ccs charger
CCS_Plim = (bytes[6]>>4)&0x03;
}
void i3LIMClass::handle272(uint32_t data[2]) //Lim data. CCS contactor state and charge flap open/close status.
{
uint8_t* bytes = (uint8_t*)data;// arrgghhh this converts the two 32bit array into bytes. See comments are useful:)
// Only the top 6-bits indicate the contactor state
uint8_t Cont_stat=bytes[2] >> 2;
Param::SetInt(Param::CCS_Contactor,Cont_stat);
uint8_t drmodes=bytes[2]&0x03;
Param::SetInt(Param::CP_DOOR,drmodes);
}
void i3LIMClass::Send10msMessages(CanHardware* can)
{
uint16_t V_Batt=Param::GetInt(Param::udc)*10;
uint8_t V_Batt2=(Param::GetInt(Param::udc))/4;
int32_t I_Batt=(Param::GetInt(Param::idc)+819)*10;//(Param::GetInt(Param::idc);FP_FROMINT
//I_Batt=0xa0a0;
//uint16_t SOC_Local=25*10;//(Param::GetInt(Param::SOC))*10;
//uint16_t SOC_Local=(Param::GetInt(Param::SOC))*10;
uint16_t SOC_Local=(Param::GetInt(Param::SOCFC))*10;
uint8_t bytes[8]; //seems to be from i3 BMS.
bytes[0] = I_Batt & 0xFF; //Battery current LSB. Scale 0.1 offset 819.2. 16 bit unsigned int
bytes[1] = I_Batt >> 8; //Battery current MSB. Scale 0.1 offset 819.2. 16 bit unsigned int
bytes[2] = V_Batt & 0xFF; //Battery voltage LSB. Scale 0.1. 16 bit unsigned int.
bytes[3] = V_Batt >> 8; //Battery voltage MSB. Scale 0.1. 16 bit unsigned int.
bytes[4] = SOC_Local & 0xFF;; //Battery SOC LSB. 12 bit unsigned int. Scale 0.1. 0-100%
bytes[5] = SOC_Local >> 8; //Battery SOC MSB. 12 bit unsigned int. Scale 0.1. 0-100%
bytes[6] = 0x65; //Low nibble battery status. Seem to need to be 0x5.
bytes[7] = V_Batt2; //zwischenkreis. Battery voltage. Scale 4. 8 bit unsigned int.
can->Send(0x112, (uint32_t*)bytes,8); //
ctr_20ms++;
if(ctr_20ms==2)
{
ctr_20ms=0;
//Vehicle speed msg should be 20ms. Lets try 10...
bytes[0] = 0x7c;
bytes[1] = 0xcb;
bytes[2] = 0x00;
bytes[3] = 0x00;
bytes[4] = 0x8a;
can->Send(0x1a1, (uint32_t*)bytes,5); // average 20ms
}
}
void i3LIMClass::Send200msMessages(CanHardware* can)
{
uint8_t bytes[8];
//Lim command 3. Used in DC mode.
if(CP_Mode==0x4||CP_Mode==0x5) bytes[0] = 0xFC;
else bytes[0] = 0xFD;
//bytes[0] = 0xFD;// FD at standby, change to FC on 5% pilot. Change back to FD during energy transfer
bytes[1] = 0xFF;//these bytes are used as a timer during energy transfer but not at setup
bytes[2] = (uint8_t)Chg_Phase<<4; //upper nibble seems to be a mode command to the ccs station. 0 when off, 9 when in constant current phase of cycle.
//more investigation needed here...
//Lower nibble seems to be intended for two end charge commands each of 2 bits.
bytes[4] = 0xff;
bytes[5] = 0xff;
bytes[6] = 0xff;
bytes[7] = 0xff;
can->Send(0x2fa, (uint32_t*)bytes,8); // this msg varies from 82ms to 1s intervals.
//////////////////////////////////////////////////////////////////////////////
//Possibly needed for dc ccs.
////////////////////////////////////
uint16_t SOC_Local=(Param::GetInt(Param::SOC))*2;
bytes[0] = 0x2c;//BMS soc msg. May need to be dynamic
bytes[1] = 0xe2;
bytes[2] = 0x10;
bytes[3] = 0xa3;
//bytes[4] = 0x30; //display soc. scale 0.5.
bytes[4] = SOC_Local; //display soc. scale 0.5.
bytes[5] = 0xff;
bytes[6] = 0x02;
bytes[7] = 0xff;
can->Send(0x432, (uint32_t*)bytes,8); // average 190ms
bytes[0] = 0x00;//network management
bytes[1] = 0x00;
bytes[2] = 0x00;
bytes[3] = 0x00;
bytes[4] = 0x50;
bytes[5] = 0x00;
bytes[6] = 0x00;
bytes[7] = 0x1a;
can->Send(0x51a, (uint32_t*)bytes,8); // average 640ms
bytes[0] = 0x00;//network management.May need to be dynamic
bytes[1] = 0x00;
bytes[2] = 0x00;
bytes[3] = 0x00;
bytes[4] = 0xfd;
bytes[5] = 0x3c;
bytes[6] = 0xff;
bytes[7] = 0x40;
can->Send(0x540, (uint32_t*)bytes,8); // average 640ms
bytes[0] = 0x40;//network management zgw
bytes[1] = 0x10;
bytes[2] = 0x20;
bytes[3] = 0x00;
bytes[4] = 0x00;
bytes[5] = 0x00;
bytes[6] = 0x00;
bytes[7] = 0x00;
can->Send(0x510, (uint32_t*)bytes,8); // average 640ms
ctr_1second++;
if(ctr_1second==5)//only send every 1 second.
{
ctr_1second=0;
sec_328++; //increment seconds counter.
bytes[0] = sec_328;//rtc msg. needs to be every 1 sec. first 32 bits are 1 second wrap counter
bytes[1] = sec_328<<8;
bytes[2] = sec_328<<16;
bytes[3] = sec_328<<24;
bytes[4] = 0x87; //day counter 16 bit.
bytes[5] = 0x1e;
can->Send(0x328, (uint32_t*)bytes,6);
uint8_t opmode = Param::GetInt(Param::opmode);
//if(Param::GetInt(Param::opmode)==MOD_RUN) bytes[0] = 0xfb;//f1=no obd reset. fb=obd reset.
//if(Param::GetInt(Param::opmode)!=MOD_RUN) bytes[0] = 0xf1;//f1=no obd reset. fb=obd reset.
bytes[0] = 0xf1;
if (opmode == MOD_RUN && LastSeenOpmode != MOD_RUN)
bytes[0] = 0xfb;//f1=no obd reset. fb=obd reset.
bytes[1] = 0xff;
can->Send(0x3e8, (uint32_t*)bytes,2);
LastSeenOpmode = opmode;
bytes[0] = 0xc0;//engine info? rex?
bytes[1] = 0xf9;
bytes[2] = 0x80;
bytes[3] = 0xe0;
bytes[4] = 0x43;
bytes[5] = 0x3c;
bytes[6] = 0xc3;//0x3=park
bytes[7] = 0xff;
can->Send(0x3f9, (uint32_t*)bytes,8); //average 1s
ctr_5second++;
if(ctr_5second==4)//only send every 4 second.
{
ctr_5second=0;
//central locking status message.
bytes[0] = 0x81; //81=flap unlock, 80=flap lock.
bytes[1] = 0x00;
bytes[2] = 0x04;
bytes[3] = 0xff;
bytes[4] = 0xff;
bytes[5] = 0xff;
bytes[6] = 0xff;
bytes[7] = 0xff;
can->Send(0x2fc, (uint32_t*)bytes,8); // average 5s.
bytes[0] = 0x88;//central locking
bytes[1] = 0x88;
bytes[2] = 0xf8;
bytes[3] = 0x0f;
bytes[4] = 0xff;
bytes[5] = 0xff;
bytes[6] = 0xff;
bytes[7] = 0xff;
can->Send(0x2a0, (uint32_t*)bytes,8); // average 5s.
bytes[0] = 0xff;//vehicle condition
bytes[1] = 0xff;
bytes[2] = 0xc0;
bytes[3] = 0xff;
bytes[4] = 0xff;
bytes[5] = 0xff;
bytes[6] = 0xff;
bytes[7] = 0xfc;
can->Send(0x3a0, (uint32_t*)bytes,8); // average 4s.
}
}
/*not needed msgs at least on efacec
bytes[0] = 0x00;//network management edme
bytes[1] = 0x00;
bytes[2] = 0x00;
bytes[3] = 0x00;
bytes[4] = 0x00;
bytes[5] = 0x00;
bytes[6] = 0x00;
bytes[7] = 0x12;
Can::GetInterface(Param::GetInt(Param::lim_can))->Send(0x512, (uint32_t*)bytes,8); // only sent once on 19 log.
bytes[0] = 0x00;//network management kombi
bytes[1] = 0x00;
bytes[2] = 0x00;
bytes[3] = 0x00;
bytes[4] = 0xfe;
bytes[5] = 0x00;
bytes[6] = 0x00;
bytes[7] = 0x60;
Can::GetInterface(Param::GetInt(Param::lim_can))->Send(0x560, (uint32_t*)bytes,8); // not on is 2019 log
bytes[0] = 0xa8;//range info, milage display
bytes[1] = 0x86;
bytes[2] = 0x01;
bytes[3] = 0x02;
bytes[4] = 0x00;
bytes[5] = 0x05;
bytes[6] = 0xac;
bytes[7] = 0x03;
Can::GetInterface(Param::GetInt(Param::lim_can))->Send(0x330, (uint32_t*)bytes,8); // not on is 2019 log
bytes[0] = 0x00;//obd msg
bytes[1] = 0x2a;
bytes[2] = 0x00;
bytes[3] = 0x6c;
bytes[4] = 0x0f;
bytes[5] = 0x55;
bytes[6] = 0x00;
Can::GetInterface(Param::GetInt(Param::lim_can))->Send(0x397, (uint32_t*)bytes,7); // not on 19 log
*/
}
////////////////////////////////////////////////////////////////////////////////
void i3LIMClass::Send100msMessages(CanHardware* can)
{
uint8_t bytes[8];
bytes[0] = 0xff;//vehicle status msg
bytes[1] = 0x5f;
bytes[2] = 0x00;
bytes[3] = 0x00;
bytes[4] = 0x00;
bytes[5] = 0x00;
bytes[6] = 0xff;
bytes[7] = 0xff;
can->Send(0x03c, (uint32_t*)bytes,8); //average 100ms
uint16_t Wh_Local=Param::GetInt(Param::BattCap);
CHG_Pwr=(CHG_Pwr & 0xFFF);
// one more check that we never ever request more A than available
FC_Cur = std::min<int16_t>(FC_Cur, Param::GetInt(Param::CCS_I_Avail));
FC_Cur = std::min<int16_t>(FC_Cur, Param::GetInt(Param::CCS_ILim));
bytes[0] = Wh_Local & 0xFF; //Battery Wh lowbyte
bytes[1] = Wh_Local >> 8; //BAttery Wh high byte
bytes[2] = (((uint8_t)CHG_Status<<4)|((uint8_t)CHG_Req)); //charge status in bits 4-7.goes to 1 then 2.8 secs later to 2. Plug locking???. Charge request in lower nibble. 1 when charging. 0 when not charging.
bytes[3] = (((CHG_Pwr)<<4)|(uint8_t)CHG_Ready); //charge readiness in bits 0 and 1. 1 = ready to charge.upper nibble is LSB of charge power.Charge power forecast not actual power!
bytes[4] = CHG_Pwr>>4; //MSB of charge power.in this case 0x28 = 40x25 = 1000W. Probably net DC power into the Batt.
bytes[5] = FC_Cur & 0xff; //LSB of the DC ccs current command
bytes[6] = ((CONT_Ctrl<<4)|(FC_Cur>>12)); //bits 0 and 1 MSB of the DC ccs current command.Upper nibble is DC ccs contactor control. Observed in DC fc logs only.
//transitions from 0 to 2 and start of charge but 2 to 1 to 0 at end. Status and Ready operate the same as in AC logs.
bytes[7] = EOC_Time; // end of charge timer.
can->Send(0x3E9, (uint32_t*)bytes,8); //average 128ms
//LIM needs to see this but doesnt control anything...
bytes[0] = 0xca;
bytes[1] = 0xff;
bytes[2] = 0x0b;
bytes[3] = 0x02;
bytes[4] = 0x69;
bytes[5] = 0x26;
bytes[6] = 0xf3;
bytes[7] = 0x4b;
can->Send(0x431, (uint32_t*)bytes,8); //.average 197ms but as low as 49ms.
bytes[0] = 0xf5;//Wake up message.
bytes[1] = 0x28;
if(Param::GetInt(Param::opmode)==MOD_RUN) bytes[2] = 0x8a;//ignition on
if(Param::GetInt(Param::opmode)!=MOD_RUN) bytes[2] = 0x86;//ignition off 86
bytes[3] = 0x1d;
bytes[4] = 0xf1;
bytes[5] = 0x35;
bytes[6] = 0x30;
bytes[7] = 0x80;
can->Send(0x12f, (uint32_t*)bytes,8); //. average 100ms
//Lim command 2. Used in DC mode
uint16_t V_limit=0;
//if(lim_state==6) V_limit=401*10;//set to 400v in energy transfer state
//if(lim_state!=6) V_limit=Param::GetInt(Param::udc)*10;
if(lim_state==4) V_limit=Param::GetInt(Param::udc)*10;// drop vlim only during precharge
else V_limit=415*10;//set to 415v in all other states
uint8_t I_limit=125;//125A limit. may not work
bytes[0] = V_limit & 0xFF; //Charge voltage limit LSB. 14 bit signed int.scale 0.1 0xfa2=4002*.1=400.2Volts
bytes[1] = V_limit >> 8; //Charge voltage limit MSB. 14 bit signed int.scale 0.1
bytes[2] = I_limit; //Fast charge current limit. Not used in logs from 2014-15 vehicle so far. 8 bit unsigned int. scale 1.so max 254amps in theory...
bytes[3] = Full_SOCt & 0xFF; //time remaining in seconds to hit soc target from byte 7 in AC mode. LSB. 16 bit unsigned int. scale 10.Full SOC.
bytes[4] = Full_SOCt >> 8; //time remaining in seconds to hit soc target from byte 7 in AC mode. MSB. 16 bit unsigned int. scale 10.Full SOC.
bytes[5] = Bulk_SOCt & 0xFF; //time remaining in seconds to hit soc target from byte 7 in ccs mode. LSB. 16 bit unsigned int. scale 10.Bulk SOC.
bytes[6] = Bulk_SOCt >> 8; //time remaining in seconds to hit soc target from byte 7 in ccs mode. MSB. 16 bit unsigned int. scale 10.Bulk SOC.
bytes[7] = 0xA0; //Fast charge SOC target. 8 bit unsigned int. scale 0.5. 0xA0=160*0.5=80%
can->Send(0x2f1, (uint32_t*)bytes,8); //. average 100ms
if(Param::GetInt(Param::opmode)!=MOD_RUN) vin_ctr=0;
if((Param::GetInt(Param::opmode)==MOD_RUN) && vin_ctr<5)
{
/*
bytes[0] = 0x56; //vin in ascii from 2017 i3 : VB87926
bytes[1] = 0x42;
bytes[2] = 0x38;
bytes[3] = 0x37;
bytes[4] = 0x39;
bytes[5] = 0x32;
bytes[6] = 0x36;
Can::GetInterface(Param::GetInt(Param::lim_can))->Send(0x380, (uint32_t*)bytes,7); //
vin_ctr++;
*/
}
}
i3LIMChargingState i3LIMClass::Control_Charge(bool RunCh)
{
int opmode = Param::GetInt(Param::opmode);
if (opmode != MOD_RUN) //only do this if we are not in run mode
{
if (Param::GetBool(Param::PlugDet)&&(CP_Mode==0x1||CP_Mode==0x2)) //if we have an enable and a plug in and a std ac pilot lets go AC charge mode.
{
lim_state=0;//return to state 0
Param::SetInt(Param::CCS_State,lim_state);
Chg_Phase=ChargePhase::Standby;
CONT_Ctrl=0x0; //dc contactor mode 0 in AC
FC_Cur=0;//ccs current request zero
EOC_Time=0xFE;
CHG_Status=ChargeStatus::Rdy;
CHG_Req=ChargeRequest::Charge;
CHG_Ready=ChargeReady::Rdy;
CHG_Pwr=6500/25;//approx 6.5kw ac
if(RunCh)return i3LIMChargingState::AC_Chg;//set ac charge mode if we are enabled on webui
if(!RunCh)
{
lim_state=0;//return to state 0
Param::SetInt(Param::CCS_State,lim_state);
Chg_Phase=ChargePhase::Standby;
CONT_Ctrl=0x0; //dc contactor mode 0 in off
FC_Cur=0;//ccs current request zero
EOC_Time=0x00;
CHG_Status=ChargeStatus::NotRdy;
CHG_Req=ChargeRequest::EndCharge;
CHG_Ready=ChargeReady::NotRdy;
CHG_Pwr=0;
return i3LIMChargingState::No_Chg;//set no charge mode if we are disabled on webui and in state 9 of dc machine
}
}
if (Param::GetBool(Param::PlugDet)&&(CP_Mode==0x4||CP_Mode==0x5||CP_Mode==0x6)) //if we have an enable and a plug in and a 5% pilot or a static pilot lets go DC charge mode.
{
/*
0=no pilot
1=10-96%PWM not charge ready
2=10-96%PWM charge ready
3=error
4=5% not charge ready
5=5% charge ready
6=pilot static
*/
Param::SetInt(Param::CCS_State,lim_state);//update state machine level on webui
switch(lim_state)
{
case 0:
{
Chg_Phase=ChargePhase::Standby;
CONT_Ctrl=0x0; //dc contactor mode control required in DC
FC_Cur=0;//ccs current request from web ui for now.
EOC_Time=0x00;//end of charge timer
CHG_Status=ChargeStatus::Init;
CHG_Req=ChargeRequest::Charge;
CHG_Ready=ChargeReady::NotRdy;
CHG_Pwr=0;//0 power
CCSI_Spnt=0;//No current
//if(CP_Mode==0x4 && opmode==MOD_CHARGE) lim_state++;
lim_stateCnt++; //increment state timer counter
if(lim_stateCnt>20)//2 second delay
{
lim_state++; //next state after 2 secs
lim_stateCnt=0;
}
}
break;
case 1:
{
//uint16_t I_avail_tmp=Param::GetInt(Param::CCS_I_Avail);
Chg_Phase=ChargePhase::Initialisation;
CONT_Ctrl=0x0; //dc contactor mode control required in DC
FC_Cur=0;//ccs current request from web ui for now.
EOC_Time=0x00;//end of charge timer
CHG_Status=ChargeStatus::Init;
CHG_Req=ChargeRequest::Charge;
CHG_Ready=ChargeReady::NotRdy;
CHG_Pwr=0;//0 power
CCSI_Spnt=0;//No current
if(CP_Mode==0x6) lim_state=0; //Reset to state 0 if we get a static pilot
//if(I_avail_tmp>10 && I_avail_tmp<500) lim_stateCnt++;
if(ChargeType==0x09) lim_stateCnt++;
if(lim_stateCnt>25)//2 secs efacec critical! 20 works. 50 does not.
{
lim_state++; //next state after 4 secs
lim_stateCnt=0;
}
}
break;
case 2:
{
//
Chg_Phase=ChargePhase::CableTest;
CONT_Ctrl=0x0; //dc contactor mode control required in DC
FC_Cur=0;//ccs current request from web ui for now.
EOC_Time=0x1E;//end of charge timer 30 mins
Bulk_SOCt=1800; //Set bulk SOC timer to 30 minutes.
Full_SOCt=2400; //Set full SOC timer to 40 minutes.
Timer_1Sec=5; //Load the 1 second loop counter. 5 loops=1sec.
Timer_60Sec=60; //Load the 60 second loop counter. 5 loops=1sec.
CHG_Status=ChargeStatus::Init;
CHG_Req=ChargeRequest::Charge;
CHG_Ready=ChargeReady::Rdy;
CHG_Pwr=44000/25;//44kw approx power
CCSI_Spnt=0;//No current
if(Cont_Volts>0)lim_state++; //we wait for the contactor voltage to rise before hitting next state.
}
break;
case 3:
{
//I don't like this state CableTest here. Should it remain in Initialisation ....
Chg_Phase=ChargePhase::CableTest;
CONT_Ctrl=0x0; //dc contactor mode control required in DC
FC_Cur=0;//ccs current request from web ui for now.
// EOC_Time=0x1E;//end of charge timer
CHG_Status=ChargeStatus::Init;
CHG_Req=ChargeRequest::Charge;
CHG_Ready=ChargeReady::Rdy;
CHG_Pwr=44000/25;//39kw approx power
CCSI_Spnt=0;//No current
/*if(Cont_Volts<=50)*/lim_stateCnt++; //we wait for the contactor voltage to drop under 50v to indicate end of cable test
if(lim_stateCnt>10)
{
if(CCS_Iso==0x1) lim_state++; //next state after 2 secs if we have valid iso test
lim_stateCnt=0;
}
}
break;
case 4:
{
Chg_Phase = ChargePhase::Subpoena; //precharge phase in this state
CONT_Ctrl = 0x0; //dc contactor mode control required in DC
FC_Cur = 0; //ccs current request from web ui for now.
// EOC_Time=0x1E;//end of charge timer
CHG_Status = ChargeStatus::Init;
CHG_Req = ChargeRequest::Charge;
CHG_Ready = ChargeReady::Rdy;
CHG_Pwr = 44000 / 25; //49kw approx power
CCSI_Spnt = 0; //No current
if ((Param::GetInt(Param::udc) - Cont_Volts) < 20)
{
lim_stateCnt++; //we wait for the contactor voltage to be 20v or less diff to main batt v
}
else
{
// If the contactor voltage wanders out of range start again
lim_stateCnt = 0;
}
// Wait for contactor voltage to be stable for 2 seconds
if (lim_stateCnt > 20)
{
lim_state++; //next state after 2 secs
lim_stateCnt = 0;
}
}
break;
case 5:
{
//precharge phase in this state but voltage close enough to close contactors
Chg_Phase = ChargePhase::Subpoena;
CONT_Ctrl = 0x2; //dc contactor closed
FC_Cur = 0; //ccs current request from web ui for now.
// EOC_Time=0x1E;//end of charge timer
CHG_Status = ChargeStatus::Init;
CHG_Req = ChargeRequest::Charge;
CHG_Ready = ChargeReady::Rdy;
CHG_Pwr = 44000 / 25; //49kw approx power
CCSI_Spnt = 0; //No current
// Once the contactors report as closed we're OK to proceed to energy transfer
if (Param::GetBool(Param::CCS_Contactor))
{
lim_state++;
}
}
break;
case 6:
{
Chg_Phase=ChargePhase::EnergyTransfer;
CONT_Ctrl=0x2; //dc contactor to close mode
//FC_Cur=Param::GetInt(Param::CCS_ICmd);//ccs manual control
FC_Cur=CCSI_Spnt;//Param::GetInt(Param::CCS_ICmd);//ccs auto ramp
CCS_Pwr_Con(); //ccs power control subroutine
Chg_Timers(); //Handle remaining time timers.
// EOC_Time=0x1E;//end of charge timer
CHG_Status=ChargeStatus::Rdy;
CHG_Req=ChargeRequest::Charge;
CHG_Ready=ChargeReady::Rdy;
CHG_Pwr=44000/25;//49kw approx power
//we chill out here charging.
if((!RunCh)||CCS_IntStat==0x02||CCS_IntStat==0x0f)//if we have a request to terminate from the web ui or the evse then move to next state.
{
FC_Cur=0;//set current to 0
lim_state++; //move to state 7 (shutdown)
}
}
break;
case 7: //shutdown state
{
Chg_Phase=ChargePhase::Shutdown;
CONT_Ctrl=0x2; //dc contactor to close mode
FC_Cur=0;//current command to 0
EOC_Time=0x1E;//end of charge timer
CHG_Status=ChargeStatus::Init;
CHG_Req=ChargeRequest::Charge;
CHG_Ready=ChargeReady::Rdy;
CHG_Pwr=44000/25;//49kw approx power
lim_stateCnt++;
if(lim_stateCnt>10) //wait 2 seconds
{
lim_state++; //next state after 2 secs
lim_stateCnt=0;
}
}
break;
case 8: //shutdown state
{
Chg_Phase=ChargePhase::Shutdown;
CONT_Ctrl=0x1; //dc contactor to open with diag mode
FC_Cur=0;//current command to 0
EOC_Time=0x1E;//end of charge timer
CHG_Status=ChargeStatus::Init;
CHG_Req=ChargeRequest::Charge;
CHG_Ready=ChargeReady::NotRdy;
CHG_Pwr=44000/25;//49kw approx power
lim_stateCnt++;
if(Cont_Volts==0)lim_stateCnt++; //we wait for the contactor voltage to return to 0 to indicate contactors open
if(lim_stateCnt>10)
{
lim_state++; //next state after 2 secs
lim_stateCnt=0;
}
}
break;
case 9: //shutdown state
{
Chg_Phase=ChargePhase::Standby;
CONT_Ctrl=0x0; //dc contactor to open mode
FC_Cur=0;//current command to 0
EOC_Time=0x1E;//end of charge timer
CHG_Status=ChargeStatus::Init;
CHG_Req=ChargeRequest::EndCharge;
CHG_Ready=ChargeReady::NotRdy;
CHG_Pwr=0;//0 power
return i3LIMChargingState::No_Chg;
}
break;
}
if(RunCh)return i3LIMChargingState::DC_Chg;//set dc charge mode if we are enabled on webui
if((!RunCh)&&lim_state==9)return i3LIMChargingState::No_Chg;//set no charge mode if we are disabled on webui and in state 9 of dc machine
}
if (!Param::GetBool(Param::PlugDet)) //if we plug remove shut down
{
lim_state=0;//return to state 0
Param::SetInt(Param::CCS_State,lim_state);
Chg_Phase=ChargePhase::Standby;
CONT_Ctrl=0x0; //dc contactor mode 0 in off
FC_Cur=0;//ccs current request zero
EOC_Time=0x00;
CHG_Status=ChargeStatus::NotRdy;
CHG_Req=ChargeRequest::EndCharge;
CHG_Ready=ChargeReady::NotRdy;
CHG_Pwr=0;
return i3LIMChargingState::No_Chg;
}
}
// If nothing matches then we aren't charging
return i3LIMChargingState::No_Chg;
}
void i3LIMClass::CCS_Pwr_Con() //here we control ccs charging during state 6.
{
uint16_t Tmp_Vbatt=Param::GetInt(Param::udc);//Actual measured battery voltage by isa shunt
uint16_t Tmp_Vbatt_Spnt=Param::GetInt(Param::Voltspnt);
uint16_t Tmp_ICCS_Lim=Param::GetInt(Param::CCS_ILim);
uint16_t Tmp_ICCS_Avail=Param::GetInt(Param::CCS_I_Avail);
//int16_t Tmp_Ibatt=Param::GetInt(Param::idc);
if(CCSI_Spnt>Tmp_ICCS_Lim)CCSI_Spnt=Tmp_ICCS_Lim; //clamp setpoint to current lim paramater.
if(CCSI_Spnt>150)CCSI_Spnt=150; //never exceed 150amps for now.
if(CCSI_Spnt>=Tmp_ICCS_Avail)CCSI_Spnt=Tmp_ICCS_Avail; //never exceed available current
if(CCSI_Spnt>250)CCSI_Spnt=0; //crude way to prevent rollover
if((Tmp_Vbatt<Tmp_Vbatt_Spnt)&&(CCS_Ilim==0x0)&&(CCS_Plim==0x0))CCSI_Spnt++;//increment if voltage lower than setpoint and power and current limts not set from charger.
if(Tmp_Vbatt>Tmp_Vbatt_Spnt)CCSI_Spnt--;//decrement if voltage equal to or greater than setpoint.
if(CCS_Ilim==0x1)CCSI_Spnt--;//decrement if current limit flag is set
if(CCS_Plim==0x1)CCSI_Spnt--;//decrement if Power limit flag is set
// force once more that we stay within our maximum bounds
if(CCSI_Spnt>=Tmp_ICCS_Avail)CCSI_Spnt=Tmp_ICCS_Avail; //never exceed available current
if(CCSI_Spnt>Tmp_ICCS_Lim)CCSI_Spnt=Tmp_ICCS_Lim; //clamp setpoint to current lim paramater.
Param::SetInt(Param::CCS_Ireq,CCSI_Spnt);
}
void i3LIMClass::Chg_Timers()
{
Timer_1Sec--; //decrement the loop counter
if(Timer_1Sec==0) //1 second has elapsed
{
Timer_1Sec=5;
Bulk_SOCt--; //Decrement timers. Just on time for now will be current based in final version
Full_SOCt--;
Timer_60Sec--; //decrement the 1 minute counter
if(Timer_60Sec==0)
{
Timer_60Sec=60;
EOC_Time--; //decrement end of charge minutes timer
}
}
}