Based on Robinson Lopez Monzon library: This library contains all the functions necesary to perform an application using the SRF02 sensor. Modified by Dury: replaced wait() functions that are not usefull for program performance. Esta librería contiene los métodos necesarios para poder configurar todos los parámetros del sensor SRF02.
Fork of SRF02 by
Revision 1:d624771f0bd3, committed 2013-02-09
- Comitter:
- dury
- Date:
- Sat Feb 09 22:26:47 2013 +0000
- Parent:
- 0:559a18121e83
- Commit message:
- This library contains all the functions necesary to perform an application using the SRF02 sensor.; Modified: replaced wait() functions that are not usefull for program performance.
Changed in this revision
SRF02.cpp | Show annotated file Show diff for this revision Revisions of this file |
SRF02.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/SRF02.cpp Fri Feb 25 17:23:42 2011 +0000 +++ b/SRF02.cpp Sat Feb 09 22:26:47 2013 +0000 @@ -1,27 +1,26 @@ #include "SRF02.h" +SRF02::SRF02(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) +{ +} -SRF02::SRF02(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) { - } - -SRF02::~SRF02() { - +SRF02::~SRF02() +{ } //Get the data in centimeters - -int SRF02::readcm() { - - char cmd[2]; - char eco_high[1],eco_low[1]; //this is because the sensor, sends the data in tow bytes, and you have tu read from two different registers 0x02 and 0x03; +int SRF02::readcm() +{ + char cmd[2]; + char eco_high[1],eco_low[1]; //this is because the sensor, sends the data in tow bytes, and you have tu read from two different registers 0x02 and 0x03; -// Get range data from SRF02 in centimeters + // Get range data from SRF02 in centimeters cmd[0] = 0x00; cmd[1] = 0x51; m_i2c.write(m_addr, cmd, 2); - wait(0.07); + wait_ranging(); cmd[0] = 0x02; @@ -38,21 +37,17 @@ } //Get the data in inches - -int SRF02::readinch() { +int SRF02::readinch() +{ + char cmd[2]; + char eco_high[1],eco_low[1]; //this is because the sensor, sends the data in tow bytes, and you have tu read from two different registers 0x02 and 0x03; - char cmd[2]; - char eco_high[1],eco_low[1]; //this is because the sensor, sends the data in tow bytes, and you have tu read from two different registers 0x02 and 0x03; - - -// Get range data from SRF02 in inches cmd[0] = 0x00; cmd[1] = 0x50; m_i2c.write(m_addr, cmd, 2); - wait(0.07); - - + wait_ranging(); + cmd[0] = 0x02; m_i2c.write(m_addr, cmd, 1, 1); m_i2c.read(m_addr, eco_high, 1); @@ -66,11 +61,10 @@ return range; } -void SRF02::change_addr(char new_addr){ - //Change adress of the device. Remember to have only one sensor conected to execute this method. - - char cmd[2]; +void SRF02::change_addr(char new_addr) +{ + char cmd[2]; cmd[0]=0x00; cmd[1]=0xA0; @@ -86,4 +80,13 @@ m_i2c.write(m_addr,cmd,2); } + +void SRF02::wait_ranging(void) +{ + char eco; + + do { + eco=m_i2c.read(1); + }while(eco == 0xff); +} \ No newline at end of file
--- a/SRF02.h Fri Feb 25 17:23:42 2011 +0000 +++ b/SRF02.h Sat Feb 09 22:26:47 2013 +0000 @@ -4,34 +4,38 @@ #include "mbed.h" - +/** Library to control SRF02 ultrasonic sensor */ class SRF02 { -public: - //!Creates an instance of the class. - /*! - Connect module at I2C address addr using I2C port pins sda and scl. - SRF08 - */ - SRF02(PinName sda, PinName scl, int addr); + public: + /** Creates an instance of the class + * + * @param sda I2C sda Pin + * @param scl I2C scl Pin + */ + SRF02(PinName sda, PinName scl, int addr); - /*! - Destroys instance. - */ - ~SRF02(); + /** Destroys instance */ + ~SRF02(); - - int readcm(); //Read the range data in centimeters - - int readinch(); //Read the range data in inches + /** Read the range data in centimeters */ + int readcm(); + + /** Read the range data in inches */ + int readinch(); - void change_addr(char new_addr); //Change the adress of the device. This is very usefull when there are more sensors. - //This function must be executed with only one sensor conected. - + /** Change the adress of the device. This is very usefull when there are more sensors. + * This function must be executed with only one sensor conected. + */ + void change_addr(char new_addr); -private: - I2C m_i2c; - int m_addr; + private: + /** wait for ranging to complete + * This function is for internal use + */ + void wait_ranging(void); + I2C m_i2c; + int m_addr; };