forked from Makuna/Tbs4Rpm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TBS4Rpm.ino
381 lines (332 loc) · 8.21 KB
/
TBS4Rpm.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
#include <TbsShieldRpm.h>
// Define number of cylinders to work with
const uint8_t numberOfCylinders = 2;
enum SamplingState
{
SS_Idle,
SS_StartZeroCalibrating,
SS_ZeroCalibrating,
SS_StartCalibrating,
SS_Calibrating,
SS_StartSampling,
SS_Sampling,
SS_StopSampling,
SS_SampleOverrun,
};
const uint32_t SamplePeriodUs = 1000; // 1ms
const uint32_t ReportPeriodUs = 100000; // 100ms
const uint32_t ZeroCalibratePeriodUs = 5000000; // 5s
const uint32_t CalibratePeriodUs = 2000000; // 2s
const uint32_t PongPeriodUs = 10000000; // 10s
const uint8_t CountOutputBuffer = 128;
const String Version = "<version=0.16>";
const uint8_t LedPin = 13;
// variables
String outputData = ""; // for streaming samples
String inputCommand = "";
char lastChar = ' ';
uint32_t prevTime;
uint32_t sampleTimeout = SamplePeriodUs;
uint32_t reportTimeout = ReportPeriodUs;
uint32_t calibrateTimeout = ZeroCalibratePeriodUs;
uint32_t idlePongTimeout = PongPeriodUs;
bool idleBeat = true;
uint8_t lastChannelReady = 0;
SamplingState samplingState = SS_Idle;
TbsShieldRpm tbs(nuberOfCylinders); // number of channels we have attached sensors for
// report format:
// kPa-100;1;2,50,1005,912,805;678
// <header> kPa-100
// <count of channels in report> 1
// <channel report> 2,50,1005,912,805
// <channel id> 2
// <sample width> 50
// <min value> 1005
// <value> 912
// <max value> 805
// <check sum> 678
//
// samples:
// 1;2,50,1005,912,805;678<etx>
// 4;0,50,1005,912,805;1,50,1005,912,805;2,50,1005,912,805;3,50,1005,912,805;1678<etx>
//
void SendReport(uint8_t channelsReady)
{
uint16_t prevLength = outputData.length();
outputData += "$";
outputData += channelsReady;
for (uint8_t channelIndex = 0; channelIndex < tbs.ChannelCount(); ++channelIndex)
{
if (tbs.SampleForCycleReady(channelIndex))
{
int16_t value;
outputData += ";";
// channel id
outputData += channelIndex;
// cycle width
outputData += ",";
outputData += tbs.SampleWidth(channelIndex);
// min
outputData += ",";
//value = tbs.SampleMin(channelIndex);
value = tbs.SampleMinKpa100(channelIndex);
outputData += value;
// using running average
outputData += ",";
//value = tbs.SampleAverage(channelIndex);
value = tbs.SampleAverageKpa100(channelIndex);
outputData += value;
// max
outputData += ",";
//value = tbs.SampleMax(channelIndex);
value = tbs.SampleMaxKpa100(channelIndex);
outputData += value;
}
}
{
// calc checksum
uint16_t sum = 0;
uint16_t count = outputData.length();
for (int index = prevLength; index < count; index++)
{
sum += outputData.charAt(index);
}
// add checksum
outputData += ";";
outputData += sum;
}
// add ETX and CR LF
outputData += "\x03\r\n"; // ETX
}
boolean ServiceReport()
{
const uint8_t countService = 128;
uint16_t length = outputData.length();
boolean needsService = (length > 0);
if (needsService)
{
if (length > countService)
{
length = countService;
}
Serial.print(outputData);
outputData = "";
}
return needsService;
}
void setup()
{
// comms setup
Serial.begin(19200);
bool stateLed = HIGH;
while (!Serial)
{
digitalWrite(LedPin, HIGH);
delay(250);
digitalWrite(LedPin, LOW);
delay(250);
; // wait for serial port to connect. Needed for Leonardo only
}
inputCommand.reserve(80);
inputCommand = "";
outputData.reserve(CountOutputBuffer);
outputData = "";
tbs.Setup();
// sample timer init
prevTime = micros();
}
void loop()
{
uint32_t nowTime = micros();
uint32_t deltaTime = nowTime - prevTime;
switch (samplingState)
{
case SS_Idle:
if (deltaTime > idlePongTimeout)
{
idlePongTimeout = PongPeriodUs;
if (idleBeat)
{
Serial.println("<idle0>");
}
else
{
Serial.println("<idle1>");
}
idleBeat = !idleBeat;
}
else
{
idlePongTimeout -= deltaTime;
}
break;
case SS_StartZeroCalibrating:
calibrateTimeout = ZeroCalibratePeriodUs;
sampleTimeout = SamplePeriodUs;
tbs.ReadSamples(); // ignore first sample read
tbs.ClearSamples();
Serial.println("<calibrating...>");
samplingState = SS_ZeroCalibrating;
break;
case SS_ZeroCalibrating:
if (deltaTime > sampleTimeout)
{
sampleTimeout = SamplePeriodUs;
tbs.ReadSamples();
}
else
{
sampleTimeout -= deltaTime;
}
if (deltaTime > calibrateTimeout)
{
samplingState = SS_Idle;
tbs.CalibrateAtZeroWithSamples();
Serial.println("<...calibrated>");
}
else
{
calibrateTimeout -= deltaTime;
}
break;
case SS_StartCalibrating:
calibrateTimeout = CalibratePeriodUs;
sampleTimeout = SamplePeriodUs;
tbs.ReadSamples(); // ignore first sample read
tbs.ClearSamples();
Serial.println("<min=-50.0 kPa>");
Serial.println("<max=0.0 kPa>");
Serial.println("<resolution=0.05 kPa>");
samplingState = SS_Calibrating;
break;
case SS_Calibrating:
if (deltaTime > sampleTimeout)
{
sampleTimeout = SamplePeriodUs;
tbs.ReadSamples();
}
else
{
sampleTimeout -= deltaTime;
}
if (deltaTime > calibrateTimeout)
{
samplingState = SS_StartSampling;
tbs.CalibrateCyclesWithSamples();
}
else
{
calibrateTimeout -= deltaTime;
}
break;
case SS_StartSampling:
sampleTimeout = SamplePeriodUs;
reportTimeout = ReportPeriodUs;
tbs.ClearSamples();
Serial.println("<running>");
samplingState = SS_Sampling;
outputData = "";
nowTime = micros(); // reset
break;
case SS_Sampling:
// split into fibers of work so as to keep work spread
// across time so sampling happens at consistent time frequency
//
if (deltaTime > sampleTimeout)
{
if (deltaTime > SamplePeriodUs)
{
samplingState = SS_SampleOverrun;
}
// read samples and reset time out to period - time past last period
sampleTimeout = SamplePeriodUs - (deltaTime - sampleTimeout);
lastChannelReady = tbs.ReadSamplesCycle();
}
else
{
sampleTimeout -= deltaTime;
if (lastChannelReady > 0)
{
// update a report on channels ready
SendReport( lastChannelReady );
lastChannelReady = 0;
}
else
{
// send out reports in chewable pieces
ServiceReport();
}
}
break;
case SS_SampleOverrun:
case SS_StopSampling:
if (!ServiceReport())
{
if (SS_SampleOverrun == samplingState)
{
Serial.println("<overrun>");
}
Serial.println("<stopped>");
samplingState = SS_Idle;
}
break;
}
prevTime = nowTime;
WorkAroundSerialEvent();
}
void WorkAroundSerialEvent()
{
static uint8_t stateLed = HIGH;
while (Serial.available())
{
digitalWrite(LedPin, stateLed);
stateLed != stateLed;
char inputChar = (char)Serial.read();
if (lastChar == '\r' && inputChar == '\n' )
{
inputCommand.trim();
if (samplingState == SS_ZeroCalibrating)
{
Serial.println("<still calibrating>");
}
else if (samplingState == SS_StartSampling ||
samplingState == SS_StartZeroCalibrating ||
samplingState == SS_StopSampling)
{
Serial.println("<still transitioning>");
}
else
{
// process command
if (inputCommand == "start")
{
samplingState = SS_StartCalibrating;
}
else if (inputCommand == "stop")
{
samplingState = SS_StopSampling;
}
else if (inputCommand == "calibrate")
{
samplingState = SS_StartZeroCalibrating;
}
else if (inputCommand == "query version")
{
Serial.println(Version);
}
else
{
Serial.print("?");
Serial.println(inputCommand);
}
}
// prepare for next command
inputCommand = "";
}
else
{
lastChar = inputChar;
inputCommand += inputChar;
}
}
}