光学的#
介绍#
光学传感器是一种基于光的传感器,利用反射光来检测物体、识别颜色以及测量亮度和色调。设置为手势模式时,它还可以识别传感器前方的手部动作。
以下是所有可用方法的列表:
方法——检测物体、颜色和手势。
object_detected – 当检测到物体时调用一个函数。
object_lost – 当对象丢失时调用一个函数。
gesture_up – 检测到向上手势时调用一个函数。
gesture_down – 检测到向下手势时调用一个函数。
gesture_left – 检测到左手势时调用一个函数。
gesture_right – 检测到右手势时调用一个函数。
gesture_enable – 将光学传感器设置为手势模式。
gesture_disable – 将光学传感器设置为彩色模式。
set_light - 设置 LED 的开或关状态。
set_light_power - 设置 LED 的亮度。
is_near_object – 返回检测到的物体是否靠近光学传感器。
color – 返回检测到的最接近的颜色匹配。
亮度 – 返回检测到的物体的亮度。
色调 – 返回检测到的物体的色调。
get_gesture – 返回检测到的手势。
rgb – 返回光学传感器检测到的 RGB 值。
object_detect_threshold - 设置检测物体的阈值。
installed – 返回光学传感器是否连接到大脑。
构造函数——手动初始化和配置光学传感器。
光学 – 创建光学传感器。
方法#
检测到物体#
object_detected
注册一个当检测到物体时的回调函数。
用法:
object_detected(callback,arg)
参数 |
描述 |
---|---|
打回来 |
先前定义的 函数,当轴的值发生变化时执行。 |
arg |
可选。包含要传递给回调函数的参数的元组。更多信息,请参阅带参数的函数](Logic/Functions.md#functions-with-parameters)。 |
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
注册一个当对象丢失时的回调函数。
用法:
object_lost(callback,arg)
参数 |
描述 |
---|---|
打回来 |
先前定义的 函数,当轴的值发生变化时执行。 |
arg |
可选。包含要传递给回调函数的参数的元组。更多信息,请参阅带参数的函数](Logic/Functions.md#functions-with-parameters)。 |
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)
手势向上#
gesture_up
注册一个当检测到向上手势时的回调函数。
手势是指光学传感器检测到的物体的运动。因此,如果物体在光学传感器的视野范围内向上移动,则为“手势向上”。相反,如果物体向下移动,则为“手势向下”。“手势左移”和“手势右移”也是如此。
用法:
gesture_up(回调,arg)
参数 |
描述 |
---|---|
打回来 |
先前定义的 函数,当轴的值发生变化时执行。 |
arg |
可选。包含要传递给回调函数的参数的元组。更多信息,请参阅带参数的函数](Logic/Functions.md#functions-with-parameters)。 |
def detect_gesture_up():
drivetrain.drive_for(FORWARD, 100, MM)
# Run detect_gesture_up when the Optical Sensor detects
# an upward gesture
optical_1.gesture_enable()
optical.gesture_up(detect_gesture_up)
手势向下#
gesture_down
注册一个当检测到向下手势时的回调函数。
手势是指光学传感器检测到的物体的运动。因此,如果物体在光学传感器的视野范围内向上移动,则为“手势向上”。相反,如果物体向下移动,则为“手势向下”。“手势左移”和“手势右移”也是如此。
用法:
gesture_down(回调,arg)
参数 |
描述 |
---|---|
打回来 |
先前定义的 函数,当轴的值发生变化时执行。 |
arg |
可选。包含要传递给回调函数的参数的元组。更多信息,请参阅带参数的函数](Logic/Functions.md#functions-with-parameters)。 |
def detect_gesture_down():
drivetrain.drive_for(REVERSE, 100, MM)
# Run detect_gesture_down when the Optical Sensor detects
# a downward gesture
optical_1.gesture_enable()
optical.gesture_down(detect_gesture_down)
手势左移#
gesture_left
注册一个回调函数,用于检测到向左手势时。
手势是指光学传感器检测到的物体的运动。因此,如果物体在光学传感器的视野范围内向上移动,则为“手势向上”。相反,如果物体向下移动,则为“手势向下”。“手势左移”和“手势右移”也是如此。
用法:
gesture_left(回调,arg)
参数 |
描述 |
---|---|
打回来 |
先前定义的 函数,当轴的值发生变化时执行。 |
arg |
可选。包含要传递给回调函数的参数的元组。更多信息,请参阅带参数的函数](Logic/Functions.md#functions-with-parameters)。 |
def detect_gesture_left():
drivetrain.turn_for(LEFT, 360, DEGREES)
# Run detect_gesture_left when the Optical Sensor detects
# a leftward gesture
optical_1.gesture_enable()
optical.gesture_left(detect_gesture_left)
手势右#
gesture_right
注册一个回调函数,用于检测到向右手势时。
手势是指光学传感器检测到的物体的运动。因此,如果物体在光学传感器的视野范围内向上移动,则为“手势向上”。相反,如果物体向下移动,则为“手势向下”。“手势左移”和“手势右移”也是如此。
用法:
gesture_right(回调,arg)
参数 |
描述 |
---|---|
打回来 |
先前定义的 函数,当轴的值发生变化时执行。 |
arg |
可选。包含要传递给回调函数的参数的元组。更多信息,请参阅带参数的函数](Logic/Functions.md#functions-with-parameters)。 |
def detect_gesture_right():
drivetrain.turn_for(RIGHT, 360, DEGREES)
# Run detect_gesture_right when the Optical Sensor detects
# a rightward gesture
optical_1.gesture_enable()
optical.gesture_right(detect_gesture_right)
手势启用#
gesture_enable
启用手势模式。
手势是指光学传感器检测到的物体的运动。因此,如果物体在光学传感器的视野范围内向上移动,则为“手势向上”。相反,如果物体向下移动,则为“手势向下”。“手势左移”和“手势右移”也是如此。
用法:
gesture_enable()
参数 |
描述 |
---|---|
该方法没有参数。 |
def detect_gesture_right():
drivetrain.turn_for(RIGHT, 360, DEGREES)
# Run detect_gesture_right when the Optical Sensor detects
# a rightward gesture
optical_1.gesture_enable()
optical.gesture_right(detect_gesture_right)
wait(15, MSEC)
手势禁用#
gesture_disable
禁用手势模式。
手势是指光学传感器检测到的物体的运动。因此,如果物体在光学传感器的视野范围内向上移动,则为“手势向上”。相反,如果物体向下移动,则为“手势向下”。“手势左移”和“手势右移”也是如此。
用法:
gesture_disable()
参数 |
描述 |
---|---|
该方法没有参数。 |
# Display detected colors after the Optical Sensor
# detects a right gesture.
optical_1.gesture_enable()
while not optical_1.get_gesture().type == GestureType.RIGHT:
wait(20, MSEC)
# Disable gestures and return to colors.
optical_1.gesture_disable()
while True:
brain.screen.clear_screen()
brain.screen.set_cursor(1, 1)
brain.screen.print(optical_1.color())
wait(0.1, SECONDS)
设置灯光#
set_light
将光学传感器的 LED 设置为开或关。
用法:
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)
设置灯光功率#
set_light_power
设置光学传感器 LED 的光功率。
用法:
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)
靠近物体#
is_near_object
返回光学传感器是否位于物体附近。
“True”——传感器靠近物体。
“False”——传感器不在物体附近。
用法:
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。
用法:
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
返回光学传感器检测到的亮度。
用法:
亮度(读绘)
参数 |
描述 |
---|---|
读出 |
可选。布尔值用于读取原始亮度数据而非百分比:
|
# 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())
色调#
hue
返回光学传感器检测到的色调值。
用法:
hue()
参数 |
描述 |
---|---|
该方法没有参数。 |
# Set a variable hue, to the value of the hue detected by
# the Optical Sensor
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)
获取手势#
get_gesture
返回光学传感器检测到的手势。
手势是指光学传感器检测到的物体的运动。因此,如果物体在光学传感器的视野范围内向上移动,则为“手势向上”。相反,如果物体向下移动,则为“手势向下”。“手势左移”和“手势右移”也是如此。
用法:
get_gesture(newobject).type
参数 |
描述 |
---|---|
新对象 |
可选。布尔值,用于确定是否创建新的 Gesture 对象:
|
optical_1.gesture_enable()
while True:
# Spin if a gesture is detected
if optical_1.get_gesture().type == GestureType.NONE:
brain.screen.clear_screen()
brain.screen.set_cursor(1, 1)
brain.screen.print("No gesture")
wait(0.5, SECONDS)
else:
drivetrain.turn_for(RIGHT, 360, DEGREES)
RGB#
rgb
返回光学传感器的 RGB 值。
用法:
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)
对象检测阈值#
object_detect_threshold
设置物体检测阈值。此方法还会返回新设置的阈值。
用法:
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)
# Alternate example
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
# Detect an object first with the detect threshold at 100,
# then again at 254
optical_1.object_detect_threshold(100)
optical_1.object_detected(detected)
wait(15, MSEC)
已安装#
“installed”返回一个布尔值,指示光学传感器是否连接到大脑。
“真”——光学传感器与大脑相连。
“错误”——光学传感器未连接到大脑。
用法:
installed()
参数 |
描述 |
---|---|
该方法没有参数。 |
# Display a message if the Optical Sensor
# is installed.
if optical_1.installed():
brain.screen.print("Installed!")
构造函数#
构造函数用于手动创建“光学”对象,这对于在 VEXcode 之外配置光学传感器是必需的。
对于下面的示例,配置的光学传感器将被命名为“optical_1”,并且在整个 API 文档的所有后续示例中引用“Optical”类方法时将使用它。
光学的#
“光学”创建一个光学传感器。
用法:
光纤(端口)
范围 |
描述 |
---|---|
|
光学传感器连接到哪个智能端口,以“PORT”表示,后跟端口号,范围从 1 到 12。 |
# 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())