Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: DISCO-F746NG_LCDTS_demo Srf08Test
SRF08.cpp
- Committer:
- brentdekker
- Date:
- 2012-07-10
- Revision:
- 1:76fb116fa28d
- Parent:
- 0:4e0a8193b92e
- Child:
- 2:ca82f89f415d
File content as of revision 1:76fb116fa28d:
/*
Copyright (c) 2010 Chris Styles ( chris dot styles at mbed dot org )
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: PinName sda: Data pin of I2C bus to which module is connected
* PinName scl: Clock pin of I2C bus to which module is connected
* int addr: address of module on the I2C bus
* Returns: void
* Description: Creates an instance of the SRF08 to communicate with a sRF08 module
*/
SRF08::SRF08(PinName sda, PinName scl, int addr) :
m_i2c(sda, scl), m_addr(addr), rangeBuf(), rangeBufIndex(0) {
char cmd[2];
// Set up SRF08 max range and receiver sensitivity over I2C bus
cmd[0] = 0x02; // Range register
cmd[1] = 0xFF;
m_i2c.write(m_addr, cmd, 2);
cmd[0] = 0x01; // Receiver gain register
cmd[1] = 0x1F; // Set max receiver gain
m_i2c.write(m_addr, cmd, 2);
}
/*
* Destructor: ~SRF08
* Args: void
* Returns: void
* Description: Destroys instance of SRF08 class
*/
SRF08::~SRF08() {
}
/*
* Function: startRanging
* Args: void
* Returns: void
* Description: Sends command to module to start ranging.
*/
void startRanging() {
// Send Tx burst command over I2C bus
cmd[0] = 0x00; // Command register
cmd[1] = 0x51; // Ranging results in cm
m_i2c.write(m_addr, cmd, 2); // Send ranging burst
}
/*
* Function: rangingFinished
* Args: void
* Returns: Bool: whether ranging is finished
* Description: Checks if the ranging process on the module is finished
*/
bool rangingFinished() {
char cmd = 0x00; //Software revision register
char echo;
m_i2c.write(m_addr, cmd, 1, 1); //Send address of software rev register
m_i2c.read(m_addr, echo, 1); //Read 1 byte echo result
if (echo == 0xFF) 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
*/
void getRange() {
char cmd[2];
char echo[2];
// Read range over I2C bus
cmd[0] = 0x02; // Address of first echo
m_i2c.write(m_addr, cmd, 1, 1); // Send address of first echo
m_i2c.read(m_addr, echo, 2); // Read two-byte echo result
// Generate PWM mark/space ratio from range data
int range = (echo[0]<<8)+echo[1];
return range;
}
/*
* Function: readRange
* Args: void
* Returns: int range
* Description: Reads the range register and converts it to a usable value
*/
int SRF08::readRange() {
char cmd[2];
char echo[2];
// Get range data from SRF08
// Send Tx burst command over I2C bus
cmd[0] = 0x00; // Command register
cmd[1] = 0x51; // Ranging results in cm
m_i2c.write(m_addr, cmd, 2); // Send ranging burst
wait(0.07); // Wait for return echo
// Read back range over I2C bus
cmd[0] = 0x02; // Address of first echo
m_i2c.write(m_addr, cmd, 1, 1); // Send address of first echo
m_i2c.read(m_addr, echo, 2); // Read two-byte echo result
// Generate PWM mark/space ratio from range data
int range = (echo[0]<<8)+echo[1];
return range;
}
/*
* Function: readBufRange
* Args: void
* Returns: int range
* Description: Reads the range register, adds it to an array and converts it to a filtered value
*/
//int SRF08::readBufRange() {
//}
int SRF08::getAverageBufRange() {
int total = 0;
for (int i = 0; i < RANGEBUFSIZE; i++) {
total += rangeBuf[i];
}
return (int)total/RANGEBUFSIZE;
}
/*
* 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(char rangeVal) {
char cmd[2];
cmd[0] = 0x02; //Range register
cmd[1] = rangeVal; //Range value
m_i2c.write(m_addr, cmd, 2);
}
/*
* 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(char address) {
char cmd[2];
cmd[0] = 0x00;
cmd[1] = 0xA0;
m_i2c.write(m_addr, cmd, 2);
cmd[1] = 0xAA;
m_i2c.write(m_addr, cmd, 2);
cmd[1] = 0xA5;
m_i2c.write(m_addr, cmd, 2);
cmd[1] = address;
m_i2c.write(m_addr, cmd, 2);
m_addr = address;
}
/*
* 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::readLightIntensity() {
char cmd[1];
char echo[1];
cmd[0] = 0x01; // Address of light intensity value
m_i2c.write(m_addr, cmd, 1, 1); // Send address of first echo
m_i2c.read(m_addr, echo, 2); // Read two-byte echo result
int lightIntensity = echo[0];
return lightIntensity;
}