VEX V5 光学传感器文档(原文+AI翻译)

April 06, 2025 / 烧瑚烙饼 / 4阅读 / 0评论 / 分类:

光学传感器

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

更多信息,请参考以下文章:

初始化 optical 类

通过以下构造函数创建光学传感器:

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

参数

描述

port

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

// 使用 optical 类构造一个名为 "Optical" 的光学传感器
optical Optical = optical(PORT1);

复制

在本文档中,所有后续示例都将使用这个 Optical 对象来引用 optical 类的方法。

类方法

hue()

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

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

// 将变量 hue 设置为光学传感器检测到的色调值
double hue = Optical.hue();

复制

brightness()

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

参数

描述

bRaw

布尔值,用于读取原始亮度数据而非百分比。默认为 false

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

// 将变量 brightness 设置为光学传感器检测到的亮度值
double brightness = Optical.brightness();

// 将光学传感器检测到的亮度值打印到 Brain 的屏幕上
brain.screen.print(brightness);

复制

color()

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

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

// 将变量 detectColor 设置为光学传感器检测到的颜色
color detectColor = Optical.color();

// 将光学传感器检测到的颜色打印到 Brain 的屏幕上
brain.screen.print(detectColor);

复制

isNearObject()

isNearObject() 方法返回光学传感器是否检测到附近有物体。

返回值: 如果检测到附近有物体,返回 true;否则返回 false

// 如果光学传感器检测到物体,打印 "near object"
if (Optical.isNearObject()){
  Brain.Screen.print("near object");
}

复制

setLight()

setLight(value) 方法设置光学传感器 LED 的开关状态。

参数

描述

value

有效的 ledStateType

返回值: 无。

// 以之前的强度打开 LED
Optical.setLight(ledState::on);

复制

setLightPower()

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

参数

描述

value

光强级别,范围 0 - 100。

units

反射率的唯一有效单位是 percent

返回值: 无。

// 将光强设置为 50%
Optical.setLightPower(50, percent);

复制

integrationTime()

该方法有以下两种调用方式:

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

参数

描述

value

积分时间(毫秒),范围 5 - 700。

返回值: 无。

// 将积分时间设置为 50 毫秒
Optical.integrationTime(50);

复制

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

返回值: 一个双精度浮点数,表示光学传感器的积分时间。

// 获取光学传感器的积分时间
double intTime = Optical.integrationTime();

复制

getRgb()

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

参数

描述

raw

布尔值,决定返回原始值还是处理后的值。默认为 true

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

// 获取光学传感器的 RGB 值
optical::rgbc value = Optical.rgb();

复制

objectDetectThreshold()

objectDetectThreshold(value) 方法设置物体检测阈值。

参数

描述

value

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

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

// 将物体检测阈值设置为 100
int value = Optical.objectDetectThreshold(100);

复制

gestureEnable()

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

手势是光学传感器检测到的物体的运动。如果物体在光学传感器的视野中向上移动,则为 gestureUp;如果向下移动,则为 gestureDown。同理适用于 gestureLeft 和 gestureRight。

返回值: 无。

gestureDisable()

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

手势是光学传感器检测到的物体的运动。如果物体在光学传感器的视野中向上移动,则为 gestureUp;如果向下移动,则为 gestureDown。同理适用于 gestureLeft 和 gestureRight。

返回值: 无。

getGesture()

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

手势是光学传感器检测到的物体的运动。如果物体在光学传感器的视野中向上移动,则为 gestureUp;如果向下移动,则为 gestureDown。同理适用于 gestureLeft 和 gestureRight。

返回值: 一个 optical::gesture 对象,包含最后一次手势检测数据。

objectDetected()

objectDetected(callback) 方法注册一个回调函数,当检测到物体时调用。

参数

描述

callback

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

返回值: 无。

// 定义 detected 函数,返回类型为 void,表示不返回值
void detected() {
  // Brain 会在屏幕上打印 "object detected"
  Brain.Screen.print("object detected");
}

int main() {
  // 初始化机器人配置。不要删除!
  vexcodeInit();

  // 当光学传感器检测到物体时运行 detected 函数
  Optical.objectDetected(detected);
}

复制

objectLost()

objectLost(callback) 方法注册一个回调函数,当物体丢失时调用。

参数

描述

callback

物体丢失时调用的回调函数。

返回值: 无。

// 定义 lost 函数,返回类型为 void,表示不返回值
void lost() {
  // Brain 会在屏幕上打印 "object lost"
  Brain.Screen.print("object lost");
}

int main() {
  // 初始化机器人配置。不要删除!
  vexcodeInit();

  // 当光学传感器丢失物体时运行 lost 函数
  Optical.objectLost(lost);
}

复制

gestureUp()

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

手势是光学传感器检测到的物体的运动。如果物体在光学传感器的视野中向上移动,则为 gestureUp;如果向下移动,则为 gestureDown。同理适用于 gestureLeft 和 gestureRight。

参数

描述

callback

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

返回值: 无。

gestureDown()

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

手势是光学传感器检测到的物体的运动。如果物体在光学传感器的视野中向上移动,则为 gestureUp;如果向下移动,则为 gestureDown。同理适用于 gestureLeft 和 gestureRight。

参数

描述

callback

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

返回值: 无。

gestureLeft()

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

手势是光学传感器检测到的物体的运动。如果物体在光学传感器的视野中向上移动,则为 gestureUp;如果向下移动,则为 gestureDown。同理适用于 gestureLeft 和 gestureRight。

参数

描述

callback

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

返回值: 无。

gestureRight()

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

手势是光学传感器检测到的物体的运动。如果物体在光学传感器的视野中向上移动,则为 gestureUp;如果向下移动,则为 gestureDown。同理适用于 gestureLeft 和 gestureRight。

参数

描述

callback

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

返回值: 无。

timestamp()

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

返回值: 最后一次状态数据包的时间戳,作为无符号 32 位整数(毫秒)。

installed()

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

返回值: 如果光学传感器已连接,返回 true;否则返回 false

optical

To make optical commands appear in VEXcode V5, an Optical Sensor must be configured in the Devices window.

For more information, refer to these articles:

Initializing the optical Class

An Optical Sensor is created by using the following constructor:

The optical constructor creates an optical object.

Parameter

Description

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

Copy

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

Class Methods

hue()

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

Returns: A double representing the value of the hue detected by the Optical Sensor as a float in the range 0 - 359.99.

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

Copy

brightness()

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

Parameters

Description

bRaw

A boolean value to read raw brightness data instead of percentage. The default is false.

Returns: A double representing the brightness detected by the Optical Sensor as a float in the range 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);

Copy

color()

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

Returns: The color detected by the Optical Sensor as an instance of the color class.

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

Copy

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

Copy

setLight()

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

Parameters

Description

value

A valid ledStateType.

Returns: None.

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

Copy

setLightPower()

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

Parameters

Description

value

The power level to set the light from 0 - 100.

units

The only valid unit for reflectivity is percent.

Returns: None.

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

Copy

integrationTime()

This method is called in the following ways:

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

Parameters

Description

value

The integration time in milliseconds from 5 - 700.

Returns: None.

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

Copy

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

Returns: A double representing the Optical Sensor’s integration time.

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

Copy

getRgb()

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

Parameters

Description

raw

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

Returns: A tuple with red, green, blue, and brightness values.

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

Copy

objectDetectThreshold()

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

Parameters

Description

value

A number in the range 0 - 255. A value of 0 will just return the current value.

Returns: The current value of the object detection threshold.

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

Copy

gestureEnable()

The gestureEnable() method enables gesture mode.

A gesture is the motion of a detected object by the Optical Sensor. So if an object moves upward in the view of the Optical Sensor, that is a gestureUp. On the other hand if an object moves downward, that is a gestureDown. The same goes for gestureLeft and gestureRight.

Returns: None.

gestureDisable()

The gestureDisable() method disables gesture mode.

A gesture is the motion of a detected object by the Optical Sensor. So if an object moves upward in the view of the Optical Sensor, that is a gestureUp. On the other hand if an object moves downward, that is a gestureDown. The same goes for gestureLeft and gestureRight.

Returns: None.

getGesture()

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

A gesture is the motion of a detected object by the Optical Sensor. So if an object moves upward in the view of the Optical Sensor, that is a gestureUp. On the other hand if an object moves downward, that is a gestureDown. The same goes for gestureLeft and gestureRight.

Returns: An optical::gesture object with the last gesture detection data.

objectDetected()

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

Parameters

Description

callback

The callback function to be called when an object is detected.

Returns: None.

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

Copy

objectLost()

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

Parameters

Description

callback

The callback function to be called when an object is lost.

Returns: None.

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

Copy

gestureUp()

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

A gesture is the motion of a detected object by the Optical Sensor. So if an object moves upward in the view of the Optical Sensor, that is a gestureUp. On the other hand if an object moves downward, that is a gestureDown. The same goes for gestureLeft and gestureRight.

Parameters

Description

callback

The callback function to be called when an upward gesture is detected.

Returns: None.

gestureDown()

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

A gesture is the motion of a detected object by the Optical Sensor. So if an object moves upward in the view of the Optical Sensor, that is a gestureUp. On the other hand if an object moves downward, that is a gestureDown. The same goes for gestureLeft and gestureRight.

Parameters

Description

callback

The callback function to be called when an downward gesture is detected.

Returns: None.

gestureLeft()

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

A gesture is the motion of a detected object by the Optical Sensor. So if an object moves upward in the view of the Optical Sensor, that is a gestureUp. On the other hand if an object moves downward, that is a gestureDown. The same goes for gestureLeft and gestureRight.

Parameters

Description

callback

The callback function to be called when an leftward gesture is detected.

Returns: None.

gestureRight()

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

A gesture is the motion of a detected object by the Optical Sensor. So if an object moves upward in the view of the Optical Sensor, that is a gestureUp. On the other hand if an object moves downward, that is a gestureDown. The same goes for gestureLeft and gestureRight.

Parameters

Description

callback

The callback function to be called when an rightward gesture is detected.

Returns: None.

timestamp()

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

Returns: Timestamp of the last status packet as an unsigned 32-bit integer in milliseconds.

installed()

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

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

文章作者:烧瑚烙饼

文章链接:https://blog.laobinghu.top/archives/optical

版权声明:本博客所有文章除特别声明外,均采用CC BY-NC-SA 4.0 许可协议,转载请注明出处!


评论