距离#
初始化距离类#
使用以下构造函数创建距离传感器:
The distance
constructor creates an object of the distance Class in the specified port.
范围 |
描述 |
---|---|
|
距离传感器连接到的有效 智能端口。 |
// Construct a Distance Sensor "Distance" with the
// distance class.
distance Distance1 = distance(PORT1);
This Distance1
object will be used in all subsequent examples throughout this API documentation when referring to distance class methods.
类方法#
objectDistance()#
The objectDistance(units)
method returns the distance the Distance Sensor is currently reading. The Distance Sensor will return a large positive number if no object is detected.
参数 |
描述 |
---|---|
单位 |
有效的 distanceUnit。 |
**返回:**以指定单位表示到检测到的物体的距离的双精度数。
// Get the distance detected by the Distance Sensor in inches.
double value = Distance1.objectDistance(inches);
objectSize()#
The objectSize()
method returns an estimate of the size of the detected object.
返回: sizeType 表示检测到的物体的估计大小。
// Get the object size detected by the Distance Sensor.
sizeType size = Distance1.objectSize();
objectVelocity()#
The objectVelocity()
method returns the velocity of the detected object. The velocity is calculated by the change in distance over the change in time.
返回: 以米/秒为单位表示检测到的物体的速度的双精度数。
isObjectDetected()#
The isObjectDetected()
method returns if an object is detected.
Returns: true
if an object is detected. false
if an object is not detected.
changed()#
The changed(callback)
method registers a callback function for when the detected object changes.
参数 |
描述 |
---|---|
打回来 |
当检测到的对象发生变化时将调用的函数。 |
**返回:**无。
// 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()#
The objectRawSize()
method returns the raw value of object size the Distance Sensor is detecting.
**返回:**一个 32 位有符号整数,表示检测到的对象的原始大小。
// Get the raw size of the object detected by the distance sensor.
int32_t size = Distance1.objectRawSize();
timestamp()#
The timestamp()
method requests the timestamp of the last received status packet from the Distance Sensor.
**返回:**最后一个状态包的时间戳,以毫秒为单位的无符号 32 位整数。
installed()#
The installed()
method returns if the Distance Sensor is connected.
Returns: true
if the Distance Sensor is connected. false
if it is not.