Ultrasonic

class wpilib.Ultrasonic(pingChannel, echoChannel, units=0)[source]

Bases: wpilib.SensorBase

Ultrasonic rangefinder control

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

Create an instance of the Ultrasonic Sensor. This is designed to supchannel 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
class Unit[source]

Bases: builtins.object

The units to return when PIDGet is called

kInches = 0
kMillimeters = 1
Ultrasonic.automaticEnabled = False

Automatic round robin mode

Ultrasonic.getDistanceUnits()[source]

Get the current DistanceUnit that is used for the PIDSource interface.

Returns:The type of DistanceUnit that is being used.
Ultrasonic.getRangeInches()[source]

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.
Return type:float
Ultrasonic.getRangeMM()[source]

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 complted, then return 0.
Return type:float
Ultrasonic.instances = 0
static Ultrasonic.isAutomaticMode()[source]
Ultrasonic.isEnabled()[source]

Is the ultrasonic enabled.

Returns:True if the ultrasonic is enabled
Ultrasonic.isRangeValid()[source]

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.

Returns:True if the range is valid
Return type:bool
Ultrasonic.kMaxUltrasonicTime = 0.1

Max time (ms) between readings.

Ultrasonic.kPingTime = 9.999999999999999e-06

Time (sec) for the ping trigger pulse.

Ultrasonic.kPriority = 90

Priority that the ultrasonic round robin task runs.

Ultrasonic.kSpeedOfSoundInchesPerSec = 13560.0
Ultrasonic.pidGet()[source]

Get the range in the current DistanceUnit (PIDSource interface).

Returns:The range in DistanceUnit
Return type:float
Ultrasonic.ping()[source]

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.

Ultrasonic.sensors = <_weakrefset.WeakSet object at 0x7f1bb91f6518>

ultrasonic sensor list

Ultrasonic.setAutomaticMode(enabling)[source]

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 (bool) – 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 preffered, it can be implemented by pinging the sensors manually and waiting for the results to come back.
Ultrasonic.setDistanceUnits(units)[source]

Set the current DistanceUnit that should be used for the PIDSource interface.

Parameters:units – The DistanceUnit that should be used.
Ultrasonic.setEnabled(enable)[source]

Set if the ultrasonic is enabled.

Parameters:enable (bool) – set to True to enable the ultrasonic
static Ultrasonic.ultrasonicChecker()[source]

Background task that goes through the list of ultrasonic sensors and pings each one in turn. The counter is configured to read the timing of the returned echo pulse.

Warning

DANGER WILL ROBINSON, DANGER WILL ROBINSON: This code runs as a task and assumes that none of the ultrasonic sensors will change while it’s running. If one does, then this will certainly break. Make sure to disable automatic mode before changing anything with the sensors!!