光学传感器#

介绍#

The optical class is used to access data from the Optical Sensor. It uses reflected light to detect objects, identify colors, and measure brightness and hue.

类构造函数#

optical Optical = optical(index);

类析构函数#

Destroys the optical object and releases associated resources.

virtual ~optical();

参数#

范围

类型

描述

index

int32_t

The Smart Port that the Optical Sensor is connected to, written as PORTx, where x is the port number (for example, PORT1).

示例#

// Create an optical instance in Port 1
optical Optical1 = optical(PORT1);

成员功能#

The optical class includes the following member functions:

  • setLight — Turns the Optical Sensor’s LED on or off.

  • setLightPower — Sets the Optical Sensor’s light power level.

  • isNearObject — Returns whether or not the Optical Sensor detects an object within range.

  • color — Returns the color detected by the Optical Sensor.

  • brightness — Returns the amount of light reflected from the object.

  • hue — Returns the hue detected by the Optical Sensor.

  • objectDetected — Registers a function to be called when the Optical Sensor detects an object.

  • objectLost — Registers a function to be called when the Optical Sensor loses an object.

Before calling any optical member functions, an optical instance must be created, as shown below:

/* 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 Functions
void  setLight(ledState state);

Parameters

范围

类型

描述

state

ledState

Sets the LED on or off. ledState::on turns the LED on at 100% brightness. ledState::off turns it off.

Return Values

此函数不返回值。

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

setLightPower#

设置光学传感器的光线亮度。光线可以帮助光学传感器更清晰地检测物体和颜色。

百分比越高,灯光越亮;百分比越低,灯光越暗。

如果光传感器的灯熄灭,将灯光功率设置为 0% 以上即可打开灯。

如果光传感器的指示灯亮着,将光功率设置为 0% 将关闭指示灯。

Available Functions
void setLightPower( 
      int32_t      intensity, 
      percentUnits units = percent );

Parameters

范围

类型

描述

intensity

int32_t

灯光亮度级别,范围从 0 到 100。

units

percentUnits

The unit that represents the light intensity: percent / pct — percent

Return Values

此函数不返回值。

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

isNearObject#

返回光学传感器是否检测到范围内的物体。

Available Functions
bool  isNearObject();

Parameters

此函数不接受任何参数。

Return Values

此函数返回一个布尔值,指示是否检测到附近物体:

  • true — An object is detected nearby.

  • false — An object is not detected.

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

color#

根据检测到的色调值,返回光学传感器检测到的颜色。

Available Functions
vex::color  color();

Parameters

此函数不接受任何参数。

Return Values

光学传感器检测到的颜色是颜色类的一个实例。

可选颜色有:

  • blue – Hue value between 196° and 229°

  • blue_green – Hue value between 140° and 195°

  • blue_violet – Hue value between 230° and 239°

  • green – Hue value between 84° and 139°

  • orange – Hue value between 21° and 30°

  • red – Hue value between 350° and 14°

  • red_orange – Hue value between 15° and 20°

  • red_violet – Hue value between 281° and 350°

  • violet – Hue value between 240° and 280°

  • yellow – Hue value between 54° and 74°

  • yellow_green – Hue value between 75° and 83°

  • yellow_orange – Hue value between 31° and 53°

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

brightness#

返回反射回光学传感器的光量。

百分比越高,意味着反射回光学传感器的光线越多;百分比越低,意味着反射回的光线越少。

Available Functions
double  brightness(bool bRaw = false);

Parameters

范围

类型

描述

bRaw

bool

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

Return Values

表示光学传感器检测到的亮度值的双精度浮点数,范围为 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#

返回光学传感器检测到的色调。

色相是用色轮上的数字来描述颜色的一种方法。

VEX 色轮,显示围绕圆周的颜色度数,红色为 0 度,随着数值增加,颜色依次过渡到彩虹的颜色。

Available Functions
double hue();

Parameters

此函数不接受任何参数。

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 Functions
void objectDetected(void (* callback)(void));

Parameters

范围

类型

描述

callback

void (*)(void)

先前定义的 函数,当光学传感器检测到新物体时执行。

Return Values

此函数不返回值。

Examples

Define the callback function (outside of 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");
}

Register the callback inside 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 Functions
void objectLost(void (* callback)(void));

Parameters

范围

类型

描述

callback

void (*)(void)

先前定义的 函数,当光学传感器丢失检测到的对象时执行。

Return Values

此函数不返回值。

Examples

Define the callback function (outside of 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");
}

Register the callback inside 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);
}