物体传感器#
介绍#
objectdetector 类用于测量反射率并通过物体传感器检测物体。
类构造函数#
objectdetector(
triport::port &port,
bool bReverse = false );
类析构函数#
销毁 objectdetector 对象并释放相关资源。
virtual ~objectdetector();
参数#
范围 |
类型 |
描述 |
|---|---|---|
|
|
物体传感器连接的 3 线端口,写成 |
|
|
可选布尔值,用于反转物体传感器的方向:
|
示例#
// 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 Functionsint32_t reflectivity(
percentUnits units = percentUnits::pct );
参数 |
类型 |
描述 |
|---|---|---|
|
|
反射率单位:
|
返回一个 int32_t 表示物体检测器测量的反射率,以百分比表示,范围为 0 - 100%。
// Display the detected reflectivity on the Brain's screen
int32_t value = ObjectSensorA.reflectivity();
Brain.Screen.print(value);
isObjectDetected#
返回对象检测器当前是否检测到某个对象。
Available Functionsbool isObjectDetected();
此函数不接受任何参数。
Return Values返回一个布尔值,指示是否检测到对象:
-
true— An object is detected. -
false— No object is detected.
setDetectionThreshold#
将目标检测器的检测阈值设置为特定值。
Available Functionsvoid setDetectionThreshold(
int32_t threshold,
percentUnits units = percentUnits::pct );
参数 |
类型 |
描述 |
|---|---|---|
|
|
目标检测的反射率阈值,范围为 0 - 100。 |
|
|
代表新阈值的单位:
|
此函数不返回值。
Examples// Set the reflectivity threshold to 50%
ObjectSensorA.setDetectionThreshold(50);
objectDetected#
注册一个回调函数,用于在检测到对象时执行该回调函数。
Available Functionsvoid objectDetected(void (* callback)(void));
范围 |
类型 |
描述 |
|---|---|---|
|
|
检测到对象时要调用的回调函数。 |
此函数不返回值。
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 Functionsvoid objectLost(void (* callback)(void));
范围 |
类型 |
描述 |
|---|---|---|
|
|
对象丢失时要调用的回调函数。 |
此函数不返回值。
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); }