srf08 library working with RTOS from BenderII
Revision 0:70df4930eee9, committed 2015-08-11
- Comitter:
- zatakon
- Date:
- Tue Aug 11 13:01:45 2015 +0000
- Commit message:
- ???????; ???????; ????????; ???????; ???????
Changed in this revision
SRF08.cpp | Show annotated file Show diff for this revision Revisions of this file |
SRF08.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 70df4930eee9 SRF08.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SRF08.cpp Tue Aug 11 13:01:45 2015 +0000 @@ -0,0 +1,150 @@ +/* +Copyright (c) 2012 Brent Dekker + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#include "SRF08.h" + +/* + * Constructor: SRF08 + * Args: + * Returns: void + * Description: Creates an instance of the SRF08 to communicate with a sRF08 module + */ +SRF08::SRF08(PinName SDA, PinName SCL, int i2cAddress) : + i2cMod(SDA, SCL), + i2cAddress(i2cAddress), + rangingBusy(false) { + +} + +/* + * Function: startRanging + * Args: void + * Returns: void + * Description: Sends command to module to start ranging. + */ +void SRF08::startRanging() { + //Create a two byte command. The first first byte is the register address + // on the SRF08 to write to. The second byte is the command which is written + // to that address ("Start ranging in cm" in this case). + const char command[] = {0x00, 0x51}; + i2cMod.write(i2cAddress, command, 2); + this->rangingBusy = true; + Thread::wait(70); + setRangingFinished(); +} + +/* + * Function: rangingFinished + * Args: void + * Returns: Bool: whether ranging is finished + * Description: Checks if the ranging process on the module is finished + */ +bool SRF08::rangingFinished() { + if(this->rangingBusy) return false; + return true; +} + +/* + * Function: getRange + * Args: void + * Returns: int range + * Description: Range in cm. This function should only be called when ranging is finished, otherwise previous value is returned + */ +int SRF08::getRange() { + while (!rangingFinished() ) + Thread::wait(10); //Wait until ranging is finished + const char command[] = {0x02}; //Address of range register + char response[] = {0x00, 0x00}; + i2cMod.write(i2cAddress, command, 1, 1); //Send command + i2cMod.read(i2cAddress, response, 2); //Read 16bits result + int range = (response[0]<<8)+response[1]; //Shift two bytes into int + return range; //Return int range +} + +/* + * Function: readLightIntensity + * Args: void + * Returns: int lightIntensity + * Description: Reads the lightIntensity from the module + * The light intensity is updated if a range command is sent, so don't use + * this function only + */ +int SRF08::getLightIntensity() { + const char command[] = {0x01}; //Light intensity register + char response[] = {0x00}; + i2cMod.write(i2cAddress, command, 1, 1); //Send command + i2cMod.read(i2cAddress, response, 1); //Read response + int lightIntensity = response[0]; + return lightIntensity; +} + +/* + * Function: setRangeRegister + * Args: rangeVal + * Returns: void + * Description: Sets the maximum range for which the module waits for an echo + * The range is ((rangeVal x 43mm) + 43mm) + * The max range is about six meters + */ +void SRF08::setRangeRegister(unsigned char rangeVal) { + while (!rangingFinished() ) + Thread::wait(10); //Wait until ranging is finished + char command[] = {0x02, rangeVal}; //Range register + i2cMod.write(i2cAddress, command, 2); //Send command +} + + +//Function setMaxGainRegister +void SRF08::setMaxGainRegister(unsigned char gainVal) { + while (!rangingFinished() ) + Thread::wait(10); //Wait until ranging is finished + char command[] = {0x01, gainVal}; //Max gain register + i2cMod.write(i2cAddress, command, 2); //Send command +} + +/* + * Function: setAddress + * Args: address + * Returns: void + * Description: Sets the address of the module on the I2C bus. The factory default address is 0x0E (224) + * The address can have the following values: + * E0 | E2 | E4 | E6 ... FC | FE + */ +void SRF08::setAddress(int address) { + //Send address change sequence + char command[] = {0x00, 0xA0}; + i2cMod.write(i2cAddress, command, 2); + command[1] = 0xAA; + i2cMod.write(i2cAddress, command, 2); + command[1] = 0xA5; + i2cMod.write(i2cAddress, command, 2); + command[1] = address; + i2cMod.write(i2cAddress, command, 2); + //Save the updated address + i2cAddress = address; +} + +//Small helper function for Timeout object +void SRF08::setRangingFinished() { + this->rangingBusy = false; +} + \ No newline at end of file
diff -r 000000000000 -r 70df4930eee9 SRF08.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SRF08.h Tue Aug 11 13:01:45 2015 +0000 @@ -0,0 +1,110 @@ +/* +Copyright (c) 2012 Brent Dekker + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#ifndef SRF08_H +#define SRF08_H + +#include "mbed.h" +#include "rtos.h" + +/** + * The SRF08 is an ultrasonic range finder with an I2C interface that allows + * the measurement to be read directly in centimetres. More information can be + * found on this website: http://www.robot-electronics.co.uk/htm/srf08tech.shtml + */ +class SRF08 { + +public: + + /** + * Create a SRF08 object connected to the specified I2C pins and address + * + * @param SDA I2C SDA pin to connect to + * @param SCL I2C SCL pin to connect to + * @param i2cAddress Address of WSRF08 on I2C bus + */ + SRF08(PinName SDA, PinName SCL, int i2cAddress); + + /** + * Send the "Start ranging in cm" command via I2C + */ + void startRanging(); + + /** + * Checks if the module has finished ranging + * + * @param returns Boolean stating module is finished or not + */ + bool rangingFinished(); + + /** + * Gets the measured range from the module + * + * @param returns Integer range in centimetre + */ + int getRange(); + + /** + * Gets the measured light intensity from the module + * + * @param returns A normalised number 0-255 representing dark to light + */ + int getLightIntensity(); + + /** + * Sets the range register of the SRF08 for faster ranging. + * + * The max range is ((rangeVal x 43mm) + 43mm). The sensors maximum range + * is about six metres + * + * @param rangeVal The value written to the range register of the SRF08 + */ + void setRangeRegister(unsigned char rangeVal); + + /** + * Sets the max gain register of the SRF08. + * + * @param gainVal The value written to the max gain register of the SRF08 + */ + void setMaxGainRegister(unsigned char gainVal); + + /** + * Changes the I2C address of the SRF08. + * + * The factory default address is 0x0E (224) + * The address can have the following values: + * E0 | E2 | E4 | E6 ... FC | FE + * + * @param i2cAddress The new I2C address for the SRF08. + */ + void setAddress(int i2cAddress); + +protected: + + I2C i2cMod; + unsigned char i2cAddress; + bool rangingBusy; + + void setRangingFinished(); +}; + +#endif \ No newline at end of file