距离#
介绍#
VEX IQ(第二代)的距离传感器 API 提供了从距离传感器返回数据的方法。这使得传感器能够检测前方物体的距离和大小。
以下是所有可用方法的列表:
(第一代)距离传感器 - 从(第一代)距离传感器返回数据。
foundObject – 返回某个对象是否在范围内。
距离 – 以英寸、毫米或厘米为单位返回到最近物体的距离。
installed – 返回距离传感器是否连接到大脑。
(第二代)距离传感器 - 从(第二代)距离传感器返回数据。
objectDistance – 以英寸、毫米或厘米为单位返回到最近物体的距离。
objectVelocity – 返回检测到的物体的速度(以米/秒为单位)。
objectSize – 返回检测到的物体的估计大小(小、中、大)。
isObjectDetected – 返回物体是否在范围内(约 406 毫米或 16 英寸)。
objectRawSize – 返回距离传感器检测到的原始值。
installed – 返回距离传感器是否连接到大脑。
构造函数——手动初始化距离传感器。
(第一代)距离传感器#
找到对象#
foundObject
检查测距仪是否在 0 - 1000 毫米范围内检测到物体。如果检测到的物体距离小于 1000 毫米,测距仪将返回 true。
用法:
Distance1.foundObject()
参数 |
描述 |
---|---|
该方法没有参数。 |
// Example coming soon
距离#
“distance” 返回测距仪当前探测到的物体距离。如果在探测范围内未探测到任何物体,测距仪将返回一个较大的正数。
用法:
Distance1.distance(units)
参数 |
描述 |
---|---|
|
表示距离的单位:
|
// Example coming soon
已安装#
如果距离传感器已连接,则返回“installed”。
用法:
installed()
参数 |
描述 |
---|---|
该方法没有参数。 |
// Display if the Distance Sensor is installed
if (Distance1.installed()){
Brain.Screen.print("Installed");
}
(第二代)距离传感器#
物体距离#
objectDistance
返回距离传感器和最近物体之间的距离。
用法:
objectDistance(units)
参数 |
描述 |
---|---|
|
表示距离的单位:
|
Drivetrain.drive(forward);
// Stop driving once close to an object
while (true){
if (Distance1.objectDistance(mm) < 50){
Brain.Screen.print("Too close!");
break;
}
}
Drivetrain.stop();
物体速度#
objectVelocity
以浮点数形式返回检测到的物体的速度,以米/秒为单位。正值表示物体正朝着距离传感器(第二代)移动,负值表示物体正远离。
**注意:**此方法仅适用于距离传感器(第二代)。
用法:
objectVelocity()
参数 |
描述 |
---|---|
该方法没有参数。 |
// Display the velocity of objects moving in front of the sensor
while (true){
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
Brain.Screen.print("Velocity: %f", Distance1.objectVelocity());
wait(.25, seconds);
}
对象大小#
objectSize
返回一个 sizeType
,表示根据距离传感器(第二代)的视野所占面积估算出的检测到的物体的大小:
sizeType – 枚举值 |
---|
<ul><li>`无` – 0 </li><li>`小` – 1 </li><li>`中` – 2 </li><li>`大` – 3 </li><ul> |
**注意:**此方法仅适用于距离传感器(第二代)。
用法:
objectSize()
参数 |
描述 |
---|---|
该方法没有参数。 |
Drivetrain.drive(forward);
while (true){
vex::sizeType size = Distance1.objectSize();
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
// Stop if the object size becomes large
if (size == sizeType::small){
Brain.Screen.print("Small Object");
}
else if (size == sizeType::medium){
Brain.Screen.print("Medium Object");
}
else if (size == sizeType::large){
Brain.Screen.print("Large Object");
Drivetrain.stop();
break;
}
else{
Brain.Screen.print("No Object");
}
wait(0.5, seconds);
}
检测到物体#
isObjectDetected
返回一个布尔值,指示距离传感器是否检测到约 406 毫米(16 英寸)范围内的物体。
1
- 距离传感器检测到约 406 毫米范围内的物体。0
- 距离传感器未检测到约 406 毫米的物体。
用法:
isObjectDetected
参数 |
描述 |
---|---|
该方法没有参数。 |
// Turn until an object is detected
Drivetrain.turn(right);
while (!Distance1.isObjectDetected()){
wait(15, msec);
}
Drivetrain.stop();
Brain.Screen.print("Detected!");
对象原始大小#
objectRawSize
以 0 - 400 之间的有符号 32 位整数形式返回距离传感器正在检测的物体大小的原始值。此整数表示相对物体大小。
用法:
objectRawSize()
参数 |
描述 |
---|---|
该方法没有参数。 |
Drivetrain.setTurnVelocity(15, percent);
Drivetrain.turn(right);
// Display the raw size of objects around the robot
while (true){
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
Brain.Screen.print("%d", Distance1.objectRawSize());
wait(50,msec);
}
已安装#
如果距离传感器已连接,则返回“installed”。
用法:
installed()
参数 |
描述 |
---|---|
该方法没有参数。 |
// Display if the Distance Sensor is installed
if (Distance1.installed()){
Brain.Screen.print("Installed");
}
构造函数#
声纳#
sonar
在指定端口创建一个 sonar 类的对象。
用法:
sonar(port)
范围 |
描述 |
---|---|
|
颜色传感器连接到哪个智能端口,以“PORT”表示,后跟端口号,范围从 1 到 12。 |
// Construct a Distance Sensor "Distance1" with the
// distance class.
distance Distance1 = sonar(PORT1);
int main(){
vexcodeInit();
Drivetrain.drive(forward);
// Stop driving once close to an object
while (true){
if (Distance1.objectDistance(mm) < 50){
Brain.Screen.print("Too close!");
break;
}
}
Drivetrain.stop();
}
距离#
distance
在指定端口创建距离类的对象。
用法:
距离(端口)
范围 |
描述 |
---|---|
|
颜色传感器连接到哪个智能端口,以“PORT”表示,后跟端口号,范围从 1 到 12。 |
// Construct a Distance Sensor "Distance1" with the
// distance class.
distance Distance1 = distance(PORT1);
int main(){
vexcodeInit();
Drivetrain.drive(forward);
// Stop driving once close to an object
while (true){
if (Distance1.objectDistance(mm) < 50){
Brain.Screen.print("Too close!");
break;
}
}
Drivetrain.stop();
}