1
+ #include < thread>
2
+
3
+ #include " mbed.h"
4
+ #include " config.h"
5
+
6
+ void initIO ();
7
+ void initChargerCAN ();
8
+ void sendCAN ();
9
+
10
+ CAN* can;
11
+
12
+ uint32_t max_voltage_mV = 0 ;
13
+ uint16_t max_dc_current_mA = 0 ;
14
+ uint8_t max_ac_current_A = 0 ;
15
+
16
+ bool enable = false ;
17
+
18
+
19
+ AnalogIn control_pilot (PIN_CONTROL_PILOT);
20
+ AnalogIn proximity_pilot (PIN_PROXIMITY_PILOT);
21
+
22
+
23
+ int main ()
24
+ {
25
+
26
+ printf (" main()\n " );
27
+ initIO ();
28
+
29
+ ThisThread::sleep_for (500ms);
30
+
31
+ printf (" Starting main loop\n " );
32
+
33
+ bool prechargeDone = false ;
34
+ bool fault = false ;
35
+ bool shutdown_closed = false ;
36
+ bool cell_temps_fine = false ;
37
+
38
+ while (true ) {
39
+ CANMessage msg;
40
+
41
+
42
+ while (can->read (msg)) {
43
+ switch (msg.id ) {
44
+ case 0x80 : // sync
45
+ sendCAN ();
46
+ break ;
47
+ case 0x188 : // ACC_TPDO_STATUS
48
+ prechargeDone = msg.data [0 ] & 0b00001000 ;
49
+ fault = msg.data [0 ] & 0b00000011 ;
50
+ shutdown_closed = msg.data [0 ] & 0b00000100 ;
51
+ cell_temps_fine = !(msg.data [1 ] & 0b00010000 );
52
+ break ;
53
+ }
54
+ }
55
+
56
+
57
+ // if proximity pilot is about 2.7v then no EVSE connected
58
+ // if proximity pilot is about 1.7v then EVSE connected and button pressed
59
+ // if proximity pilot is about 0.9v then EVSE connected and button not pressed
60
+ bool proximity_pilot_ready = (proximity_pilot.read () * 3.3 < 1.2 );
61
+
62
+ // printf("pp: %f\n",proximity_pilot.read());
63
+
64
+ // Duty cycle is 31 times voltage on control pilot
65
+ // Duty cycle times 0.6 is max allowed continuous current draw
66
+ // control pilot voltage times 18.5 is the max allowed current
67
+ int max_ac_current_CP = std::floor (control_pilot.read () * 3.3 * 19 );
68
+
69
+ // printf ("proximity pilot: %x\n", proximity_pilot_ready);
70
+
71
+ // printf("cp: %f\n", control_pilot.read());
72
+ //
73
+ // printf("max ac current: %d\n", max_ac_current_CP);
74
+
75
+ max_ac_current_A = std::min (max_ac_current_CP, MAX_AC_CURRENT);
76
+
77
+
78
+
79
+ max_voltage_mV = VOLTAGE_TARGET_MV;
80
+ max_dc_current_mA = CURRENT_MAX_MA;
81
+
82
+ printf (" pp_ready: %x, precharge done: %x, fault: %x, sh closed: %x, cell temps fine: %x\n " , proximity_pilot_ready, prechargeDone, fault, shutdown_closed, cell_temps_fine);
83
+ enable = proximity_pilot_ready && prechargeDone && !fault && shutdown_closed && cell_temps_fine;
84
+ printf (" enable: %x\n " , enable);
85
+ }
86
+
87
+ // main() is expected to loop forever.
88
+ // If main() actually returns the processor will halt
89
+ return 0 ;
90
+ }
91
+
92
+ void initIO () {
93
+ printf (" initIO()\n " );
94
+ can = new CAN (PIN_CAN1_RD, PIN_CAN1_TD, CAN_FREQUENCY);
95
+
96
+ // LSS assign charger
97
+ initChargerCAN ();
98
+ }
99
+
100
+ void initChargerCAN () {
101
+ printf (" initChargerCAN()\n " );
102
+
103
+ ThisThread::sleep_for (2500ms);
104
+
105
+ // Switch state global protocal, switch to LSS configuration state
106
+ uint8_t lss0_data[8 ] = {0x04 , 0x01 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 };
107
+ CANMessage lss0_msg (0x7E5 , lss0_data);
108
+ can->write (lss0_msg);
109
+
110
+ ThisThread::sleep_for (500ms);
111
+
112
+ // Configurate node ID protocal, set node ID to 0x10
113
+ uint8_t lss1_data[8 ] = {0x11 , 0x10 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 };
114
+ CANMessage lss1_msg (0x7E5 , lss1_data);
115
+ can->write (lss1_msg);
116
+
117
+ ThisThread::sleep_for (100ms);
118
+
119
+ }
120
+
121
+
122
+ void sendCAN () {
123
+ // send charge limits
124
+ uint8_t charge_limits_data[8 ] = {
125
+ 0x10 ,
126
+ static_cast <uint8_t >(max_voltage_mV),
127
+ static_cast <uint8_t >(max_voltage_mV >> 8 ),
128
+ static_cast <uint8_t >(max_voltage_mV >> 16 ),
129
+ static_cast <uint8_t >(max_voltage_mV >> 24 ),
130
+ static_cast <uint8_t >(max_dc_current_mA),
131
+ static_cast <uint8_t >(max_dc_current_mA >> 8 ),
132
+ max_ac_current_A
133
+ };
134
+ CANMessage charge_limits_msg (0x306 , charge_limits_data);
135
+ can->write (charge_limits_msg);
136
+
137
+ // ThisThread::sleep_for(1ms);
138
+
139
+ // send charge control
140
+ uint8_t charge_control_data[8 ] = {
141
+ 0x10 ,
142
+ static_cast <uint8_t >((enable << 1 ) + 0b00100000 ),
143
+ 0x00 ,
144
+ 0x00 ,
145
+ 0x00 ,
146
+ 0x00 ,
147
+ 0x00 ,
148
+ 0x00
149
+ };
150
+ CANMessage charge_control_msg (0x206 , charge_control_data);
151
+ can->write (charge_control_msg);
152
+ ThisThread::sleep_for (1ms);
153
+ }
0 commit comments