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@4:aae70f15357f, 2015-12-07 (annotated)
- Committer:
- tbjazic
- Date:
- Mon Dec 07 09:37:21 2015 +0000
- Revision:
- 4:aae70f15357f
- Parent:
- 3:9a7899cf5e3a
- Child:
- 5:a667b621f625
Functions for setting the sensor range and reading its values are added. Function for returning the measured distance in milimeters added.
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 | 0:8871082486ac | 11 | * Serial pc(USBTX, USBRX); // communication with terminal |
tbjazic | 0:8871082486ac | 12 | * |
tbjazic | 0:8871082486ac | 13 | * int main() { |
tbjazic | 0:8871082486ac | 14 | * HCSR04 sensor(p5, p7); // instantiate the sensor object |
tbjazic | 0:8871082486ac | 15 | * while(1) { |
tbjazic | 0:8871082486ac | 16 | * pc.printf("Distance: %5.1f cm\r", sensor.getDistance_cm()); |
tbjazic | 0:8871082486ac | 17 | * wait_ms(975); // print the result every 1 second |
tbjazic | 0:8871082486ac | 18 | * } |
tbjazic | 0:8871082486ac | 19 | * } |
tbjazic | 2:aa59a9c531be | 20 | * @endcode |
tbjazic | 0:8871082486ac | 21 | */ |
tbjazic | 0:8871082486ac | 22 | class HCSR04 { |
tbjazic | 0:8871082486ac | 23 | |
tbjazic | 0:8871082486ac | 24 | public: |
tbjazic | 0:8871082486ac | 25 | |
tbjazic | 0:8871082486ac | 26 | /** Receives two PinName variables. |
tbjazic | 0:8871082486ac | 27 | * @param echoPin mbed pin to which the echo signal is connected to |
tbjazic | 0:8871082486ac | 28 | * @param triggerPin mbed pin to which the trigger signal is connected to |
tbjazic | 0:8871082486ac | 29 | */ |
tbjazic | 0:8871082486ac | 30 | HCSR04(PinName echoPin, PinName triggerPin); |
tbjazic | 0:8871082486ac | 31 | |
tbjazic | 4:aae70f15357f | 32 | /** Calculates the distance in cm, with the calculation time of approximatelly 23.7 ms. |
tbjazic | 0:8871082486ac | 33 | * @returns distance of the measuring object in cm. |
tbjazic | 0:8871082486ac | 34 | */ |
tbjazic | 0:8871082486ac | 35 | float getDistance_cm(); |
tbjazic | 0:8871082486ac | 36 | |
tbjazic | 4:aae70f15357f | 37 | /** Calculates the distance in mm, with the calculation time of approximatelly 23.7 ms. |
tbjazic | 4:aae70f15357f | 38 | * @returns distance of the measuring object in mm. |
tbjazic | 4:aae70f15357f | 39 | */ |
tbjazic | 4:aae70f15357f | 40 | float getDistance_mm(); |
tbjazic | 4:aae70f15357f | 41 | |
tbjazic | 4:aae70f15357f | 42 | /** Sets the minimum and maximum ranges between the factory values of 2 cm and 400 cm. |
tbjazic | 4:aae70f15357f | 43 | * @param minRange Minimum range in cm. Must be between 2 cm and maxRange. |
tbjazic | 4:aae70f15357f | 44 | * @param maxRange Maximum range in cm. Must be between minRange and 400 cm. |
tbjazic | 4:aae70f15357f | 45 | */ |
tbjazic | 4:aae70f15357f | 46 | void setRanges(float minRange, float maxRange); |
tbjazic | 4:aae70f15357f | 47 | |
tbjazic | 4:aae70f15357f | 48 | /** Retreives the minimum sensor range set by the user. |
tbjazic | 4:aae70f15357f | 49 | * @returns the minimum sensor range set by the user in cm. |
tbjazic | 4:aae70f15357f | 50 | */ |
tbjazic | 4:aae70f15357f | 51 | float getMinRange(); |
tbjazic | 4:aae70f15357f | 52 | |
tbjazic | 4:aae70f15357f | 53 | /** Retreives the maximum sensor range set by the user. |
tbjazic | 4:aae70f15357f | 54 | * @returns the maximum sensor range set by the user in cm. |
tbjazic | 4:aae70f15357f | 55 | */ |
tbjazic | 4:aae70f15357f | 56 | float getMaxRange(); |
tbjazic | 4:aae70f15357f | 57 | |
tbjazic | 0:8871082486ac | 58 | private: |
tbjazic | 0:8871082486ac | 59 | |
tbjazic | 0:8871082486ac | 60 | InterruptIn echo; // echo pin |
tbjazic | 0:8871082486ac | 61 | DigitalOut trigger; // trigger pin |
tbjazic | 0:8871082486ac | 62 | Timer timer; // echo pulsewidth measurement |
tbjazic | 0:8871082486ac | 63 | float distance; // store the distance in cm |
tbjazic | 3:9a7899cf5e3a | 64 | float minDistance; // minimum measurable distance |
tbjazic | 3:9a7899cf5e3a | 65 | float maxDistance; // maximum measurable distance |
tbjazic | 0:8871082486ac | 66 | |
tbjazic | 0:8871082486ac | 67 | /** Start the timer. */ |
tbjazic | 0:8871082486ac | 68 | void startTimer(); |
tbjazic | 0:8871082486ac | 69 | |
tbjazic | 0:8871082486ac | 70 | /** Stop the timer. */ |
tbjazic | 0:8871082486ac | 71 | void stopTimer(); |
tbjazic | 0:8871082486ac | 72 | |
tbjazic | 0:8871082486ac | 73 | /** Initialization. */ |
tbjazic | 0:8871082486ac | 74 | void init(); |
tbjazic | 0:8871082486ac | 75 | |
tbjazic | 0:8871082486ac | 76 | /** Start the measurement. */ |
tbjazic | 0:8871082486ac | 77 | void startMeasurement(); |
tbjazic | 0:8871082486ac | 78 | }; |
tbjazic | 0:8871082486ac | 79 | |
tbjazic | 0:8871082486ac | 80 | #endif |