-
Notifications
You must be signed in to change notification settings - Fork 0
/
URMSensor.h
399 lines (329 loc) · 10.4 KB
/
URMSensor.h
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
#ifndef URMSENSOR_H
#define URMSENSOR_H
#include "Arduino.h"
/**
* Value that marks invalid result of the measure. Receiving this value as the measurement result
* usually means that the URMSensor class either detected invalid state of the sensor pins on
* startMeasure() call, timed out waiting for sensor's response when starting measure, or
* did not receive echo from sensor in time.
*/
#define URM_INVALID_VALUE 0xFFFFFFFF
/**
* Unlocks direct work with port registers instead of digitalRead() and digitalWrite().
* This will make refreshState() and finishedMeasure() to work a bit faster (for about 5-10 us),
* but the URMSensor class will consume more RAM (about 12 bytes per instance), and the code
* will no more be compatible with Arduino Due.
*/
#define URM_USE_PORTS_DIRECTLY
/**
* Constant used to convert pulse width from DFRobot URM37 sensor (in PWM mode) to range.
*/
#define URM37_US_PER_CM 50 // us/cm (from datasheet)
#define URM37_TRIG_ACTIVE_STATE LOW
#define URM37_ECHO_ACTIVE_STATE LOW
#define URM37_TRIG_PULSE_WIDTH 1 // us
#define URM37_TIMEOUT_FOR_PULSE_START 50000 // us
#define URM37_MAX_PULSE_WIDTH 45000 // us
/**
* Constant used to convert pulse width from HC-SR04 sensor to range.
*/
#define HC_SR04_US_PER_CM 61 // us/cm (1e6 us/s / (100 cm/m * 330 m/s / 2))
#define HC_SR04_TRIG_ACTIVE_STATE HIGH
#define HC_SR04_ECHO_ACTIVE_STATE HIGH
#define HC_SR04_TRIG_PULSE_WIDTH 10 // us
#define HC_SR04_TIMEOUT_FOR_PULSE_START 10000 // us
#define HC_SR04_MAX_PULSE_WIDTH HC_SR04_US_PER_CM * 450
/**
* Possible states of any instance of class URMSensor. You can use it to interpret
* the getState() return value.
*/
enum URMState
{
/**
* The library is idle. Usually this state will be encountered only between
* startup and first startMeasure() call, after interruptMeasure() call or
* after encountering any errors while doing measure. The finishedMeasure()
* method will return true and URM_INVALID_VALUE in this state.
*/
Idle,
/**
* The library did receive startMeasure() call and is now waiting for ECHO line
* to switch to the ACTIVE state. The finishedMeasure() method will return false
* in this state.
*/
WaitingForPulse,
/**
* The library did receive startMeasure() call, received pulse start from the sensor
* and started to measure it's width. The finishedMeasure() method will
* return false in this state.
*
*/
Measuring,
/**
* The library did successfully measure the distance. The finishedMeasure() method
* will return true and measured distance in this state.
*/
FinishedMeasure
};
/**
* A class representing single ultrasonic ranging sensor.
*
* @author Andrey A. Vasenev
*/
class URMSensor
{
public:
/**
* Constructor.
*/
URMSensor()
{
_isAttached = false;
}
// ====== Attach- and detach-related methods ==================================================================
/**
* Attaches the sensor to physical pins of the target Arduino board. You should call this method
* inside the setup() function of your sketch for every instance of this class - otherwise it
* will not work.
*
* @param trigPin Number of the Arduino pin connected to the TRIG pin on the sensor.
*
* @param echoPin Number of the Arduino pin connected to the ECHO/PWM pin on the sensor.
*
* @param usPerCm Constant that will be used to convert pulse width to distance in centimeters (us/cm).
* For DFRobot URM37, you can use the value URM37_US_PER_CM; for HC-SR04, you can use the value
* HC_SR04_US_PER_CM. For other sensors, you should take this constant from the datasheet.
*
* @param timeoutForPulseStart Maximal allowed time between calling startMeasure() and
* receiving response from sensor (ECHO pin must change its state from IDLE to ACTIVE),
* in microseconds.
*
* @param maxPulseDuration Maximal allowed duration of pulse received from sensor (in microseconds).
*/
void attach(byte trigPin, byte echoPin, int usPerCm, unsigned long timeoutForPulseStart,
unsigned long maxPulseDuration, byte trigActiveState, byte echoActiveState,
unsigned int trigPulseWidth)
{
setTrigPin(trigPin);
setEchoPin(echoPin);
_usPerCm = usPerCm;
_timeoutForPulseStart = timeoutForPulseStart;
_maxPulseDuration = maxPulseDuration;
_trigActiveState = trigActiveState;
_echoActiveState = echoActiveState;
_trigPulseWidth = trigPulseWidth;
_isAttached = true;
}
/**
* Detaches the sensor from physical pins of the target Arduino board. This instance of
* the URMSensor class will not be able to communicate with the sensor after calling
* this method.
*/
void detach()
{
_isAttached = false;
}
/**
* Indicates whether this instance is attached to sensor.
*
* @return true if this instance is attached to sensor.
*/
boolean isAttached()
{
return _isAttached;
}
// ====== Methods for asynchronous data reading ===============================================================
/**
* Starts the measure.
*/
void startMeasure();
/**
* Indicates whether this instance is currently doing the measure.
*
* @return true if this instance currently measures the distance.
*/
inline boolean isMeasuring();
/**
* Interrupts the measure.
*/
void interruptMeasure();
/**
* Refreshes the state of this instance. If the ECHO pin of the sensor is connected
* to the interrupt- or PCINT-enabled pin, you can call this method from the interrupt
* handler function. You can also call this method by timer or in your loop() function,
* just ensure that you can call this method frequently enough.
*/
void refreshState();
/**
* Indicates whether this instance finished the measure. If this method returned true,
* you can retrieve measured distance by calling getMeasuredDistance().
*
* @return true if this instance is not currently measuring the distance.
*/
boolean finishedMeasure();
/**
* Simply retrieves the value from the previous measure.
*
* @return Distance from the previous measure, or URM_INVALID_VALUE if
* the previous measure failed or if this instance is currently measuring
* the distance.
*/
unsigned long getMeasuredDistance();
// ====== Synchronous distance reading ========================================================================
/**
* Synchronously gets the distance in front of the sensor. Maximal working time for
* this method is (timeoutForPulseStart + maxPulseDuration) us. This method is suitable
* for simple applications or for situations when you just need to get distance and
* don't mind if the board's core will be busy for 40-50 ms doing it.
*
* @return Distance in front of the sensor in centimeters, or URM_INVALID_VALUE
* in case of failure.
*/
unsigned long measureDistance();
// ====== Debugging-related methods ===========================================================================
/**
* Retrieves current state of this instance. May be useful for debugging
* any sensor- or library-related issues.
*/
byte getState()
{
return _currentState;
}
private:
boolean _isAttached;
#ifndef URM_USE_PORTS_DIRECTLY
byte _trigPin;
byte _echoPin;
#else
byte _trigMask;
volatile byte* _trigPORT;
volatile byte* _trigPIN;
volatile byte* _trigDDR;
byte _echoMask;
volatile byte* _echoPORT;
volatile byte* _echoPIN;
volatile byte* _echoDDR;
#endif
void setTrigPin(byte trigPin)
{
#ifdef URM_USE_PORTS_DIRECTLY
_trigMask = digitalPinToBitMask(trigPin);
byte trigPortIndex = digitalPinToPort(trigPin);
_trigPORT = portOutputRegister(trigPortIndex);
_trigPIN = portInputRegister(trigPortIndex);
_trigDDR = portModeRegister(trigPortIndex);
#else
_trigPin = trigPin;
#endif
setTrigMode(OUTPUT);
}
byte fastDigitalReadTrig()
{
byte state = LOW;
setTrigMode(INPUT);
#ifdef URM_USE_PORTS_DIRECTLY
return ((*_trigPIN & _trigMask) != 0) ? HIGH : LOW;
#else
state = digitalRead(_trigPin);
#endif
setTrigMode(OUTPUT);
return state;
}
void setTrigMode(byte newMode)
{
#ifdef URM_USE_PORTS_DIRECTLY
if (newMode == OUTPUT)
{
*_trigDDR |= _trigMask;
}
else
{
*_trigDDR &= ~_trigMask;
}
#else
pinMode(_trigPin, newMode);
#endif
}
void fastDigitalWriteTrig(byte value)
{
#ifdef URM_USE_PORTS_DIRECTLY
if (value == HIGH)
*_trigPORT |= _trigMask;
else
*_trigPORT &= ~_trigMask;
#else
digitalWrite(_trigPin, value);
#endif
}
void setEchoPin(byte echoPin)
{
#ifdef URM_USE_PORTS_DIRECTLY
_echoMask = digitalPinToBitMask(echoPin);
byte echoPortIndex = digitalPinToPort(echoPin);
_echoPORT = portOutputRegister(echoPortIndex);
_echoPIN = portInputRegister(echoPortIndex);
_echoDDR = portModeRegister(echoPortIndex);
*_echoDDR &= ~_echoMask;
*_echoPORT &= ~_echoMask;
#else
_echoPin = echoPin;
pinMode(_echoPin, INPUT);
#endif
}
byte fastDigitalReadEcho()
{
#ifdef URM_USE_PORTS_DIRECTLY
return ((*_echoPIN & _echoMask) != 0) ? HIGH : LOW;
#else
return digitalRead(_echoPin);
#endif
}
int _usPerCm;
unsigned long _timeoutForPulseStart;
unsigned long _maxPulseDuration;
byte _trigActiveState;
byte _echoActiveState;
byte getOppositeStateFor(byte activeState)
{
return (activeState == HIGH) ? LOW : HIGH;
}
unsigned int _trigPulseWidth;
unsigned long _startMeasureTime;
unsigned long _currentDuration;
void resetTime()
{
_currentDuration = 0;
_startMeasureTime = micros();
}
unsigned long getCurrentDuration()
{
_currentDuration = micros() - _startMeasureTime;
return _currentDuration;
}
unsigned long convertCurrentDurationToDistance()
{
return _currentDuration / _usPerCm;
}
byte _currentState;
};
class URM37 : public URMSensor
{
public:
void attach(byte trigPin, byte pwmPin)
{
URMSensor::attach(trigPin, pwmPin, URM37_US_PER_CM, URM37_TIMEOUT_FOR_PULSE_START,
URM37_MAX_PULSE_WIDTH, URM37_TRIG_ACTIVE_STATE, URM37_ECHO_ACTIVE_STATE,
URM37_TRIG_PULSE_WIDTH);
}
};
class HC_SR04 : public URMSensor
{
public:
void attach(byte trigPin, byte echoPin)
{
URMSensor::attach(trigPin, echoPin, HC_SR04_US_PER_CM, HC_SR04_TIMEOUT_FOR_PULSE_START,
HC_SR04_MAX_PULSE_WIDTH, HC_SR04_TRIG_ACTIVE_STATE, HC_SR04_ECHO_ACTIVE_STATE,
HC_SR04_TRIG_PULSE_WIDTH);
}
};
#endif