光学的#

介绍#

光学传感器提供了从不同物体返回数据的方法。这使得大脑能够检测颜色及其色调和亮度。

For the examples below, the configured Optical Sensor will be named Optical1 and will be used in all subsequent examples throughout this API documentation when referring to optical class methods.

以下是所有可用方法的列表:

动作——使用光学传感器检测物体。

  • objectDetected – Registers a function to call when the Optical Sensor detects an object.

  • objectLost – Registers a function to call when the Optical Sensor loses an object.

变异器——修改光学传感器灯和检测范围。

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

  • setLightPower – Sets the Optical Sensor’s LED to a specific brightness.

  • objectDetectThreshold – Sets the distance an object has to be from the Optical Sensor for it to be detected.

Getters – 使用光学传感器返回对象数据。

  • isNearObject – Returns whether or not an object is close to the sensor.

  • color – Return the detected color as a predefined color object.

  • brightness – Returns the detected brightness of an object.

  • hue – Returns the detected hue value as a float from 0 to 359.99 degrees.

  • installed – Returns whether or not the Optical Sensor is connected to the Brain.

构造函数——手动初始化光学传感器。

  • optical – Create an Optical Sensor.

行动#

objectDetected#

objectDetected registers a function to be called when the Color Sensor detects an object.

Usage:
Optical1.objectDetected(callback);

参数

描述

callback

当光学传感器检测到物体时执行的先前定义的功能。

// Example coming soon

objectLost#

objectLost registers a function to be called when the Color Sensor loses an object.

Usage:
Optical1.objectLost(callback);

参数

描述

callback

先前定义的当光学传感器丢失物体时执行的功能。

// Example coming soon

修改器#

setLight#

setLight turns the Optical Sensor’s LED lights on or off.

Usage:
Optical1.setLight(state);

参数

描述

state

The state to set the LED lights to:

  • ledState::off
  • ledState::on
// Example coming soon

setLightPower#

setLightPower sets the specific brightness of the Optical Sensor’s LED lights.

Usage:
Optical1.setLightPower(power, units);

参数

描述

power

LED 灯的亮度将设置为 0 至 100。

units

The units that represent the LED brightness:

  • percent
// Example coming soon

objectDetectThreshold#

objectDetectThreshold sets the distance away an object must be before the Optical Sensor will detect it.

Usage:
Optical1.objectDetectThreshold(value);

参数

描述

value

新的检测阈值为 0 到 255 之间的整数。值为 0 表示阈值没有变化。

// Example coming soon

吸气剂#

isNearObject#

isNearObject returns a Boolean indicating whether or not the Optical Sensor detects a close object.

  • 1 – The Optical Sensor detects a close object.

  • 0 – The Optical Sensor does not detect a close object.

Usage:
Optical1.isNearObject()

参数

描述

该方法没有参数。

// Example coming soon

color#

color returns the detected color from the Optical Sensor as an instance of a predefined color object, as shown below:

  • red

  • green

  • blue

  • white

  • yellow

  • orange

  • purple

  • cyan

  • red_violet

  • violet

  • blue_violet

  • blue_green

  • yellow_green

  • yellow_orange

  • red_orange

  • black

  • transparent

Usage:
Optical1.color()

参数

描述

该方法没有参数。

// Example coming soon

brightness#

brightness returns the brightness value from the Color Sensor as a double from 0 to 100.

Usage:
Optical1.brightness()

参数

描述

该方法没有参数。

// Example coming soon

hue#

hue returns the hue value detected by the Color Sensor as a double from 0 to 359.99.

Usage:
Optical1.hue()

参数

描述

该方法没有参数。

// Example coming soon

installed#

installed returns a Boolean indicating whether the Optical Sensor is currently connected to the Brain.

  • 1 – The Optical Sensor is connected to the Brain.

  • 0 – The Optical Sensor is not connected to the Brain.

Usage:
Optical1.installed()

参数

描述

该方法没有参数。

// Example coming soon

构造函数#

optical#

optical creates an object of the optical Class in the specified port.

Usage:
optical Optical1 = optical(port);

范围

描述

port

Which Smart Port that the Optical Sensor is connected to as PORT followed by the port number, ranging from 1 to 12.

// Example coming soon