光学的#
介绍#
光学传感器是一种基于光的传感器,它使用反射光来检测物体、识别颜色以及测量亮度和色调。
您还可以配置传感器内部指示灯的亮度。这些工具非常适合需要实时响应彩色物体、光照条件或传感器反馈的程序。
本页面使用 optical_1 作为示例光学传感器名称。请根据需要将其替换为您自己配置的名称。
以下是所有方法的列表:
操作——打开或关闭光学传感器的 LED 指示灯。
set_light— 设置 LED 的开或关状态。
自定义设置 — 调整 LED 亮度和物体检测设置。
set_light_power— 设置 LED 的亮度。object_detect_threshold— 设置检测对象的阈值。
获取器 — 读取物体是否存在、颜色、亮度、色调、RGB 值和传感器状态。
is_near_object— 返回检测到的对象是否靠近光学传感器。color— 返回检测到的最接近的颜色匹配。brightness— 返回检测到的对象的亮度。hue— 返回检测到的对象的色调。rgb— 返回光学传感器检测到的 RGB 值。installed— 返回光学传感器是否已连接到大脑。
回调函数——当检测到物体或物体丢失时运行的代码。
object_detected— 注册一个在检测到对象时要调用的函数。object_lost— 注册一个在对象丢失时要调用的函数。
构造函数——手动初始化和配置光学传感器。
Optical— 创建光学传感器。
行动#
set_light#
set_light 将光学传感器的 LED 设置为开、关或设定的亮度级别。
Usage:
optical_1.set_light(state)
参数 |
描述 |
|---|---|
|
设置灯光状态或功率百分比,范围为 0 到 100。状态可以是以下几种:
|
# Turn the LED on and off forever
while True:
optical_1.set_light(LedStateType.ON)
wait(0.5, SECONDS)
optical_1.set_light(LedStateType.OFF)
wait(0.5, SECONDS)
# Turn the LED on and off forever
while True:
optical_1.set_light(100)
wait(0.5, SECONDS)
optical_1.set_light(0)
wait(0.5, SECONDS)
修改器#
set_light_power#
set_light_power 设置光学传感器的灯光亮度。如果 LED 灯尚未亮起,此方法还会将其打开。
Usage:
optical_1.set_light_power(value)
参数 |
描述 |
|---|---|
|
将灯光的功率级别设置为从 0 到 100 的百分比。 |
# Turn the LED light on at different
# intensities
optical_1.set_light(LedStateType.ON)
wait(2, SECONDS)
optical_1.set_light_power(10)
wait(2, SECONDS)
optical_1.set_light_power(100)
object_detect_threshold#
object_detect_threshold 设置传感器可以检测物体的范围。此方法还会返回新设置的阈值。
数字越接近 255,表示范围越小;数字越接近 0,表示范围越大。
Usage:
optical_1.object_detect_threshold(value)
参数 |
描述 |
|---|---|
|
0 - 255 范围内的数字。值为 0 时将仅返回当前值。 |
def detected():
# The Brain will print that an object was detected on
# the Brain's screen.
brain.screen.print("object detected")
brain.screen.next_row()
optical_1.object_detect_threshold(254)
optical_1.object_detected(detected_closer)
def detected_closer():
# Detect an object closer to the sensor.
brain.screen.print("Detected closer")
# Run detected when the Optical Sensor detects an object.
optical_1.object_detect_threshold(100)
optical_1.object_detected(detected)
# Detect an object first with the detect threshold at 100,
# then again at 254
stage = 0
def detected():
global stage
if stage == 0:
brain.screen.print("Get closer...")
brain.screen.next_row()
stage = 1
optical_1.object_detect_threshold(254)
optical_1.object_detected(detected_closer)
def detected_closer():
global stage
if stage == 1:
brain.screen.print("Too close!")
brain.screen.next_row()
stage = 2
optical_1.object_detect_threshold(100)
optical_1.object_detected(detected)
wait(15, MSEC)
吸气剂#
is_near_object#
is_near_object 返回光学传感器是否靠近某个物体。
True— 传感器靠近物体。False— 传感器附近没有物体。
Usage:
optical_1.is_near_object()
参数 |
描述 |
|---|---|
该方法没有参数。 |
# Stop driving when Optical Sensor is
# near an object
while True:
if optical_1.is_near_object():
drivetrain.stop()
else:
drivetrain.drive(FORWARD)
color#
color 返回光学传感器检测到的预定义颜色,颜色选项如下:
BLACKBLUECYANGREENORANGEPURPLEREDTRANSPARENTWHITEYELLOW
Usage:
optical_1.color()
参数 |
描述 |
|---|---|
该方法没有参数。 |
# Stop the robot if the Optical Sensor
# detects red
while True:
if optical_1.color() == Color.RED:
drivetrain.stop()
else:
drivetrain.turn(RIGHT)
brightness#
brightness 返回光学传感器检测到的亮度。
百分比越高,表示光学传感器检测到的光线越多;百分比越低,表示光学传感器检测到的光线越少。
Usage:
optical_1.brightness(readraw)
参数 |
描述 |
|---|---|
|
Optional. A Boolean value to read raw brightness data instead of percentage:
|
hue#
hue 返回光学传感器检测到的色调值,该值是一个介于 0 到 359.99 度之间的浮点数。
色相是一种用色轮上的数字来描述颜色的方法,如下图所示。

Usage:
optical_1.hue()
参数 |
描述 |
|---|---|
该方法没有参数。 |
# Check the hue of a detected object
# to determine if it is pink
while True:
brain.screen.clear_screen()
brain.screen.set_cursor(1, 1)
if 290 < optical_1.hue() < 350:
brain.screen.print("Pink!")
wait(0.1,SECONDS)
else:
brain.screen.print("Not pink.")
wait(0.1,SECONDS)
rgb#
rgb 返回光学传感器检测到的红色、绿色、蓝色和亮度值,以元组形式返回。
Usage:
optical_1.rgb(raw)
参数 |
描述 |
|---|---|
|
可选。一个布尔值 用于确定返回原始值还是处理后的值:
|
# Display the rgb values and brightness detected
# by the Optical Sensor.
brain.screen.set_font(FontType.MONO12)
while True:
brain.screen.clear_screen()
brain.screen.set_cursor(1, 1)
brain.screen.print(optical_1.rgb())
wait(0.1, SECONDS)
installed#
installed 返回一个布尔值,指示光学传感器是否连接到大脑。
True— 光学传感器与大脑相连。False— 光学传感器未连接到大脑。
Usage:
optical_1.installed()
参数 |
描述 |
|---|---|
该方法没有参数。 |
# Display a message if the Optical Sensor
# is installed.
if optical_1.installed():
brain.screen.print("Installed!")
打回来#
object_detected#
object_detected 注册一个在检测到对象时执行的回调函数。
Usage:
optical_1.object_detected(callback, arg)
参数 |
描述 |
|---|---|
|
先前定义的当检测到对象时要执行的函数。 |
|
Optional. A tuple containing arguments to pass to the callback function. See Functions with Parameters for more information. |
def detected():
drivetrain.stop()
brain.screen.set_cursor(1, 1)
brain.screen.print("object detected")
wait(0.5,SECONDS)
brain.screen.clear_screen()
# Display a message when the
# Optical Sensor detects an object
drivetrain.drive(FORWARD)
optical_1.object_detected(detected)
object_lost#
object_lost 注册一个回调函数,用于在光学传感器丢失检测到的对象时进行回调。
Usage:
optical_1.object_lost(callback, arg)
参数 |
描述 |
|---|---|
|
先前定义的当对象丢失时执行的函数。 |
|
Optional. A tuple containing arguments to pass to the callback function. See Functions with Parameters for more information. |
def lost():
drivetrain.stop()
brain.screen.set_cursor(1, 1)
brain.screen.print("object lost")
wait(0.5,SECONDS)
brain.screen.clear_screen()
# Display a message when the Optical
# Sensor loses sight of on object
drivetrain.drive(REVERSE)
optical_1.object_lost(lost)
构造函数#
构造函数用于手动创建 Optical 对象,这是在 VEXcode 之外配置光学传感器所必需的。
Optical#
Optical 创建一个光学传感器。
Usage:
Optical(smartport)
范围 |
描述 |
|---|---|
|
光传感器所连接的智能端口,记为 PORTx,其中 x 为端口号。 |
# Construct an Optical Sensor "optical_1" with the
# Optical class
optical_1 = Optical(Ports.PORT1)
# Display the brightness of a red object detected
# by the Optical Sensor
drivetrain.turn(RIGHT)
while not optical_1.color() == Color.RED:
wait(50, MSEC)
drivetrain.stop()
brain.screen.print(optical_1.brightness())