Provides interface for reading distance measurements from the HCSR04 ultrasonic distance sensor.
HCSR04.h@1:f76eb64003f2, 2015-08-03 (annotated)
- Committer:
- aagnone3
- Date:
- Mon Aug 03 18:27:02 2015 +0000
- Revision:
- 1:f76eb64003f2
- Parent:
- 0:73ece82e5548
Initial revision.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aagnone3 | 0:73ece82e5548 | 1 | #ifndef MBED_HCSR04_H |
aagnone3 | 0:73ece82e5548 | 2 | #define MBED_HCSR04_H |
aagnone3 | 0:73ece82e5548 | 3 | |
aagnone3 | 0:73ece82e5548 | 4 | #include "PulseManager.h" |
aagnone3 | 0:73ece82e5548 | 5 | |
aagnone3 | 0:73ece82e5548 | 6 | class HCSR04 { |
aagnone3 | 0:73ece82e5548 | 7 | public: |
aagnone3 | 0:73ece82e5548 | 8 | /** |
aagnone3 | 0:73ece82e5548 | 9 | * Create a HCSR04 object |
aagnone3 | 0:73ece82e5548 | 10 | * @param triggerPin Digital I/O pin to use for sending the trigger pulse |
aagnone3 | 0:73ece82e5548 | 11 | * @param echoPin Digital I/O pin to use for measuring the width of the echo pulse |
aagnone3 | 0:73ece82e5548 | 12 | * @param timeout Time to wait for a pulse before giving up. |
aagnone3 | 0:73ece82e5548 | 13 | */ |
aagnone3 | 0:73ece82e5548 | 14 | HCSR04(PinName triggerPin, PinName echoPin, int timeout); |
aagnone3 | 0:73ece82e5548 | 15 | |
aagnone3 | 0:73ece82e5548 | 16 | /** Destructor to free allocated memory */ |
aagnone3 | 0:73ece82e5548 | 17 | ~HCSR04(); |
aagnone3 | 0:73ece82e5548 | 18 | |
aagnone3 | 0:73ece82e5548 | 19 | /** Return distance to nearest object in inches, or -1.0 if the reading of the pulse times out */ |
aagnone3 | 0:73ece82e5548 | 20 | float getReadingInches(); |
aagnone3 | 0:73ece82e5548 | 21 | |
aagnone3 | 0:73ece82e5548 | 22 | /** Return distance to nearest object in centimeters, or -1.0 if the reading of the pulse times out */ |
aagnone3 | 0:73ece82e5548 | 23 | float getReadingCm(); |
aagnone3 | 0:73ece82e5548 | 24 | |
aagnone3 | 0:73ece82e5548 | 25 | /** Constant scaling for converting distance readings to inches */ |
aagnone3 | 0:73ece82e5548 | 26 | static const int SCALE_INCHES = 148; |
aagnone3 | 0:73ece82e5548 | 27 | |
aagnone3 | 0:73ece82e5548 | 28 | /** Constant scaling for converting distance readings to inches */ |
aagnone3 | 0:73ece82e5548 | 29 | static const int SCALE_CM = 58; |
aagnone3 | 0:73ece82e5548 | 30 | |
aagnone3 | 0:73ece82e5548 | 31 | private: |
aagnone3 | 0:73ece82e5548 | 32 | |
aagnone3 | 0:73ece82e5548 | 33 | /** Pulse manager for sending/measuring pulses */ |
aagnone3 | 0:73ece82e5548 | 34 | PulseManager* pulseManager; |
aagnone3 | 0:73ece82e5548 | 35 | |
aagnone3 | 0:73ece82e5548 | 36 | /** Timeout limit in us */ |
aagnone3 | 0:73ece82e5548 | 37 | int timeout; |
aagnone3 | 0:73ece82e5548 | 38 | |
aagnone3 | 0:73ece82e5548 | 39 | /** Use the pulse manager to get a distance reading */ |
aagnone3 | 0:73ece82e5548 | 40 | float getReading(); |
aagnone3 | 0:73ece82e5548 | 41 | }; |
aagnone3 | 0:73ece82e5548 | 42 | |
aagnone3 | 0:73ece82e5548 | 43 | #endif |