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@6:5beda7c318d5, 2015-12-22 (annotated)
- Committer:
- dzoni
- Date:
- Tue Dec 22 12:00:15 2015 +0000
- Revision:
- 6:5beda7c318d5
- Parent:
- 5:2f6992aad3a0
- Child:
- 12:5bf5a7e62c5d
Touch-ups on documentation.
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 | 5:2f6992aad3a0 | 6 | #define TIMEOUT_DELAY_US (25000) // Timeout in microseconds |
dzoni | 5:2f6992aad3a0 | 7 | #define CALC_COEFF (340.0f/(2.0f*1000.0f)) // Coefficient for distance calculation (distance = coeff * microseconds) |
dzoni | 5:2f6992aad3a0 | 8 | #define TICKS_RANGE_MAX (15000) // Maximum reliable measurement - echo pulse width in microseconds |
dzoni | 5:2f6992aad3a0 | 9 | #define TICKS_RANGE_MIN (150) // Minimum reliable measurement - echo pulse width in microseconds |
dzoni | 5:2f6992aad3a0 | 10 | #define TRIG_PULSE_US (50) // Width of trigger pulse in microseconds |
dzoni | 0:4fbf332e6759 | 11 | |
dzoni | 6:5beda7c318d5 | 12 | /** Definition of measurement FSM states. |
dzoni | 6:5beda7c318d5 | 13 | * |
dzoni | 6:5beda7c318d5 | 14 | * IDLE: No activity. Ready to start measurement with trigger(). This is state after reset(). |
dzoni | 6:5beda7c318d5 | 15 | * STARTED: Measurement started and in progress. This is state after trigger(). |
dzoni | 6:5beda7c318d5 | 16 | * COMPLETED: Measurement succesfuly completed by falling edge of echo signal. Measured values in range. |
dzoni | 6:5beda7c318d5 | 17 | * TIMEOUT: It was not possible to complete echo pulse width measurement in given time. Measurement cancelled. |
dzoni | 6:5beda7c318d5 | 18 | * OUT_OF_RANGE_MIN: Measurement completed. Measured distance under reliable range. |
dzoni | 6:5beda7c318d5 | 19 | * OUT_OF_RANGE_MAX: Measurement completed. Measured distance over reliable range. |
dzoni | 6:5beda7c318d5 | 20 | * ERROR_SIG: Faulty value of echo signal just after trigger pulse (echo != 0). Possible HW fault. |
dzoni | 6:5beda7c318d5 | 21 | */ |
dzoni | 0:4fbf332e6759 | 22 | typedef enum { IDLE, STARTED, COMPLETED, TIMEOUT, OUT_OF_RANGE_MIN, OUT_OF_RANGE_MAX, ERROR_SIG } Distance_HC_SR04_state; |
dzoni | 0:4fbf332e6759 | 23 | |
dzoni | 0:4fbf332e6759 | 24 | /** Distance_HC_SR04 class. |
dzoni | 0:4fbf332e6759 | 25 | * |
dzoni | 0:4fbf332e6759 | 26 | * Library for interfacing ultrasonic distance measurement device HC-SR04. |
dzoni | 4:71f01fbc9246 | 27 | * Works in 2 basic modes: |
dzoni | 6:5beda7c318d5 | 28 | * - blocking (via measureTicks() or measureDistance()) |
dzoni | 6:5beda7c318d5 | 29 | * - non-blocking asynchronous (via reset() + trigger() + getState() + getTicks() or getDistance()) |
dzoni | 4:71f01fbc9246 | 30 | * |
dzoni | 0:4fbf332e6759 | 31 | * Functionality includes detection of error in echo signal, detection of timeout and limits of reliable measurement. |
dzoni | 0:4fbf332e6759 | 32 | * |
dzoni | 0:4fbf332e6759 | 33 | */ |
dzoni | 0:4fbf332e6759 | 34 | class Distance_HC_SR04 { |
dzoni | 0:4fbf332e6759 | 35 | |
dzoni | 0:4fbf332e6759 | 36 | public: |
dzoni | 0:4fbf332e6759 | 37 | Distance_HC_SR04(PinName trig, PinName echo, uint32_t tout_us = TIMEOUT_DELAY_US, float coeff = CALC_COEFF, |
dzoni | 0:4fbf332e6759 | 38 | uint32_t tmin_us = TICKS_RANGE_MIN, uint32_t tmax_us = TICKS_RANGE_MAX); |
dzoni | 0:4fbf332e6759 | 39 | void trigger(void); |
dzoni | 0:4fbf332e6759 | 40 | Distance_HC_SR04_state getState(void); |
dzoni | 0:4fbf332e6759 | 41 | void reset(void); |
dzoni | 0:4fbf332e6759 | 42 | uint32_t getTicks(void); |
dzoni | 0:4fbf332e6759 | 43 | float getDistance(void); |
dzoni | 0:4fbf332e6759 | 44 | float getCoeff(void); |
dzoni | 0:4fbf332e6759 | 45 | void setCoeff(float coeff); |
dzoni | 0:4fbf332e6759 | 46 | float measureDistance(void); |
dzoni | 0:4fbf332e6759 | 47 | uint32_t measureTicks(void); |
dzoni | 0:4fbf332e6759 | 48 | |
dzoni | 6:5beda7c318d5 | 49 | private: |
dzoni | 0:4fbf332e6759 | 50 | void _tout(void); |
dzoni | 0:4fbf332e6759 | 51 | void _rising(void); |
dzoni | 0:4fbf332e6759 | 52 | void _falling(void); |
dzoni | 0:4fbf332e6759 | 53 | |
dzoni | 0:4fbf332e6759 | 54 | DigitalOut _trig; |
dzoni | 0:4fbf332e6759 | 55 | InterruptIn _echo; |
dzoni | 0:4fbf332e6759 | 56 | uint32_t _tout_us; |
dzoni | 0:4fbf332e6759 | 57 | float _coeff; |
dzoni | 0:4fbf332e6759 | 58 | uint32_t _tmin_us; |
dzoni | 0:4fbf332e6759 | 59 | uint32_t _tmax_us; |
dzoni | 0:4fbf332e6759 | 60 | |
dzoni | 0:4fbf332e6759 | 61 | Timer _timer; |
dzoni | 0:4fbf332e6759 | 62 | Timeout _timeout; |
dzoni | 0:4fbf332e6759 | 63 | |
dzoni | 0:4fbf332e6759 | 64 | volatile Distance_HC_SR04_state _state; |
dzoni | 0:4fbf332e6759 | 65 | uint32_t _ticks_us; |
dzoni | 0:4fbf332e6759 | 66 | }; |
dzoni | 0:4fbf332e6759 | 67 | |
dzoni | 0:4fbf332e6759 | 68 | #endif |