Dev Joshi / Mbed 2 deprecated SRF02_lib

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SRF02.cpp Source File

SRF02.cpp

00001 /*Library for SRF02 ultrasonic range finder*/
00002 #include "SRF02.h"
00003 
00004 SRF02::SRF02(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr){
00005     //no initialisation code required here
00006     }
00007     
00008 SRF02::~SRF02(){
00009 
00010 }
00011 
00012 float SRF02::distancecm(){
00013     //local variables
00014     const char m_addr = 0xE0;  //srf02 default slave address
00015     char commandsequence[2];
00016     
00017     // Get range data from SRF02 in cm
00018         // Send Tx burst command over I2C bus
00019         commandsequence[0] = 0x00;               // Command register
00020         commandsequence[1] = 0x51;               // Ranging results in cm
00021         m_i2c.write(m_addr, commandsequence, 2); // Request ranging from sensor
00022         wait(0.07);  // Average wait time for sonar pulse and processing
00023         // Read back range over I2C bus
00024         commandsequence[0] = 0x02;                   // 
00025         m_i2c.write(m_addr, commandsequence, 1, 1);  // 
00026         m_i2c.read(m_addr, commandsequence, 2);             // Read two-byte echo result
00027         //combine upper and lower bytes in to a single value
00028         float range = ((commandsequence[0] << 8) + commandsequence[1]);
00029         return range;
00030 
00031 }
00032 
00033 float SRF02::distancein(){
00034     //local variables
00035     const char m_addr = 0xE0;  //srf02 default slave address
00036     char commandsequence[2];
00037     
00038     // Get range data from SRF02 in cm
00039         // Send Tx burst command over I2C bus
00040         commandsequence[0] = 0x00;               // Command register
00041         commandsequence[1] = 0x50;               // Ranging results in cm
00042         m_i2c.write(m_addr, commandsequence, 2); // Request ranging from sensor
00043         wait(0.07);  // Average wait time for sonar pulse and processing
00044         // Read back range over I2C bus
00045         commandsequence[0] = 0x02;                   // 
00046         m_i2c.write(m_addr, commandsequence, 1, 1);  // 
00047         m_i2c.read(m_addr, commandsequence, 2);             // Read two-byte echo result
00048         //combine upper and lower bytes in to a single value
00049         float range = ((commandsequence[0] << 8) + commandsequence[1]);
00050         return range;
00051 
00052 }
00053 
00054 float SRF02::distanceus(){
00055     //local variables
00056     const char m_addr = 0xE0;  //srf02 default slave address
00057     char commandsequence[2];
00058     
00059     // Get range data from SRF02 in cm
00060         // Send Tx burst command over I2C bus
00061         commandsequence[0] = 0x00;               // Command register
00062         commandsequence[1] = 0x52;               // Ranging results in cm
00063         m_i2c.write(m_addr, commandsequence, 2); // Request ranging from sensor
00064         wait(0.07);  // Average wait time for sonar pulse and processing
00065         // Read back range over I2C bus
00066         commandsequence[0] = 0x02;                   // 
00067         m_i2c.write(m_addr, commandsequence, 1, 1);  // 
00068         m_i2c.read(m_addr, commandsequence, 2);             // Read two-byte echo result
00069         //combine upper and lower bytes in to a single value
00070         float range = ((commandsequence[0] << 8) + commandsequence[1]);
00071         return range;
00072 
00073 }
00074 
00075 
00076