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)
Diff: Distance_HC_SR04.h
- Revision:
- 6:5beda7c318d5
- Parent:
- 5:2f6992aad3a0
- Child:
- 12:5bf5a7e62c5d
--- a/Distance_HC_SR04.h Tue Dec 22 11:22:56 2015 +0000 +++ b/Distance_HC_SR04.h Tue Dec 22 12:00:15 2015 +0000 @@ -9,14 +9,24 @@ #define TICKS_RANGE_MIN (150) // Minimum reliable measurement - echo pulse width in microseconds #define TRIG_PULSE_US (50) // Width of trigger pulse in microseconds +/** Definition of measurement FSM states. + * + * IDLE: No activity. Ready to start measurement with trigger(). This is state after reset(). + * STARTED: Measurement started and in progress. This is state after trigger(). + * COMPLETED: Measurement succesfuly completed by falling edge of echo signal. Measured values in range. + * TIMEOUT: It was not possible to complete echo pulse width measurement in given time. Measurement cancelled. + * OUT_OF_RANGE_MIN: Measurement completed. Measured distance under reliable range. + * OUT_OF_RANGE_MAX: Measurement completed. Measured distance over reliable range. + * ERROR_SIG: Faulty value of echo signal just after trigger pulse (echo != 0). Possible HW fault. + */ typedef enum { IDLE, STARTED, COMPLETED, TIMEOUT, OUT_OF_RANGE_MIN, OUT_OF_RANGE_MAX, ERROR_SIG } Distance_HC_SR04_state; /** Distance_HC_SR04 class. * * Library for interfacing ultrasonic distance measurement device HC-SR04. * Works in 2 basic modes: - * - blocking (via reset() + trigger() + getState() + getTicks() or detDistance() - * - non-blocking (asynchronous) via measureTicks() or measureDistance() + * - blocking (via measureTicks() or measureDistance()) + * - non-blocking asynchronous (via reset() + trigger() + getState() + getTicks() or getDistance()) * * Functionality includes detection of error in echo signal, detection of timeout and limits of reliable measurement. * @@ -36,11 +46,11 @@ float measureDistance(void); uint32_t measureTicks(void); +private: void _tout(void); void _rising(void); void _falling(void); -private: DigitalOut _trig; InterruptIn _echo; uint32_t _tout_us;