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
SRF08.cpp
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 #include "SRF08.h" 00024 00025 /* 00026 * Constructor: SRF08 00027 * Args: 00028 * Returns: void 00029 * Description: Creates an instance of the SRF08 to communicate with a sRF08 module 00030 */ 00031 SRF08::SRF08(PinName SDA, PinName SCL, int i2cAddress) : 00032 i2cMod(SDA, SCL), 00033 i2cAddress(i2cAddress), 00034 rangeTimeout(), 00035 rangingBusy(false) { 00036 00037 } 00038 00039 /* 00040 * Function: startRanging 00041 * Args: void 00042 * Returns: void 00043 * Description: Sends command to module to start ranging. 00044 */ 00045 void SRF08::startRanging() { 00046 //Create a two byte command. The first first byte is the register address 00047 // on the SRF08 to write to. The second byte is the command which is written 00048 // to that address ("Start ranging in cm" in this case). 00049 const char command[] = {0x00, 0x51}; 00050 i2cMod.write(i2cAddress, command, 2); 00051 this->rangingBusy = true; 00052 rangeTimeout.attach(this, &SRF08::setRangingFinished, 0.07); 00053 } 00054 00055 /* 00056 * Function: rangingFinished 00057 * Args: void 00058 * Returns: Bool: whether ranging is finished 00059 * Description: Checks if the ranging process on the module is finished 00060 */ 00061 bool SRF08::rangingFinished() { 00062 if(this->rangingBusy) return false; 00063 return true; 00064 } 00065 00066 /* 00067 * Function: getRange 00068 * Args: void 00069 * Returns: int range 00070 * Description: Range in cm. This function should only be called when ranging is finished, otherwise previous value is returned 00071 */ 00072 int SRF08::getRange() { 00073 //while (!rangingFinished() ) wait(0.01); //Wait until ranging is finished 00074 const char command[] = {0x02}; //Address of range register 00075 char response[] = {0x00, 0x00}; 00076 i2cMod.write(i2cAddress, command, 1, 1); //Send command 00077 i2cMod.read(i2cAddress, response, 2); //Read 16bits result 00078 int range = (response[0]<<8)+response[1]; //Shift two bytes into int 00079 return range; //Return int range 00080 } 00081 00082 /* 00083 * Function: readLightIntensity 00084 * Args: void 00085 * Returns: int lightIntensity 00086 * Description: Reads the lightIntensity from the module 00087 * The light intensity is updated if a range command is sent, so don't use 00088 * this function only 00089 */ 00090 int SRF08::getLightIntensity() { 00091 const char command[] = {0x01}; //Light intensity register 00092 char response[] = {0x00}; 00093 i2cMod.write(i2cAddress, command, 1, 1); //Send command 00094 i2cMod.read(i2cAddress, response, 1); //Read response 00095 int lightIntensity = response[0]; 00096 return lightIntensity; 00097 } 00098 00099 /* 00100 * Function: setRangeRegister 00101 * Args: rangeVal 00102 * Returns: void 00103 * Description: Sets the maximum range for which the module waits for an echo 00104 * The range is ((rangeVal x 43mm) + 43mm) 00105 * The max range is about six meters 00106 */ 00107 void SRF08::setRangeRegister(unsigned char rangeVal) { 00108 while (!rangingFinished() ) wait(0.01); //Wait until ranging is finished 00109 char command[] = {0x02, rangeVal}; //Range register 00110 i2cMod.write(i2cAddress, command, 2); //Send command 00111 } 00112 00113 00114 //Function setMaxGainRegister 00115 void SRF08::setMaxGainRegister(unsigned char gainVal) { 00116 while (!rangingFinished() ) wait(0.01); //Wait until ranging is finished 00117 char command[] = {0x01, gainVal}; //Max gain register 00118 i2cMod.write(i2cAddress, command, 2); //Send command 00119 } 00120 00121 /* 00122 * Function: setAddress 00123 * Args: address 00124 * Returns: void 00125 * Description: Sets the address of the module on the I2C bus. The factory default address is 0x0E (224) 00126 * The address can have the following values: 00127 * E0 | E2 | E4 | E6 ... FC | FE 00128 */ 00129 void SRF08::setAddress(int address) { 00130 //Send address change sequence 00131 char command[] = {0x00, 0xA0}; 00132 i2cMod.write(i2cAddress, command, 2); 00133 command[1] = 0xAA; 00134 i2cMod.write(i2cAddress, command, 2); 00135 command[1] = 0xA5; 00136 i2cMod.write(i2cAddress, command, 2); 00137 command[1] = address; 00138 i2cMod.write(i2cAddress, command, 2); 00139 //Save the updated address 00140 i2cAddress = address; 00141 } 00142 00143 //Small helper function for Timeout object 00144 void SRF08::setRangingFinished() { 00145 this->rangingBusy = false; 00146 }
Generated on Tue Jul 12 2022 15:01:06 by 1.7.2