颜色#

介绍#

颜色传感器是一种光学传感器,可以检测表面颜色、测量光照水平并感知附近的物体。它内置一个 LED,用于照亮表面,从而提高检测的准确性。

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

以下是所有方法的列表:

操作——使用颜色传感器检测物体。

  • object_detected — Registers a function to be called when an object is detected.

变异器——修改颜色传感器上的指示灯。

  • 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.

获取器 — 返回带有颜色传感器的对象数据。

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

  • color — Returns the hex code of the detected color.

  • brightness — Returns the detected brightness.

  • hue — Returns the detected hue value.

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

构造函数——手动初始化和配置颜色传感器。

行动#

object_detected#

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

Usage:
color_1.object_detected(callback, args)

参数

描述

callback

先前定义的当颜色传感器检测到物体时要执行的函数

args

可选。包含要传递给回调函数的参数的元组。更多信息,请参阅带参数的函数](…/Logic/Functions.md#functions-with-parameters)。

修改器#

set_light#

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

Usage:
color_1.set_light(value, units)

参数

描述

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)

# Turn the LED on and off forever
while True:
    color_1.set_light(LedStateType.ON, 75)
    wait(0.5, SECONDS)
    color_1.set_light(0)
    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)

参数

描述

value

LED 的亮度以 0 到 100 的百分比表示。值为 0 时 LED 熄灭。

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

吸气剂#

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()

参数

描述

该方法没有参数。

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()

参数

描述

该方法没有参数。

# Display the currently 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()

参数

描述

该方法没有参数。

# 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, corresponding to positions on the color wheel shown below.

一个圆形色轮,显示完整的色调光谱,边缘标有度数,从顶部的 0° 到 360°,以 30° 为增量递增。

Usage:
color_1.hue()

参数

描述

该方法没有参数。

# Display the currently detected hue 
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()

参数

描述

该方法没有参数。

构造函数#

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(smartport)

范围

描述

smartport

颜色传感器所连接的智能端口,记为 PORTx,其中 x 是端口号。

# Create a Color Sensor object in Port 3
color_1 = ColorSensor(Ports.PORT3)