Dev Joshi
/
SRF02_libv2
Library for the SRF02 ultrasonic rangefinder
Revision 0:862f11d627f9, committed 2011-07-10
- Comitter:
- go2dev
- Date:
- Sun Jul 10 16:40:35 2011 +0000
- Commit message:
- v2 of this library, condenses the code base as discussed in this thread: http://mbed.org/forum/helloworld/topic/2234/
Changed in this revision
diff -r 000000000000 -r 862f11d627f9 SRF02.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SRF02.cpp Sun Jul 10 16:40:35 2011 +0000 @@ -0,0 +1,52 @@ + + +/*Library for SRF02 ultrasonic range finder*/ +#include "SRF02.h" + + + +SRF02::SRF02(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr){ + //no initialisation code required here + } + +SRF02::~SRF02(){ + +} + +float SRF02::measurecm() { + float result = dosonar(0x51); + return result; +} + +float SRF02::measureus() { + float result = dosonar(0x52); + return result; +} + +float SRF02::measurein() { + float result = dosonar(0x50); + return result; +} + + +float SRF02::dosonar(char rangetype){ + //local variables + const char m_addr = 0xE0; //srf02 default slave address + char commandsequence[2]; + + // Get range data from SRF02 in cm + // Send Tx burst command over I2C bus + commandsequence[0] = 0x00; // Command register + commandsequence[1] = rangetype; // Ranging results type + m_i2c.write(m_addr, commandsequence, 2); // Request ranging from sensor + wait(0.07); // Average wait time for sonar pulse and processing + // Read back range over I2C bus + commandsequence[0] = 0x02; // + m_i2c.write(m_addr, commandsequence, 1, 1); // + m_i2c.read(m_addr, commandsequence, 2); // Read two-byte echo result + //combine upper and lower bytes in to a single value + float range = ((commandsequence[0] << 8) + commandsequence[1]); + return range; + +} +
diff -r 000000000000 -r 862f11d627f9 SRF02.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SRF02.h Sun Jul 10 16:40:35 2011 +0000 @@ -0,0 +1,95 @@ +/* mbed SRF02 Ultra Sonic Range Finder Library + * Copyright (c) 2011 by djoshi [go2dev] + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + *copies of the Software, and to permit persons to whom the Software is + *furnished to do so, subject to the following conditions: + * + *The above copyright notice and this permission notice shall be included in + *all copies or substantial portions of the Software. + * + *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + *THE SOFTWARE. + */ + + +/*Header file for the SRF02 sonar range finder class*/ +#ifndef SRF02_H +#define SRF02_H + +#include "mbed.h" + +//!Library for the SRF02 Ultrasonic Ranger +/* + * The SRF02 is an I2C Ultrasonic rangefinder which can return values for measured + * distance as either time of flight (uS) or directly in cm or in. + + * Example Usage: + * @code + * //Take a measurement of the current distance (in cm), print the result to the terminal via the debug port + * #include "mbed.h" + * #include "SRF02.h" + * + * Serial debugport(USBTX,USBRX); + * SRF02 mySensor(p9,p10,0xE0); + * main() { + * while (1) { + * debugport.printf("current distance: %.2f cm. \r\n", mySensor.measurecm()); + * } + * } + * @endcode +*/ + +*/ + +class SRF02 { +public: + /** Create a SRF02 object connected to the specified I2C pins + * + * @param sda I2C Data pin to connect to + * @param scl I2C Clock pin to connect to + */ + SRF02(PinName sda, PinName scl, int addr); + + /** Destroys an instance of SRD02 + */ + ~SRF02(); + + /** Get the current distance in centimetres + * + * @param returns the measured distance in centimetres as a float + */ + float measurecm(); + + /** Get the current distance in centimetres + * + * @param returns the measured distance in inches as a float + */ + float measurein(); + + /** Get the current distance in centimetres + * + * @param returns the time of flight from the pulse sent from the sensor in microseconds (uS) as a float + */ + float measureus(); + + +private: + I2C m_i2c; + int m_addr; + + /** dosonar is an internal function for this library which actually manages the I2C transaction with the sensor to get a reading. + * It is not directly accessible by the user via the libaray + */ + float dosonar(char rangetype); + + +}; +#endif \ No newline at end of file
diff -r 000000000000 -r 862f11d627f9 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sun Jul 10 16:40:35 2011 +0000 @@ -0,0 +1,30 @@ +//Useage example for the SRF02 Library + +#include "mbed.h" +#include "SRF02.h" + + +Serial debugport(USBTX,USBRX); +SRF02 mySensor(p9,p10,0xE0); + + + + +main() { + + time_t seconds = time(NULL); //Internal RTC + debugport.baud(115200); //set the buadrate (default is 9600 if this command is not used) + debugport.format(8,Serial::None,1); //config the serial port (default is 8N1) + debugport.printf("Hello World! %d \r\n", seconds); //send a message out of the serial port to check all is well + //include a time stamp for reference (seconds since power up) + + + while (1) { + + + debugport.printf("current distance: %.2f cm. (v2) \r\n", mySensor.measurecm()); + debugport.printf("current distance: %.2f inches.\r\n", mySensor.measurein()); + debugport.printf("current flightime: %.2f microseconds.\r\n", mySensor.measureus()); + + } +}
diff -r 000000000000 -r 862f11d627f9 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sun Jul 10 16:40:35 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912