-
Notifications
You must be signed in to change notification settings - Fork 2
/
BESMeasurement_v1.0.ino
454 lines (379 loc) · 10.4 KB
/
BESMeasurement_v1.0.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
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
#include <Adafruit_BMP085.h>
#include <Wire.h>
#include <avr/pgmspace.h>
//-Definations----------------------------------------------------------//
//Analog Devices
int engTempPin = A0;
int exhTempPin = A1;
//Digital Devices
int rearRPMPin = 3;
int engRPMPin = 2;
int statusPinF = 4;
int statusPinW = 5;
int statusPinS = 6;
int workingPin1 = 7;
int workingPin2 = 8;
//Version
float progvers = 1.0;
//-Declarations----------------------------------------------------------//
void serialEvent();
//Devices
Adafruit_BMP085 bmp;
//Environment
float envTemp;
float envPress;
float envAlt;
//Thermos
float engTemp;
float exhTemp;
float u_eng;
float u_exh;
uint8_t measCount;
//RPM
unsigned long dEngTime;
unsigned long dEngHigh;
unsigned long dEngLow;
unsigned long lastEngTime;
unsigned long dWheelTime;
unsigned long dWheelHigh;
unsigned long dWheelLow;
unsigned long lastWheelTime;
unsigned long refMicros;
//Measurement
boolean continous;
boolean temp;
//-Functions------------------------------------------------------------//
//CRC Table
PROGMEM const uint32_t crc_table[16] = {
0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
};
//CRC Functions
unsigned long crc_update(unsigned long crc, byte data)
{
byte tbl_idx;
tbl_idx = crc ^ (data >> (0 * 4));
crc = pgm_read_dword_near(crc_table + (tbl_idx & 0x0f)) ^ (crc >> 4);
tbl_idx = crc ^ (data >> (1 * 4));
crc = pgm_read_dword_near(crc_table + (tbl_idx & 0x0f)) ^ (crc >> 4);
return crc;
}
unsigned long crc_string(char *s)
{
unsigned long crc = ~0L;
while (*s)
crc = crc_update(crc, *s++);
crc = ~crc;
return crc;
}
char* string2char(String command) {
if (command.length() != 0) {
char *p = const_cast<char*>(command.c_str());
return p;
}
}
String createCRC(String msg) {
return String(crc_string(string2char(msg)));
}
String createTelegram(String msg) {
return ':' + msg + '>' + createCRC(msg) + ';';
}
//Measurement Functions
boolean readEnvironment () {
if (!bmp.begin()) {
setStatusSevere();
return false;
} else {
envTemp = bmp.readTemperature();
envPress = bmp.readPressure();
envAlt = bmp.readAltitude();
if (envPress == 0) {
setStatusWarning();
}
return true;
}
}
void readThermos() {
//ENGINE
u_eng = (analogRead(A0) * 4.5) / 1024;
engTemp = (u_eng - 1.248) / 0.005;
//EXHAUST
u_exh = (analogRead(A1) * 4.5) / 1024;
exhTemp = (u_exh - 1.248) / 0.005;
if (engTemp <= 0 || exhTemp <= 0) {
setStatusWarning();
}
}
//Status LED Functions
void setStatusFine() {
digitalWrite(statusPinF, LOW);
digitalWrite(statusPinW, HIGH);
digitalWrite(statusPinS, HIGH);
}
void setStatusWarning() {
digitalWrite(statusPinF, HIGH);
digitalWrite(statusPinW, LOW);
digitalWrite(statusPinS, HIGH);
}
void setStatusSevere() {
digitalWrite(statusPinF, HIGH);
digitalWrite(statusPinW, HIGH);
digitalWrite(statusPinS, LOW);
}
void setStatusMaxProblems() {
digitalWrite(statusPinF, HIGH);
digitalWrite(statusPinW, LOW);
digitalWrite(statusPinS, LOW);
}
void setNoStatus() {
digitalWrite(statusPinF, LOW);
digitalWrite(statusPinW, LOW);
digitalWrite(statusPinS, LOW);
}
void visualizeInitialization() {
setStatusFine();
delay(50);
setStatusWarning();
delay(50);
setStatusSevere();
delay(50);
setStatusWarning();
delay(50);
setStatusFine();
delay(50);
setStatusWarning();
delay(50);
setStatusSevere();
delay(50);
setStatusWarning();
delay(50);
setStatusFine();
delay(50);
setStatusWarning();
delay(50);
setStatusSevere();
delay(50);
setStatusWarning();
}
void visualizeInitComplete() {
setStatusFine();
delay(200);
setStatusWarning();
delay(200);
setStatusSevere();
delay(200);
setStatusWarning();
delay(200);
setStatusFine();
delay(200);
setStatusWarning();
}
void toggleWorkingLed1() {
digitalWrite(workingPin1, !digitalRead(workingPin1));
}
void toggleWorkingLed2() {
digitalWrite(workingPin2, !digitalRead(workingPin2));
}
//Reset Variables
void resetMeasurement() {
refMicros = micros();
}
//-Setup-once called----------------------------------------------------------//
void setup() {
Serial.begin(57600, SERIAL_8N1);
pinMode(workingPin1, OUTPUT);
pinMode(workingPin2, OUTPUT);
pinMode(statusPinF, OUTPUT);
pinMode(statusPinW, OUTPUT);
pinMode(statusPinS, OUTPUT);
pinMode(engRPMPin, INPUT_PULLUP);
pinMode(rearRPMPin, INPUT);
digitalWrite(workingPin1, LOW);
digitalWrite(workingPin2, LOW);
visualizeInitialization();
setNoStatus();
analogReference(EXTERNAL);
resetMeasurement();
dEngTime = 0;
dWheelTime = 0;
lastEngTime = 0;
lastWheelTime = 0;
dEngLow = 0;
dEngHigh = 0;
dWheelHigh = 0;
dWheelLow = 0;
measCount = 9;
continous = false;
temp = false;
attachInterrupt(digitalPinToInterrupt(engRPMPin), engISR, RISING);
attachInterrupt(digitalPinToInterrupt(rearRPMPin), rearISR, RISING);
}
//-Main---------------------------------------------------------------------//
void loop() {
dEngLow = pulseInLong(engRPMPin, LOW, 100000);
dEngHigh = pulseInLong(engRPMPin, HIGH, 100000);
dEngTime = dEngLow + dEngHigh;
dWheelLow = pulseInLong(rearRPMPin, LOW, 100000);
dWheelHigh = pulseInLong(rearRPMPin, HIGH, 100000);
dWheelTime = dWheelLow + dWheelHigh;
if (continous) {
if (temp) {
if (measCount == 9) {
readThermos();
measCount = 0;
}
measCount++;
String meas = String(dEngTime) + '#' + String(dWheelTime) + '#' + String(engTemp) + '#' + String(exhTemp) + '#' + String(micros()-refMicros);
Serial.println(createTelegram(meas));
Serial.flush();
} else {
String meas = String(dEngTime) + '#' + String(dWheelTime) + '#' + String(micros()-refMicros);
Serial.println(createTelegram(meas));
Serial.flush();
}
}
}
//-ISR-------------------------------------------------------------------//
//ISR for Engine
void engISR() {
toggleWorkingLed1();
}
//ISR for Rear-Wheel
void rearISR() {
toggleWorkingLed2();
}
//ISR for Communication
//INIT: :BESDyno>crc;
//START: :envTemp#envPress#envAlt>crc;
//ENGINE: :engTemp#exhTemp>crc;
//ALL: :engTime#rearTime#engTemp#exhTemp#Time>crc;
//MEASURE: :engTime#rearTime#Time>crc;
//MEASURENO: :rearTime#Time>crc;
//FINE: :FINE>crc;
//WARNING: :WARNING>crc;
//SEVERE: :SEVERE>crc;
//MAXPROBLEMS: :MAXPROBLEMS>crc;
//KILL: :KILL>CRC;
//VERSION: :version>crc;
//DEBUG: readable message in the terminal
//CRC is calculated without ':' and ';'
//Every Response: :Message>Checksum;
void serialEvent() {
while (Serial.available()) {
char req = (char)Serial.read();
if (req == 'i') {
visualizeInitialization();
String init = "BESDyno";
Serial.println(createTelegram(init));
Serial.flush();
delay(50);
visualizeInitComplete();
} else if (req == 's') {
setStatusFine();
if (readEnvironment()) {
String environment = String(envTemp) + '#' + String(envPress) + '#' + String(envAlt);
Serial.println(createTelegram(environment));
} else {
Serial.println(createTelegram("BMP-ERROR"));
}
Serial.flush();
resetMeasurement();
} else if (req == 'e') {
setStatusFine();
readThermos();
String thermos = String(engTemp) + '#' + String(exhTemp);
Serial.println(createTelegram(thermos));
Serial.flush();
} else if (req == 'a') {
measCount++;
if (dEngTime > 0 && dWheelTime > 0) {
setStatusFine();
} else {
setStatusWarning();
}
if (measCount == 9) {
readThermos();
measCount = 0;
}
String all = String(dEngTime) + '#' + String(dWheelTime) + '#' + String(engTemp) + '#' + String(exhTemp) + '#' + String(micros()-refMicros);
Serial.println(createTelegram(all));
Serial.flush();
} else if (req == 'm') {
if (dEngTime > 0 && dWheelTime > 0) {
setStatusFine();
} else {
setStatusWarning();
}
String measure = String(dEngTime) + '#' + String(dWheelTime) + '#' + String(micros()-refMicros);
Serial.println(createTelegram(measure));
Serial.flush();
} else if (req == 'n') {
if (dWheelTime > 0) {
setStatusFine();
} else {
setStatusWarning();
}
String measureno = String(dWheelTime) + '#' + String(micros()-refMicros);
Serial.println(createTelegram(measureno));
Serial.flush();
} else if (req == 'f') {
setStatusFine();
Serial.println(createTelegram("FINE"));
Serial.flush();
} else if (req == 'w') {
setStatusWarning();
Serial.println(createTelegram("WARNING"));
Serial.flush();
} else if (req == 'v') {
setStatusSevere();
Serial.println(createTelegram("SEVERE"));
Serial.flush();
} else if (req == 'x') {
setStatusMaxProblems();
Serial.println(createTelegram("MAXPROBLEMS"));
Serial.flush();
} else if (req == 'k') {
setStatusFine();
Serial.println(createTelegram("KILL"));
Serial.flush();
resetMeasurement();
} else if (req == 'p') {
setStatusFine();
Serial.println(createTelegram(String(progvers)));
Serial.flush();
} else if (req == 'd') {
setNoStatus();
readEnvironment();
readThermos();
Serial.println("Temperatur: " + String(envTemp));
Serial.println("Luftdruck: " + String(envPress));
Serial.println("Höhenmeter: " + String(envAlt));
Serial.println("A0 Motor: " + String(engTemp));
Serial.println("A0 U_Eng: " + String(u_eng));
Serial.println("A1 Abgas: " + String(exhTemp));
Serial.println("A1 U_Exh: " + String(u_exh));
Serial.println("D2 Motor: " + String(dEngTime));
Serial.println("D3 Walze: " + String(dWheelTime));
Serial.println("Zeit (µs): " + String(micros()-refMicros) + "\n");
Serial.flush();
} else if (req == 'c') {
continous = true;
Serial.println(createTelegram("CS"));
Serial.flush();
} else if (req == 'z') {
continous = false;
Serial.println(createTelegram("CT"));
Serial.flush();
} else if (req == 't') {
temp = true;
Serial.println(createTelegram("TE"));
Serial.flush();
} else if (req == 'u') {
temp = false;
Serial.println(createTelegram("TD"));
Serial.flush();
}
}
}