光学传感器#
介绍#
The optical class is used to access data from the EXP Optical Sensor. It uses reflected light to detect objects, identify colors, and measure brightness and hue.
类构造函数#
optical(
int32_t index );
类析构函数#
Destroys the optical object and releases associated resources.
virtual ~optical();
参数#
范围 |
类型 |
描述 |
|---|---|---|
|
|
The Smart Port that the Optical Sensor is connected to, written as |
示例#
// 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 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 instance
optical Optical1 = optical(
PORT1); // Smart Port 1
setLight#
Sets Optical Sensor’s LED to on or off. The light can help the Optical Sensor detect objects and colors more clearly.
Available Functionsvoid setLight(ledState state);
范围 |
类型 |
描述 |
|---|---|---|
|
|
Sets the LED on or off. |
此函数不返回值。
Examples// Turn on LED with previous intensity.
Optical.setLight(ledState::on);
setLightPower#
Sets the brightness of the Optical Sensor’s LED. The light can help the Optical Sensor detect objects and colors more clearly.
If the Optical Sensor’s light is off, setting the light power above 0% will turn the light on.
If the Optical Sensor’s light is on, setting the light power at 0% will turn the light off.
A higher percentage makes the light brighter. A lower percentage makes the light dimmer.
Available Functionsvoid setLightPower(
int32_t intensity,
percentUnits units = percent );
范围 |
类型 |
描述 |
|---|---|---|
|
|
灯光亮度级别,范围从 0 到 100。 |
|
|
The unit that represents the light intensity: |
此函数不返回值。
Examples// Set the light power to 50 percent.
Optical.setLightPower(50, percent);
isNearObject#
返回光学传感器检测到附近物体时的值。
Available Functionsbool isNearObject();
此函数不接受任何参数。
Return Values此函数返回一个布尔值,指示是否检测到附近物体:
true– The Optical Sensor detects an object.false– The Optical Sensor does not detect an object.
// If an object is detected by the Optical Sensor, print
// "near object".
if (Optical.isNearObject()){
Brain.Screen.print("near object");
}
color#
返回光学传感器检测到的颜色。
Available Functionsvex::color color();
此函数不接受任何参数。
Return Values光学传感器检测到的颜色是颜色类的一个实例。
Possible colors are:
blackwhitered- A hue value between 340° - 20°green- A hue value between 80° - 140°blue- A hue value between 200° - 240°yellow- A hue value between 40° - 60°orange- A hue value between 20° - 40°purple- A hue value between 240° - 280°cyan- A hue value between 140° - 200°
// 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#
Returns the amount of light reflected from the object.
A higher percentage means more light is reflected back to the Optical Sensor. A lower percentage means less light is reflected back.
Available Functionsdouble brightness(bool bRaw = false);
范围 |
类型 |
描述 |
|---|---|---|
|
|
A Boolean value to read raw brightness data instead of percentage. The default is |
A double representing the brightness detected by the Optical Sensor as a double in the range 0 - 100%, or a double representing the raw data detected by the sensor.
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#
Returns the hue value of the detected color.
Hue is a way to describe color using numbers around a color wheel.

double hue();
此函数不接受任何参数。
Return ValuesA double representing the value of the hue detected by the Optical Sensor in the range 0 — 359.99.
Examples// Set a variable, hue, to the value of the hue detected
// by the Optical Sensor.
double hue = Optical.hue();
objectDetected#
注册一个回调函数,用于在检测到对象时执行该回调函数。
Available Functionsvoid objectDetected(void (* callback)(void));
范围 |
类型 |
描述 |
|---|---|---|
|
|
A previously defined function that executes when the Optical Sensor detects a new object. |
此函数不返回值。
ExamplesDefine 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 Functionsvoid objectLost(void (* callback)(void));
范围 |
类型 |
描述 |
|---|---|---|
|
|
A previously defined function that executes when the Optical Sensor loses a detected object. |
此函数不返回值。
ExamplesDefine 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); }