-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
475 lines (391 loc) · 10.6 KB
/
main.cc
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//
// Copyright 2017 Janick Bergeron <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include <error.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
unsigned int gDebug = 0;
bool gIsRead = false;
FILE *gFp = NULL;
static struct channel_s {
double average;
bool isIdle;
bool isChanging;
uint32_t changeCount;
uint64_t detectTime;
bool hasEvent;
} channelData[2] = {
{0x200, true, 0, 0, false},
{0x200, true, 0, 0, false}
};
uint64_t frontWheelStamp = 0;
void
analyzeChannel(unsigned int chan,
uint16_t pressure,
uint64_t stamp)
{
// Reject obviously bad samples
if (pressure < 0x0180 || 0x1000 < pressure) return;
char HxL = 'x';
if (pressure >= channelData[chan].average + 0x0c0) {
// High pressure detected
HxL = 'H';
if (channelData[chan].isIdle) {
// Are we in the middle of a transition?
if (channelData[chan].isChanging) {
// Have we reached the end of the transition?
// Must see 20 consecutive high-pressure samples
if (channelData[chan].changeCount++ >= 20) {
channelData[chan].isIdle = false;
channelData[chan].isChanging = false;
channelData[chan].changeCount = 0;
channelData[chan].detectTime = stamp;
channelData[chan].hasEvent = true;
if (gDebug > 0) {
unsigned int otherChan = ((chan + 1) & 0x1);
if (channelData[otherChan].hasEvent) {
printf("DTCT %d %04x > %04x at %08llx with pending event on %d %lld ms ago\n",
chan, pressure, (unsigned int) channelData[chan].average, stamp,
otherChan, stamp - channelData[otherChan].detectTime);
} else {
printf("DTCT %d %04x > %04x at %08llx with no event on %d\n",
chan, pressure, (unsigned int) channelData[chan].average, stamp, otherChan);
}
}
}
} else {
// We started changing
channelData[chan].isChanging = true;
channelData[chan].changeCount = 1;
}
} else {
// A single high-pressure sample cancels a low-pressure transition
channelData[chan].isChanging = false;
channelData[chan].changeCount = 0;
}
} else if (pressure <= channelData[chan].average + 0x020) {
// Low ressure detected
HxL = 'L';
if (not channelData[chan].isIdle) {
// Are we in the middle of a transition?
if (channelData[chan].isChanging) {
// Must see > 20 consecutive low-pressure samples
if (channelData[chan].changeCount++ >= 60) {
channelData[chan].isIdle = true;
channelData[chan].isChanging = false;
channelData[chan].changeCount = 0;
if (gDebug > 0) {
printf("IDLE %d %04x < %04x at %08llx\n",
chan, pressure, (unsigned int) channelData[chan].average, stamp);
}
}
} else {
// We started changing
channelData[chan].isChanging = true;
channelData[chan].changeCount = 1;
}
} else {
// A single low-pressure sample cancels a high-pressure transition
channelData[chan].isChanging = false;
channelData[chan].changeCount = 0;
}
}
if (gDebug > 1) {
printf("%04x %04x %c %08llx %c%s%3d ", pressure, (unsigned int) channelData[chan].average, HxL, stamp,
channelData[chan].isIdle ? 'L' : 'H',
channelData[chan].isChanging ? "->" : " ",
channelData[chan].changeCount);
}
if (channelData[chan].isIdle && !channelData[chan].isChanging) {
// Update the running average
#define AVERAGE_WIN 250
channelData[chan].average = ((channelData[chan].average * (double) (AVERAGE_WIN-1)) + pressure) / (double) AVERAGE_WIN;
}
}
void
analyzeSample(uint16_t chan0,
uint16_t chan1,
uint64_t stamp)
{
if (gFp != NULL && !gIsRead) {
fprintf(gFp, "%04x %04x %08llx\n", chan0, chan1, stamp);
}
analyzeChannel(0, chan0, stamp);
analyzeChannel(1, chan1, stamp);
if (gDebug > 1) {
printf("\n");
}
// printf("%d %f\n", chan0, channelData[0].average);
// if (channelData[0].average < 425) exit(9);
// Do we have an event recorded on both channels?
if (!channelData[0].hasEvent || !channelData[1].hasEvent) return;
// Which one occured first?
long long int ms = channelData[0].detectTime - channelData[1].detectTime;
bool isUp = true;
if (ms < 0) {
isUp = false;
ms = -ms;
}
// Reject detections that are way to slow
if (ms > 2000) {
// But save the latest event to recover
if (isUp) channelData[1].hasEvent = false;
else channelData[0].hasEvent = false;
return;
}
// If it takes 'ms' to cover 12 inches, what is the speed?
double mph = ((double) 681.8) / ms;
// Marked these event has handled
channelData[0].hasEvent = false;
channelData[1].hasEvent = false;
time_t now = stamp/1000;
struct tm *lt = localtime(&now);
printf("%ld %4d/%02d/%02d %02d:%02d:%02d ", now,
lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec);
// Reject if the speed is too high
if (0 && mph > 60) {
printf(" ");
} else {
printf("%6.1f MPH %4shill.", mph, (isUp) ? "Up" : "Down");
};
// Measure wheel base (It does not matter which hose we use)
stamp = channelData[1].detectTime;
ms = stamp - frontWheelStamp;
if (ms < 0) ms = -ms;
double feet = 0.00147 * ms * mph;
frontWheelStamp = stamp;
// Reject if the wheelbase is obviously too long
if (feet < 25) {
printf(" Wheel base =%5.1f ft.", feet);
}
printf("\n");
fflush(stdout);
}
//
// GPIO operations
//
int
gpioOpen(int num, char rw)
{
int fd;
char buf[256];
fd = open("/sys/class/gpio/unexport", O_WRONLY);
if (fd < 0) {
fprintf(stderr, "Unable to open GPIO unexport: ");
perror(NULL);
return -1;
}
sprintf(buf, "%d", num);
if (write(fd, buf, 3) < 3) {
fprintf(stderr, "Unable to unexport GPIO %d: ", num);
perror(NULL);
// That's OK, as long as they can be exported next...
}
close(fd);
fd = open("/sys/class/gpio/export", O_WRONLY);
if (fd < 0) {
fprintf(stderr, "Unable to export GPIO %d: ", num);
perror(NULL);
return -1;
}
if (write(fd, buf, 3) < 3) {
fprintf(stderr, "Unable to export GPIO %d: ", num);
perror(NULL);
close(fd);
return -1;
}
close(fd);
sprintf(buf, "/sys/class/gpio/gpio%d/direction", num);
fd = open(buf, O_WRONLY);
if (fd < 0) {
fprintf(stderr, "Unable to open GPIO %d: ", num);
perror(NULL);
return -1;
}
if (rw == 'w') {
if (write(fd, "out", 3) < 3) {
fprintf(stderr, "Unable to set GPIO %d to OUT: ", num);
perror(NULL);
close(fd);
return -1;
}
} else {
if (write(fd, "in", 2) < 2) {
fprintf(stderr, "Unable to set GPIO %d to IN: ", num);
perror(NULL);
close(fd);
return -1;
}
}
sprintf(buf, "/sys/class/gpio/gpio%d/value", num);
fd = open(buf, (rw == 'w') ? O_WRONLY : O_RDONLY);
if (fd < 0) {
fprintf(stderr, "Unable to open GPIO %d: ", num);
perror(NULL);
return -1;
}
return fd;
}
char rbuf[32];
bool
gpioRead(int fd)
{
lseek(fd, 0, SEEK_SET);
while (1) {
int n = read(fd, rbuf, sizeof(rbuf));
for (int i = 0; i < n; i++) {
if (rbuf[i] == '1') return 1;
if (rbuf[i] == '0') return 0;
}
}
return 0;
}
//
// fd for the GPIO 'value' files
//
int CSn = 0;
int CLK = 0;
int DO = 0;
int DI = 0;
//
// Initialize the MCP3202 serial interface
//
void
initADC()
{
write(CSn, "1", 1);
write(CLK, "0", 1);
}
//
// Read a digital channel on a MCP3202 ADC
//
uint16_t readADC(int channel)
{
// Prepare next conversion
write(CLK, "1", 1);
write(CLK, "0", 1);
// Start bit
write(CSn, "0", 1);
write(DI, "1", 1);
write(CLK, "1", 1);
write(CLK, "0", 1);
// Single-Ended
write(CLK, "1", 1);
write(CLK, "0", 1);
// Channel selection
write(DI, (channel) ? "1" : "0", 1);
write(CLK, "1", 1);
write(CLK, "0", 1);
// MSBF
write(DI, "1", 1);
write(CLK, "1", 1);
write(CLK, "0", 1);
// Null bit
write(CLK, "1", 1);
write(CLK, "0", 1);
uint16_t dval = 0;
for (int i = 0; i < 12; i++) {
// Bn
write(CLK, "1", 1);
dval = (dval << 1) | gpioRead(DO);
write(CLK, "0", 1);
}
// End of conversion
write(CSn, "1", 1);
return dval;
}
int
main(int argc, char* argv[])
{
const char* fname = NULL;
int optc;
while ((optc = getopt(argc, argv, "D:hr:w:")) != -1) {
switch (optc) {
case 'D':
gDebug = atoi(optarg);
break;
case 'h':
case '?':
fprintf(stderr, "Usage: %s [-D n] [-r fname | -w fname]\n", argv[0]);
exit(1);
case 'r':
gIsRead = true;
fname = optarg;
break;
case 'w':
gIsRead = false;
fname = optarg;
break;
}
}
if (fname != NULL) {
gFp = fopen(fname, (gIsRead) ? "r" : "w");
if (gFp == NULL) {
fprintf(stderr, "ERROR: Cannot open \"%s\": ", fname);
perror(NULL);
return -1;
}
}
//
// Write our process ID in a file so we can be easily killed later
//
int pid = getpid();
FILE *fp = fopen("CarCount.pid", "w");
if (fp == NULL) {
fprintf(stderr, "Cannot open \"CarCount.pid\" for writing: ");
perror(0);
exit(-1);
}
fprintf(fp, "%d\n", pid);
fclose(fp);
uint64_t ms;
uint32_t chan0;
uint32_t chan1;
if (gFp != NULL && gIsRead) {
fscanf(gFp, "%x%x%llx", &chan0, &chan1, &ms);
channelData[0].average = chan0;
channelData[1].average = chan1;
while (fscanf(gFp, "%x%x%llx", &chan0, &chan1, &ms) == 3) {
if (ms < 0x10000000000) ms += 0x16100000000;
analyzeSample(chan0, chan1, ms);
}
fclose(gFp);
return 0;
}
CSn = gpioOpen(132, 'w');
CLK = gpioOpen(134, 'w');
DO = gpioOpen(136, 'r');
DI = gpioOpen(138, 'w');
if (CSn <= 0 || CLK <= 0 || DO <= 0 || DI <= 0) return -1;
initADC();
channelData[0].average = readADC(0);
channelData[1].average = readADC(1);
struct timeval tv;
while (1) {
chan0 = readADC(0);
chan1 = readADC(1);
gettimeofday(&tv, NULL);
ms = (((uint64_t) tv.tv_sec) * 1000) + (tv.tv_usec / 1000);
analyzeSample(chan0, chan1, ms);
}
return 0;
}