物体探测器#
初始化 objectdetector 类#
使用以下构造函数创建对象传感器:
objectdetector
构造函数在指定的三线端口中创建一个 objectdetector 对象,并将反向标志设置为指定值。
范围 |
描述 |
---|---|
|
|
|
设置此项可反转检测逻辑。默认值为“false”。 |
必须先创建 Brain 或 3-Wire Expander,然后才能使用 objectdetector 类构造函数创建对象。
// Create the Brain.
brain Brain;
// Construct an ObjectSensor "objdet" with the
// objectdetector class.
objectdetector objdet = objectdetector(Brain.ThreeWirePort.A, true);
当引用 objectdetector 类方法时,此 objdet
对象将在整个 API 文档的所有后续示例中使用。
类方法#
反射率()#
reflectivity(units)
方法返回物体传感器测量的反射率。该反射率是基于传感器原始值的估算值。
反射率为 0% 时,原始值等于或大于 3000。反射率为 100% 时,原始值等于 0。
参数 |
描述 |
---|---|
单位 |
反射率的唯一有效单位是“百分比”。 |
**返回:**一个整数,表示物体传感器测量的反射率,范围在 0% - 100% 之间。
// Get Object Sensor reflectivity in range of 0% - 100%.
int value = ObjectDetectorA.reflectivity();
// Print the reflectivity to the Brain's screen.
Brain.Screen.print(value);
设置检测阈值()#
setDetectionThreshold(value, units)
方法将对象传感器的检测阈值设置为特定值。
参数 |
描述 |
---|---|
价值 |
物体检测的反射率阈值。范围为 0 - 100。 |
单位 |
反射率的唯一有效单位是“百分比”。 |
**返回:**无。
// Set the reflectivity threhold to 50%.
objdet.setDetectionThreshold(50);
已检测到对象()#
如果检测到物体,则 isObjectDetected()
方法返回。
**返回:**如果检测到物体则返回 true
。如果没有检测到物体则返回 false
。
已更改()#
changed(callback)
方法注册一个回调函数,用于当对象传感器的值发生变化时执行的操作。
参数 |
描述 |
---|---|
打回来 |
当对象传感器的值发生变化时调用的回调函数。 |
**返回:**无。
// Define the detectorChanged function with a void return
// type, showing it doesn't return a value.
void detectorChanged() {
// The Brain will print that the value of the Object Sensor
// changed on the Brain's screen.
Brain.Screen.print("Object Sensor value changed");
}
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Run detectorChanged when the value of the Object Sensor changes.
objdet.changed(detectorChanged);
}
检测到物体()#
objectDetected(callback)
方法注册一个在检测到对象时回调的函数。
参数 |
描述 |
---|---|
打回来 |
当检测到物体时调用的回调函数。 |
**返回:**无。
// Define the detected function with a void return type,
// showing it doesn't return a value.
void detected() {
// The Brain will print that the Object Sensor detected
// an object to the Brain's screen.
Brain.Screen.print("object detected");
}
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Run detected when the Object Sensor detects an object.
objdet.objectDetected(detected);
}
对象丢失()#
objectLost(callback)
方法注册一个当对象丢失时的回调函数。
这是一种非等待方法,允许下一个方法无延迟运行。
参数 |
描述 |
---|---|
打回来 |
当对象丢失时调用的回调函数。 |
**返回:**无。
// Define the lost function with a void return type,
// showing it doesn't return a value.
void lost() {
// The Brain will print that the Object Sensor
// lost an object to the Brain's screen.
Brain.Screen.print("object lost");
}
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Run lost when the Object Sensor loses an object.
objdet.objectLost(lost);
}