物体传感器#

介绍#

The objectdetector class is used to measure reflectivity and detect objects by the Object Sensor.

类构造函数#

objectdetector( 
  triport::port &port, 
  bool           bReverse = false );

类析构函数#

Destroys the objectdetector object and releases associated resources.

virtual ~objectdetector();

参数#

范围

类型

描述

port

triport::port &

The 3-Wire Port that the Object Sensor is connected to, written as Brain.ThreeWirePort.X or ExpanderName.X, where X is the port letter (for example, Brain.ThreeWirePort.A or Expander1.A).

bReverse

bool

An optional boolean to reverse the direction of the Object Sensor:

  • false (default) — Do not reverse the direction
  • true — Reverse the direction

示例#

// Create an objectdetector instance in Port A
objectdetector ObjectSensorA = objectdetector(Brain.ThreeWirePort.A, true);

成员功能#

The objectdetector class includes the following member functions:

  • reflectivity — Returns the reflectivity detected by the Object Sensor.

  • isObjectDetected — Returns whether the Object Sensor detects an object.

  • setDetectionThreshold — Sets the detection range of the Object Sensor.

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

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

Before calling any objectdetector member functions, a objectdetector instance must be created, as shown below:

// Create an objectdetector instance in Port A
objectdetector ObjectSensorA = objectdetector(Brain.ThreeWirePort.A, true);

reflectivity#

返回物体传感器测量的反射率。

Available Functions
int32_t reflectivity( 
  percentUnits units = percentUnits::pct );

Parameters

参数

类型

描述

units

percentUnits

The unit that represents the reflectivity:

  • percent / pct — percent

Return Values

Returns an int32_t representing the reflectivity measured by the Object Detector as a percent in the range 0 - 100%.

Examples
// Display the detected reflectivity on the Brain's screen
int32_t value = ObjectSensorA.reflectivity();
Brain.Screen.print(value);

isObjectDetected#

返回对象检测器当前是否检测到某个对象。

Available Functions
bool isObjectDetected();

Parameters

此函数不接受任何参数。

Return Values

返回一个布尔值,指示是否检测到对象:

  • true — An object is detected.
  • false — No object is detected.

setDetectionThreshold#

将目标检测器的检测阈值设置为特定值。

Available Functions
void setDetectionThreshold( 
  int32_t      threshold, 
  percentUnits units = percentUnits::pct );

Parameters

参数

类型

描述

threshold

int32_t

目标检测的反射率阈值,范围为 0 - 100。

units

percentUnits

The unit that represents the new threshold:

  • percent / pct — percent

Return Values

此函数不返回值。

Examples
// Set the reflectivity threshold to 50%
ObjectSensorA.setDetectionThreshold(50);

objectDetected#

注册一个回调函数,用于在检测到对象时执行该回调函数。

Available Functions
void objectDetected(void (* callback)(void));

Parameters

范围

类型

描述

callback

void (*)(void)

检测到对象时要调用的回调函数。

Return Values

此函数不返回值。

Examples
void detected() {
  Brain.Screen.print("object detected");
}

// Run detected when the Object Detector detects an object
ObjectSensorA.objectDetected(detected);

objectLost#

注册一个对象丢失时的回调函数。

Available Functions
void objectLost(void (* callback)(void));

Parameters

范围

类型

描述

callback

void (*)(void)

对象丢失时要调用的回调函数。

Return Values

此函数不返回值。

Examples
void lost() {
  Brain.Screen.print("object lost");
}

// Run lost when the Object Detector loses an object
ObjectSensorA.objectLost(lost);