光学的#

初始化光学类#

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

The optical constructor creates an optical object.

范围

描述

port

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

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

This Optical object will be used in all subsequent examples throughout this API documentation when referring to optical class methods.

类方法#

hue()#

The hue() method returns the value of the hue detected by the Optical Sensor.

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

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

brightness()#

The brightness(bRaw) method returns the brightness detected by the Optical Sensor.

参数

描述

bRaw

A boolean value to read raw brightness data instead of percentage. The default is 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()#

The color() method returns the color detected by the Optical Sensor.

**返回:**光学传感器检测到的颜色,作为 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()#

The isNearObject() method returns if the Optical Sensor detects a nearby object.

Returns: true if a nearby object is detected. false if one is not.

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

setLight()#

The setLight(value) method sets Optical Sensor’s LED to on or off.

参数

描述

价值

有效的 ledStateType

**返回:**无。

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

setLightPower()#

The setLightPower(value) method sets the light power of the Optical Sensor’s LED.

参数

描述

价值

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

单位

The only valid unit for reflectivity is percent.

**返回:**无。

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

integrationTime()#

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

The integrationTime(value) method sets the Optical Sensor’s integration time.

参数

描述

价值

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

**返回:**无。

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

The integrationTime() method gets the Optical Sensor’s integration time.

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

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

objectDetectThreshold()#

The objectDetectThreshold(value) method sets the object detection threshold.

参数

描述

价值

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

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

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

gestureEnable()#

The gestureEnable() method enables gesture mode.

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

**返回:**无。

gestureDisable()#

The gestureDisable() method disables gesture mode.

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

**返回:**无。

getGesture()#

The getGesture() method returns the gesture detected by the Optical Sensor.

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

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

objectDetected()#

The objectDetected(callback) method registers a callback function for when an object is detected.

参数

描述

打回来

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

**返回:**无。

// 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()#

The objectLost(callback) method registers a callback function for when an object is lost.

参数

描述

打回来

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

**返回:**无。

// 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()#

The gestureUp(callback) method registers a callback function for when an upward gesture is detected.

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

参数

描述

打回来

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

**返回:**无。

gestureDown()#

The gestureDown(callback) method registers a callback function for when an downward gesture is detected.

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

参数

描述

打回来

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

**返回:**无。

gestureLeft()#

The gestureLeft(callback) method registers a callback function for when an leftward gesture is detected.

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

参数

描述

打回来

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

**返回:**无。

gestureRight()#

The gestureRight(callback) method registers a callback function for when an rightward gesture is detected.

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

参数

描述

打回来

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

**返回:**无。

timestamp()#

The timestamp() method requests the timestamp of the last received status packet from the Optical Sensor.

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

installed()#

The installed() method checks if the Optical Sensor is connected.

Returns: true if the Optical Sensor is connected. false if it is not.