WPILibC++ 2024.3.2
frc::Ultrasonic Class Reference

Ultrasonic rangefinder class. More...

#include <frc/Ultrasonic.h>

Inheritance diagram for frc::Ultrasonic:
wpi::Sendable wpi::SendableHelper< Ultrasonic >

Public Member Functions

 Ultrasonic (int pingChannel, int echoChannel)
 Create an instance of the Ultrasonic Sensor. More...
 
 Ultrasonic (DigitalOutput *pingChannel, DigitalInput *echoChannel)
 Create an instance of an Ultrasonic Sensor from a DigitalInput for the echo channel and a DigitalOutput for the ping channel. More...
 
 Ultrasonic (DigitalOutput &pingChannel, DigitalInput &echoChannel)
 Create an instance of an Ultrasonic Sensor from a DigitalInput for the echo channel and a DigitalOutput for the ping channel. More...
 
 Ultrasonic (std::shared_ptr< DigitalOutput > pingChannel, std::shared_ptr< DigitalInput > echoChannel)
 Create an instance of an Ultrasonic Sensor from a DigitalInput for the echo channel and a DigitalOutput for the ping channel. More...
 
 ~Ultrasonic () override
 
 Ultrasonic (Ultrasonic &&)=default
 
Ultrasonicoperator= (Ultrasonic &&)=default
 
int GetEchoChannel () const
 Returns the echo channel. More...
 
void Ping ()
 Single ping to ultrasonic sensor. More...
 
bool IsRangeValid () const
 Check if there is a valid range measurement. More...
 
units::meter_t GetRange () const
 Get the range from the ultrasonic sensor. More...
 
bool IsEnabled () const
 
void SetEnabled (bool enable)
 
void InitSendable (wpi::SendableBuilder &builder) override
 Initializes this Sendable object. More...
 
- Public Member Functions inherited from wpi::Sendable
virtual ~Sendable ()=default
 
virtual void InitSendable (SendableBuilder &builder)=0
 Initializes this Sendable object. More...
 
- Public Member Functions inherited from wpi::SendableHelper< Ultrasonic >
 SendableHelper (const SendableHelper &rhs)=default
 
 SendableHelper (SendableHelper &&rhs)
 
SendableHelperoperator= (const SendableHelper &rhs)=default
 
SendableHelperoperator= (SendableHelper &&rhs)
 

Static Public Member Functions

static void SetAutomaticMode (bool enabling)
 Turn Automatic mode on/off. More...
 

Additional Inherited Members

- Protected Member Functions inherited from wpi::SendableHelper< Ultrasonic >
 SendableHelper ()=default
 
 ~SendableHelper ()
 

Detailed Description

Ultrasonic rangefinder class.

The Ultrasonic rangefinder measures absolute distance based on the round-trip time of a ping generated by the controller. These sensors use two transducers, a speaker and a microphone both tuned to the ultrasonic range. A common ultrasonic sensor, the Daventech SRF04 requires a short pulse to be generated on a digital channel. This causes the chirp to be emitted. A second line becomes high as the ping is transmitted and goes low when the echo is received. The time that the line is high determines the round trip distance (time of flight).

Constructor & Destructor Documentation

◆ Ultrasonic() [1/5]

frc::Ultrasonic::Ultrasonic ( int  pingChannel,
int  echoChannel 
)

Create an instance of the Ultrasonic Sensor.

This is designed to support the Daventech SRF04 and Vex ultrasonic sensors.

Parameters
pingChannelThe digital output channel that sends the pulse to initiate the sensor sending the ping.
echoChannelThe digital input channel that receives the echo. The length of time that the echo is high represents the round trip time of the ping, and the distance.

◆ Ultrasonic() [2/5]

frc::Ultrasonic::Ultrasonic ( DigitalOutput pingChannel,
DigitalInput echoChannel 
)

Create an instance of an Ultrasonic Sensor from a DigitalInput for the echo channel and a DigitalOutput for the ping channel.

Parameters
pingChannelThe digital output object that starts the sensor doing a ping. Requires a 10uS pulse to start.
echoChannelThe digital input object that times the return pulse to determine the range.

◆ Ultrasonic() [3/5]

frc::Ultrasonic::Ultrasonic ( DigitalOutput pingChannel,
DigitalInput echoChannel 
)

Create an instance of an Ultrasonic Sensor from a DigitalInput for the echo channel and a DigitalOutput for the ping channel.

Parameters
pingChannelThe digital output object that starts the sensor doing a ping. Requires a 10uS pulse to start.
echoChannelThe digital input object that times the return pulse to determine the range.

◆ Ultrasonic() [4/5]

frc::Ultrasonic::Ultrasonic ( std::shared_ptr< DigitalOutput pingChannel,
std::shared_ptr< DigitalInput echoChannel 
)

Create an instance of an Ultrasonic Sensor from a DigitalInput for the echo channel and a DigitalOutput for the ping channel.

Parameters
pingChannelThe digital output object that starts the sensor doing a ping. Requires a 10uS pulse to start.
echoChannelThe digital input object that times the return pulse to determine the range.

◆ ~Ultrasonic()

frc::Ultrasonic::~Ultrasonic ( )
override

◆ Ultrasonic() [5/5]

frc::Ultrasonic::Ultrasonic ( Ultrasonic &&  )
default

Member Function Documentation

◆ GetEchoChannel()

int frc::Ultrasonic::GetEchoChannel ( ) const

Returns the echo channel.

Returns
The echo channel.

◆ GetRange()

units::meter_t frc::Ultrasonic::GetRange ( ) const

Get the range from the ultrasonic sensor.

Returns
Range of the target returned from the ultrasonic sensor. If there is no valid value yet, i.e. at least one measurement hasn't completed, then return 0.

◆ InitSendable()

void frc::Ultrasonic::InitSendable ( wpi::SendableBuilder builder)
overridevirtual

Initializes this Sendable object.

Parameters
buildersendable builder

Implements wpi::Sendable.

◆ IsEnabled()

bool frc::Ultrasonic::IsEnabled ( ) const

◆ IsRangeValid()

bool frc::Ultrasonic::IsRangeValid ( ) const

Check if there is a valid range measurement.

The ranges are accumulated in a counter that will increment on each edge of the echo (return) signal. If the count is not at least 2, then the range has not yet been measured, and is invalid.

◆ operator=()

Ultrasonic & frc::Ultrasonic::operator= ( Ultrasonic &&  )
default

◆ Ping()

void frc::Ultrasonic::Ping ( )

Single ping to ultrasonic sensor.

Send out a single ping to the ultrasonic sensor. This only works if automatic (round robin) mode is disabled. A single ping is sent out, and the counter should count the semi-period when it comes in. The counter is reset to make the current value invalid.

◆ SetAutomaticMode()

static void frc::Ultrasonic::SetAutomaticMode ( bool  enabling)
static

Turn Automatic mode on/off.

When in Automatic mode, all sensors will fire in round robin, waiting a set time between each sensor.

Parameters
enablingSet to true if round robin scheduling should start for all the ultrasonic sensors. This scheduling method assures that the sensors are non-interfering because no two sensors fire at the same time. If another scheduling algorithm is preferred, it can be implemented by pinging the sensors manually and waiting for the results to come back.

◆ SetEnabled()

void frc::Ultrasonic::SetEnabled ( bool  enable)

The documentation for this class was generated from the following file: