距离#

初始化距离类#

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

Distance(port)

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

范围

描述

port

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

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

This distance object will be used in all subsequent examples throughout this API documentation when referring to Distance class methods.

类方法#

object_distance()#

The object_distance(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.

参数

描述

单位

Optional. A valid DistanceUnits type. The default unit is 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()#

The object_size() method returns an estimate of the size of the detected object.

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

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

object_velocity()#

The object_velocity() method returns the velocity of the detected object. The velocity is calculated by the change in distance over the change in time.

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

is_object_detected()#

The is_object_detected() method checks if an object is detected.

Returns: True if an object is detected. False if an object is not detected.

changed()#

The changed(callback, arg) method registers a callback function for when the detected object changes.

参数

描述

打回来

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

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()#

The object_rawsize() method returns the raw value of object size the sensor is detecting.

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

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

installed()#

The installed() method checks if the Distance Sensor is connected.

Returns: True if the Distance Sensor is connected. False if it is not.

timestamp()#

The timestamp() method returns the timestamp of the last received status packet from the Distance Sensor.

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