目标检测器#
要使 objectdetector 命令在 VEXcode V5 中可用,必须在“设备”窗口中配置对象传感器。
更多信息,请参阅以下文章:
初始化目标检测器类#
对象传感器是通过使用以下构造函数创建的:
The objectdetector constructor creates an objectdetector object in the specified Three Wire Port with the reverse flag set to the specified value.
范围 |
描述 |
|---|---|
|
The 3-Wire Port that the Object Sensor is connected to, whether it’s a port on the |
|
Set this to reverse the detection logic. The default is |
A Brain or 3-Wire Expander must be created first before they can be used to create an object with the objectdetector Class constructor.
// Create the Brain.
brain Brain;
// Construct an ObjectSensor "objdet" with the
// objectdetector class.
objectdetector objdet = objectdetector(Brain.ThreeWirePort.A, true);
This objdet object will be used in all subsequent examples throughout this API documentation when referring to objectdetector class methods.
类方法#
reflectivity()#
The reflectivity(units) method returns the reflectivity measured by the Object Sensor. The reflectivity is an estimation based on the raw value of the sensor.
反射率为 0% 时,原始值为 3000 或更高。反射率为 100% 时,原始值为 0。
参数 |
描述 |
|---|---|
|
The only valid unit for reflectivity is |
返回值: 一个整数,表示物体传感器测量的反射率,范围为 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()#
The setDetectionThreshold(value, units) method sets the detection threshold for the Object Sensor to a specific value.
参数 |
描述 |
|---|---|
|
用于目标检测的反射率阈值,范围为0-100。 |
|
The only valid unit for reflectivity is |
**返回值:**无。
// Set the reflectivity threhold to 50%.
objdet.setDetectionThreshold(50);
isObjectDetected()#
The isObjectDetected() method returns if an object is detected.
Returns: true if an object is detected. false if one is not.
changed()#
The changed(callback) method registers a callback function for when the value of an Object Sensor changes.
参数 |
描述 |
|---|---|
|
当对象传感器的值发生变化时要调用的回调函数。 |
**返回值:**事件类的一个实例。
// 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()#
The objectDetected(callback) method registers a callback function for when an object is detected.
参数 |
描述 |
|---|---|
|
检测到对象时要调用的回调函数。 |
**返回值:**事件类的一个实例。
// 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()#
The objectLost(callback) method registers a callback function for when an object is lost.
这是一个非等待方法,允许下一个方法立即运行。
参数 |
描述 |
|---|---|
|
对象丢失时要调用的回调函数。 |
**返回值:**事件类的一个实例。
// 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);
}