-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Maxbotix.cpp
270 lines (207 loc) · 4.9 KB
/
Maxbotix.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
#include "Maxbotix.h"
Maxbotix::Maxbotix(uint8_t pin, MAXBOTIX_INPUT_t input, MAXBOTIX_MODEL_t model, MAXBOTIX_FILTER_t filter,
uint8_t sample_size) :
pin(pin), input(input), model(model), filter(filter), sample_size(sample_size)
{
#ifdef MAXBOTIX_WITH_SOFTWARE_SERIAL
if (input == TX) {
serial = new RxSoftwareSerial(pin, true);
((RxSoftwareSerial*)serial)->begin(9600);
} else {
#endif
pinMode(pin, INPUT);
#ifdef MAXBOTIX_WITH_SOFTWARE_SERIAL
}
#endif
init();
}
#ifdef MAXBOTIX_WITH_SOFTWARE_SERIAL
Maxbotix::Maxbotix(Stream* serial, MAXBOTIX_MODEL_t model, MAXBOTIX_FILTER_t filter, uint8_t sample_size) :
input(TX), model(model), serial(serial), filter(filter), sample_size(sample_size)
{
init();
}
#endif
void Maxbotix::init()
{
switch (filter) {
case MEDIAN:
case HIGHEST_MODE:
case LOWEST_MODE:
case BEST:
if (sample_size == 0)
sample_size = 5;
else if (sample_size % 2)
sample_size++;
break;
case SIMPLE:
if (sample_size == 0)
sample_size = 2;
break;
case NONE:
default:
sample_size = 1;
break;
}
sample = new float[sample_size];
}
Maxbotix::~Maxbotix()
{
delete[] sample;
delete serial;
}
float Maxbotix::getRange()
{
float range;
readSample();
switch (filter) {
case MEDIAN:
range = getSampleMedian();
break;
case HIGHEST_MODE:
range = getSampleMode(true);
break;
case LOWEST_MODE:
range = getSampleMode(false);
break;
case SIMPLE:
while (sample[0] != sample[sample_size - 1])
pushToSample(readSensor());
range = sample[0];
break;
case BEST:
range = getSampleBest();
break;
case NONE:
default:
range = sample[0];
break;
}
return range;
}
float Maxbotix::getSampleMedian()
{
return sample[sample_size / 2];
}
float Maxbotix::getSampleMode(bool highest)
{
float mode = sample[0];
uint8_t mode_count = 1;
uint8_t count = 1;
for (int i = 1; i < sample_size; i++) {
if (sample[i] == sample[i - 1])
count++;
else
count = 1;
if (sample[i] == mode)
mode_count++;
else if (!highest && count > mode_count || highest && count == mode_count) {
mode_count = count;
mode = sample[i];
}
}
return mode;
}
float Maxbotix::getSampleBest()
{
float range;
if ((range = getSampleMode(true)) != getSampleMode(false))
range = getSampleMedian();
return range;
}
float Maxbotix::readSensor()
{
float result;
switch (input) {
case PW:
switch (model) {
case LV:
result = toCentimeters(pulseIn(pin, HIGH) / 147.0);
break;
case XL:
result = pulseIn(pin, HIGH) / 58.0;
break;
case HRLV:
result = pulseIn(pin, HIGH) / 10.0;
break;
default:
break;
}
break;
case AN:
switch (model) {
case LV:
result = toCentimeters(analogRead(pin) / 2.0);
break;
case XL:
result = analogRead(pin);
break;
case HRLV:
result = analogRead(pin) * 5.0 / 10.0;
break;
default:
break;
}
break;
#ifdef MAXBOTIX_WITH_SOFTWARE_SERIAL
case TX:
switch (model) {
case LV:
result = toCentimeters(readSensorSerial(3));
break;
case XL:
result = readSensorSerial(3);
break;
case HRLV:
result = readSensorSerial(4) / 10.0;
break;
default:
break;
}
break;
#endif
default:
break;
}
return result;
}
unsigned short Maxbotix::readSensorSerial(uint8_t length)
{
char buffer[length];
// flush and wait for a range reading
serial->flush();
while (!serial->available() || serial->read() != 'R');
// read the range
for (int i = 0; i < length; i++) {
while (!serial->available());
buffer[i] = serial->read();
}
return atoi(buffer);
}
void Maxbotix::readSample()
{
// read
for (int i = 0; i < sample_size; i++) {
sample[i] = readSensor();
if (input == AN && i != sample_size - 1)
delay(ad_sample_delay);
}
// sort
sortSample();
}
void Maxbotix::pushToSample(float value)
{
for (int i = 0; i < sample_size - 1; i++)
sample[i] = sample[i + 1];
sample[sample_size - 1] = value;
}
void Maxbotix::sortSample()
{
for (int i = 1; i < sample_size; i++) {
float j = sample[i];
int k;
for (k = i - 1; (k >= 0) && (j < sample[k]); k--)
sample[k + 1] = sample[k];
sample[k + 1] = j;
}
}