![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Resit Target Localisation Ciaran Kane 18689005
Dependencies: mbed
Ultrasonic/ultrasonic.h@7:acf82069b794, 23 months ago (annotated)
- Committer:
- ciarankane123
- Date:
- Thu Aug 25 13:50:39 2022 +0000
- Revision:
- 7:acf82069b794
- Parent:
- 0:479e94f82332
Final Code;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ciarankane123 | 0:479e94f82332 | 1 | #ifndef ULTRASONIC_H |
ciarankane123 | 0:479e94f82332 | 2 | #define ULTRASONIC_H |
ciarankane123 | 0:479e94f82332 | 3 | |
ciarankane123 | 0:479e94f82332 | 4 | #include "mbed.h" |
ciarankane123 | 0:479e94f82332 | 5 | |
ciarankane123 | 0:479e94f82332 | 6 | /** Ultrasonic Class |
ciarankane123 | 0:479e94f82332 | 7 | @brief Acknowledgements to EJ Teb |
ciarankane123 | 0:479e94f82332 | 8 | @brief Library for interfacing with ultrasonic sensor |
ciarankane123 | 0:479e94f82332 | 9 | |
ciarankane123 | 0:479e94f82332 | 10 | |
ciarankane123 | 0:479e94f82332 | 11 | Example: |
ciarankane123 | 0:479e94f82332 | 12 | |
ciarankane123 | 0:479e94f82332 | 13 | @code |
ciarankane123 | 0:479e94f82332 | 14 | |
ciarankane123 | 0:479e94f82332 | 15 | #include "mbed.h" |
ciarankane123 | 0:479e94f82332 | 16 | #include "ultrasonic.h" |
ciarankane123 | 0:479e94f82332 | 17 | |
ciarankane123 | 0:479e94f82332 | 18 | void dist(int distance) |
ciarankane123 | 0:479e94f82332 | 19 | { |
ciarankane123 | 0:479e94f82332 | 20 | printf("Distance changed to %dmm\r\n", distance); |
ciarankane123 | 0:479e94f82332 | 21 | |
ciarankane123 | 0:479e94f82332 | 22 | } |
ciarankane123 | 0:479e94f82332 | 23 | |
ciarankane123 | 0:479e94f82332 | 24 | |
ciarankane123 | 0:479e94f82332 | 25 | ultrasonic mu(PTD0, PTC12, .1, 1, &dist); //Set the trigger pin to PTD0 and the echo pin to PTC12 |
ciarankane123 | 0:479e94f82332 | 26 | //have updates every .1 seconds and a timeout after 1 |
ciarankane123 | 0:479e94f82332 | 27 | //second, and call dist when the distance changes |
ciarankane123 | 0:479e94f82332 | 28 | |
ciarankane123 | 0:479e94f82332 | 29 | int main() |
ciarankane123 | 0:479e94f82332 | 30 | { |
ciarankane123 | 0:479e94f82332 | 31 | mu.startUpdates();//start mesuring the distance |
ciarankane123 | 0:479e94f82332 | 32 | while(1) |
ciarankane123 | 0:479e94f82332 | 33 | { |
ciarankane123 | 0:479e94f82332 | 34 | //Do something else here |
ciarankane123 | 0:479e94f82332 | 35 | mu.checkDistance(); //call checkDistance() as much as possible, as this is where |
ciarankane123 | 0:479e94f82332 | 36 | //the class checks if dist needs to be called. |
ciarankane123 | 0:479e94f82332 | 37 | } |
ciarankane123 | 0:479e94f82332 | 38 | } |
ciarankane123 | 0:479e94f82332 | 39 | |
ciarankane123 | 0:479e94f82332 | 40 | |
ciarankane123 | 0:479e94f82332 | 41 | * @endcode |
ciarankane123 | 0:479e94f82332 | 42 | */ |
ciarankane123 | 0:479e94f82332 | 43 | class ultrasonic |
ciarankane123 | 0:479e94f82332 | 44 | { |
ciarankane123 | 0:479e94f82332 | 45 | public: |
ciarankane123 | 0:479e94f82332 | 46 | /**iniates the class with the specified trigger pin, echo pin, update speed and timeout**/ |
ciarankane123 | 0:479e94f82332 | 47 | ultrasonic(PinName trigPin, PinName echoPin, float updateSpeed, float timeout); |
ciarankane123 | 0:479e94f82332 | 48 | /**iniates the class with the specified trigger pin, echo pin, update speed, timeout and method to call when the distance changes**/ |
ciarankane123 | 0:479e94f82332 | 49 | ultrasonic(PinName trigPin, PinName echoPin, float updateSpeed, float timeout, void onUpdate(int)); |
ciarankane123 | 0:479e94f82332 | 50 | /** returns the last measured distance**/ |
ciarankane123 | 0:479e94f82332 | 51 | int getCurrentDistance(void); |
ciarankane123 | 0:479e94f82332 | 52 | /**pauses measuring the distance**/ |
ciarankane123 | 0:479e94f82332 | 53 | void pauseUpdates(void); |
ciarankane123 | 0:479e94f82332 | 54 | /**starts mesuring the distance**/ |
ciarankane123 | 0:479e94f82332 | 55 | void startUpdates(void); |
ciarankane123 | 0:479e94f82332 | 56 | /**attachs the method to be called when the distances changes**/ |
ciarankane123 | 0:479e94f82332 | 57 | void attachOnUpdate(void method(int)); |
ciarankane123 | 0:479e94f82332 | 58 | /**changes the speed at which updates are made**/ |
ciarankane123 | 0:479e94f82332 | 59 | void changeUpdateSpeed(float updateSpeed); |
ciarankane123 | 0:479e94f82332 | 60 | /**gets whether the distance has been changed since the last call of isUpdated() or checkDistance()**/ |
ciarankane123 | 0:479e94f82332 | 61 | int isUpdated(void); |
ciarankane123 | 0:479e94f82332 | 62 | /**gets the speed at which updates are made**/ |
ciarankane123 | 0:479e94f82332 | 63 | float getUpdateSpeed(void); |
ciarankane123 | 0:479e94f82332 | 64 | /**call this as often as possible in your code, eg. at the end of a while(1) loop, |
ciarankane123 | 0:479e94f82332 | 65 | and it will check whether the method you have attached needs to be called**/ |
ciarankane123 | 0:479e94f82332 | 66 | void checkDistance(void); |
ciarankane123 | 0:479e94f82332 | 67 | private: |
ciarankane123 | 0:479e94f82332 | 68 | DigitalOut _trig; |
ciarankane123 | 0:479e94f82332 | 69 | InterruptIn _echo; |
ciarankane123 | 0:479e94f82332 | 70 | Timer _t; |
ciarankane123 | 0:479e94f82332 | 71 | Timeout _tout; |
ciarankane123 | 0:479e94f82332 | 72 | int _distance; |
ciarankane123 | 0:479e94f82332 | 73 | float _updateSpeed; |
ciarankane123 | 0:479e94f82332 | 74 | int start; |
ciarankane123 | 0:479e94f82332 | 75 | int end; |
ciarankane123 | 0:479e94f82332 | 76 | volatile int done; |
ciarankane123 | 0:479e94f82332 | 77 | void (*_onUpdateMethod)(int); |
ciarankane123 | 0:479e94f82332 | 78 | void _startT(void); |
ciarankane123 | 0:479e94f82332 | 79 | void _updateDist(void); |
ciarankane123 | 0:479e94f82332 | 80 | void _startTrig(void); |
ciarankane123 | 0:479e94f82332 | 81 | float _timeout; |
ciarankane123 | 0:479e94f82332 | 82 | int d; |
ciarankane123 | 0:479e94f82332 | 83 | }; |
ciarankane123 | 0:479e94f82332 | 84 | #endif |