Sensor de distancia#

Introducción#

El Sensor de Distancia V5 mide la distancia a la que se encuentra un objeto, calcula su tamaño y detecta la velocidad a la que se acerca o se aleja del robot. Funciona a distancias de 20 mm a 2000 mm.

La precisión del sensor es de aproximadamente ±15 mm cuando un objeto está a menos de 200 mm y de aproximadamente ±5 % para objetos que se encuentran más allá de ese rango.

El sensor de distancia utiliza un láser de clase 1, que proporciona una trayectoria de detección precisa y directa y es totalmente seguro para su uso en el aula.

El sensor de distancia VEX V5.

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 from the sensor to the nearest object.

  • object_velocity – Returns the relative speed of an object traveling toward or away from the Distance Sensor.

  • object_size – Returns whether the detected object is of a certain size.

  • is_object_detected – Returns whether the Distance Sensor is detecting an object.

  • changed – Registers a function to be called whenever the Distance Sensor’s value changes.

Constructor – Inicializa manualmente un sensor de distancia.

  • Distance – Creates a Distance Sensor.

distancia_del_objeto#

object_distance returns the distance from the sensor to the nearest object as a float.

Usage:
distance_sensor.object_distance(units)

Parámetros

Descripción

units

Optional. The unit to represent the distance:

  • MM (default) – Millimeters, 20 to 2000
  • INCHES – 0.78 to 78.74

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

velocidad del objeto#

object_velocity returns the relative speed of an object traveling toward or away from the Distance Sensor in m/s (meters per second).

Usage:
distance_sensor.object_velocity()

Parámetros

Descripción

Este método no tiene parámetros.

tamaño del objeto#

object_size returns the size of a detected object:

  • ObjectSizeType(0, “NONE”) – No object is detected

  • ObjectSizeType(1, “SMALL”)

  • ObjectSizeType(2, “MEDIUM”)

  • ObjectSizeType(3, “LARGE”)

El tamaño está determinado por la proporción del área de visión del sensor de distancia que ocupa el objeto detectado.

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 is detecting an object.

  • True – The Distance Sensor is detecting an object.

  • False – The Distance Sensor is not detecting 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 < 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, which are necessary for configuring a Distance Sensor outside of VEXcode.

Distance#

Distance creates a Distance Sensor.

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 number of the port.

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