-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
346 lines (270 loc) · 8.04 KB
/
main.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
#include "mbed.h"
#include "WattBob_TextLCD.h"
#include <vector>
#define WATCHDOG_PIN p25
#define EXECUTION_PULSE_PIN p26
#define INPUT_WAVE_PIN p5
#define DIGITAL_SWITCH_PIN p8
#define SHUTDOWN_SWITCH_PIN p29
#define ANALOGUE_INPUT_1_PIN p18
#define ANALOGUE_INPUT_2_PIN p20
// Code function:
// - 1. Measure frequency of 3.3v square wave once every second
// - 2. Read one digital input every 1/3 second (300ms for simplicity OR 3 times a second)
// - 3. Output a watchdog pulse every 4 seconds
// - 4. Read two analogue inputs every 1/2 second (500ms)
// - 5. Display values on LCD every 2 seconds
// - 6. Switch check every 1/2 second (500ms) and display on LCD
// - 7. Log values every 5 seconds
// - 8. Check shutdown switch every other slot
// P5~P8 for digital
// P15 ~ P20 for analogue
//**********************************************
// Input & Output pins
//**********************************************
DigitalOut watchdog(WATCHDOG_PIN);
DigitalOut executionPulse(EXECUTION_PULSE_PIN);
DigitalIn wave(INPUT_WAVE_PIN);
DigitalIn digIn(DIGITAL_SWITCH_PIN);
DigitalIn shutdown(SHUTDOWN_SWITCH_PIN);
AnalogIn input1(ANALOGUE_INPUT_1_PIN);
AnalogIn input2(ANALOGUE_INPUT_2_PIN);
//Serial connection for logging
Serial pc(USBTX, USBRX);
//**********************************************
// Variables
//**********************************************
//Period and frequency of input wave signal
float period = 0;
int frequency = 0;
//Digital input switch
int switch_1 = 0;
//Analogue inputs
float analogue_in_1;
float analogue_in_2;
//Filtered analogue inputs
float average_analogue_in_1 = 0;
float average_analogue_in_2 = 0;
//Analogue input data array vectors
vector<float> analog1(4,0);
vector<float> analog2(4,0);
//Error code
int errorCode = 0;
//Execution time for each task
float exec1 = 0;
float exec2 = 0;
float exec3 = 0;
float exec4 = 0;
float exec5 = 0;
float exec6 = 0;
float exec7 = 0;
//Tick number
int ticks = 0;
//Pointer to 16-bit I/O object
MCP23017 *par_port;
//Pointer to LCD object
WattBob_TextLCD *lcd;
//Ticker for cyclic executive
Ticker ticker;
//Timers for wave period and task execution times
Timer timer;
Timer execTimer;
//**********************************************
// Tasks
//**********************************************
//Task 1: measure frequency of 3.3v square wave
void measureFrequency(){
//Start timer
execTimer.reset();
execTimer.start();
//Reset timer for signal width
timer.reset();
//Case for encountering rising edge
if(wave == 0){
while(wave==0){}
timer.start();
while(wave==1){}
}
//Case for encountering falling edge
else{
while(wave==1){}
timer.start();
while(wave==0){}
}
//Stop the signal width timer
timer.stop();
//Calculate period = time(low->high) OR time(high->low) * 2
//multiplied by 2 because of 50% duty cycle wave
period = timer.read_us()*2;
frequency = 1000000/period;
//Stop and store time taken to perform task
execTimer.stop();
exec1 = execTimer.read_us();
}
//Task 2: Read digital switch input
void readDigitalInput(){
//Start timer
execTimer.reset();
execTimer.start();
//Read digital input switch
switch_1 = digIn.read();
//Stop and store time taken to perform task
execTimer.stop();
exec2 = execTimer.read_us();
}
//Task 3: Output a watchdog signal of width 7ms
void outputWatchdog(){
//Start timer
execTimer.reset();
execTimer.start();
//Generate pulse of 7 ms
watchdog = 1;
wait_ms(7);
watchdog = 0;
//Stop and store time taken to perform task
execTimer.stop();
exec3 = execTimer.read_us();
}
//Task 4: Read analogue inputs and store average over past 4 inputs
void readAnalogueInput(){
//Start timer
execTimer.reset();
execTimer.start();
//Read analogue inputs
analogue_in_1 = input1.read();
analogue_in_2 = input2.read();
//Insert new reading at head of array, and remove oldest data from tail
analog1.insert(analog1.begin(), analogue_in_1);
analog1.erase(analog1.end());
analog2.insert(analog2.begin(), analogue_in_2);
analog2.erase(analog2.end());
float sum1 = 0;
float sum2 = 0;
//Calculate sum of data
for(int i = 0; i< analog1.size(); i++){
sum1 += analog1[i];
sum2 += analog2[i];
}
//Calculate average and set to 0~3.3v range
average_analogue_in_1 = (sum1/4)*3.3;
average_analogue_in_2 = (sum2/4)*3.3;
//Stop and store time taken to perform task
execTimer.stop();
exec4 = execTimer.read_us();
}
//Task 5: Display variables on LCD
void display(){
//Start timer
execTimer.reset();
execTimer.start();
lcd -> locate(0,0);
lcd -> printf("F:%i SW:%i", frequency, switch_1);
lcd -> locate(1,0);
lcd -> printf("%.2f %.2f", average_analogue_in_1, average_analogue_in_2);
//Stop and store time taken to perform task
execTimer.stop();
exec5 = execTimer.read_us();
}
//Task 6: Check error status
void errorCodes(){
//Start timer
execTimer.reset();
execTimer.start();
//Check error condition
if((switch_1 == 1) && (average_analogue_in_1 > average_analogue_in_2)){
errorCode = 3;
}
else{
errorCode = 0;
}
//Print error state to LCD
lcd -> locate(0,13);
if(errorCode == 3){
lcd -> printf("E:3");
}
else{
lcd -> printf("E:0");
}
//Stop and store time taken to perform task
execTimer.stop();
exec6 = execTimer.read_us();
}
//Task 7: Log variables to serial port
void log(){
//Start timer
execTimer.reset();
execTimer.start();
//Print to serial
pc.printf("%i, %i, %.2f, %.2f \r\n", frequency, switch_1, average_analogue_in_1, average_analogue_in_2);
//for debugging purposes, prints out execution times
//pc.printf("%.1f, %.1f, %.1f, %.1f, %.1f, %.1f, %.1f \r\n", exec1, exec2, exec3, exec4, exec5, exec6, exec7);
//Stop and store time taken to perform task
execTimer.stop();
exec7 = execTimer.read_us();
}
//Task 8: Check shutdown switch for system shutdown
void checkShutdown(){
//Read shutdown switch
int shutdownSwitch = shutdown.read();
if(shutdownSwitch){
//Print out shutdown message
lcd -> cls();
lcd -> locate(0,0);
lcd -> printf("Shutdown");
exit(0);
}
}
//**********************************************
// Cyclic Executive
//**********************************************
//Clock time of 50 ms
void CyclicExecutive(){
//Task 1
if(ticks % 20 == 1){
measureFrequency();
}
//Task 2
//else if(ticks % 6 == 2){
else if(ticks % 20 == 2 || ticks % 20 == 8 || ticks % 20 == 14){
readDigitalInput();
}
//Task 3
else if(ticks % 80 == 3){
outputWatchdog();
}
//Task 4
else if(ticks % 10 == 5){
readAnalogueInput();
}
//Task 5
else if(ticks % 40 == 7){
display();
}
//Task 6
else if(ticks % 10 == 9){
executionPulse = 1;
errorCodes();
executionPulse = 0;
}
//Task 7
else if(ticks % 100 == 11){
log();
}
//Task 8
else{
checkShutdown();
}
//Increment ticks
ticks++;
}
int main(){
//Initialise I/O
par_port = new MCP23017(p9,p10,0x40);
//Initialise LCD
lcd = new WattBob_TextLCD(par_port);
//Clear LCD & enable backlight
lcd -> cls();
par_port -> write_bit(1,BL_BIT);
//Start ticker at 50ms clock time
ticker.attach(&CyclicExecutive, 0.050);
}