距离#

介绍#

VEX IQ(第二代)距离传感器提供了与 VEX IQ(第二代)传感器交互的方法。这些方法包括跟踪距离和速度。

This page uses Distance1 as the example Distance Sensor name. Replace it with your own configured name as needed.

以下是所有可用方法的列表:

Getters – 从距离传感器返回对象数据。

  • foundObject – (1st Gen) Returns whether an object is within range.

  • distance – (1st Gen) Returns the distance to the nearest object.

  • objectDistance – (2nd Gen) Returns the distance to the nearest object.

  • objectVelocity – (2nd Gen) Returns the speed of a detected object.

  • objectSize – (2nd Gen) Returns the estimated size of the detected object.

  • isObjectDetected – (2nd Gen) Returns whether an object is within range.

  • installed – Returns whether the Distance Sensor is connected to the Brain.

构造函数——手动初始化距离传感器。

  • sonar – Create a (1st Gen) Distance Sensor.

  • distance – Create a (2nd Gen) Distance Sensor.

吸气剂#

foundObject#

foundObject returns a Boolean indicating whether or not the Distance Sensor detects an object.

  • 1 – The Distance Sensor detects an object.

  • 0 – The Distance Sensor does not detect an object.

**注意:**这是一种(第一代)距离传感器方法。

Usage:
Distance1.foundObject()

参数

描述

该方法没有参数。

distance#

distance returns the current distance from the Distance Sensor and a detected object as a double.

**注意:**这是一种(第一代)距离传感器方法。

Usage:
Distance1.distance(units)

参数

描述

units

The unit that represents the distance:

  • mm – millimeters
  • inches
  • distanceUnits::cm – centimeters

objectDistance#

objectDistance returns the distance between the Distance Sensor and the nearest object as a double.

**注意:**这是一种(第二代)距离传感器方法。

Usage:
Distance1.objectDistance(units)

参数

描述

units

The unit that represents the distance:

  • mm – millimeters
  • inches
  • distanceUnits::cm – centimeters
int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Drive forward until an object is 200 mm away
  while (Distance1.objectDistance(mm) > 200) {
    Drivetrain.drive(forward);
  }

  Drivetrain.stop();
  Brain.Screen.print("200 mm reached");
}

objectVelocity#

objectVelocity returns the velocity of a detected object measured in meters per second as a double. A positive value indicates the object is moving toward the Distance Sensor, while a negative value indicates it is moving away.

**注意:**这是一种(第二代)距离传感器方法。

Usage:
Distance1.objectVelocity()

参数

描述

该方法没有参数。

objectSize#

objectSize returns an enumerated value (enum) representing the estimated size of the detected object based on how much of the Distance Sensor (2nd gen)’s field of view it occupies:

直接打印时,它会显示与该尺寸相关的数值:

尺寸

数值

none

0

small

1

medium

2

large

3

**注意:**这是一种(第二代)距离传感器方法。

Usage:
Distance1.objectSize()

参数

描述

该方法没有参数。

isObjectDetected#

isObjectDetected returns a Boolean indicating whether the Distance Sensor detects an object.

  • 1 – The Distance Sensor detects an object.

  • 0 – The Distance Sensor does not detect an object.

**注意:**这是一种(第二代)距离传感器方法。

Usage:
Distance1.isObjectDetected()

参数

描述

该方法没有参数。

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Turn until an object is detected
  Drivetrain.turn(right);
  while (!Distance1.isObjectDetected()) {
    wait(15, msec);
  }
  Drivetrain.stop();
  Brain.Screen.print("Object detected!");
}

installed#

installed returns if the Distance Sensor is connected.

  • 1 – The Distance Sensor is connected to the Brain.

  • 0 – The Distance Sensor is not connected to the Brain.

Usage:
Distance1.installed()

参数

描述

该方法没有参数。

构造函数#

sonar#

sonar creates an object of the sonar Class in the specified port.

Usage:
sonar Distance1 = sonar(smartport);

范围

描述

smartport

The Smart Port that the Distance Sensor is connected to, written as PORTx where x is the number of the port.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Create a 1st Gen Distance Sensor in Port 1
  sonar mySonar = sonar(PORT1);
}

distance#

distance creates an object of the distance Class in the specified port.

Usage:
distance Distance1 = distance(smartport);

范围

描述

smartport

The Smart Port that the Distance Sensor is connected to, written as PORTx where x is the number of the port.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Create a 2nd Gen Distance Sensor in Port 1
  distance myDistance = distance(PORT1);
}