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:   DISCO-F746NG_LCDTS_demo Srf08Test

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 /**
00029  * The SRF08 is an ultrasonic range finder with an I2C interface that allows 
00030  * the measurement to be read directly in centimetres. More information can be
00031  * found on this website: http://www.robot-electronics.co.uk/htm/srf08tech.shtml
00032  */
00033 class SRF08 {
00034 
00035 public:
00036 
00037     /**
00038      * Create a SRF08 object connected to the specified I2C pins and address
00039      *
00040      * @param SDA I2C SDA pin to connect to
00041      * @param SCL I2C SCL pin to connect to
00042      * @param i2cAddress Address of WSRF08 on I2C bus
00043      */
00044     SRF08(PinName SDA, PinName SCL, int i2cAddress);
00045     
00046     /**
00047      * Send the "Start ranging in cm" command via I2C
00048      */
00049     void startRanging();
00050     
00051     /**
00052      * Checks if the module has finished ranging
00053      *
00054      * @param returns Boolean stating module is finished or not
00055      */
00056     bool rangingFinished();
00057     
00058     /**
00059      * Gets the measured range from the module
00060      *
00061      * @param returns Integer range in centimetre
00062      */
00063     int getRange();
00064     
00065     /**
00066      * Gets the measured light intensity from the module
00067      *
00068      * @param returns A normalised number 0-255 representing dark to light
00069      */
00070      int getLightIntensity();
00071      
00072     /**
00073      * Sets the range register of the SRF08 for faster ranging.
00074      * 
00075      * The max range is ((rangeVal x 43mm) + 43mm). The sensors maximum range
00076      *  is about six metres
00077      *
00078      * @param rangeVal The value written to the range register of the SRF08
00079      */
00080     void setRangeRegister(unsigned char rangeVal);
00081      
00082     /**
00083      * Sets the max gain register of the SRF08.
00084      * 
00085      * @param gainVal The value written to the max gain register of the SRF08
00086      */
00087     void setMaxGainRegister(unsigned char gainVal);
00088     
00089     /**
00090      * Changes the I2C address of the SRF08.
00091      *
00092      * The factory default address is 0x0E (224)
00093      *  The address can have the following values:
00094      *   E0 | E2 | E4 | E6 ... FC | FE
00095      * 
00096      * @param i2cAddress The new I2C address for the SRF08. 
00097      */
00098     void setAddress(int i2cAddress);
00099      
00100 protected:
00101 
00102     I2C i2cMod;
00103     unsigned char i2cAddress;
00104     Timeout rangeTimeout;
00105     bool rangingBusy;
00106     
00107     void setRangingFinished();
00108 };
00109 
00110 #endif