Library for interfacing with the Parallax Ping))) sensor.
ping.h@4:ddffe9339bb1, 2014-07-28 (annotated)
- Committer:
- d34d
- Date:
- Mon Jul 28 00:17:57 2014 +0000
- Revision:
- 4:ddffe9339bb1
- Parent:
- 3:0c337c262d84
- Child:
- 5:cbe07c09c64c
Make getDistanceXX methods static.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
d34d | 0:99ee7c741e9d | 1 | #ifndef PING_H |
d34d | 0:99ee7c741e9d | 2 | #define PING_H |
d34d | 0:99ee7c741e9d | 3 | |
d34d | 0:99ee7c741e9d | 4 | #include "mbed.h" |
d34d | 0:99ee7c741e9d | 5 | #include "FPointer.h" |
d34d | 0:99ee7c741e9d | 6 | |
d34d | 3:0c337c262d84 | 7 | #define MEASUREMENT_DELAY_US 200 |
d34d | 3:0c337c262d84 | 8 | #define SPEED_OF_SOUND_CM_PER_US 0.034029 |
d34d | 3:0c337c262d84 | 9 | #define SPEED_OF_SOUND_IN_PER_US 0.0133972441 |
d34d | 1:72fbcb01291d | 10 | |
d34d | 0:99ee7c741e9d | 11 | /** |
d34d | 0:99ee7c741e9d | 12 | * Class for interfacing with the Parallax Ping))) Ultrasonic Sensor |
d34d | 0:99ee7c741e9d | 13 | * |
d34d | 0:99ee7c741e9d | 14 | * See the Ping)) documentation for more information. |
d34d | 0:99ee7c741e9d | 15 | * http://www.parallax.com/sites/default/files/downloads/28015-PING-Documentation-v1.6.pdf |
d34d | 0:99ee7c741e9d | 16 | */ |
d34d | 0:99ee7c741e9d | 17 | class Ping { |
d34d | 0:99ee7c741e9d | 18 | public: |
d34d | 0:99ee7c741e9d | 19 | /** |
d34d | 0:99ee7c741e9d | 20 | * @param pin Digital I/O pin used for communicating with the Ping))) |
d34d | 0:99ee7c741e9d | 21 | */ |
d34d | 0:99ee7c741e9d | 22 | Ping(PinName pin); |
d34d | 0:99ee7c741e9d | 23 | |
d34d | 0:99ee7c741e9d | 24 | /** |
d34d | 0:99ee7c741e9d | 25 | * Starts a one shot reading. |
d34d | 0:99ee7c741e9d | 26 | * |
d34d | 0:99ee7c741e9d | 27 | * @return True if the reading was successfuly started, false if not. |
d34d | 0:99ee7c741e9d | 28 | */ |
d34d | 0:99ee7c741e9d | 29 | bool startReading(); |
d34d | 0:99ee7c741e9d | 30 | |
d34d | 0:99ee7c741e9d | 31 | /** |
d34d | 0:99ee7c741e9d | 32 | * Starts a reading. |
d34d | 0:99ee7c741e9d | 33 | * |
d34d | 0:99ee7c741e9d | 34 | * @param continuous Continuous readings if true, one-shot if false. |
d34d | 0:99ee7c741e9d | 35 | * @return True if the reading was successfuly started, false if not. |
d34d | 0:99ee7c741e9d | 36 | */ |
d34d | 0:99ee7c741e9d | 37 | bool startReading(bool continuous); |
d34d | 0:99ee7c741e9d | 38 | |
d34d | 0:99ee7c741e9d | 39 | /** |
d34d | 0:99ee7c741e9d | 40 | * Returns whether the sensor is busy getting a reading |
d34d | 0:99ee7c741e9d | 41 | * |
d34d | 0:99ee7c741e9d | 42 | * @return True if busy, false if not. |
d34d | 0:99ee7c741e9d | 43 | */ |
d34d | 0:99ee7c741e9d | 44 | bool isBusy() { return mBusy; } |
d34d | 0:99ee7c741e9d | 45 | |
d34d | 0:99ee7c741e9d | 46 | /** |
d34d | 0:99ee7c741e9d | 47 | * Returns whether the valid reading is available |
d34d | 0:99ee7c741e9d | 48 | * |
d34d | 0:99ee7c741e9d | 49 | * @return True if valid, false if not. |
d34d | 0:99ee7c741e9d | 50 | */ |
d34d | 0:99ee7c741e9d | 51 | bool isValid() { return mValid; } |
d34d | 0:99ee7c741e9d | 52 | |
d34d | 0:99ee7c741e9d | 53 | /** |
d34d | 0:99ee7c741e9d | 54 | * Returns the raw reading from the sensor. |
d34d | 0:99ee7c741e9d | 55 | * |
d34d | 0:99ee7c741e9d | 56 | * @return The time in uS that the sound travelled from the sensor to an object. |
d34d | 0:99ee7c741e9d | 57 | */ |
d34d | 0:99ee7c741e9d | 58 | uint32_t getRawReading() { return mRawReading; } |
d34d | 0:99ee7c741e9d | 59 | |
d34d | 0:99ee7c741e9d | 60 | /** |
d34d | 3:0c337c262d84 | 61 | * Gets the distance sound can travel, in centimeters, for a given time in uS. |
d34d | 3:0c337c262d84 | 62 | * |
d34d | 3:0c337c262d84 | 63 | * @param time_us Time traveled in microseconds. |
d34d | 3:0c337c262d84 | 64 | * @return The distance traveled in centimeters. |
d34d | 3:0c337c262d84 | 65 | */ |
d34d | 4:ddffe9339bb1 | 66 | static float getDistanceCm(uint32_t time_us) { return time_us * SPEED_OF_SOUND_CM_PER_US; } |
d34d | 3:0c337c262d84 | 67 | |
d34d | 3:0c337c262d84 | 68 | /** |
d34d | 3:0c337c262d84 | 69 | * Gets the distance sound can travel, in inches, for a given time in uS. |
d34d | 3:0c337c262d84 | 70 | * |
d34d | 3:0c337c262d84 | 71 | * @param time_us Time traveled in microseconds. |
d34d | 3:0c337c262d84 | 72 | * @return The distance traveled in inches. |
d34d | 3:0c337c262d84 | 73 | */ |
d34d | 4:ddffe9339bb1 | 74 | static float getDistanceIn(uint32_t time_us) { return time_us * SPEED_OF_SOUND_IN_PER_US; } |
d34d | 3:0c337c262d84 | 75 | |
d34d | 3:0c337c262d84 | 76 | /** |
d34d | 0:99ee7c741e9d | 77 | * Attach a callback function that will be called when a valid reading is available. |
d34d | 0:99ee7c741e9d | 78 | * |
d34d | 0:99ee7c741e9d | 79 | * @param function Function pointer for callback. |
d34d | 0:99ee7c741e9d | 80 | */ |
d34d | 0:99ee7c741e9d | 81 | void attach(uint32_t (*function)(uint32_t) = 0) { mCallback.attach(function); } |
d34d | 0:99ee7c741e9d | 82 | |
d34d | 0:99ee7c741e9d | 83 | /** |
d34d | 0:99ee7c741e9d | 84 | * Attach a class method that will be called when a valid reading is available. |
d34d | 0:99ee7c741e9d | 85 | * |
d34d | 0:99ee7c741e9d | 86 | * @param item Class object that contains the method to call. |
d34d | 0:99ee7c741e9d | 87 | * @param method Method to call. |
d34d | 0:99ee7c741e9d | 88 | */ |
d34d | 0:99ee7c741e9d | 89 | template<class T> |
d34d | 0:99ee7c741e9d | 90 | void attach(T* item, uint32_t (T::*method)(uint32_t)) { mCallback.attach(item, method); } |
d34d | 0:99ee7c741e9d | 91 | |
d34d | 0:99ee7c741e9d | 92 | private: |
d34d | 0:99ee7c741e9d | 93 | DigitalInOut mSignalIo; |
d34d | 0:99ee7c741e9d | 94 | InterruptIn mEvent; |
d34d | 0:99ee7c741e9d | 95 | |
d34d | 0:99ee7c741e9d | 96 | bool mBusy; |
d34d | 0:99ee7c741e9d | 97 | bool mValid; |
d34d | 0:99ee7c741e9d | 98 | bool mContinuous; |
d34d | 0:99ee7c741e9d | 99 | |
d34d | 0:99ee7c741e9d | 100 | Timer mTimer; |
d34d | 1:72fbcb01291d | 101 | Ticker mMeasureDelayTicker; |
d34d | 0:99ee7c741e9d | 102 | |
d34d | 0:99ee7c741e9d | 103 | uint32_t mRawReading; |
d34d | 0:99ee7c741e9d | 104 | |
d34d | 0:99ee7c741e9d | 105 | FPointer mCallback; |
d34d | 0:99ee7c741e9d | 106 | |
d34d | 0:99ee7c741e9d | 107 | void start(); |
d34d | 0:99ee7c741e9d | 108 | void stop(); |
d34d | 1:72fbcb01291d | 109 | void nextMeasurement(); |
d34d | 0:99ee7c741e9d | 110 | }; |
d34d | 0:99ee7c741e9d | 111 | |
d34d | 0:99ee7c741e9d | 112 | #endif |