物体传感器#

介绍#

objectdetector 类用于测量反射率并通过物体传感器检测物体。

类构造函数#

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

类析构函数#

销毁 objectdetector 对象并释放相关资源。

virtual ~objectdetector();

参数#

范围

类型

描述

port

triport::port &

物体传感器连接的 3 线端口,写成 Brain.ThreeWirePort.X 或 ExpanderName.X,其中 X 是端口字母(例如, Brain.ThreeWirePort.A 或 Expander1.A)。

bReverse

bool

可选布尔值,用于反转物体传感器的方向:

  • false (默认值)— 不反转方向
  • true — 反转方向

示例#

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

成员功能#

objectdetector 类包含以下成员函数:

  • reflectivity — 返回物体传感器检测到的反射率。

  • isObjectDetected — 返回对象传感器是否检测到对象。

  • setDetectionThreshold — 设置物体传感器的检测范围。

  • objectDetected — 注册一个当物体传感器检测到物体时要调用的函数。

  • objectLost — 注册一个当物体传感器丢失物体时要调用的函数。

在调用任何 objectdetector 函数之前,必须先创建 objectdetector 实例,如下所示:

// 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

反射率单位:

  • percent 百分比 pct 百分比

Return Values

返回一个 int32_t 表示物体检测器测量的反射率,以百分比表示,范围为 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

代表新阈值的单位:

  • percent pct — 百分比

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

定义回调函数(在 int main()之外)

// Display a message when an object is detected.
void detected() {
  Brain.Screen.print("object detected");
}

在 int main()中注册回调函数

int main() {
  // 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

定义回调函数(在 int main()之外)

// Display a message when an object is lost.
void lost() {
  Brain.Screen.print("object lost");
}

在 int main()中注册回调函数

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