距离#

初始化距离类#

使用以下构造函数创建距离传感器:

distance 构造函数在指定端口创建距离类的对象。

范围

描述

端口

距离传感器连接到的有效 智能端口

// Construct a Distance Sensor "Distance" with the
// distance class.
distance Distance1 = distance(PORT1);

当引用距离类方法时,此“Distance1”对象将在整个 API 文档的所有后续示例中使用。

类方法#

物体距离()#

objectDistance(units) 方法返回距离传感器当前读取的距离。如果未检测到任何物体,距离传感器将返回一个较大的正数。

参数

描述

单位

有效的 distanceUnit

**返回:**以指定单位表示到检测到的物体的距离的双精度数。

// Get the distance detected by the Distance Sensor in inches.
double value = Distance1.objectDistance(inches);

对象大小()#

objectSize() 方法返回检测到的物体的大小估计值。

返回: sizeType 表示检测到的物体的估计大小。

// Get the object size detected by the Distance Sensor.
sizeType size = Distance1.objectSize();

对象速度()#

objectVelocity() 方法返回检测到的物体的速度。速度是通过距离随时间变化计算得出的。

返回: 以米/秒为单位表示检测到的物体的速度的双精度数。

已检测到对象()#

如果检测到物体,则 isObjectDetected() 方法返回。

**返回:**如果检测到物体则返回 true。如果未检测到物体则返回 false

已更改()#

changed(callback) 方法注册一个回调函数,用于当检测到的对象发生变化时。

参数

描述

打回来

当检测到的对象发生变化时将调用的函数。

**返回:**无。

// Define the distanceChanged function with a void return
// type, showing it doesn't return a value.
void distanceChanged() {
  // The Brain will print that the distance changed on the
  // Brain's screen.
  Brain.Screen.print("distance changed");
}

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

  // Drive the robot forward.
  Drivetrain.drive(forward);

  // Run distanceChanged when the value of the distance 
  // detected by the Distance Sensor changes.
  Distance1.changed(distanceChanged);
}

对象原始大小()#

objectRawSize() 方法返回距离传感器正在检测的物体尺寸的原始值。

**返回:**一个 32 位有符号整数,表示检测到的对象的原始大小。

// Get the raw size of the object detected by the distance sensor.
int32_t size = Distance1.objectRawSize();

时间戳()#

timestamp() 方法请求距离传感器最后接收到的状态包的时间戳。

**返回:**最后一个状态包的时间戳,以毫秒为单位的无符号 32 位整数。

安装()#

如果距离传感器已连接,则 installed() 方法返回。

**返回:**如果距离传感器已连接,则返回“true”。如果未连接,则返回“false”。