光学的#

要使光学命令出现在 VEXcode V5 中,必须在设备窗口中配置光学传感器。

有关详细信息,请参阅以下文章:

初始化光学类#

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

The optical constructor creates an optical object.

范围

描述

port

A valid Smart Port that the Optical Sensor is connected to.

// 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.

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

// 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.

参数

描述

value

A valid 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.

参数

描述

value

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

units

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.

参数

描述

value

积分时间(以毫秒为单位)从 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();

getRgb()#

The getRgb(raw) method returns the Optical Sensor’s RGB value.

参数

描述

raw

A boolean value to determine if you return raw or processed values. The default is true.

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

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

objectDetectThreshold()#

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

参数

描述

value

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

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

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

objectLost()#

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

参数

描述

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

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.