Craig Evans / SRF02

Dependents:   Project_A el13jarDistanceSensorProject UltrasonicDistanceSensor-el13jb Distance_Sensor_SRF02 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SRF02.h Source File

SRF02.h

Go to the documentation of this file.
00001 /**
00002 @file SRF02.h
00003 
00004 @brief Header file containing member functions and variables
00005 
00006 */
00007 
00008 #ifndef SRF02_H
00009 #define SRF02_H
00010 
00011 // registers
00012 #define CMD_REG         0x00
00013 #define RANGE_H_REG     0x02
00014 #define RANGE_L_REG     0x03
00015 
00016 // commands
00017 #define INCH_CMD    0x50
00018 #define CM_CMD      0x51
00019 #define US_CMD      0x52
00020 
00021 #include "mbed.h"
00022 
00023 /**
00024 @brief Library for interfacing with SRF02 Ultrasonic Sensor in I2C
00025 @see http://www.robot-electronics.co.uk/htm/srf02tech.htm
00026 
00027 @brief Revision 1.1
00028 
00029 @author Craig A. Evans
00030 @date   June 2016
00031  *
00032  * Example:
00033  * @code
00034 
00035  #include "mbed.h"
00036  #include "SRF02.h"
00037  
00038  SRF02 sensor(p28,p27,0xE0);  // SDA, SCL, address
00039 
00040  int main() {
00041 
00042     while(1) {
00043 
00044         // read sensor distance in cm and print over serial port
00045         int distance = sensor.getDistanceCm();
00046         serial.printf("Distance = %d cm\n",distance);
00047         // short delay before next measurement
00048         wait(0.5);
00049 
00050     }
00051 }
00052  * @endcode
00053  */
00054 
00055 class SRF02
00056 {
00057 public:
00058 
00059     /** Create a SRF02 object connected to the specified I2C pins
00060     *
00061     * @param sdaPin - mbed SDA pin 
00062     * @param sclPin - mbed SCL pin
00063     * @param addr - write address of the SRF02 sensor 
00064     * 
00065     */
00066     SRF02(PinName sdaPin, PinName sclPin, char addr);
00067     /** Read distance in centimetres
00068     *
00069     * @returns distance in centimetres (int)
00070     * 
00071     */
00072     int getDistanceCm();
00073     /** Change I2C address of SRF02  sensor
00074     *
00075     * @param address - @see https://www.robot-electronics.co.uk/htm/srf02techI2C.htm for valid addresses
00076     *
00077     */
00078     void changeAddress(char addr);
00079 
00080 private:
00081     /** Hangs in infinite loop flashing 'blue lights of death'
00082     *
00083     */ 
00084     void error();
00085 
00086 
00087 private:  // private variables
00088     I2C* i2c;
00089     BusOut* leds;
00090     
00091     char w_addr_; // write address of sensor
00092     char r_addr_; // read address (= write address + 1)
00093 };
00094 
00095 #endif