Dev Joshi
/
SRF02_libv2
Library for the SRF02 ultrasonic rangefinder
SRF02.h@0:862f11d627f9, 2011-07-10 (annotated)
- Committer:
- go2dev
- Date:
- Sun Jul 10 16:40:35 2011 +0000
- Revision:
- 0:862f11d627f9
v2 of this library, condenses the code base as discussed in this thread: http://mbed.org/forum/helloworld/topic/2234/
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
go2dev | 0:862f11d627f9 | 1 | /* mbed SRF02 Ultra Sonic Range Finder Library |
go2dev | 0:862f11d627f9 | 2 | * Copyright (c) 2011 by djoshi [go2dev] |
go2dev | 0:862f11d627f9 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
go2dev | 0:862f11d627f9 | 4 | * of this software and associated documentation files (the "Software"), to deal |
go2dev | 0:862f11d627f9 | 5 | * in the Software without restriction, including without limitation the rights |
go2dev | 0:862f11d627f9 | 6 | *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
go2dev | 0:862f11d627f9 | 7 | *copies of the Software, and to permit persons to whom the Software is |
go2dev | 0:862f11d627f9 | 8 | *furnished to do so, subject to the following conditions: |
go2dev | 0:862f11d627f9 | 9 | * |
go2dev | 0:862f11d627f9 | 10 | *The above copyright notice and this permission notice shall be included in |
go2dev | 0:862f11d627f9 | 11 | *all copies or substantial portions of the Software. |
go2dev | 0:862f11d627f9 | 12 | * |
go2dev | 0:862f11d627f9 | 13 | *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
go2dev | 0:862f11d627f9 | 14 | *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
go2dev | 0:862f11d627f9 | 15 | *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
go2dev | 0:862f11d627f9 | 16 | *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
go2dev | 0:862f11d627f9 | 17 | *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
go2dev | 0:862f11d627f9 | 18 | *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
go2dev | 0:862f11d627f9 | 19 | *THE SOFTWARE. |
go2dev | 0:862f11d627f9 | 20 | */ |
go2dev | 0:862f11d627f9 | 21 | |
go2dev | 0:862f11d627f9 | 22 | |
go2dev | 0:862f11d627f9 | 23 | /*Header file for the SRF02 sonar range finder class*/ |
go2dev | 0:862f11d627f9 | 24 | #ifndef SRF02_H |
go2dev | 0:862f11d627f9 | 25 | #define SRF02_H |
go2dev | 0:862f11d627f9 | 26 | |
go2dev | 0:862f11d627f9 | 27 | #include "mbed.h" |
go2dev | 0:862f11d627f9 | 28 | |
go2dev | 0:862f11d627f9 | 29 | //!Library for the SRF02 Ultrasonic Ranger |
go2dev | 0:862f11d627f9 | 30 | /* |
go2dev | 0:862f11d627f9 | 31 | * The SRF02 is an I2C Ultrasonic rangefinder which can return values for measured |
go2dev | 0:862f11d627f9 | 32 | * distance as either time of flight (uS) or directly in cm or in. |
go2dev | 0:862f11d627f9 | 33 | |
go2dev | 0:862f11d627f9 | 34 | * Example Usage: |
go2dev | 0:862f11d627f9 | 35 | * @code |
go2dev | 0:862f11d627f9 | 36 | * //Take a measurement of the current distance (in cm), print the result to the terminal via the debug port |
go2dev | 0:862f11d627f9 | 37 | * #include "mbed.h" |
go2dev | 0:862f11d627f9 | 38 | * #include "SRF02.h" |
go2dev | 0:862f11d627f9 | 39 | * |
go2dev | 0:862f11d627f9 | 40 | * Serial debugport(USBTX,USBRX); |
go2dev | 0:862f11d627f9 | 41 | * SRF02 mySensor(p9,p10,0xE0); |
go2dev | 0:862f11d627f9 | 42 | * main() { |
go2dev | 0:862f11d627f9 | 43 | * while (1) { |
go2dev | 0:862f11d627f9 | 44 | * debugport.printf("current distance: %.2f cm. \r\n", mySensor.measurecm()); |
go2dev | 0:862f11d627f9 | 45 | * } |
go2dev | 0:862f11d627f9 | 46 | * } |
go2dev | 0:862f11d627f9 | 47 | * @endcode |
go2dev | 0:862f11d627f9 | 48 | */ |
go2dev | 0:862f11d627f9 | 49 | |
go2dev | 0:862f11d627f9 | 50 | */ |
go2dev | 0:862f11d627f9 | 51 | |
go2dev | 0:862f11d627f9 | 52 | class SRF02 { |
go2dev | 0:862f11d627f9 | 53 | public: |
go2dev | 0:862f11d627f9 | 54 | /** Create a SRF02 object connected to the specified I2C pins |
go2dev | 0:862f11d627f9 | 55 | * |
go2dev | 0:862f11d627f9 | 56 | * @param sda I2C Data pin to connect to |
go2dev | 0:862f11d627f9 | 57 | * @param scl I2C Clock pin to connect to |
go2dev | 0:862f11d627f9 | 58 | */ |
go2dev | 0:862f11d627f9 | 59 | SRF02(PinName sda, PinName scl, int addr); |
go2dev | 0:862f11d627f9 | 60 | |
go2dev | 0:862f11d627f9 | 61 | /** Destroys an instance of SRD02 |
go2dev | 0:862f11d627f9 | 62 | */ |
go2dev | 0:862f11d627f9 | 63 | ~SRF02(); |
go2dev | 0:862f11d627f9 | 64 | |
go2dev | 0:862f11d627f9 | 65 | /** Get the current distance in centimetres |
go2dev | 0:862f11d627f9 | 66 | * |
go2dev | 0:862f11d627f9 | 67 | * @param returns the measured distance in centimetres as a float |
go2dev | 0:862f11d627f9 | 68 | */ |
go2dev | 0:862f11d627f9 | 69 | float measurecm(); |
go2dev | 0:862f11d627f9 | 70 | |
go2dev | 0:862f11d627f9 | 71 | /** Get the current distance in centimetres |
go2dev | 0:862f11d627f9 | 72 | * |
go2dev | 0:862f11d627f9 | 73 | * @param returns the measured distance in inches as a float |
go2dev | 0:862f11d627f9 | 74 | */ |
go2dev | 0:862f11d627f9 | 75 | float measurein(); |
go2dev | 0:862f11d627f9 | 76 | |
go2dev | 0:862f11d627f9 | 77 | /** Get the current distance in centimetres |
go2dev | 0:862f11d627f9 | 78 | * |
go2dev | 0:862f11d627f9 | 79 | * @param returns the time of flight from the pulse sent from the sensor in microseconds (uS) as a float |
go2dev | 0:862f11d627f9 | 80 | */ |
go2dev | 0:862f11d627f9 | 81 | float measureus(); |
go2dev | 0:862f11d627f9 | 82 | |
go2dev | 0:862f11d627f9 | 83 | |
go2dev | 0:862f11d627f9 | 84 | private: |
go2dev | 0:862f11d627f9 | 85 | I2C m_i2c; |
go2dev | 0:862f11d627f9 | 86 | int m_addr; |
go2dev | 0:862f11d627f9 | 87 | |
go2dev | 0:862f11d627f9 | 88 | /** dosonar is an internal function for this library which actually manages the I2C transaction with the sensor to get a reading. |
go2dev | 0:862f11d627f9 | 89 | * It is not directly accessible by the user via the libaray |
go2dev | 0:862f11d627f9 | 90 | */ |
go2dev | 0:862f11d627f9 | 91 | float dosonar(char rangetype); |
go2dev | 0:862f11d627f9 | 92 | |
go2dev | 0:862f11d627f9 | 93 | |
go2dev | 0:862f11d627f9 | 94 | }; |
go2dev | 0:862f11d627f9 | 95 | #endif |