Distance#

Introduction#

The Distance Sensor can detect whether an object is in front of the sensor and return how far away that object is.

The VEX IQ Distance Sensor (1st gen) uses sonar to detect objects and measure distance. The VEX IQ Distance Sensor (2nd gen) uses a classroom-safe laser and can also return the detected object’s relative velocity and estimate whether the object is small, medium, or large.

This page uses distance_1 as the example Distance Sensor name. Replace it with your own configured name as needed.

Below is a list of all available methods:

Getters — Return data from the Distance Sensor.

  • is_object_detected — (VEX IQ Distance Sensor (1st gen) or (2nd gen)) Returns whether the Distance Sensor currently detects an object.

  • distance — (VEX IQ Distance Sensor (1st gen)) Returns the distance between the Distance Sensor and the nearest detected object.

  • object_distance — (VEX IQ Distance Sensor (2nd gen)) Returns the distance between the Distance Sensor and the nearest detected object.

  • object_velocity — (VEX IQ Distance Sensor (2nd gen)) Returns how quickly the detected object is moving toward or away from the Distance Sensor.

  • object_size — (VEX IQ Distance Sensor (2nd gen)) Returns the estimated size of the detected object.

  • installed — (VEX IQ Distance Sensor (1st gen) or (2nd gen)) Returns whether the Distance Sensor is installed and connected.

Constructors — Manually initialize and configure a Distance Sensor.

  • Sonar — Creates a VEX IQ Distance Sensor (1st gen).

  • Distance — Creates a VEX IQ Distance Sensor (2nd gen).

Getters#

is_object_detected#

is_object_detected returns whether the Distance Sensor currently detects an object within range.

  • True — The Distance Sensor detects an object.

  • False — The Distance Sensor does not detect an object.

Usage:
distance_1.is_object_detected()

Parameters

Description

This method has no parameters.

# Turn until an object is detected
drivetrain.turn(RIGHT)
while not distance_1.is_object_detected():
    wait(15, MSEC)
drivetrain.stop()
brain.screen.print("Object detected!")

distance#

distance returns the distance between the Distance Sensor and the nearest detected object as a decimal (float).

Note: This is a VEX IQ Distance Sensor (1st gen) method.

Usage:
distance_1.distance(units)

Parameters

Description

units

Optional. The distance unit: MM (default, 24 mm to 1000 mm) or INCHES (1 inch to 40 inches).

# Display the distance to an object
if distance_1.is_object_detected():
    print("Distance: ")
    print(distance_1.distance(MM))
else:
    print("No object detected")

object_distance#

object_distance returns the distance between the Distance Sensor and the nearest detected object as a decimal (float).

Note: This is a VEX IQ Distance Sensor (2nd gen) method.

Usage:
distance_1.object_distance(units)

Parameters

Description

units

Optional. The distance unit: MM (default, 20 mm to 2000 mm), INCHES (0.78 inches to 78 inches), or DistanceUnits.CM (2 cm to 200 cm).

# Drive forward until an object is 200 mm away
while distance_1.object_distance(MM) > 200:
    drivetrain.drive(FORWARD)
drivetrain.stop()

object_velocity#

object_velocity returns the relative velocity of the detected object as a decimal (float) in m/s (meters per second).

Velocity shows how quickly the object is moving toward or away from the Distance Sensor. A positive value means the object is moving toward the Distance Sensor. A negative value means the object is moving away. A value near 0 means the object is not moving much relative to the sensor.

Note: This is a VEX IQ Distance Sensor (2nd gen) method.

Usage:
distance_1.object_velocity()

Parameters

Description

This method has no parameters.

# Display the velocity of a moving object
while True:
    brain.screen.clear_screen()
    brain.screen.set_cursor(1, 1)
    brain.screen.print("Velocity: ")
    brain.screen.print(distance_1.object_velocity())
    wait(100, MSEC)

object_size#

object_size returns the estimated size of the object detected by the Distance Sensor.

The Distance Sensor estimates object size based on how much of the sensor’s view is taken up by the detected object.

  • ObjectSizeType.NONE — No object is detected.

  • ObjectSizeType.SMALL — A small object is detected.

  • ObjectSizeType.MEDIUM — A medium object is detected.

  • ObjectSizeType.LARGE — A large object is detected.

Note: This is a VEX IQ Distance Sensor (2nd gen) method.

Usage:
distance_1.object_size()

Parameters

Description

This method has no parameters.

installed#

installed returns whether the Distance Sensor is installed and connected.

  • True — The Distance Sensor is installed and connected.

  • False — The Distance Sensor is not installed or not connected.

Usage:
distance_1.installed()

Parameters

Description

This method has no parameters.

Constructors#

Constructors are used to manually create Distance and Sonar objects, such as when configuring a Distance Sensor outside of VEXcode.

Sonar#

Sonar creates a VEX IQ Distance Sensor (1st gen) object.

Usage:
distance_1 = Sonar(smartport)

Parameter

Description

smartport

The Smart Port that the Distance Sensor is connected to, written as Ports.PORTx, where x is the port number.

# Create a Sonar object in Port 1
distance_1 = Sonar(Ports.PORT1)

Distance#

Distance creates a VEX IQ Distance Sensor (2nd gen) object.

Usage:
distance_1 = Distance(smartport)

Parameter

Description

smartport

The Smart Port that the Distance Sensor is connected to, written as Ports.PORTx, where x is the port number.

# Create a Distance object in Port 1
distance_1 = Distance(Ports.PORT1)