光学传感器#
介绍#
The optical class is used to access data from the V5 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();
参数#
范围 |
类型 |
描述 |
|---|---|---|
|
|
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 LED to a specific brightness.isNearObject— Returns whether or not an object is close to the sensor.color— Returns the detected color as a predefined color object.brightness— Returns the detected brightness of an object.hue— Returns the detected hue value.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:
// Create an optical object in Port 1
optical Optical1 = optical(PORT1);
setLight#
设置光学传感器的 LED 指示灯为开或关。
Available Functionsvoid setLight(ledState state);
范围 |
类型 |
描述 |
|---|---|---|
|
|
A valid
|
此函数不返回值。
Examples// Turn on LED with previous intensity.
Optical.setLight(ledState::on);
setLightPower#
设置光学传感器的LED的光功率。
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— An object is detected nearby.false— An object is not detected.
// 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光学传感器检测到的颜色是颜色类的一个实例。
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 Functionsdouble brightness(bool bRaw = false);
范围 |
类型 |
描述 |
|---|---|---|
|
|
A Boolean value to read raw brightness data instead of percentage. The default is |
一个双精度浮点数,表示光学传感器检测到的亮度,范围为 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#
返回光学传感器检测到的色调值。
Available Functionsdouble hue();
此函数不接受任何参数。
Return Values表示光学传感器检测到的色调值的双精度浮点数,范围为 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));
范围 |
类型 |
描述 |
|---|---|---|
|
|
检测到对象时要调用的回调函数。 |
此函数不返回值。
Examples// Define the lost 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");
}
// Run lost when the Optical Sensor detects an object.
Optical.objectDetected(detected);
objectLost#
注册一个对象丢失时的回调函数。
Available Functionsvoid objectLost(void (* callback)(void));
范围 |
类型 |
描述 |
|---|---|---|
|
|
对象丢失时要调用的回调函数。 |
此函数不返回值。
Examples// 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");
}
// Run lost when the Optical Sensor loses an object.
Optical.objectLost(lost);