Smartcar Shield
Public Member Functions | List of all members
SRF08 Class Reference

#include <SRF08.hpp>

Inheritance diagram for SRF08:
Inheritance graph
Collaboration diagram for SRF08:
Collaboration graph

Public Member Functions

 SRF08 (Runtime &runtime, uint8_t address)
 Constructs an SRF08 sensor that communicates over I2C. More...
 
unsigned int getDistance () override
 Gets the distance measured by the sensor in centimeters. More...
 
unsigned int getMedianDistance (uint8_t iterations=smartcarlib::constants::srf08::kDefaultIterations) override
 Gets the median distance from the specified number of measurements. More...
 
void setGain (uint8_t gain)
 Combined with the range setting affects how much time each measurement will last as well as the maximum detectable distance. More...
 
void setRange (uint8_t range)
 Combined with the gain setting affects how much time each measurement will last as well as the maximum detectable distance. More...
 
unsigned long setPingDelay (unsigned long milliseconds=smartcarlib::constants::srf08::kDefaultPingDelay)
 Delay between ranging measurements. More...
 
uint8_t getLightReading ()
 Conducts a measurement from the on-board light sensor. More...
 
uint8_t changeAddress (uint8_t newAddress)
 Changes the address of the specific SRF08 sensor. More...
 
- Public Member Functions inherited from DistanceSensor
virtual ~DistanceSensor ()=default
 

Detailed Description

The SRF08 sensor is an ultrasonic sensor that measures distances and communicates over the I2C bus. You can find more information on the module here.

Definition at line 26 of file SRF08.hpp.

Constructor & Destructor Documentation

◆ SRF08()

SRF08::SRF08 ( Runtime runtime,
uint8_t  address 
)

Constructs an SRF08 sensor that communicates over I2C.

Parameters
runtimeThe runtime environment you want to run the class for
addressI2C address which should be within the range of [112, 127]

Example:

ArduinoRuntime arduinoRuntime;
SRF08 srf08(arduinoRuntime, 112);

Definition at line 18 of file SRF08.cpp.

Member Function Documentation

◆ changeAddress()

uint8_t SRF08::changeAddress ( uint8_t  newAddress)

Changes the address of the specific SRF08 sensor.

Parameters
newAddressNew address to use [112, 127]
Returns
The in-bounds address set for the sensor

Example:

unsigned short badAddress = 130;
unsigned short actualAddress = srf08.changeAddress(badAddress);
// The actual newly acquired address should now be 127

Definition at line 132 of file SRF08.cpp.

Here is the call graph for this function:

◆ getDistance()

unsigned int SRF08::getDistance ( )
overridevirtual

Gets the distance measured by the sensor in centimeters.

Calling this might trigger a new measurement by the sensor.

Returns
0 if an error has occured, otherwise the measured distance in centimeters

Example:

unsigned int distance = sensor.getDistance();

Implements DistanceSensor.

Definition at line 35 of file SRF08.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getLightReading()

uint8_t SRF08::getLightReading ( )

Conducts a measurement from the on-board light sensor.

Returns
The light measurement [0, 255]

Example:

unsigned short lightIntensity = srf08.getLightReading();

Definition at line 111 of file SRF08.cpp.

Here is the call graph for this function:

◆ getMedianDistance()

unsigned int SRF08::getMedianDistance ( uint8_t  iterations = smartcarlib::constants::srf08::kDefaultIterations)
overridevirtual

Gets the median distance from the specified number of measurements.

Parameters
iterationsNumber of measurements to conduct (at most kMaxMedianMeasurements)
Returns
The median of the conducted measurements or an error value if the number of iterations is 0 or larger than kMaxMedianMeasurements

Example:

// Get the median of `10` measurements
unsigned int distance = sensor.getMedianDistance(10);

Implements DistanceSensor.

Definition at line 61 of file SRF08.cpp.

Here is the call graph for this function:

◆ setGain()

void SRF08::setGain ( uint8_t  gain)

Combined with the range setting affects how much time each measurement will last as well as the maximum detectable distance.

Parameters
gainGain Value [0, 31]

By default, the SRF08 sensor measurements last 70 milliseconds, blocking the execution of the sketch. If you need to measure distances faster then you need to set a default gain, range and ping delay manually, during setup(). You can find more information about this here.

The allowed range for gain values is from 0 to 31. There is no rule about which combinations of values work for every range. Some empirical range, gain and ping delay combinations are listed below. Please beware that these might not apply in your setting. Experiment and adapt accordingly.

Settings: Gain: 31 Range: 7 Delay: 6 Results: Maximum range: 34 centimeters - Measurements every 6 milliseconds

Settings: Gain: 0 Range: 23 Delay: 11 Results: Maximum range: 103 centimeters - Measurements every 11 milliseconds

Settings: Gain: 10 Range: 35 Delay: 15 Results: Maximum range: 155 centimeters - Measurements every 15 milliseconds

Example:

srf08.setGain(10);

Definition at line 78 of file SRF08.cpp.

Here is the call graph for this function:

◆ setPingDelay()

unsigned long SRF08::setPingDelay ( unsigned long  milliseconds = smartcarlib::constants::srf08::kDefaultPingDelay)

Delay between ranging measurements.

If set too low without gain and range being properly configured will cause errors during measurements.

Parameters
millisecondsDelay between ranging measurements
Returns
The set delay

Use this method to set the delay between the measurements. Remember, that just setting this delay, does not by itself mean that the sensor will be able to initiate measurements faster or have a shorter range. If you need to do that, then you will have to set the rest of the values as well. Refer to the documentation of SRF08::setGain for more details.

Example:

srf08.setPingDelay(15);

Definition at line 105 of file SRF08.cpp.

◆ setRange()

void SRF08::setRange ( uint8_t  range)

Combined with the gain setting affects how much time each measurement will last as well as the maximum detectable distance.

The maximum range can be calculated as Max_Range = (range * 3.4) + 3.4 in cm

Parameters
rangeRange Value

By default, the SRF08 sensor measurements last 70 milliseconds, blocking the execution of the sketch. If you need to measure distances faster, then you need to set a default gain, range and ping delay manually, during setup(). You can find more information about this here.

The maximum range the sensor can measure, is calculated by the following formula, giving a result in centimeters: (rangeValue + 1) * 4.3. Remember, that just setting the range, will not change the delay between the measurements or how fast the measurements can be initiated. If you need to do this, then you will have to set the rest of the values as well. There is no rule about which combinations of values work for every range. Some empirical range, gain and ping delay combinations are listed in the documentation of SRF08::setGain. Please beware that these might not apply in your setting. Experiment and adapt accordingly.

Example:

srf08.setRange(35);

Definition at line 92 of file SRF08.cpp.

Here is the call graph for this function:

The documentation for this class was generated from the following files:
SRF08
Definition: SRF08.hpp:26
ArduinoRuntime
Definition: ArduinoRuntime.hpp:11