Color#

Introducción#

El sensor de color es un sensor óptico que detecta colores de superficies, mide los niveles de luz y detecta objetos cercanos. Incorpora un LED para iluminar las superficies y lograr una detección más precisa.

For the examples below, the configured Color Sensor will be named color_1 and will be used in all subsequent examples throughout this API documentation when referring to ColorSensor class methods.

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

Acciones: Detectar objetos con el sensor de color.

Mutadores: modifican las luces del sensor de color.

  • set_light – Turns the Color Sensor’s LED on, off, or to a set brightness level.

  • set_light_power – Sets the brightness of the Color Sensor’s LED.

**Obtenedores: devuelven datos de objetos con el sensor de color.**x

  • is_near_object – Returns whether an object is within range of the sensor.

  • color – Returns the hex code of the detected color as a string.

  • brightness – Returns the detected brightness as a percent.

  • hue – Returns the detected hue value as a float from 0 to 359.99 degrees.

  • installed – Returns whether the Color Sensor is connected to the Brain.

Constructores: inicializan y configuran manualmente un sensor de color.

Comportamiento#

object_detected#

object_detected registers a function to be called when the Color Sensor detects an object.

Usage:
color_1.object_detected(callback, args)

Parámetros

Descripción

callback

Una función que se define previamente para ejecutarse cuando se suelta el interruptor de parachoques.

arg

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

# Example coming soon

Mutadores#

set_light#

set_light turns the Color Sensor’s LED on or off.

Usage:
color_1.set_light(value, units)

Parámetros

Descripción

value

Specifies whether to turn the LED on or off using one of the following:

  • LedStateType.ON – Sets the LED’s brightness to 100%.
  • LedStateType.OFF – Sets the LED’s brightness to 0%.
Alternatively, a percent from 0 to 100 can be used to set the LED brightness. A value of 0 turns the LED off; any value above 0 turns it on at that brightness.

units

Optional. The unit that represents the brightness value: PERCENT.

# Turn the LED on and off forever
while True:
    color_1.set_light(LedStateType.ON)
    wait(0.5, SECONDS)
    color_1.set_light(LedStateType.OFF)
    wait(0.5, SECONDS)

set_light_power#

set_light_power sets the brightness of the Color Sensor’s LED. If the LED is not already on, this method will also turn the LED on.

Usage:
color_1.set_light_power(value)

Parámetros

Descripción

value

El brillo del LED como porcentaje de 0 a 100. Un valor de 0 apaga el LED.

# Turn the LED light on at different intensities
color_1.set_light_power(10)
wait(2, SECONDS)
color_1.set_light_power(100)

Captadores#

is_near_object#

is_near_object returns a Boolean indicating whether the Color Sensor is within approximately 57 millimeters (2.25 inches) of an object. Detection depends on reflected light, so dark or non-reflective surfaces may not be detected reliably.

  • True - The detected object is within approximately 57 millimeters.

  • False - The detected object is not within approximately 57 millimeters.

Usage:
color_1.is_near_object()

Parámetros

Descripción

Este método no tiene parámetros.

# Stop driving when the Color Sensor is 
# near an object
while True:
    if color_1.is_near_object():
        drivetrain.stop()
    else:
        drivetrain.drive(FORWARD)

color#

color returns the hex code of the detected color as a string in the format “Color” followed by two zeros and the color’s hex value. For example, detecting red (#FF0000) would return “Color 00FF0000”.

Usage:
color_1.color()

Parámetros

Descripción

Este método no tiene parámetros.

# Display the detected hex value
while True:
    brain.screen.clear_screen()
    brain.screen.set_cursor(1, 1)
    brain.screen.print(color_1.color())
    wait(0.5,SECONDS)

brightness#

brightness returns the detected brightness as a percent.

Usage:
color_1.brightness()

Parámetros

Descripción

Este método no tiene parámetros.

# Display the currently 
# detected brightness
while True:
    brain.screen.clear_screen()
    brain.screen.set_cursor(1, 1)
    brain.screen.print("Brightness:")
    brain.screen.next_row()
    brain.screen.print(color_1.brightness())
    wait(0.5,SECONDS)

hue#

hue returns the detected hue value as a float in the range of 0 to 359.99 degrees.

Usage:
color_1.hue()

Parámetros

Descripción

Este método no tiene parámetros.

# Display the currently detected hue 
# of the sensor
while True:
    brain.screen.clear_screen()
    brain.screen.set_cursor(1, 1)
    brain.screen.print("Hue: ")
    brain.screen.print(color_1.hue())
    wait(0.5,SECONDS)

installed#

installed returns a Boolean indicating whether the Color Sensor is connected to the Brain.

  • True - The Color Sensor is connected to the Brain.

  • False - The Color Sensor is not connected to the Brain.

Usage:
color_1.installed()

Parámetros

Descripción

Este método no tiene parámetros.

# Display a message if the Color Sensor
# is installed.
if color_1.installed():
    brain.screen.print("Installed!")

Constructores#

Constructors are used to manually create ColorSensor objects, which are necessary for configuring a Color Sensor outside of VEXcode.

ColorSensor#

ColorSensor creates a Color Sensor.

Usage:
color_1 = ColorSensor(port)

Parámetro

Descripción

port

Which Smart Port that the Color Sensor is connected to as PORT followed by the port number, ranging from 1 to 12.

# Construct a Color Sensor "color_1" with the
# ColorSensor class.
color_1 = ColorSensor(Ports.PORT1)

while True:
    brain.screen.clear_screen()
    brain.screen.set_cursor(1, 1)
    brain.screen.print(color_1.color())
    wait(0.5,SECONDS)