Sensor óptico#

Introducción#

El Sensor Óptico V5 detecta el color, el brillo y la presencia de objetos cercanos mediante una combinación de un sensor de color RGB y un sensor de proximidad. También puede usar su LED integrado para iluminar objetos y lograr una detección más precisa.

El sensor óptico VEX V5.

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

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

  • set_light – Turns the Optical Sensor’s LED on or off.

  • set_light_power – Adjusts the brightness of the Optical Sensor’s LED.

  • is_near_object – Returns whether the Optical Sensor is detecting an object.

  • color – Returns whether the Optical Sensor is detecting a specified color.

  • brightness – Returns the amount of light reflected from the object as a percent.

  • hue – Returns the hue value of the detected color.

  • object_detected – Registers a function to be called whenever the Optical Sensor detects an object.

  • object_lost – Registers a function to be called whenever the Optical Sensor loses an object.

Constructor – Inicializa manualmente un sensor óptico.

  • Optical – Creates an Optical Sensor.

establecer_luz#

set_light turns the Optical Sensor’s LED on or off. This can help increase the sensor’s accuracy when detecting colors.

Usage:
optical_sensor.set_light(state)

Parámetros

Descripción

estado

The state to set the LED to:

# Turn on the sensor's LED
optical_sensor.set_light(LEDStateType.ON)

establecer_potencia_de_luz#

set_light_power sets the brightness of the Optical Sensor’s LED. If the LED’s brightness is set above 0 while it is off, it will automatically turn the LED on.

Usage:
optical_sensor.set_light_power(percent)

Parámetros

Descripción

por ciento

El brillo que se debe establecer para el LED como porcentaje.

unidades

The unit to represent the brightness:

  • PERCENT

# Turn on the sensor's at half brightness
optical_sensor.set_light_power(50, PERCENT)

está_cerca_del_objeto#

is_near_object returns whether the Optical Sensor is detecting an object.

  • True – The Optical Sensor is detecting an object.

  • False – The Optical Sensor is not detecting an object.

Usage:
optical_sensor.is_near_object()

Parámetros

Descripción

Este método no tiene parámetros.

# Drive forward until an object is found
drivetrain.drive(FORWARD)
while not optical_sensor.is_near_object():
  pass
drivetrain.stop()

color#

color returns an RGB value corresponding to a predefined color. These can be compared to Color objects to create conditional statements.

Los colores posibles son:

  • Color.BLACK

  • Color.WHITE

  • Color.RED - A hue value between 340° - 20°

  • Color.GREEN - A hue value between 80° - 140°

  • Color.BLUE - A hue value between 200° - 240°

  • Color.YELLOW - A hue value between 40° - 60°

  • Color.ORANGE - A hue value between 20° - 40°

  • Color.PURPLE - A hue value between 240° - 280°

  • Color.CYAN - A hue value between 140° - 200°

Note: The Optical Sensor is looking for hue ranges that match the specified color. For detecting specific hue ranges, see hue.

Usage:
optical_sensor.color()

Parámetros

Descripción

Este método no tiene parámetros.

# Drive forward until red is found
drivetrain.drive(FORWARD)
while not optical_sensor.color() == Color.RED:
  pass
drivetrain.stop()

brillo#

brightness returns the amount of light reflected from the object as a percent.

Usage:
optical_sensor.brightness()

Parámetros

Descripción

Este método no tiene parámetros.

matiz#

hue returns the hue value of the detected color.

Este valor varía de 0 a 359 grados, lo que se correlaciona con la rueda de color:

La rueda de colores VEX que muestra los grados de los colores alrededor de un círculo, con el rojo en 0 grados y en transición a través del arco iris a medida que aumenta el valor.

Usage: optical_sensor.hue

Parámetros

Descripción

Este método no tiene parámetros.

# Look for the color pink using hue
while True:
  brain.screen.clear_screen()
  brain.screen.set_cursor(1, 1)

  if 300 < optical_sensor.hue() < 359:
    brain.screen.print("This is pink!")
  else:
    brain.screen.print("No pink detected!")

objeto_detectado#

object_detected registers a function to be called whenever the Optical Sensor detects a new object.

Usage:
optical_sensor.object_detected(callback, arg)

Parámetros

Descripción

callback

Una función previamente definida que se ejecuta cuando el sensor óptico detecta un nuevo objeto.

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("Optical Sensor detected an object")

# Call my_function whenever optical_sensor detects an object
optical_sensor.object_detected(my_function)

objeto_perdido#

object_lost registers a function to be called whenever the Optical Sensor loses a detected object.

Usage:
optical_sensor.object_lost(callback, arg)

Parámetros

Descripción

callback

Una función previamente definida que se ejecuta cuando el sensor óptico pierde un objeto detectado.

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("Optical Sensor lost an object")

# Call my_function whenever optical_sensor loses an object
optical_sensor.object_lost(my_function)

Constructor#

Constructors are used to manually create Optical objects, which are necessary for configuring an Optical Sensor outside of VEXcode.

Optical#

Optical creates an Optical Sensor.

Usage:
Optical(smartport)

Parámetro

Descripción

smartport

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

# Create an Optical Sensor in Port 10
optical_sensor = Optical(Ports.PORT10)