Sensor de distancia#

Introducción#

The Distance Sensor can detect whether an object is in front of the sensor and return how far away that object is. It can also return the detected object’s relative velocity and estimate whether the object is small, medium, or large.

The Distance Sensor operates over a range of 20 mm to 2000 mm.

Below 200mm, accuracy is approximately ±15mm. Above 200mm, accuracy improves to about 5%.

The Distance Sensor uses a Class 1 laser. Detection is directed straight ahead and is classroom-safe.

The Distance Sensor.

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

A continuación se muestra una lista de los métodos disponibles:

  • object_distance — Returns the distance between the Distance Sensor and the nearest detected object.

  • object_velocity — Returns how quickly the detected object is moving toward or away from the Distance Sensor.

  • object_size — Returns the estimated size of the detected object.

  • is_object_detected — Returns whether the Distance Sensor currently detects an object.

  • changed — Registers a function to run whenever the Distance Sensor’s value changes.

Constructor — Manually initialize a Distance Sensor.

  • Distance — Creates a Distance Sensor.

distancia_del_objeto#

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

Usage:
distance_sensor.object_distance(units)

Parámetros

Descripción

units

Optional. The distance unit: MM (default, 20 mm to 2000 mm) or INCHES (0.78 inches to 78.74 inches).

# Drive forward until the object is within 50 mm away
drivetrain.drive(FORWARD)
while not distance_sensor.object_distance(MM) < 50:
  pass
drivetrain.stop()

velocidad del objeto#

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 value near 0 means the object is not moving much relative to the sensor.

Usage:
distance_sensor.object_velocity()

Parámetros

Descripción

Este método no tiene parámetros.

tamaño del objeto#

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(0, “NONE”) — No object is detected.

  • ObjectSizeType(1, “SMALL”) — A small object is detected.

  • ObjectSizeType(2, “MEDIUM”) — A medium object is detected.

  • ObjectSizeType(3, “LARGE”) — A large object is detected.

Usage:
distance_sensor.object_size()

Parámetros

Descripción

Este método no tiene parámetros.

se ha detectado un objeto#

is_object_detected returns whether the Distance Sensor currently detects an object.

  • True — The Distance Sensor detects an object.

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

Usage:
distance_sensor.is_object_detected()

Parámetros

Descripción

Este método no tiene parámetros.

# Drive forward until the object is within 50 mm away
while True:
  drivetrain.drive(FORWARD)
  if distance_sensor.is_object_detected():
    if distance_sensor.object_distance(MM) < 50:
      drivetrain.stop()
      break

cambió#

changed registers a function to be called whenever the Distance Sensor’s value changes.

Usage:
distance_sensor.changed(callback, arg)

Parámetros

Descripción

callback

Una función previamente definida que se ejecuta cuando cambia el valor del sensor de distancia.

arg

Opcional. Una tupla que contiene los argumentos que se pasan a la función de devolución de llamada. Consulte Uso de funciones con parámetros para obtener más información.

def my_function():
  brain.screen.print("Distance changed")

# Call my_function whenever distance_sensor's value changes
distance_sensor.changed(my_function)

Constructor#

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

Distance#

Distance creates a Distance Sensor object.

Usage:
Distance(smartport)

Parámetro

Descripción

smartport

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

# Create a Distance Sensor in Port 10
distance_sensor = Distance(Ports.PORT10)