-
Notifications
You must be signed in to change notification settings - Fork 2
/
Arduino_eVCU.ino
410 lines (347 loc) · 9.31 KB
/
Arduino_eVCU.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
/*
* Copyright (C) 2015 Michal Podhradsky
*
* This file is part of Viking Motorsports Arduino_eVCU.
*
* Viking Motorsports Arduino_eVCU is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* Viking Motorsports Arduino_eVCU is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Viking Motorsports Arduino_eVCU; see the file COPYING. If not, write to
* the Free Software Foundation, 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
*/
/**
* @file Arduino_eVCU.ino
*
* Application main file
*/
#include <DueTimer.h>
#include "variant.h"
#include <due_can.h>
#include "main.h"
/**
* PIN macros
*/
#define PIN_ON(_x) digitalWrite(_x, HIGH)
#define PIN_OFF(_x) digitalWrite(_x, LOW)
#define PIN_TOGGLE(_x) pin_toggle(_x)
inline void pin_toggle(int pin) {
int state = digitalRead(pin);
if (state==HIGH) {
digitalWrite(pin, LOW);
}
else {
digitalWrite(pin,HIGH);
}
}
/**
* LED macros
*/
#define LED_ON(_x) PIN_ON(_x)
#define LED_OFF(_x) PIN_OFF(_x)
#define LED_TOGGLE(_x) PIN_TOGGLE(_x)
/**
* Devices
*/
DeviceGEVCU* device;
DeviceBMS* bms;
/**
* Timer flags
*/
uint8_t flag_console;
uint8_t flag_telemetry;
uint8_t flag_failsafe;
uint8_t flag_bms_can;
uint8_t flag_rms_can;
uint8_t flag_sensors_input;
uint8_t flag_datalog;
/**
* Timer callbacks
*/
void console_timer(void){
flag_console = 1;
}
void telemetry_timer(void){
flag_telemetry = 1;
}
void failsafe_timer(void){
flag_failsafe = 1;
}
void bms_can_timer(void){
flag_bms_can= 1;
}
void rms_can_timer(void){
flag_rms_can= 1;
}
void sensors_input_timer(void){
flag_sensors_input = 1;
}
/**
* Systime
* We are hoping for 10ms resolution
*/
void systime_timer(void){
device->sys_time++;
}
void datalog_timer(void){
flag_datalog = 1;
}
/**
* Setup Arduino
*/
void setup() {
// DOUT0 - RTDS OFF
pinMode(RTDS_PIN, OUTPUT);
PIN_OFF(RTDS_PIN);
// DOUT1 - FW_ENABLE ON
pinMode(FW_ENABLE, OUTPUT);
PIN_ON(FW_ENABLE);
// DOUT2 - REGEN_DISABLE OFF
pinMode(REGEN_DISABLE, OUTPUT);
PIN_OFF(REGEN_DISABLE);
// DOUT3 - NC
pinMode(PIN_DOUT3, OUTPUT);
PIN_OFF(PIN_DOUT3);
// DOUT4 - RMS_THROTTLE ON
pinMode(RMS_THROTTLE, OUTPUT);
PIN_ON(RMS_THROTTLE);
// DOUT5 - AMS_STATUS ON (LOW=OK)
pinMode(AMS_STATUS, OUTPUT);
PIN_ON(AMS_STATUS);
// DOUT6 - PWR_READY ON
pinMode(PWR_READY, OUTPUT);
PIN_ON(PWR_READY);
// DOUT7 - PWR_STDBY OFF
pinMode(PWR_STDBY, OUTPUT);
PIN_OFF(PWR_STDBY);
// Analog inputs
//pinMode(THROTTLE_IN_1, INTPUT);
//pinMode(THROTTLE_IN_2, INTPUT);
//setup ports
Can0.begin(CAN_BPS_1000K); // RMS can == CAN
// right now we are using Can1 (aka CAN2 for RMS)
Can1.begin(CAN_BPS_500K); // BMS can == CAN2
int filter;
// just one frame
Can0.setRXFilter(0, 0, 0, false);
Can1.setRXFilter(0, 0, 0, false);
#if PRINT_DATA
SerialUSB.begin(921600); // use SerialUSB only as the programming port doesn't work
#endif
// Instantiate device
device = new DeviceGEVCU();
bms = new DeviceBMS();
/**
* Attach timers.
* It is calling for threads...
*/
// 2. console (serial over usb)
Timer2.attachInterrupt(console_timer).setFrequency(CONSOLE_FREQUENCY).start();
delay(50); // to offset the timer firing a little bit
// 3. telemetry (wifi)
Timer3.attachInterrupt(telemetry_timer).setFrequency(TELEMETRY_FREQUENCY).start();
delay(50); // to offset the timer firing a little bit
// 4. failsafe
Timer4.attachInterrupt(failsafe_timer).setFrequency(FAILSAFE_FREQUENCY).start();
delay(50); // to offset the timer firing a little bit
// 5. BMS CAN
Timer5.attachInterrupt(bms_can_timer).setFrequency(BMS_CAN_FREQUENCY).start();
delay(50); // to offset the timer firing a little bit
// 8. system time
Timer8.attachInterrupt(systime_timer).setFrequency(SYSTIME_FREQUENCY).start();
delay(50); // to offset the timer firing a little bit
}
/**
* Main loop, equivalent of while(1) {...}
*/
void loop() {
// Process RMS CAN
device->process();
// Process BMS CAN
bms->process();
// handle periodic tasks based on trigger from timers
handle_periodic_tasks();
}
/**
* Handle periodic tasks
*
* Timer calls the function within an ISR, so it
* should not be used for calling bulky or library functions
* (such as printf). Instead it triggers the flag, which is
* then handled here.
*/
inline void handle_periodic_tasks(void){
if (flag_console) {
bms->update_battery_data();
device->setMaxCellTemp(bms->getMaxCellTemp());
device->setMinCellTemp(bms->getMinCellTemp());
device->setMaxCellVolt(bms->getMaxCellVolt());
device->setMinCellVolt(bms->getMinCellVolt());
device->console_periodic();
bms->vsm_state = device->getVSMState();
flag_console = 0;
}
if (flag_telemetry) {
flag_telemetry = 0;
}
// start failsafe checks only after T_MIN secs
// so BMS have time to get CAN responses
if (flag_failsafe ) {
if (flag_failsafe && (device->sys_time > T_MIN)) {
failsafe_periodic();
}
flag_failsafe = 0;
}
if (flag_bms_can) {
bms->can_periodic();
flag_bms_can = 0;
}
if (flag_rms_can) {
flag_rms_can = 0;
}
if (flag_sensors_input) {
flag_sensors_input = 0;
}
if (flag_datalog) {
flag_datalog = 0;
}
}
/**
* Failsafe check
*
* This function will save your life is anything goes wrong.
* At least in theory - split to different functions at different frequencies
*
*/
inline void failsafe_periodic(void) {
/*
static int counter = 0;
if (counter > 255) {
counter = 0;
}
// send the square wave signal to the LED:
analogWrite(RMS_THROTTLE, counter);
counter++;
*/
// check for RTDS sound
static int rtds_cnt;
if ((device->vsm_state == VSM_ready) && (rtds_cnt < 10)) {
rtds_cnt++;
PIN_ON(RTDS_PIN);
}
else{
rtds_cnt = 0;
PIN_OFF(RTDS_PIN);
}
// check rlecs for faults
for (int i=0;i<NUM_RLECS;i++){
if (bms->rlecsX[i].status == Active) {
// critical faults
if ((bms->rlecsX[i].faults & RLEC_CELL_1_AD_FAULT) != 0) {
SerialUSB.print("!RLEC_CELL_1_AD_FAULT - shutting down...\r\n");
failsafe_shutdown();
}
else if ((bms->rlecsX[i].faults & RLEC_CELL_VOLTAGE_CONNECTION_FAULT) != 0) {
SerialUSB.print("!RLEC_CELL_VOLTAGE_CONNECTION_FAULT - shutting down...\r\n");
failsafe_shutdown();
}
else if ((bms->rlecsX[i].faults & RLEC_CELL_VOLTAGE_AD_FAULT) != 0) {
SerialUSB.print("!RLEC_CELL_VOLTAGE_AD_FAULT - shutting down...\r\n");
failsafe_shutdown();
}
else if ((bms->rlecsX[i].faults & RLEC_MODULE_VOLTAGE_AD_FAULT) != 0) {
SerialUSB.print("!RLEC_MODULE_VOLTAGE_AD_FAULT - shutting down...\r\n");
failsafe_shutdown();
}
else if ((bms->rlecsX[i].faults & RLEC_CELL_1_VOLTAGE_FAULT) != 0) {
SerialUSB.print("!RLEC_CELL_1_VOLTAGE_FAULT - shutting down...\r\n");
failsafe_shutdown();
}
// warnings
else if ((bms->rlecsX[i].faults & RLEC_CELL_TEMP_AD_FAULT) != 0) {
SerialUSB.print("!RLEC_CELL_TEMP_AD_FAULT - warning light on.\r\n");
failsafe_warning();
}
else if ((bms->rlecsX[i].faults & RLEC_RLEC_TEMP_AD_FAULT) != 0) {
SerialUSB.print("!RLEC_RLEC_TEMP_AD_FAULT - warning light on.\r\n");
failsafe_warning();
}
// Charging - overcharge protection
if (bms->rlecsX[i].max_cell_volt > MAX_CELL_VOLT) {
SerialUSB.print("Max cell voltage reached- shutting down.\r\n");
charger_shutdown();
}
// Voltage limits
// warning if minimal allowed voltage reached
if (bms->rlecsX[i].min_cell_volt < MIN_CELL_VOLT) {
//SerialUSB.print("Minimal voltage value: " +() "\r\n");
SerialUSB.print("Minimal voltage reached - shutting down.\r\n");
SerialUSB.print("RLEC " + String(i) + "\r\n");
SerialUSB.print("Min voltage: " + String((float)bms->rlecsX[i].min_cell_volt*0.00244) + "\r\n");
failsafe_shutdown();
}
// warning if cell below low threshold
else if (bms->rlecsX[i].min_cell_volt < BAT_LOW) {
}
// warning if cell below very low threshold
else if (bms->rlecsX[i].min_cell_volt < BAT_VERY_LOW) {
}
//Temperature limits
if (bms->rlecsX[i].max_cell_temp > MAX_CELL_TEMP) {
failsafe_shutdown();
SerialUSB.print("Max cell temperature reached- shutting down.\r\n");
}
}
}
}
/*
* Charger stop (HLIM low)
* HLIM: 1=ON, 0=OFF
*/
inline void charger_shutdown( void ) {
failsafe_shutdown();
}
/*
* Open main contactor (LLIM low)
* LLIM: 1=ON, 0 = FF
*/
inline void failsafe_shutdown( void ) {
PIN_OFF(FW_ENABLE); // disable FW_EN
delay(500); // give some time to remove current from AIRs
PIN_OFF(AMS_STATUS); // disable AIR (low is FAULT)
failsafe_warning(); // light up LED
}
/*
* Light up warning light
*/
inline void failsafe_warning( void ) {
LED_OFF(PWR_STDBY);
}
/*
* Light up BatLow
*/
inline void batlow_warning( void ) {
}
/*
* Light up BatLow & reduce throttle
*/
inline void batverylow_warning( void ) {
}
/*
* Battery ciritical (i.e. lowest allowed limit)
*/
inline void batcritical_warning( void ) {
PIN_OFF(FW_ENABLE); // disable FW_EN
delay(500); // give some time to remove current from AIRs
failsafe_shutdown(); // open shutdown circuit
}