Development and testing of ultrasonic distance measurement library for device HC-SR04.
Dependencies: TextLCD_improved mbed Distance_HC_SR04
Distance_HC_SR04.h@5:1a69f40177b0, 2015-12-21 (annotated)
- Committer:
- dzoni
- Date:
- Mon Dec 21 20:54:09 2015 +0000
- Branch:
- CLASS_IMPLEMENTATION
- Revision:
- 5:1a69f40177b0
Class implementation completed and successfully tested.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dzoni | 5:1a69f40177b0 | 1 | #ifndef MBED_DISTANCE_HC_SR04_H |
dzoni | 5:1a69f40177b0 | 2 | #define MBED_DISTANCE_HC_SR04_H |
dzoni | 5:1a69f40177b0 | 3 | |
dzoni | 5:1a69f40177b0 | 4 | #include "mbed.h" |
dzoni | 5:1a69f40177b0 | 5 | |
dzoni | 5:1a69f40177b0 | 6 | #define TIMEOUT_DELAY_US (25000) |
dzoni | 5:1a69f40177b0 | 7 | #define CALC_COEFF (340.0f/(2.0f*1000.0f)) |
dzoni | 5:1a69f40177b0 | 8 | #define TICKS_RANGE_MAX (15000) |
dzoni | 5:1a69f40177b0 | 9 | #define TICKS_RANGE_MIN (150) |
dzoni | 5:1a69f40177b0 | 10 | #define TRIG_PULSE_US (50) |
dzoni | 5:1a69f40177b0 | 11 | |
dzoni | 5:1a69f40177b0 | 12 | typedef enum { IDLE, STARTED, COMPLETED, TIMEOUT, OUT_OF_RANGE_MIN, OUT_OF_RANGE_MAX, ERROR_SIG } Distance_HC_SR04_state; |
dzoni | 5:1a69f40177b0 | 13 | |
dzoni | 5:1a69f40177b0 | 14 | /** Distance_HC_SR04 class. |
dzoni | 5:1a69f40177b0 | 15 | * |
dzoni | 5:1a69f40177b0 | 16 | * Library for interfacing ultrasonic distance measurement device HC-SR04. |
dzoni | 5:1a69f40177b0 | 17 | * Functionality includes detection of error in echo signal, detection of timeout and limits of reliable measurement. |
dzoni | 5:1a69f40177b0 | 18 | * |
dzoni | 5:1a69f40177b0 | 19 | */ |
dzoni | 5:1a69f40177b0 | 20 | class Distance_HC_SR04 { |
dzoni | 5:1a69f40177b0 | 21 | |
dzoni | 5:1a69f40177b0 | 22 | public: |
dzoni | 5:1a69f40177b0 | 23 | Distance_HC_SR04(PinName trig, PinName echo, uint32_t tout_us = TIMEOUT_DELAY_US, float coeff = CALC_COEFF, |
dzoni | 5:1a69f40177b0 | 24 | uint32_t tmin_us = TICKS_RANGE_MIN, uint32_t tmax_us = TICKS_RANGE_MAX); |
dzoni | 5:1a69f40177b0 | 25 | void trigger(void); |
dzoni | 5:1a69f40177b0 | 26 | Distance_HC_SR04_state getState(void); |
dzoni | 5:1a69f40177b0 | 27 | void reset(void); |
dzoni | 5:1a69f40177b0 | 28 | uint32_t getTicks(void); |
dzoni | 5:1a69f40177b0 | 29 | float getDistance(void); |
dzoni | 5:1a69f40177b0 | 30 | float getCoeff(void); |
dzoni | 5:1a69f40177b0 | 31 | void setCoeff(float coeff); |
dzoni | 5:1a69f40177b0 | 32 | float measureDistance(void); |
dzoni | 5:1a69f40177b0 | 33 | uint32_t measureTicks(void); |
dzoni | 5:1a69f40177b0 | 34 | |
dzoni | 5:1a69f40177b0 | 35 | void _tout(void); |
dzoni | 5:1a69f40177b0 | 36 | void _rising(void); |
dzoni | 5:1a69f40177b0 | 37 | void _falling(void); |
dzoni | 5:1a69f40177b0 | 38 | |
dzoni | 5:1a69f40177b0 | 39 | private: |
dzoni | 5:1a69f40177b0 | 40 | DigitalOut _trig; |
dzoni | 5:1a69f40177b0 | 41 | InterruptIn _echo; |
dzoni | 5:1a69f40177b0 | 42 | uint32_t _tout_us; |
dzoni | 5:1a69f40177b0 | 43 | float _coeff; |
dzoni | 5:1a69f40177b0 | 44 | uint32_t _tmin_us; |
dzoni | 5:1a69f40177b0 | 45 | uint32_t _tmax_us; |
dzoni | 5:1a69f40177b0 | 46 | |
dzoni | 5:1a69f40177b0 | 47 | Timer _timer; |
dzoni | 5:1a69f40177b0 | 48 | Timeout _timeout; |
dzoni | 5:1a69f40177b0 | 49 | |
dzoni | 5:1a69f40177b0 | 50 | volatile Distance_HC_SR04_state _state; |
dzoni | 5:1a69f40177b0 | 51 | uint32_t _ticks_us; |
dzoni | 5:1a69f40177b0 | 52 | }; |
dzoni | 5:1a69f40177b0 | 53 | |
dzoni | 5:1a69f40177b0 | 54 | #endif |