Ultrasonic

class wpilib.Ultrasonic(*args, **kwargs)

Bases: wpilib.ErrorBase, wpilib.Sendable, wpilib.interfaces.PIDSource

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).

Overloaded function.

  1. __init__(self: wpilib._wpilib.Ultrasonic, pingChannel: int, echoChannel: int, units: wpilib._wpilib.Ultrasonic.DistanceUnit = <DistanceUnit.kInches: 0>) -> None

Create an instance of the Ultrasonic Sensor.

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

Parameters
  • pingChannel – The digital output channel that sends the pulse to initiate the sensor sending the ping.

  • echoChannel – The 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.

  • units – The units returned in either kInches or kMilliMeters

  1. __init__(self: wpilib._wpilib.Ultrasonic, pingChannel: wpilib._wpilib.DigitalOutput, echoChannel: wpilib._wpilib.DigitalInput, units: wpilib._wpilib.Ultrasonic.DistanceUnit = <DistanceUnit.kInches: 0>) -> None

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

Parameters
  • pingChannel – The digital output object that starts the sensor doing a ping. Requires a 10uS pulse to start.

  • echoChannel – The digital input object that times the return pulse to determine the range.

  • units – The units returned in either kInches or kMilliMeters

class DistanceUnit(self: wpilib._wpilib.Ultrasonic.DistanceUnit, value: int) → None

Bases: pybind11_builtins.pybind11_object

Members:

kInches

kMilliMeters

kInches = <DistanceUnit.kInches: 0>
kMilliMeters = <DistanceUnit.kMilliMeters: 1>
property name
getDistanceUnits(self: wpilib.Ultrasonic) → wpilib._wpilib.Ultrasonic.DistanceUnit

Get the current DistanceUnit that is used for the PIDSource base object.

Returns

The type of DistanceUnit that is being used.

getRangeInches(self: wpilib.Ultrasonic) → float

Get the range in inches from the ultrasonic sensor.

Returns

Range in inches 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.

getRangeMM(self: wpilib.Ultrasonic) → float

Get the range in millimeters from the ultrasonic sensor.

Returns

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

initSendable(self: wpilib.Ultrasonic, builder: wpilib.SendableBuilder) → None
isEnabled(self: wpilib.Ultrasonic) → bool
isRangeValid(self: wpilib.Ultrasonic) → bool

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.

pidGet(self: wpilib.Ultrasonic) → float

Get the range in the current DistanceUnit for the PIDSource base object.

Returns

The range in DistanceUnit

ping(self: wpilib.Ultrasonic) → None

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.

static setAutomaticMode(enabling: bool) → None

Turn Automatic mode on/off.

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

Parameters

enabling – Set 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.

setDistanceUnits(self: wpilib.Ultrasonic, units: wpilib._wpilib.Ultrasonic.DistanceUnit) → None

Set the current DistanceUnit that should be used for the PIDSource base object.

Parameters

units – The DistanceUnit that should be used.

setEnabled(self: wpilib.Ultrasonic, enable: bool) → None
setPIDSourceType(self: wpilib.Ultrasonic, pidSource: wpilib.interfaces.PIDSourceType) → None