光学的#

要使光学命令出现在 VEXcode V5 中,必须在设备窗口中配置光学传感器。

有关详细信息,请参阅以下文章:

初始化光学类#

使用以下构造函数创建光学传感器:

Optical(port)

此构造函数使用一个参数:

范围

描述

port

A valid Smart Port that the Optical Sensor is connected to.

# Construct an Optical Sensor "optical" with the
# Optical class.
optical = Optical(Ports.Port1)

This optical object will be used in all subsequent examples throughout this API documentation when referring to Optical class methods.

类方法#

hue()#

The hue() method returns the value of the hue detected by the Optical Sensor.

**返回:**光学传感器检测到的色调值,范围为 0 - 359.99 内的浮点数。

# Set a variable hue, to the value of the hue detected by
# the Optical Sensor.
hue = optical.hue()

brightness()#

The brightness(readraw) method returns the brightness detected by the Optical Sensor.

参数

描述

readraw

**可选。**用于读取原始亮度数据而不是百分比的布尔值。

**返回:**光学传感器检测到的亮度,范围为 0 - 100% 之间的浮点数。

# Set a variable brightness, to the value of the brightness
# detected by the Optical Sensor.
brightness = optical.brightness()

# Print the brightness detected by the Optical Sensor to
# the Brain's screen.
brain.screen.print(brightness)

color()#

The color() method returns the color detected by the Optical Sensor.

**返回:**光学传感器检测到的颜色,作为 颜色类 的实例。

# Set a variable, color, to the color detected by the
# Optical Sensor.
color = optical.color()

# Print the color detected by the Optical Sensor to the
# Brain's screen.
brain.screen.print(color)

is_near_object()#

The is_near_object() method checks if the Optical Sensor detects a nearby object.

Returns: True if a nearby object is detected. False if one is not.

# If an object is detected yb the Optical Sensor, print "near object".
if optical.is_near_object():
    brain.screen.print("near object")

set_light()#

The set_light(value) method sets Optical Sensor’s LED to on or off.

参数

描述

value

设置灯光状态或功率百分比,范围为0-100。

**返回:**无。

# Turn on led with previous intensity.
optical.set_light(LedStateType.ON)

# Turn on led with new intensity.
optical.set_light(65)

set_light_power()#

The set_light_power(value) method sets the light power of the Optical Sensor’s LED.

参数

描述

value

将灯光的功率级别设置为 0 - 100 的百分比。

**返回:**无。

# Set the light power to 50 percent.
optical.set_light_power(50)

integration_time()#

The integration_time(value) method sets the Optical Sensor’s LED to the requested power.

参数

描述

value

**可选。**积分时间(以毫秒为单位),范围为 5 - 700。

**返回:**无。

# Set the integration time to 50 milliseconds.
optical.integration_time(50)

rgb()#

The rgb(raw) method returns the Optical Sensor’s RGB value.

参数

描述

raw

**可选。**一个布尔值,用于确定是否返回原始值或处理后的值。

**返回:**一个包含红色、绿色、蓝色和亮度值的元组。

# Get the RGB value of the Optical Sensor.
rgb = optical.rgb()

object_detect_threshold()#

The object_detect_threshold(value) method sets the object detection threshold.

参数

描述

value

0 - 255 范围内的数字。值为 0 时将仅返回当前值。

**返回:**物体检测阈值的当前值。

# Set the object detection threshold to 100.
optical.value = object_detect_threshold(100)

object_detected()#

The object_detected(callback, arg) method registers a callback function for when an object is detected.

参数

描述

callback

当检测到物体时调用的回调函数。

arg

**可选。**传递给回调函数的参数元组。

**返回:**事件类的一个实例。

# Define a function detected().
def detected():
    # The Brain will print that an object was detected on
    # the Brain's screen.
    brain.screen.print("object detected")

# Run detected when the Optical Sensor detects an object.
optical.object_detected(detected)

object_lost()#

The object_lost(callback, arg) method registers a callback function for when an object is lost.

参数

描述

callback

当对象丢失时调用的回调函数。

arg

**可选。**传递给回调函数的参数元组。

**返回:**事件类的一个实例。

# Define a function lost().
def lost():
    # The Brain will print that an object was lost on the
    # Brain's screen.
    brain.screen.print("object lost")

# Run lost when the Optical Sensor loses an object.
optical.object_lost(lost)

installed()#

The installed() method checks if the Optical Sensor is connected.

Returns: True if the Optical Sensor is connected. False if it is not.

timestamp()#

The timestamp() method returns the timestamp of the last received status packet from the Optical Sensor.

**返回:**光学传感器最后接收到的状态包的时间戳(以毫秒为单位)。