光学的#

初始化光学类#

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

optical 构造函数创建一个光学对象。

范围

描述

端口

光学传感器连接到的有效 智能端口

// Construct an Optical Sensor "Optical" with the
// optical class.
optical Optical = optical(PORT1);

当引用光学类方法时,此“Optical”对象将在整个 API 文档的所有后续示例中使用。

类方法#

色调()#

hue() 方法返回光学传感器检测到的色调值。

返回: 一个双精度值,表示光学传感器检测到的色调值,范围为 0 - 359.99 内的浮点数。

// Set a variable, hue, to the value of the hue detected
// by the Optical Sensor.
double hue = Optical.hue();

亮度()#

brightness(bRaw) 方法返回光学传感器检测到的亮度。

参数

描述

bRaw

用于读取原始亮度数据(而非百分比)的布尔值。默认值为“false”。

返回: 一个双精度数,表示光学传感器检测到的亮度,范围在 0 - 100% 之间。

// 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);

颜色()#

color() 方法返回光学传感器检测到的颜色。

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

// Set a variable, detectColor, to the color detected by the
// Optical Sensor.
color detectColor = Optical.color();

// Print the color detected by the Optical Sensor
// to the Brain's screen.
Brain.Screen.print(detectColor);

isNearObject()#

如果光学传感器检测到附近的物体,则 isNearObject() 方法返回。

**返回:**如果检测到附近物体,则返回 true。如果没有检测到,则返回 false

// If an object is detected yb the Optical Sensor, print
// "near object".
if (Optical.isNearObject()){
  Brain.Screen.print("near object");
}

设置灯光()#

setLight(value) 方法将光学传感器的 LED 设置为开或关。

参数

描述

价值

有效的 ledStateType

**返回:**无。

// Turn on LED with previous intensity.
Optical.setLight(ledState::on);

设置灯光功率()#

setLightPower(value) 方法设置光学传感器 LED 的光功率。

参数

描述

价值

设置灯光的功率级别为 0 - 100。

单位

反射率的唯一有效单位是“百分比”。

**返回:**无。

// Set the light power to 50 percent.
Optical.setLightPower(50, percent);

积分时间()#

该方法通过以下方式调用:

integrationTime(value) 方法设置光学传感器的积分时间。

参数

描述

价值

积分时间(以毫秒为单位)从 5 到 700。

**返回:**无。

// Set the integration time to 50 milliseconds.
Optical.integrationTime(50);

integrationTime() 方法获取光学传感器的积分时间。

**返回:**表示光学传感器积分时间的双精度数。

// Get the Optical Sensor's integration time.
double intTime = Optical.integrationTime();

获取RGB()#

getRgb(raw) 方法返回光学传感器的 RGB 值。

参数

描述

生的

一个布尔值,用于确定返回的是原始值还是处理后的值。默认值为 true。

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

// Get the RGB value of the Optical Sensor.
optical::rgbc value = Optical.rgb();

对象检测阈值()#

objectDetectThreshold(value) 方法设置对象检测阈值。

参数

描述

价值

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

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

// Set the object detection threshold to 100.
int value = Optical.objectDetectThreshold(100);

手势启用()#

gestureEnable() 方法启用手势模式。

手势是指光学传感器检测到的物体的运动。因此,如果物体在光学传感器的视野范围内向上移动,则为“手势向上”。相反,如果物体向下移动,则为“手势向下”。“手势左移”和“手势右移”也是如此。

**返回:**无。

手势禁用()#

gestureDisable() 方法禁用手势模式。

手势是指光学传感器检测到的物体的运动。因此,如果物体在光学传感器的视野范围内向上移动,则为“手势向上”。相反,如果物体向下移动,则为“手势向下”。“手势左移”和“手势右移”也是如此。

**返回:**无。

获取手势()#

getGesture() 方法返回光学传感器检测到的手势。

手势是指光学传感器检测到的物体的运动。因此,如果物体在光学传感器的视野范围内向上移动,则为“手势向上”。相反,如果物体向下移动,则为“手势向下”。“手势左移”和“手势右移”也是如此。

**返回:**具有最后一个手势检测数据的 Optical::Gesture 对象。

检测到物体()#

objectDetected(callback) 方法注册一个在检测到对象时回调的函数。

参数

描述

打回来

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

**返回:**无。

// Define the detected function with a void return type,
// showing it doesn't return a value.
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() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Run detected when the Optical Sensor detects an object.
  Optical.objectDetected(detected);
}

对象丢失()#

objectLost(callback) 方法注册一个当对象丢失时的回调函数。

参数

描述

打回来

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

**返回:**无。

// Define the lost function with a void return type,
// showing it doesn't return a value.
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() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Run lost when the Optical Sensor loses an object.
  Optical.objectLost(lost);
}

手势向上()#

gestureUp(callback) 方法注册一个当检测到向上手势时的回调函数。

手势是指光学传感器检测到的物体的运动。因此,如果物体在光学传感器的视野范围内向上移动,则为“手势向上”。相反,如果物体向下移动,则为“手势向下”。“手势左移”和“手势右移”也是如此。

参数

描述

打回来

当检测到向上手势时调用的回调函数。

**返回:**无。

手势向下()#

gestureDown(callback) 方法注册一个当检测到向下手势时的回调函数。

手势是指光学传感器检测到的物体的运动。因此,如果物体在光学传感器的视野范围内向上移动,则为“手势向上”。相反,如果物体向下移动,则为“手势向下”。“手势左移”和“手势右移”也是如此。

参数

描述

打回来

当检测到向下手势时调用的回调函数。

**返回:**无。

手势左移()#

gestureLeft(callback) 方法注册一个当检测到左向手势时的回调函数。

手势是指光学传感器检测到的物体的运动。因此,如果物体在光学传感器的视野范围内向上移动,则为“手势向上”。相反,如果物体向下移动,则为“手势向下”。“手势左移”和“手势右移”也是如此。

参数

描述

打回来

当检测到向左手势时调用的回调函数。

**返回:**无。

手势右()#

gestureRight(callback) 方法注册一个回调函数,用于在检测到向右手势时使用。

手势是指光学传感器检测到的物体的运动。因此,如果物体在光学传感器的视野范围内向上移动,则为“手势向上”。相反,如果物体向下移动,则为“手势向下”。“手势左移”和“手势右移”也是如此。

参数

描述

打回来

当检测到向右手势时调用的回调函数。

**返回:**无。

时间戳()#

timestamp() 方法请求光学传感器最后接收到的状态包的时间戳。

**返回:**最后一个状态包的时间戳,以毫秒为单位的无符号 32 位整数。

安装()#

installed() 方法检查光学传感器是否已连接。

**返回:**如果光学传感器已连接,则返回“true”。如果未连接,则返回“false”。