距离#

初始化距离类#

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

距离(端口)

此构造函数使用一个参数:

范围

描述

端口

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

# Construct a Distance Sensor "distance" with the
# Distance class.
distance = Distance(Ports.PORT1)

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

类方法#

object_distance()#

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

参数

描述

单位

**可选。**有效的 DistanceUnits 类型。默认单位为 MM

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

# Get the distance detected by the Distance Sensor in mm.
value = distance.object_distance()

# Get the distance detected by the Distance Sensor in inches.
value = distance.object_distance(INCHES)

object_size()#

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

**返回:**检测到的物体的估计大小。

# Get the object size detected by the Distance Sensor.
size = distance.object_size()

object_velocity()#

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

**返回:**检测到的物体的速度(以米/秒为单位)。

is_object_detected()#

is_object_detected() 方法检查是否检测到物体。

返回: 如果检测到物体,则返回 True。如果未检测到物体,则返回 False

changed()#

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

参数

描述

打回来

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

arg

**可选。**用于向回调函数传递参数的元组。

**返回:**事件类的一个实例。

# Define function distance_changed().
def distance_changed():
    # The Brain will print that the distance changed on
    # the Brain's screen.
    brain.screen.print("distance changed ")
# Run distance_changed() when the value detected by the 
# Distance Sensor changes.
distance.changed(distance_changed)

object_rawsize()#

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

**返回:**检测到的物体的原始尺寸。

# Get the raw size of the object detected by the
# Distance Sensor.
size = distance.object_rawsize()

installed()#

installed() 方法检查距离传感器是否已连接。

返回: 如果距离传感器已连接,则返回 True。如果未连接,则返回 False

timestamp()#

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

**返回:**最后接收的状态包的时间戳(以毫秒为单位)。