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