Dev Joshi
/
SRF02_lib
SRF02 Ultrasonic range finder library
Revision 0:f0cf55dd23f6, committed 2011-05-02
- Comitter:
- go2dev
- Date:
- Mon May 02 18:44:46 2011 +0000
- Commit message:
Changed in this revision
diff -r 000000000000 -r f0cf55dd23f6 SRF02.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SRF02.cpp Mon May 02 18:44:46 2011 +0000 @@ -0,0 +1,76 @@ +/*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::distancecm(){ + //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] = 0x51; // Ranging results in cm + 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; + +} + +float SRF02::distancein(){ + //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] = 0x50; // Ranging results in cm + 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; + +} + +float SRF02::distanceus(){ + //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] = 0x52; // Ranging results in cm + 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 f0cf55dd23f6 SRF02.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SRF02.h Mon May 02 18:44:46 2011 +0000 @@ -0,0 +1,38 @@ +/*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 Ultrasonic range finder, with an I2C interface that allows the measurement to be read directly in centimetres +*/ + +class SRF02 +{ +public: + //Create an instance of the class + //Connect the peripheral over I2C using pins defined by sda and scl + SRF02(PinName sda, PinName scl, int addr); + + //Destroys an instance + ~SRF02(); + + //Read the distance in cm + float distancecm(); + + //Read the distance in in + float distancein(); + + //Read the distance in microseconds + float distanceus(); + + +private: + I2C m_i2c; + int m_addr; + + +}; +#endif \ No newline at end of file
diff -r 000000000000 -r f0cf55dd23f6 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon May 02 18:44:46 2011 +0000 @@ -0,0 +1,28 @@ +#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.\r\n", mySensor.distancecm()); + debugport.printf("current distance: %.2f inches.\r\n", mySensor.distancein()); + debugport.printf("current flightime: %.2f microseconds.\r\n", mySensor.distanceus()); + + } +}
diff -r 000000000000 -r f0cf55dd23f6 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon May 02 18:44:46 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/e2ac27c8e93e