距离#

介绍#

距离传感器可以检测物体是否在传感器前方,并返回该物体与传感器的距离。

第一代VEX IQ距离传感器使用声呐探测物体并测量距离。第二代VEX IQ距离传感器采用教室安全激光,还能返回被探测物体的相对速度,并估算物体的大小(小、中、大)。

This page uses distance_1 as the example Distance Sensor name. Replace it with your own configured name as needed.

以下是所有可用方法的列表:

获取器 — 返回距离传感器的数据。

  • is_object_detected — (VEX IQ Distance Sensor (1st gen) or (2nd gen)) Returns whether the Distance Sensor currently detects an object.

  • distance — (VEX IQ Distance Sensor (1st gen)) Returns the distance between the Distance Sensor and the nearest detected object.

  • object_distance — (VEX IQ Distance Sensor (2nd gen)) Returns the distance between the Distance Sensor and the nearest detected object.

  • object_velocity — (VEX IQ Distance Sensor (2nd gen)) Returns how quickly the detected object is moving toward or away from the Distance Sensor.

  • object_size — (VEX IQ Distance Sensor (2nd gen)) Returns the estimated size of the detected object.

  • installed — (VEX IQ Distance Sensor (1st gen) or (2nd gen)) Returns whether the Distance Sensor is installed and connected.

构造函数——手动初始化和配置距离传感器。

  • Sonar — Creates a VEX IQ Distance Sensor (1st gen).

  • Distance — Creates a VEX IQ Distance Sensor (2nd gen).

吸气剂#

is_object_detected#

is_object_detected returns whether the Distance Sensor currently detects an object within range.

  • True — The Distance Sensor detects an object.

  • False — The Distance Sensor does not detect an object.

Usage:
distance_1.is_object_detected()

参数

描述

该方法没有参数。

# Turn until an object is detected
drivetrain.turn(RIGHT)
while not distance_1.is_object_detected():
    wait(15, MSEC)
drivetrain.stop()
brain.screen.print("Object detected!")

distance#

distance returns the distance between the Distance Sensor and the nearest detected object as a decimal (float).

注意: 这是 VEX IQ 距离传感器(第一代)的测量方法。

Usage:
distance_1.distance(units)

参数

描述

units

Optional. The distance unit: MM (default, 24 mm to 1000 mm) or INCHES (1 inch to 40 inches).

# Display the distance to an object
if distance_1.is_object_detected():
    print("Distance: ")
    print(distance_1.distance(MM))
else:
    print("No object detected")

object_distance#

object_distance returns the distance between the Distance Sensor and the nearest detected object as a decimal (float).

注意: 这是 VEX IQ 距离传感器(第二代)的测量方法。

Usage:
distance_1.object_distance(units)

参数

描述

units

Optional. The distance unit: MM (default, 20 mm to 2000 mm), INCHES (0.78 inches to 78 inches), or DistanceUnits.CM (2 cm to 200 cm).

# Drive forward until an object is 200 mm away
while distance_1.object_distance(MM) > 200:
    drivetrain.drive(FORWARD)
drivetrain.stop()

object_velocity#

object_velocity returns the relative velocity of the detected object as a decimal (float) in m/s (meters per second).

速度表示物体朝向或远离距离传感器的移动速度。正值表示物体正朝向距离传感器移动。负值表示物体正远离距离传感器。接近 0 的值表示物体相对于传感器的移动速度很慢。

注意: 这是 VEX IQ 距离传感器(第二代)的测量方法。

Usage:
distance_1.object_velocity()

参数

描述

该方法没有参数。

# Display the velocity of a moving object
while True:
    brain.screen.clear_screen()
    brain.screen.set_cursor(1, 1)
    brain.screen.print("Velocity: ")
    brain.screen.print(distance_1.object_velocity())
    wait(100, MSEC)

object_size#

object_size returns the estimated size of the object detected by the Distance Sensor.

距离传感器根据被检测到的物体在传感器视野中所占的比例来估计物体的大小。

  • ObjectSizeType.NONE — No object is detected.

  • ObjectSizeType.SMALL — A small object is detected.

  • ObjectSizeType.MEDIUM — A medium object is detected.

  • ObjectSizeType.LARGE — A large object is detected.

注意: 这是 VEX IQ 距离传感器(第二代)的测量方法。

Usage:
distance_1.object_size()

参数

描述

该方法没有参数。

installed#

installed returns whether the Distance Sensor is installed and connected.

  • True — The Distance Sensor is installed and connected.

  • False — The Distance Sensor is not installed or not connected.

Usage:
distance_1.installed()

参数

描述

该方法没有参数。

构造函数#

Constructors are used to manually create Distance and Sonar objects, such as when configuring a Distance Sensor outside of VEXcode.

Sonar#

Sonar creates a VEX IQ Distance Sensor (1st gen) object.

Usage:
distance_1 = Sonar(smartport)

范围

描述

smartport

The Smart Port that the Distance Sensor is connected to, written as Ports.PORTx, where x is the port number.

# Create a Sonar object in Port 1
distance_1 = Sonar(Ports.PORT1)

Distance#

Distance creates a VEX IQ Distance Sensor (2nd gen) object.

Usage:
distance_1 = Distance(smartport)

范围

描述

smartport

The Smart Port that the Distance Sensor is connected to, written as Ports.PORTx, where x is the port number.

# Create a Distance object in Port 1
distance_1 = Distance(Ports.PORT1)