光学传感器#
介绍#
optical 类用于访问来自光学传感器的数据。它利用反射光来检测物体、识别颜色以及测量亮度和色调。
类构造函数#
optical Optical = optical(index);
类析构函数#
销毁 optical 对象并释放相关资源。
virtual ~optical();
参数#
范围 |
类型 |
描述 |
|---|---|---|
|
|
光传感器连接的智能端口,记为 |
示例#
// Create an optical instance in Port 1
optical Optical1 = optical(PORT1);
成员功能#
optical 类包含以下成员函数:
setLight— 打开或关闭光学传感器的 LED。setLightPower— 设置光学传感器的光功率级别。isNearObject— 返回光学传感器是否检测到范围内的物体。colorname— Returns the color detected by the Optical Sensor.brightness— 返回物体反射的光量。hue— 返回光学传感器检测到的色调。objectDetected— 注册一个当光学传感器检测到物体时要调用的函数。objectLost— 注册一个当光学传感器丢失物体时要调用的函数。
在调用任何 optical 成员函数之前,必须创建一个 optical 实例,如下所示:
/* This constructor is required when using VS Code.
Optical Sensor configuration is generated automatically
in VEXcode using the Device Menu. Replace the values
as needed. */
// Create an optical object in Port 1
optical Optical1 = optical(PORT1);
setLight#
设置光学传感器的 LED 指示灯亮起或熄灭。指示灯可以帮助光学传感器更清晰地检测物体和颜色。
Available Functionsvoid setLight(ledState state);
范围 |
类型 |
描述 |
|---|---|---|
|
|
设置 LED 的亮灭状态。0 |
此函数不返回值。
Examples// Turn on LED with previous intensity.
Optical.setLight(ledState::on);
setLightPower#
设置光学传感器的光线亮度。光线可以帮助光学传感器更清晰地检测物体和颜色。
百分比越高,灯光越亮;百分比越低,灯光越暗。
如果光传感器的灯熄灭,将灯光功率设置为 0% 以上即可打开灯。
如果光传感器的指示灯亮着,将光功率设置为 0% 将关闭指示灯。
Available Functionsvoid setLightPower(
int32_t intensity,
percentUnits units = percent );
范围 |
类型 |
描述 |
|---|---|---|
|
|
灯光亮度级别,范围从 0 到 100。 |
|
|
表示光照强度的单位: |
此函数不返回值。
Examples// Set the light power to 50 percent.
Optical.setLightPower(50, percent);
isNearObject#
返回光学传感器是否检测到范围内的物体。
Available Functionsbool isNearObject();
此函数不接受任何参数。
Return Values此函数返回一个布尔值,指示是否检测到附近物体:
true— 检测到附近有物体。false— 未检测到物体。
// If an object is detected by the Optical Sensor, print
// "near object".
if (Optical.isNearObject()){
Brain.Screen.print("near object");
}
colorname#
根据检测到的色调值,返回光学传感器检测到的颜色。
Available Functionsvex::colorType colorname();
此函数不接受任何参数。
Return Values光学传感器检测到的颜色是颜色类的一个实例。
可选颜色有:
blue– 色调值介于 196° 和 229° 之间blue_green– 色调值介于 140° 和 195° 之间blue_violet– 色调值介于 230° 和 239° 之间green– 色调值介于 84° 和 139° 之间orange– 色调值介于 21° 和 30° 之间red– 色调值介于 350° 和 14° 之间red_orange– 色调值介于 15° 和 20° 之间red_violet– 色调值介于 281° 和 350° 之间violet– 色调值介于 240° 和 280° 之间yellow– 色调值介于 54° 和 74° 之间yellow_green– 色调值介于 75° 和 83° 之间yellow_orange– 色调值介于 31° 和 53° 之间
// Set a variable, detectColor, to the color detected by the
// Optical Sensor
colorType detectColor = Optical.colorname();
// Print the color detected by the Optical Sensor
// to the Brain's screen
Brain.Screen.print(detectColor);
brightness#
返回反射回光学传感器的光量。
百分比越高,意味着反射回光学传感器的光线越多;百分比越低,意味着反射回的光线越少。
Available Functionsdouble brightness(bool bRaw = false);
范围 |
类型 |
描述 |
|---|---|---|
|
|
一个布尔值,用于读取原始亮度数据而不是百分比。默认值为 |
表示光学传感器检测到的亮度值的双精度浮点数,范围为 0 - 100%;或者表示传感器检测到的原始数据的双精度浮点数。
Examples// Set a variable, brightness, to the value of the brightness
// detected by the Optical Sensor
double brightness = Optical.brightness();
// Print the brightness detected by the Optical Sensor to the
// Brain's screen
Brain.Screen.print(brightness);
hue#
返回光学传感器检测到的色调。
色相是用色轮上的数字来描述颜色的一种方法。

double hue();
此函数不接受任何参数。
Return Values表示光学传感器检测到的色调值的双精度浮点数,范围为 0 — 359.00。
Examples// Set a variable, hue, to the value of the hue detected
// by the Optical Sensor.
double hue = Optical.hue();
objectDetected#
注册一个回调函数,用于在检测到对象时执行该回调函数。
Available Functionsvoid objectDetected(void (* callback)(void));
范围 |
类型 |
描述 |
|---|---|---|
|
|
先前定义的 函数,当光学传感器检测到新物体时执行。 |
此函数不返回值。
Examples定义回调函数(在
int main()之外)// Called when the Optical Sensor detects an object void detected() { // The Brain will print that the Optical Sensor detected an // object to the Brain's screen. Brain.Screen.print("object detected"); }在
int main()中注册回调函数/* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ int main() { // Run when the Optical Sensor detects an object. Optical.objectDetected(detected); }
objectLost#
注册一个对象丢失时的回调函数。
Available Functionsvoid objectLost(void (* callback)(void));
范围 |
类型 |
描述 |
|---|---|---|
|
|
先前定义的 函数,当光学传感器丢失检测到的对象时执行。 |
此函数不返回值。
Examples定义回调函数(在
int main()之外)// Called when the Optical Sensor loses an object void lost() { // The Brain will print that the Optical Sensor lost an // object to the Brain's screen. Brain.Screen.print("object lost"); }在
int main()中注册回调函数/* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ int main() { // Run lost when the Optical Sensor loses an object. Optical.objectLost(lost); }