-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathIRSensor.cpp
180 lines (152 loc) · 3.92 KB
/
IRSensor.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
#ifndef IRSENSOR_C
#define IRSENSOR_C
#include "IRSensor.h"
#include <Arduino.h>
//Uncomment to enable Sensor debugging with MegunoLink plotter software
//#define DEBUG_SENSOR true
#ifdef DEBUG_SENSOR
#define DBG_SENSOR sendPlotData
#else
#define DBG_SENSOR
#endif
IRSensor::IRSensor():
threshold(990),
preSilenceRequired(1000),
postSilenceRequired(1000),
maxSequenceDuration(1000), //1 s
maxPeaksAllowed(3),
peaksDistanceVariationAllowed(250), //ms
lastSequenceStart(0),
lastSequenceDuration(0),
lastSilence(0),
sequenceStart(0),
peakTime(0),
peaks(0),
lastPeak(0)
{
}
bool IRSensor::process(short sensor) {
bool val = _process(sensor);
if (val) {
DBG_SENSOR("sequence", 1025);
}
else {
DBG_SENSOR("sequence", threshold);
}
DBG_SENSOR("raw", sensor);
return val;
}
bool IRSensor::_process(short sensor)
{
unsigned long myTime = millis();
//Sequence is running for too much:
if(sequenceStart != 0 && peaks != maxPeaksAllowed && myTime - sequenceStart > maxSequenceDuration) {
//Forcibly ends the sequence
peaks = 0;
sequenceStart = 0;
DBG_SENSOR("ERR_TOO_LONG", 1025);
}
else if(sequenceStart != 0 && peaks == maxPeaksAllowed && lastSilence != 0 && myTime - lastSilence > postSilenceRequired) {
//Possibly Valid Sequence Found: all peaks received, silence detected and for more than postSilenceRequired
DBG_SENSOR("peaks", threshold); //Null peaks graph
//Check peaks validity:
int avgD = 0;
int maxD = 0;
int d = 0;
for(int c=1; c < peaks; c++) { //NB: forse qua dovrei escludere 1 picco con il maxD dalla media, per non alterarla
int d = peakTimes[c] - peakTimes[c-1];
if(d > maxD) {
maxD = d;
}
avgD += d;
}
avgD = 1.0 * avgD / peaks;
if(maxD > avgD + peaksDistanceVariationAllowed) {
//Ends the current sequence:
DBG_SENSOR("rejected", 1025);
peaks = 0;
sequenceStart = 0;
return false; //NOT VALID, there is an irregular peak
}
else {
lastSequenceDuration = peakTimes[peaks-1] - sequenceStart;
lastSequenceStart = sequenceStart;
//Ends the current sequence:
peaks = 0;
sequenceStart = 0;
return true; //VALID: al peaks in range d
}
}
else {
//Look for peaks:
int peakValue = peakDetection(sensor);
if(peakValue != -1) {
//Peak found
DBG_SENSOR("peaks", peakValue);
if(sequenceStart == 0) {
if(myTime - lastSilence > preSilenceRequired) {
//We have a new sequence!
peakTimes[peaks] = peakTime; //NB: we use peak time becuse peakDetection return when the edge goes low (at the end of th peak curve)
peaks++;
sequenceStart = peakTime;
}
else {
DBG_SENSOR("ERR_NO_SILENCE", 1025);
}
}
else if(peaks > maxPeaksAllowed) {
//Forcibly ends the sequence
peaks = 0;
sequenceStart = 0;
}
else {
//Continue the sequence
peakTimes[peaks] = peakTime;
peaks++;
}
}
else {
DBG_SENSOR("peaks", threshold);
}
}
//Edge detect of sensor:
if(sensor > threshold) {
lastSilence = 0;
DBG_SENSOR("lastSilence", threshold);
}
else {
if(lastSilence == 0) {
lastSilence = myTime;
DBG_SENSOR("lastSilence", 1025);
}
}
return false;
}
//For Meguno link
void IRSensor::sendPlotData(String seriesName, int data)
{
#ifdef DEBUG_SENSOR
Serial.print("{");
Serial.print(seriesName);
Serial.print(",T,");
Serial.print((float) data);
Serial.println("}");
#endif
}
int IRSensor::peakDetection(int sensorValue) {
if (sensorValue > lastPeak) {
lastPeak = sensorValue;
peakTime = millis();
}
if (sensorValue <= threshold ) {
if (lastPeak > threshold) {
// you have a peak value:
int peakValue = lastPeak;
// reset the peak value:
lastPeak = 0;
return peakValue;
}
}
return -1;
}
#endif