A distance measurement class using ultrasonic sensor HC-SR04.
Dependents: Esercitazione4_4 HC-SR04 Group10_slave Oled_Gus ... more
The purpose of this library is to encourage students to develope their own classes. Instructions how to follow the development of this library for ultrasonic distance measurement are given here.
HCSR04.h@6:cf3e4e307d15, 2016-12-10 (annotated)
- Committer:
- tbjazic
- Date:
- Sat Dec 10 08:26:18 2016 +0000
- Revision:
- 6:cf3e4e307d15
- Parent:
- 5:a667b621f625
Filtering with Ticker object corrected.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tbjazic | 0:8871082486ac | 1 | #ifndef HCSR04_H_TVZMT |
tbjazic | 0:8871082486ac | 2 | #define HCSR04_H_TVZMT |
tbjazic | 0:8871082486ac | 3 | |
tbjazic | 0:8871082486ac | 4 | /** A distance measurement class using ultrasonic sensor HC-SR04. |
tbjazic | 0:8871082486ac | 5 | * |
tbjazic | 0:8871082486ac | 6 | * Example of use: |
tbjazic | 2:aa59a9c531be | 7 | * @code |
tbjazic | 0:8871082486ac | 8 | * #include "mbed.h" |
tbjazic | 0:8871082486ac | 9 | * #include "HCSR04.h" |
tbjazic | 0:8871082486ac | 10 | * |
tbjazic | 5:a667b621f625 | 11 | * Serial pc(USBTX, USBRX); |
tbjazic | 6:cf3e4e307d15 | 12 | * Timer timer; |
tbjazic | 5:a667b621f625 | 13 | * |
tbjazic | 0:8871082486ac | 14 | * int main() { |
tbjazic | 5:a667b621f625 | 15 | * HCSR04 sensor(p5, p7); |
tbjazic | 5:a667b621f625 | 16 | * sensor.setRanges(10, 110); |
tbjazic | 5:a667b621f625 | 17 | * pc.printf("Min. range = %g cm\n\rMax. range = %g cm\n\r", |
tbjazic | 5:a667b621f625 | 18 | * sensor.getMinRange(), sensor.getMaxRange()); |
tbjazic | 6:cf3e4e307d15 | 19 | * while(true) { |
tbjazic | 6:cf3e4e307d15 | 20 | * timer.reset(); |
tbjazic | 6:cf3e4e307d15 | 21 | * timer.start(); |
tbjazic | 6:cf3e4e307d15 | 22 | * sensor.startMeasurement(); |
tbjazic | 6:cf3e4e307d15 | 23 | * while(!sensor.isNewDataReady()) { |
tbjazic | 6:cf3e4e307d15 | 24 | * // wait for new data |
tbjazic | 6:cf3e4e307d15 | 25 | * // waiting time depends on the distance |
tbjazic | 6:cf3e4e307d15 | 26 | * } |
tbjazic | 5:a667b621f625 | 27 | * pc.printf("Distance: %5.1f mm\r", sensor.getDistance_mm()); |
tbjazic | 6:cf3e4e307d15 | 28 | * timer.stop(); |
tbjazic | 6:cf3e4e307d15 | 29 | * wait_ms(500 - timer.read_ms()); // time the loop |
tbjazic | 0:8871082486ac | 30 | * } |
tbjazic | 0:8871082486ac | 31 | * } |
tbjazic | 2:aa59a9c531be | 32 | * @endcode |
tbjazic | 0:8871082486ac | 33 | */ |
tbjazic | 0:8871082486ac | 34 | class HCSR04 { |
tbjazic | 0:8871082486ac | 35 | |
tbjazic | 0:8871082486ac | 36 | public: |
tbjazic | 0:8871082486ac | 37 | |
tbjazic | 0:8871082486ac | 38 | /** Receives two PinName variables. |
tbjazic | 0:8871082486ac | 39 | * @param echoPin mbed pin to which the echo signal is connected to |
tbjazic | 0:8871082486ac | 40 | * @param triggerPin mbed pin to which the trigger signal is connected to |
tbjazic | 0:8871082486ac | 41 | */ |
tbjazic | 0:8871082486ac | 42 | HCSR04(PinName echoPin, PinName triggerPin); |
tbjazic | 0:8871082486ac | 43 | |
tbjazic | 6:cf3e4e307d15 | 44 | /** Start the measurement. Measurement time depends on the distance. |
tbjazic | 6:cf3e4e307d15 | 45 | * Maximum measurement time is limited to 25 ms (400 cm). |
tbjazic | 6:cf3e4e307d15 | 46 | */ |
tbjazic | 6:cf3e4e307d15 | 47 | void startMeasurement(); |
tbjazic | 6:cf3e4e307d15 | 48 | |
tbjazic | 6:cf3e4e307d15 | 49 | /** Returns the distance in cm. Requires previous call of startMeasurement(). |
tbjazic | 0:8871082486ac | 50 | * @returns distance of the measuring object in cm. |
tbjazic | 0:8871082486ac | 51 | */ |
tbjazic | 0:8871082486ac | 52 | float getDistance_cm(); |
tbjazic | 0:8871082486ac | 53 | |
tbjazic | 6:cf3e4e307d15 | 54 | /** Returns the distance in mm. Requires previous call of startMeasurement(). |
tbjazic | 4:aae70f15357f | 55 | * @returns distance of the measuring object in mm. |
tbjazic | 4:aae70f15357f | 56 | */ |
tbjazic | 4:aae70f15357f | 57 | float getDistance_mm(); |
tbjazic | 4:aae70f15357f | 58 | |
tbjazic | 4:aae70f15357f | 59 | /** Sets the minimum and maximum ranges between the factory values of 2 cm and 400 cm. |
tbjazic | 4:aae70f15357f | 60 | * @param minRange Minimum range in cm. Must be between 2 cm and maxRange. |
tbjazic | 4:aae70f15357f | 61 | * @param maxRange Maximum range in cm. Must be between minRange and 400 cm. |
tbjazic | 4:aae70f15357f | 62 | */ |
tbjazic | 4:aae70f15357f | 63 | void setRanges(float minRange, float maxRange); |
tbjazic | 4:aae70f15357f | 64 | |
tbjazic | 4:aae70f15357f | 65 | /** Retreives the minimum sensor range set by the user. |
tbjazic | 4:aae70f15357f | 66 | * @returns the minimum sensor range set by the user in cm. |
tbjazic | 4:aae70f15357f | 67 | */ |
tbjazic | 4:aae70f15357f | 68 | float getMinRange(); |
tbjazic | 4:aae70f15357f | 69 | |
tbjazic | 4:aae70f15357f | 70 | /** Retreives the maximum sensor range set by the user. |
tbjazic | 4:aae70f15357f | 71 | * @returns the maximum sensor range set by the user in cm. |
tbjazic | 4:aae70f15357f | 72 | */ |
tbjazic | 4:aae70f15357f | 73 | float getMaxRange(); |
tbjazic | 4:aae70f15357f | 74 | |
tbjazic | 6:cf3e4e307d15 | 75 | /** Checks if the new data is ready. |
tbjazic | 6:cf3e4e307d15 | 76 | * @returns true if new data is ready, false otherwise. |
tbjazic | 6:cf3e4e307d15 | 77 | */ |
tbjazic | 6:cf3e4e307d15 | 78 | bool isNewDataReady(); |
tbjazic | 6:cf3e4e307d15 | 79 | |
tbjazic | 0:8871082486ac | 80 | private: |
tbjazic | 0:8871082486ac | 81 | |
tbjazic | 0:8871082486ac | 82 | InterruptIn echo; // echo pin |
tbjazic | 0:8871082486ac | 83 | DigitalOut trigger; // trigger pin |
tbjazic | 0:8871082486ac | 84 | Timer timer; // echo pulsewidth measurement |
tbjazic | 0:8871082486ac | 85 | float distance; // store the distance in cm |
tbjazic | 3:9a7899cf5e3a | 86 | float minDistance; // minimum measurable distance |
tbjazic | 3:9a7899cf5e3a | 87 | float maxDistance; // maximum measurable distance |
tbjazic | 6:cf3e4e307d15 | 88 | Timeout triggerTimeout, echoTimeout; |
tbjazic | 6:cf3e4e307d15 | 89 | bool newDataReady, timerStarted; |
tbjazic | 0:8871082486ac | 90 | |
tbjazic | 0:8871082486ac | 91 | /** Start the timer. */ |
tbjazic | 0:8871082486ac | 92 | void startTimer(); |
tbjazic | 0:8871082486ac | 93 | |
tbjazic | 0:8871082486ac | 94 | /** Stop the timer. */ |
tbjazic | 0:8871082486ac | 95 | void stopTimer(); |
tbjazic | 0:8871082486ac | 96 | |
tbjazic | 0:8871082486ac | 97 | /** Initialization. */ |
tbjazic | 0:8871082486ac | 98 | void init(); |
tbjazic | 0:8871082486ac | 99 | |
tbjazic | 6:cf3e4e307d15 | 100 | void turnOffTrigger(); |
tbjazic | 0:8871082486ac | 101 | }; |
tbjazic | 0:8871082486ac | 102 | |
tbjazic | 0:8871082486ac | 103 | #endif |