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