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)

Dependents:   TEST_Dist_lib

Committer:
dzoni
Date:
Mon Dec 21 21:01:53 2015 +0000
Revision:
0:4fbf332e6759
Child:
4:71f01fbc9246
Functionality completed and tested.

Who changed what in which revision?

UserRevisionLine numberNew 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 0:4fbf332e6759 17 * Functionality includes detection of error in echo signal, detection of timeout and limits of reliable measurement.
dzoni 0:4fbf332e6759 18 *
dzoni 0:4fbf332e6759 19 */
dzoni 0:4fbf332e6759 20 class Distance_HC_SR04 {
dzoni 0:4fbf332e6759 21
dzoni 0:4fbf332e6759 22 public:
dzoni 0:4fbf332e6759 23 Distance_HC_SR04(PinName trig, PinName echo, uint32_t tout_us = TIMEOUT_DELAY_US, float coeff = CALC_COEFF,
dzoni 0:4fbf332e6759 24 uint32_t tmin_us = TICKS_RANGE_MIN, uint32_t tmax_us = TICKS_RANGE_MAX);
dzoni 0:4fbf332e6759 25 void trigger(void);
dzoni 0:4fbf332e6759 26 Distance_HC_SR04_state getState(void);
dzoni 0:4fbf332e6759 27 void reset(void);
dzoni 0:4fbf332e6759 28 uint32_t getTicks(void);
dzoni 0:4fbf332e6759 29 float getDistance(void);
dzoni 0:4fbf332e6759 30 float getCoeff(void);
dzoni 0:4fbf332e6759 31 void setCoeff(float coeff);
dzoni 0:4fbf332e6759 32 float measureDistance(void);
dzoni 0:4fbf332e6759 33 uint32_t measureTicks(void);
dzoni 0:4fbf332e6759 34
dzoni 0:4fbf332e6759 35 void _tout(void);
dzoni 0:4fbf332e6759 36 void _rising(void);
dzoni 0:4fbf332e6759 37 void _falling(void);
dzoni 0:4fbf332e6759 38
dzoni 0:4fbf332e6759 39 private:
dzoni 0:4fbf332e6759 40 DigitalOut _trig;
dzoni 0:4fbf332e6759 41 InterruptIn _echo;
dzoni 0:4fbf332e6759 42 uint32_t _tout_us;
dzoni 0:4fbf332e6759 43 float _coeff;
dzoni 0:4fbf332e6759 44 uint32_t _tmin_us;
dzoni 0:4fbf332e6759 45 uint32_t _tmax_us;
dzoni 0:4fbf332e6759 46
dzoni 0:4fbf332e6759 47 Timer _timer;
dzoni 0:4fbf332e6759 48 Timeout _timeout;
dzoni 0:4fbf332e6759 49
dzoni 0:4fbf332e6759 50 volatile Distance_HC_SR04_state _state;
dzoni 0:4fbf332e6759 51 uint32_t _ticks_us;
dzoni 0:4fbf332e6759 52 };
dzoni 0:4fbf332e6759 53
dzoni 0:4fbf332e6759 54 #endif