光学传感器#
介绍#
光学传感类别包含用于控制 VEX IQ 光学传感器并从中检索数据的模块。该传感器可以检测物体、识别颜色以及测量亮度和色调。
光学传感器带有一个可以开启或关闭的指示灯。该指示灯可以帮助光学传感器更清晰地检测物体和颜色。
以下是所有模块的列表:
设置光学灯#
“设置光学照明”模块用于打开或关闭光学传感器上的照明灯。当光学传感器位于黑暗环境中时,照明灯可以帮助它更好地识别物体。
set [Optical1 v] light [on v]
参数 |
描述 |
|---|---|
光学传感器 |
使用哪种光学传感器,在设备窗口中配置。 |
状态 |
光学传感器指示灯状态设置:
|
例子
when started
[Repeatedly flash the Optical Sensor's lights.]
forever
set [Optical1 v] light [on v]
wait (1) seconds
set [Optical1 v] light [off v]
wait (1) seconds
设置光学光功率#
“设置光学光功率”模块用于设置光学传感器的光强。光强可以帮助光学传感器更清晰地检测物体和颜色。
百分比越高,灯光越亮;百分比越低,灯光越暗。
如果光传感器的灯熄灭,将灯光功率设置为 0% 以上即可打开灯。
如果光传感器的指示灯亮着,将光功率设置为 0% 将关闭指示灯。
set [Optical1 v] light power to (50)%
**注意:**如果光学传感器的灯关闭,此块将打开灯。
参数 |
描述 |
|---|---|
光学传感器 |
使用哪种光学传感器,在设备窗口中配置。 |
亮度 |
光学传感器的光亮度从 0 到 100 的百分比。 |
例子
when started
[Repeatedly change the brightness of the Optical Sensor's lights.]
forever
set [Optical1 v] light power to (10)%
wait (2) seconds
set [Optical1 v] light power to (50)%
wait (2) seconds
set [Optical1 v] light power to (100)%
wait (2) seconds
光学仪器发现物体了吗?#
**光学传感器检测到物体?**布尔模块报告光学传感器是否在范围内检测到物体。
正确 — 光学传感器已检测到物体。
错误 — 光学传感器未检测到物体。
<[Optical1 v] found an object?>
参数 |
描述 |
|---|---|
光学传感器 |
使用哪种光学传感器,在设备窗口中配置。 |
例子
when started
[Turn until the Optical Sensor detects an object.]
set turn velocity to (10) [% v]
set [Optical1 v] light power to [75]%
turn [right v]
wait until <[Optical1 v] found an object?>
print [Object detected] on screen ◀ and set cursor to next row
stop driving
光学检测颜色?#
**光学传感器检测到颜色?**布尔模块报告光学传感器是否检测到特定颜色。
正确 — 光学传感器已检测到指定颜色。
错误 — 光学传感器未检测到指定的颜色。
<[Optical1 v] detects [red v]?>
参数 |
描述 |
|---|---|
光学传感器 |
使用哪种光学传感器,在设备窗口中配置。 |
颜色 |
要检查的颜色:
|
例子
when started
[Turn until the Optical Sensor detects a red object.]
set turn velocity to (10) [% v]
set [Optical1 v] light power to [75]%
turn [right v]
wait until <[Optical1 v] detects [red v]?>
print [Red detected] on screen ◀ and set cursor to next row
stop driving
光学颜色名称#
光学颜色名称报告模块报告光学传感器检测到的颜色名称。
可能的颜色有:
红色的
绿色的
蓝色的
黄色的
橙子
紫色的
青色
**注意:**此模块可以插入到 设置触摸 LED 颜色 模块 中,使触摸 LED 与检测到的颜色相匹配。
([Optical1 v] color name)
参数 |
描述 |
|---|---|
光学传感器 |
使用哪种光学传感器,在设备窗口中配置。 |
例子
when started
[Display a message when the Optical Sensor detects red.]
wait until <[Optical1 v] detects [red v]?>
print ([Optical1 v] color name) on screen ◀ and set cursor to next row
光学亮度#
光学亮度报告模块以百分比(0% 到 100%)报告检测到的光的亮度。
百分比越高,表示光学传感器检测到的光线越多;百分比越低,表示光学传感器检测到的光线越少。
([Optical1 v] brightness in %)
参数 |
描述 |
|---|---|
光学传感器 |
使用哪种光学传感器,在设备窗口中配置。 |
例子
when started
[Display the brightness when the Optical Sensor detects red.]
wait until <[Optical1 v] detects [red v]?>
print ([Optical1 v] brightness in %) on screen ◀ and set cursor to next row
光学色调#
光学色调报告模块将光学传感器检测到的色调报告为 0 到 359 度之间的数字。
色相是一种用色轮上的数字来描述颜色的方法,如下图所示:

([Optical1 v] hue in degrees)
参数 |
描述 |
|---|---|
光学传感器 |
使用哪种光学传感器,在设备窗口中配置。 |
例子
when started
[Print when the Optical Sensor detects pink.]
forever
clear screen
set cursor to row (1) column (1) on screen
if <<([Optical1 v] hue in degrees) [math_greater_than v] [290]> and <([Optical1 v] hue in degrees) [math_less_than v] [350]>> then
print [Pink!] on screen ▶
wait (0.1) seconds
else
print [Not pink.] on screen ▶
wait (0.1) seconds
当光学#
当选定的光学传感器检测到或丢失物体时,When Optical 模块会运行所连接的模块堆栈。
when [Optical1 v] [detects v] an object
参数 |
描述 |
|---|---|
光学传感器 |
使用哪种光学传感器,在设备窗口中配置。 |
状态 |
当连接的积木栈运行时:
|
例子
when [Optical1 v] [detects v] an object
[Find an object when it is lost.]
turn [right v]
wait until <[Optical1 v] found an object?>
stop driving