Library for interfacing the SRF08 ultrasonic range sensor. Most functions of the SRF08 are covered, including interrupt-based waiting for the ranging process to finish

Dependents:   Project6

Fork of SRF08 by Brent Dekker

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SRF08.h Source File

SRF08.h

00001 /*
00002 Copyright (c) 2012 Brent Dekker
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a copy
00005 of this software and associated documentation files (the "Software"), to deal
00006 in the Software without restriction, including without limitation the rights
00007 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008 copies of the Software, and to permit persons to whom the Software is
00009 furnished to do so, subject to the following conditions:
00010 
00011 The above copyright notice and this permission notice shall be included in
00012 all copies or substantial portions of the Software.
00013 
00014 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020 THE SOFTWARE.
00021 */
00022 
00023 #ifndef SRF08_H
00024 #define SRF08_H
00025 
00026 #include "mbed.h"
00027 
00028 #define INCHES 0x50 //Ranging Mode - Result in inches
00029 #define CM  0x51 //Ranging Mode - Result in centimeters
00030 #define US  0x52 //Ranging Mode - Result in micro-seconds
00031 
00032 /**
00033  * The SRF08 is an ultrasonic range finder with an I2C interface that allows 
00034  * the measurement to be read directly in centimetres. More information can be
00035  * found on this website: http://www.robot-electronics.co.uk/htm/srf08tech.shtml
00036  */
00037 class SRF08 {
00038 
00039 public:
00040 
00041     /**
00042      * Create a SRF08 object connected to the specified I2C pins and address
00043      *
00044      * @param SDA I2C SDA pin to connect to
00045      * @param SCL I2C SCL pin to connect to
00046      * @param i2cAddress Address of WSRF08 on I2C bus
00047      */
00048     SRF08(PinName SDA, PinName SCL, int i2cAddress);
00049     
00050     /**
00051      * Send the "Start ranging in cm" command via I2C
00052      */
00053     void startRanging(char rangingType);
00054     
00055     /**
00056      * Checks if the module has finished ranging
00057      *
00058      * @param returns Boolean stating module is finished or not
00059      */
00060     bool rangingFinished();
00061     
00062     /**
00063      * Gets the measured range from the module
00064      *
00065      * @param returns Integer range in centimetre
00066      */
00067     int getRange();
00068     
00069     /**
00070      * Gets the measured light intensity from the module
00071      *
00072      * @param returns A normalised number 0-255 representing dark to light
00073      */
00074      int getLightIntensity();
00075      
00076     /**
00077      * Sets the range register of the SRF08 for faster ranging.
00078      * 
00079      * The max range is ((rangeVal x 43mm) + 43mm). The sensors maximum range
00080      *  is about six metres
00081      *
00082      * @param rangeVal The value written to the range register of the SRF08
00083      */
00084     void setRangeRegister(unsigned char rangeVal);
00085      
00086     /**
00087      * Sets the max gain register of the SRF08.
00088      * 
00089      * @param gainVal The value written to the max gain register of the SRF08
00090      */
00091     void setMaxGainRegister(unsigned char gainVal);
00092     
00093     /**
00094      * Changes the I2C address of the SRF08.
00095      *
00096      * The factory default address is 0x0E (224)
00097      *  The address can have the following values:
00098      *   E0 | E2 | E4 | E6 ... FC | FE
00099      * 
00100      * @param i2cAddress The new I2C address for the SRF08. 
00101      */
00102     void setAddress(int i2cAddress);
00103      
00104 protected:
00105 
00106     I2C i2cMod;
00107     unsigned char i2cAddress;
00108     Timeout rangeTimeout;
00109     bool rangingBusy;
00110     
00111     void setRangingFinished();
00112 };
00113 
00114 #endif