物体传感器#
介绍#
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();
参数#
范围 |
类型 |
描述 |
|---|---|---|
|
|
The 3-Wire Port that the Object Sensor is connected to, written as |
|
|
An optional boolean to reverse the direction of the Object Sensor:
|
示例#
// 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 Functionsint32_t reflectivity(
percentUnits units = percentUnits::pct );
参数 |
类型 |
描述 |
|---|---|---|
|
|
The unit that represents the reflectivity:
|
Returns an int32_t representing the reflectivity measured by the Object Detector as a percent in the range 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。 |
|
|
The unit that represents the new threshold:
|
此函数不返回值。
Examples// Set the reflectivity threshold to 50%
ObjectSensorA.setDetectionThreshold(50);
objectDetected#
注册一个回调函数,用于在检测到对象时执行该回调函数。
Available Functionsvoid objectDetected(void (* callback)(void));
范围 |
类型 |
描述 |
|---|---|---|
|
|
检测到对象时要调用的回调函数。 |
此函数不返回值。
Examplesvoid detected() {
Brain.Screen.print("object detected");
}
// Run detected when the Object Detector detects an object
ObjectSensorA.objectDetected(detected);
objectLost#
注册一个对象丢失时的回调函数。
Available Functionsvoid objectLost(void (* callback)(void));
范围 |
类型 |
描述 |
|---|---|---|
|
|
对象丢失时要调用的回调函数。 |
此函数不返回值。
Examplesvoid lost() {
Brain.Screen.print("object lost");
}
// Run lost when the Object Detector loses an object
ObjectSensorA.objectLost(lost);