距离传感器#
介绍#
V5距离传感器可以测量物体与机器人之间的距离,估算物体的大小,并检测物体靠近或远离机器人的速度。其工作距离范围为20毫米至2000毫米。
当物体距离传感器小于 200 毫米时,传感器的精度约为 ±15 毫米;当物体距离传感器大于 200 毫米时,精度约为 ±5%。
距离传感器采用 1 类激光,可提供精确的直线探测路径,完全适用于教室环境。

This page uses distance_sensor as the example Distance Sensor name. Replace it with your own configured name as needed.
以下是可用方法列表:
object_distance– Returns the distance from the sensor to the nearest object.object_velocity– Returns the relative speed of an object traveling toward or away from the Distance Sensor.object_size– Returns whether the detected object is of a certain size.is_object_detected– Returns whether the Distance Sensor is detecting an object.changed– Registers a function to be called whenever the Distance Sensor’s value changes.
构造函数 - 手动初始化距离传感器。
Distance– Creates a Distance Sensor.
物体距离#
object_distance returns the distance from the sensor to the nearest object as a float.
Usage:
distance_sensor.object_distance(units)
参数 |
描述 |
|---|---|
|
Optional. The unit to represent the distance:
|
# Drive forward until the object is within 50 mm away
drivetrain.drive(FORWARD)
while not distance_sensor.object_distance < 50:
pass
drivetrain.stop()
物体速度#
object_velocity returns the relative speed of an object traveling toward or away from the Distance Sensor in m/s (meters per second).
Usage:
distance_sensor.object_velocity()
参数 |
描述 |
|---|---|
此方法没有参数。 |
对象大小#
object_size returns the size of a detected object:
ObjectSizeType(0, “NONE”)– No object is detectedObjectSizeType(1, “SMALL”)ObjectSizeType(2, “MEDIUM”)ObjectSizeType(3, “LARGE”)
大小取决于距离传感器视野中被检测到的物体所占的比例。
Usage:
distance_sensor.object_size()
参数 |
描述 |
|---|---|
此方法没有参数。 |
检测到物体#
is_object_detected returns whether the Distance Sensor is detecting an object.
True– The Distance Sensor is detecting an object.False– The Distance Sensor is not detecting an object.
Usage:
distance_sensor.is_object_detected()
参数 |
描述 |
|---|---|
此方法没有参数。 |
# Drive forward until the object is within 50 mm away
while True:
drivetrain.drive(FORWARD)
if distance_sensor.is_object_detected():
if distance_sensor.object_distance < 50:
drivetrain.stop()
break
已更改#
changed registers a function to be called whenever the Distance Sensor’s value changes.
Usage:
distance_sensor.changed(callback, arg)
参数 |
描述 |
|---|---|
|
先前定义的 函数,当距离传感器的值发生变化时执行。 |
|
可选。包含要传递给回调函数的参数的元组。有关更多信息,请参阅使用带参数的函数。 |
def my_function():
brain.screen.print("Distance changed")
# Call my_function whenever distance_sensor's value changes
distance_sensor.changed(my_function)
构造函数#
Constructors are used to manually create Distance objects, which are necessary for configuring a Distance Sensor outside of VEXcode.
Distance#
Distance creates a Distance Sensor.
Usage:
Distance(smartport)
范围 |
描述 |
|---|---|
|
The Smart Port that the Distance Sensor is connected to, written as |
# Create a Distance Sensor in Port 10
distance_sensor = Distance(Ports.PORT10)