物体探测器#
初始化 objectdetector 类#
使用以下构造函数创建对象传感器:
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);
}