Synchronous and asynchronous library for ultrasonic distance measurement for the device HC-SR04 with error handling functionality (out of range detection, HW error detection). Main features: "echo" puls duration measurement, distance measurement from "echo" pulse duration, detection of "echo" signal malfunction, timeout detection, detection of measured values outside reliable limits (min, max)
Distance_HC_SR04.h@4:71f01fbc9246, 2015-12-22 (annotated)
- Committer:
- dzoni
- Date:
- Tue Dec 22 09:29:04 2015 +0000
- Revision:
- 4:71f01fbc9246
- Parent:
- 0:4fbf332e6759
- Child:
- 5:2f6992aad3a0
Minor touches
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dzoni | 0:4fbf332e6759 | 1 | #ifndef MBED_DISTANCE_HC_SR04_H |
dzoni | 0:4fbf332e6759 | 2 | #define MBED_DISTANCE_HC_SR04_H |
dzoni | 0:4fbf332e6759 | 3 | |
dzoni | 0:4fbf332e6759 | 4 | #include "mbed.h" |
dzoni | 0:4fbf332e6759 | 5 | |
dzoni | 0:4fbf332e6759 | 6 | #define TIMEOUT_DELAY_US (25000) |
dzoni | 0:4fbf332e6759 | 7 | #define CALC_COEFF (340.0f/(2.0f*1000.0f)) |
dzoni | 0:4fbf332e6759 | 8 | #define TICKS_RANGE_MAX (15000) |
dzoni | 0:4fbf332e6759 | 9 | #define TICKS_RANGE_MIN (150) |
dzoni | 0:4fbf332e6759 | 10 | #define TRIG_PULSE_US (50) |
dzoni | 0:4fbf332e6759 | 11 | |
dzoni | 0:4fbf332e6759 | 12 | typedef enum { IDLE, STARTED, COMPLETED, TIMEOUT, OUT_OF_RANGE_MIN, OUT_OF_RANGE_MAX, ERROR_SIG } Distance_HC_SR04_state; |
dzoni | 0:4fbf332e6759 | 13 | |
dzoni | 0:4fbf332e6759 | 14 | /** Distance_HC_SR04 class. |
dzoni | 0:4fbf332e6759 | 15 | * |
dzoni | 0:4fbf332e6759 | 16 | * Library for interfacing ultrasonic distance measurement device HC-SR04. |
dzoni | 4:71f01fbc9246 | 17 | * Works in 2 basic modes: |
dzoni | 4:71f01fbc9246 | 18 | * - blocking (via reset() + trigger() + getState() + getTicks() or detDistance() |
dzoni | 4:71f01fbc9246 | 19 | * - non-blocking (asynchronous) via measureTicks() or measureDistance() |
dzoni | 4:71f01fbc9246 | 20 | * |
dzoni | 0:4fbf332e6759 | 21 | * Functionality includes detection of error in echo signal, detection of timeout and limits of reliable measurement. |
dzoni | 0:4fbf332e6759 | 22 | * |
dzoni | 0:4fbf332e6759 | 23 | */ |
dzoni | 0:4fbf332e6759 | 24 | class Distance_HC_SR04 { |
dzoni | 0:4fbf332e6759 | 25 | |
dzoni | 0:4fbf332e6759 | 26 | public: |
dzoni | 0:4fbf332e6759 | 27 | Distance_HC_SR04(PinName trig, PinName echo, uint32_t tout_us = TIMEOUT_DELAY_US, float coeff = CALC_COEFF, |
dzoni | 0:4fbf332e6759 | 28 | uint32_t tmin_us = TICKS_RANGE_MIN, uint32_t tmax_us = TICKS_RANGE_MAX); |
dzoni | 0:4fbf332e6759 | 29 | void trigger(void); |
dzoni | 0:4fbf332e6759 | 30 | Distance_HC_SR04_state getState(void); |
dzoni | 0:4fbf332e6759 | 31 | void reset(void); |
dzoni | 0:4fbf332e6759 | 32 | uint32_t getTicks(void); |
dzoni | 0:4fbf332e6759 | 33 | float getDistance(void); |
dzoni | 0:4fbf332e6759 | 34 | float getCoeff(void); |
dzoni | 0:4fbf332e6759 | 35 | void setCoeff(float coeff); |
dzoni | 0:4fbf332e6759 | 36 | float measureDistance(void); |
dzoni | 0:4fbf332e6759 | 37 | uint32_t measureTicks(void); |
dzoni | 0:4fbf332e6759 | 38 | |
dzoni | 0:4fbf332e6759 | 39 | void _tout(void); |
dzoni | 0:4fbf332e6759 | 40 | void _rising(void); |
dzoni | 0:4fbf332e6759 | 41 | void _falling(void); |
dzoni | 0:4fbf332e6759 | 42 | |
dzoni | 0:4fbf332e6759 | 43 | private: |
dzoni | 0:4fbf332e6759 | 44 | DigitalOut _trig; |
dzoni | 0:4fbf332e6759 | 45 | InterruptIn _echo; |
dzoni | 0:4fbf332e6759 | 46 | uint32_t _tout_us; |
dzoni | 0:4fbf332e6759 | 47 | float _coeff; |
dzoni | 0:4fbf332e6759 | 48 | uint32_t _tmin_us; |
dzoni | 0:4fbf332e6759 | 49 | uint32_t _tmax_us; |
dzoni | 0:4fbf332e6759 | 50 | |
dzoni | 0:4fbf332e6759 | 51 | Timer _timer; |
dzoni | 0:4fbf332e6759 | 52 | Timeout _timeout; |
dzoni | 0:4fbf332e6759 | 53 | |
dzoni | 0:4fbf332e6759 | 54 | volatile Distance_HC_SR04_state _state; |
dzoni | 0:4fbf332e6759 | 55 | uint32_t _ticks_us; |
dzoni | 0:4fbf332e6759 | 56 | }; |
dzoni | 0:4fbf332e6759 | 57 | |
dzoni | 0:4fbf332e6759 | 58 | #endif |