-
Notifications
You must be signed in to change notification settings - Fork 0
/
BMS.ino
326 lines (300 loc) · 10.4 KB
/
BMS.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
#include <TimerOne.h>
#include "TaskStruct.h"
#include "SOC.h"
#include "TFT.h"
#include "Measurement.h"
#include "Contactor.h"
#include "Alarm.h"
#include "RemoteTerminal.h"
#include "DataLogging.h"
// pin for the HVIL Interrupt Alarm
const byte interruptPin = 21;
// period with which the timeBaseFlag should be set (100000ms = 10hz); how often the tasks shall run
const int interruptTime = 200000;
// Global variables declarations:
// flags:
bool turnOnReq;
bool alarmTrig;
bool alarmAck;
volatile bool timeBaseFlag;
bool maxCurrFlag = false;
bool minCurrFlag = false;
bool maxVolFlag = false;
bool minVolFlag = false;
bool maxSocFlag = false;
bool minSocFlag = false;
bool resetLog = false;
//contractor flags
bool hvilAlarmFlag = false;
bool hvilInterruptAlarmFlag = false;
bool overCurrentAlarmFlag = false;
bool outOfRangeVoltageFlag = false;
//Remote terminal flag
bool resetEEProm = false;
// data:
int hvilData = 0;
int tempData;
float currentData;
float voltageData;
long isoResData;
int isoCKTData;
float socData;
// max/min history:
float maxCurrent;
float minCurrent;
float maxVoltage;
float minVoltage;
float maxSOC;
float minSOC;
// alarms:
String hvilAlarm;
String overcurrentAlarm;
String hvorAlarm;
String hvilInterrupt;
// for timing of specific tasks
long counter = 0;
// Task Declarations
TCB tft;
TftData tftTaskData;
TCB measurement;
MeasurementData measurementTaskData;
TCB soc;
SocData socTaskData;
TCB contactor;
ContactorData contactorTaskData;
TCB alarm;
AlarmData alarmTaskData;
TCB remoteTerminal;
RemoteTerminalData remoteTerminalTaskData;
TCB dataLog;
DataLoggingData dataLogTaskData;
// Reference to top of the task queue and the task currently being executed
TCB* topOfQueue = &tft;
TCB* currentTask = topOfQueue;
TCB* lastTask = &alarm;
// Initialize the variables
void initVars(){
tempData = 0;
hvilData = 1;
currentData = 10;
voltageData = 335;
isoResData = 0;
isoCKTData = 0;
socData = 0;
maxCurrent = 0;
minCurrent = 0;
maxVoltage = -1;
minVoltage = -1;
maxSOC = -1;
minSOC = -1;
hvilAlarm = "NOT ACTIVE";
overcurrentAlarm = "NOT ACTIVE";
hvorAlarm = "NOT ACTIVE";
hvilInterrupt = "NOT ACTIVE";
turnOnReq = false;
alarmTrig = false;
alarmAck = false;
pinMode(interruptPin, INPUT_PULLUP);
timeBaseFlag = 0;
};
// Initialize task and their fields
void initTasks(){
// TFT
tft.myTask = &tftTaskFnc;
tft.taskDataPtr = &tftTaskData;
tftTaskData.tftSocPtr = &socData;
tftTaskData.tftTempPtr = &tempData;
tftTaskData.tftCurrPtr = ¤tData;
tftTaskData.tftVoltPtr = &voltageData;
tftTaskData.tftIsoResPtr = &isoResData;
tftTaskData.tftIsoCktPtr = &isoCKTData;
tftTaskData.tftHvilAlarmPtr = &hvilAlarm;
tftTaskData.tftOverCurrPtr = &overcurrentAlarm;
tftTaskData.tftHvorPtr = &hvorAlarm;
tftTaskData.tftHvilInterrupt = &hvilInterrupt;
tftTaskData.tftTurnOnPtr = &turnOnReq;
tftTaskData.tftHvilPtr = &hvilData;
tftTaskData.tftAlarmTrigPtr = &alarmTrig;
tftTaskData.tftAlarmAckPtr = &alarmAck;
tftTaskData.tftCounterPtr = &counter;
// MEASUREMENT
measurement.myTask = &measurementTaskFnc;
measurement.taskDataPtr = &measurementTaskData;
measurementTaskData.currentDataPtr = ¤tData;
measurementTaskData.voltageDataPtr = &voltageData;
measurementTaskData.maxCurrentPtr = &maxCurrent;
measurementTaskData.maxVoltagePtr = &maxVoltage;
measurementTaskData.minCurrentPtr = &minCurrent;
measurementTaskData.minVoltagePtr = &minVoltage;
measurementTaskData.maxCurrFlagPtr = &maxCurrFlag;
measurementTaskData.maxVolFlagPtr = &maxVolFlag;
measurementTaskData.minVolFlagPtr = &minVolFlag;
measurementTaskData.minCurrFlagPtr = &minCurrFlag;
// SOC
soc.myTask = &socTaskFnc;
soc.taskDataPtr = &socTaskData;
// socTaskData.dataPtr = &socData;
socTaskData.voltageDataPtr = &voltageData;
socTaskData.currentDataPtr = ¤tData;
socTaskData.SOCDataPtr = &socData;
socTaskData.maxCurrDataPtr = &maxCurrent;
socTaskData.minCurrDataPtr = &minCurrent;
socTaskData.maxCurrFlag = &maxCurrFlag;
socTaskData.minCurrFlag = &minCurrFlag;
socTaskData.maxVoltDataPtr = &maxVoltage;
socTaskData.minVoltDataPtr = &minVoltage;
socTaskData.maxVolFlag = &maxVolFlag;
socTaskData.minVolFlag = &minVolFlag;
socTaskData.maxSOC = &maxSOC;
socTaskData.minSOC = &minSOC;
socTaskData.maxSOCFlag = &maxSocFlag;
socTaskData.minSOCFlag = &minSocFlag;
// CONTACTOR
contactor.myTask = &contactorTaskFnc;
contactor.taskDataPtr = &contactorTaskData;
contactorTaskData.turnOnReqPtr = &turnOnReq;
//contactorTaskData.hvilInterruptPtr = &hvilInterrupt;
contactorTaskData.hvilAlarmFlagPtr = &hvilAlarmFlag;
contactorTaskData.hvilInterruptAlarmFlagPtr = &hvilInterruptAlarmFlag;
contactorTaskData.overCurrentAlarmFlagPtr = &overCurrentAlarmFlag;
contactorTaskData.outOfRangeVoltageFlagPtr = &outOfRangeVoltageFlag;
// ALARM
alarm.myTask = &alarmTaskFnc;
alarm.taskDataPtr = &alarmTaskData;
alarmTaskData.hvilAlarmPtr = &hvilAlarm;
alarmTaskData.overcurrentAlarmPtr = &overcurrentAlarm;
alarmTaskData.hvorAlarmPtr = &hvorAlarm;
alarmTaskData.hvilInterruptPtr = &hvilInterrupt;
alarmTaskData.alarmTriggerPtr = &alarmTrig;
alarmTaskData.alarmAckPtr = &alarmAck;
alarmTaskData.hvilPtr = &hvilData;
alarmTaskData.currentPtr = ¤tData;
alarmTaskData.voltagePtr = &voltageData;
alarmTaskData.hvilAlarmFlagPtr = &hvilAlarmFlag;
alarmTaskData.hvilInterruptAlarmFlagPtr = &hvilInterruptAlarmFlag;
alarmTaskData.overCurrentAlarmFlagPtr = &overCurrentAlarmFlag;
alarmTaskData.outOfRangeVoltageFlagPtr = &outOfRangeVoltageFlag;
// REMOTE TERMINAL
remoteTerminal.myTask = &remoteTaskFnc;
remoteTerminal.taskDataPtr = &remoteTerminalTaskData;
remoteTerminalTaskData.resetEEpromPtr = &resetLog;
remoteTerminalTaskData.maxCurrDataPtr = &maxCurrent;
remoteTerminalTaskData.minCurrDataPtr = &minCurrent;
remoteTerminalTaskData.maxVoltDataPtr = &maxVoltage;
remoteTerminalTaskData.minVoltDataPtr = &minVoltage;
// DATA LOGGING
dataLog.myTask = &dataLoggingTaskFnc;
dataLog.taskDataPtr = &dataLogTaskData;
dataLogTaskData.maxCurrentPtr = &maxCurrent;
dataLogTaskData.minCurrentPtr = &minCurrent;
dataLogTaskData.maxVoltagePtr = &maxVoltage;
dataLogTaskData.minVoltagePtr = &minVoltage;
dataLogTaskData.maxSOCPtr = &maxSOC;
dataLogTaskData.minSOCPtr = &minSOC;
dataLogTaskData.maxCurrFlagPtr = &maxCurrFlag;
dataLogTaskData.minCurrFlagPtr = &minCurrFlag;
dataLogTaskData.maxVolFlagPtr = &maxVolFlag;
dataLogTaskData.minVolFlagPtr = &minVolFlag;
dataLogTaskData.maxSocFlagPtr = &maxSocFlag;
dataLogTaskData.minSocFlagPtr = &minSocFlag;
dataLogTaskData.resetFlagPtr = &resetLog;
};
// Initialize the queue
void initQueue() {
tft.prev = NULL;
tft.next = &measurement;
measurement.prev = &tft;
measurement.next = &soc;
soc.prev = &measurement;
soc.next = &contactor;
contactor.prev = &soc;
contactor.next = &alarm;
alarm.prev = &contactor;
alarm.next = &remoteTerminal;
remoteTerminal.prev = &alarm;
remoteTerminal.next = &dataLog;
dataLog.prev = &remoteTerminal;
dataLog.next = NULL;
};
// Initialize the screen display
void initDisplay() {
screen.reset();
uint16_t identifier = screen.readID();
if(identifier != 0x9325 && identifier != 0x9328 && identifier != 0x4535 && identifier != 0x7575 && identifier != 0x9341 && identifier != 0x8357) {
identifier=0x9341;
}
screen.begin(identifier);
screen.setRotation(2);
screen.fillScreen(BLACK);
// create main screen buttons
for (uint8_t col=0; col<3; col++) {
buttons[col].initButton(&screen, BUTTON_X+col*(BUTTON_W+BUTTON_SPACING_X),
BUTTON_Y, // x, y, w, h, outline, fill, text
BUTTON_W, BUTTON_H, ILI9341_WHITE, buttoncolor, ILI9341_WHITE,
buttonlabels[col], BUTTON_TEXTSIZE);
buttons[col].drawButton();
}
// create the back button
buttons[3].initButton(&screen, BUTTON_X,
300, // x, y, w, h, outline, fill, text
BUTTON_W, BUTTON_H, ILI9341_WHITE, buttoncolor, ILI9341_WHITE,
buttonlabels[3], BUTTON_TEXTSIZE);
// create the battery on button
buttons[4].initButton(&screen, 100,
150, // x, y, w, h, outline, fill, text
BUTTON_W, BUTTON_H, ILI9341_WHITE, buttoncolor, ILI9341_WHITE,
buttonlabels[4], BUTTON_TEXTSIZE);
// create the battery off button
buttons[5].initButton(&screen, 180,
150, // x, y, w, h, outline, fill, text
BUTTON_W, BUTTON_H, ILI9341_WHITE, buttoncolor, ILI9341_WHITE,
buttonlabels[5], BUTTON_TEXTSIZE);
// create the acknowledge button
buttons[6].initButton(&screen, 130,
150, // x, y, w, h, outline, fill, text
120, BUTTON_H, ILI9341_WHITE, buttoncolor, ILI9341_WHITE,
buttonlabels[6], BUTTON_TEXTSIZE);
// keep track of last screen displayed
currScreen = 0;
prevScreen = 0;
};
// function for the timed interrupt
void interruptScheduler() {
timeBaseFlag = 1;
};
// function for the pin interrupt
// puts it in Active, Not Acknowledged state, triggers the alarm, and makes sure the contactors are OFF
void interruptAlarm() {
hvilInterrupt = "ACTIVE, NOT ACKNOWLEDGED";
hvilInterruptAlarmFlag = true;
hvilInterState = 1;
hvilInterTrig = true;
digitalWrite(24, LOW);
};
// startup task
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial1.begin(9600);
Serial1.setTimeout(1000);
initVars();
initTasks();
initQueue();
initDisplay();
attachInterrupt(digitalPinToInterrupt(interruptPin), interruptAlarm, RISING);
Timer1.initialize(interruptTime);
Timer1.attachInterrupt(interruptScheduler);
}
// the scheduler; loops through the tasks if prompted by the timed interrupt, until it reached the last task
// measures HVIL input at every iteration
void loop() {
if(1 == timeBaseFlag) {
timeBaseFlag = 0;
currentTask = topOfQueue;
while (currentTask != NULL) {
currentTask->myTask(currentTask->taskDataPtr);
currentTask = currentTask->next;
}
}
hvilData = !digitalRead(interruptPin);
}