全球定位系统#

要使 Gps 命令出现在 VEXcode V5 中,必须在设备窗口中配置 GPS(游戏定位系统™)传感器。

有关详细信息,请参阅以下文章:

初始化 Gps 类#

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

Gps(port, origin_x, origin_y, units, heading_offset)

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

范围

描述

port

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

origin_x

Optional. The X location of the GPS Sensor with respect to origin (also known as the reference point) of the robot. By default, this is 0.
Note: If the Y location is provided, the origin_x parameter must also be provided.

origin_y

Optional. The Y location of the GPS Sensor with respect to origin (also known as the reference point) of the robot. By default, this is 0.
Note: If the Y location is provided, the origin_x parameter must also be provided.

units

**可选。**指定 X 和 Y 位置的单位。默认情况下为 MM。

heading_offset

可选。 GPS 传感器航向的偏移角度(以度为单位)。默认值为 180 度。

# Create a GPS Sensor "gps_1" with the
# Gps class.
gps_1 = Gps(Ports.PORT1, 0.00, 0.00, MM, 180)

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

类方法#

calibrate()#

The calibrate() method calibrates the GPS Sensor when the sensor has been configured as a part of a V5 Drivetrain. This will set the GPS’ configured heading (according to its angle offset) as the current heading of the Drivetrain.

在校准过程中,GPS 传感器和传动系统必须保持静止

**返回:**无

在这个例子中,GPS 传感器将进行校准,然后使用传感器的航向将机器人转到场地的 180 度航向。

# Turn to face the lower Field wall using data from the GPS Sensor.
drivetrain_gps.calibrate()
drivetrain.turn_to_heading(180, DEGREES)

is_calibrating()#

The is_calibrating() method checks if the GPS Sensor is currently calibrating or not.

返回: 如果 GPS 传感器当前正在校准,则返回 True。如果不是,则返回 False。

set_heading()#

The set_heading() method sets the GPS Sensor’s heading to a new value. The new value for heading should be in the range 0 - 359.99 degrees.

参数

描述

价值

用于标题的新值。

单位

Optional. A valid RotationUnit. The default is DEGREES.

**返回:**无

# Set the current GPS Sensor's heading to 180 degrees.
gps_1.set_heading(180)

reset_heading()#

The reset_heading() method sets the GPS Sensor’s heading to 0 degrees.

**返回:**无

heading()#

The heading() method reports the heading of a GPS Sensor on a Field based on the sensor’s readings from the GPS Field Code.

参数

描述

单位

Optional. A valid RotationUnit. The default is DEGREES.

The gps.heading command reports a range from 0.00 to 359.99 degrees.

航向对应于场方向,范围为顺时针方向 0º 至 359.9º。0º 位于 12 点钟位置。

设置gps_location_field

By default the gps.heading command reports the heading of a GPS Sensor.

如果在配置 GPS 传感器时添加了角度偏移,则报告的航向将是机器人上的参考点。

**返回:**单位指定范围内的航向值。

在这个例子中,机器人(起始方向面向场地的12点钟方向)转向场地右侧墙壁,直到航向报告大于90度。然后机器人停止行驶。

# Turn slowly to face the right wall.
drivetrain.set_turn_velocity(20, PERCENT)
drivetrain.turn(RIGHT)
# Stop turning when the GPS reports a heading greater than 90 degrees.
while not gps_20.heading() > 90:
    wait(5, MSEC)
drivetrain.stop()

set_rotation()#

The set_rotation() method sets the GPS Sensor’s rotation to a new value.

参数

描述

价值

用于旋转的新值。

单位

Optional. A valid RotationUnit. The default is DEGREES.

**返回:**无

# Set the current rotation of the GPS Sensor to 400 degrees.
gps_1.set_rotation(400)

reset_rotation()#

The reset_rotation() method resets the GPS Sensor’s rotation to 0 degrees.

**返回:**无

rotation()#

The rotation() method reads the current rotation of the GPS Sensor. Rotation is not limited; it can be both positive and negative and shows the absolute angle of the GPS Sensor.

参数

描述

单位

Optional. A valid RotationUnit. The default is DEGREES.

**返回:**单位指定范围内的航向值。

# Report the current rotation of the GPS Sensor.
rotation = gps_1.rotation()

x_position()#

The x_position() method returns the current x coordinate of a GPS Sensor on the Field.

参数

描述

单位

Optional. A valid DistanceUnit. The default is MM.

By default the x_position method returns the X coordinate location of a GPS Sensor.

如果在配置 GPS 传感器时添加了偏移量,则报告的 X 位置将是机器人上的参考点。

**返回:**指定单位的 x 坐标值。

在本例中,机器人(从场地右侧出发)向中心移动,直到 X 轴位置报告小于 0 毫米。然后机器人停止移动。

# Drive slowly towards the center of the Field.
drivetrain.set_drive_velocity(20, PERCENT)
drivetrain.drive(FORWARD)
# Stop driving when the GPS reports an X position near the center of the Field.
while not gps_20.x_position(MM) < 0:
    wait(5, MSEC)
drivetrain.stop()

y_position()#

The y_position() method returns the current y coordinate of a GPS Sensor on the Field.

参数

描述

单位

Optional. A valid DistanceUnit. The default is MM.

By default the y_position method returns the Y coordinate location of a GPS Sensor.

如果在配置 GPS 传感器时添加了偏移量,则报告的 Y 位置将是机器人上的参考点。

**返回:**指定单位的 y 坐标值。

在此示例中,机器人(从场地顶部出发)向中心移动,直到 Y 位置报告小于 0 毫米。然后机器人停止移动。

# Drive slowly towards the center of the Field.
drivetrain.set_drive_velocity(20, PERCENT)
drivetrain.drive(FORWARD)
# Stop driving when the GPS reports an Y position near the center of the Field.
while not gps_20.y_position(MM) < 0:
    wait(5, MSEC)
drivetrain.stop()

quality()#

The quality() method returns a numerical value from 0-100 representing the current signal quality of a GPS Sensor.

报告值

描述

100

GPS 传感器报告的所有位置和航向数据均有效。

~90

任何位置数据不再通过捕获 VEX 字段代码中的信息来计算,而是通过其他方式。

0 - 80

只有 GPS 传感器航向值有效,但随着时间的推移,GPS 传感器扫描的 VEX 字段代码不足以准确确定位置和航向信息,报告的信号质量将持续下降直至为 0,此时任何 GPS 传感器数据都会被有效冻结且不再有效。

返回: 0 到 100 范围内的质量值。

In this code snippet, the signal quality will constantly be checked due to the while True loop. The true branch will only run if the signal quality from the GPS Sensor is reporting values above 90, to ensure the GPS Sensor data being used in the project is valid.

while True:
    if gps_20.quality() > 90:
        pass
    else:
        pass
    wait(5, MSEC)

set_origin()#

The set_origin() method sets the origin of the GPS Sensor. An alternate way of setting sensor origin if not provided in the Gps class constructor.

参数

描述

x

GPS 传感器相对于机器人原点的 X 位置。

y

GPS 传感器相对于机器人原点的 Y 位置。

单位

Optional. A valid DistanceUnit. The default is MM.

**返回:**无

# Set the GPS Sensor's origin +6 inches on the x axis and
# -6 inches on the y axis.
gps_1.set_origin(6, -6, INCHES)

set_location()#

The set_location() method sets the X, Y coordinates, and heading of a GPS Sensor to a known value at the beginning of a project. This can be done to help reduce GPS data inaccuracies due a the sensor’s proximity to field walls when a project is started.

参数

描述

x

X 坐标。

y

Y 坐标。

单位

Optional. A valid DistanceUnit. The default is MM.

角度

**可选。**机器人的航向。

单位

Optional. A valid RotationUnit. The default is DEGREES.

当 GPS 传感器距离场地墙壁太近而无法读取场地代码时(例如,当机器人处于场地上的比赛起始位置时),如果代码依赖于 GPS 的坐标和航向数据,则经常会出现意外行为。

设置gps_location_field

The X and Y parameters are the X and Y coordinates of the reference point on your robot on the field in this known location. The reference point on your robot is the same location that was used to calculate the offsets when configuring the GPS Sensor.

The HEADING parameter is the heading of the reference point on your robot. Using the example image above, the GPS Sensor is mounted facing behind the robot, while the reference point is at the end of the arm on the robot. Because the reference point is facing the right wall (heading of 90), the heading value that should be input is 90.

**返回:**无

此处显示的示例设置了机器人(手臂)上的参考点的位置,以匹配机器人在场地上的位置。

# Wait so the GPS has time to initialize.
wait(0.5, SECONDS)
# Set the starting position to the bottom left corner of the Field facing the right wall.
drivetrain_gps.set_location(-1200, -1200, MM, 90)

返回: False

orientation()#

The orientation() method reads the orientation for one axis of the GPS Sensor.

参数

描述

有效的 OrientationType

单位

Optional. A valid RotationUnit. The default is DEGREES.

  • OrientationType.ROLL 轴报告一个介于 -180180 度之间的值,表示 GPS 传感器向左或向右倾斜时的方向。

  • OrientationType.PITCH 轴报告一个介于 -9090 度之间的值,表示 GPS 传感器向前或向后倾斜时的方向。

  • OrientationType.YAW 轴报告一个介于 -180180 度之间的值,表示 GPS 传感器绕垂直轴旋转时的方向。

**返回:**指定单位的轴方向值。

gyro_rate()#

The gyro_rate() method reads the gyro rate for one axis of the GPS Sensor.

参数

描述

有效的 AxisType

单位

Optional. A valid VelocityUnit. The default is DPS.

**返回:**以指定单位表示的轴陀螺仪速率值。

acceleration()#

The acceleration() method reads the acceleration for one axis of the GPS Sensor.

参数

描述

有效的 AxisType

**返回:**以重力单位表示的轴加速度值。

set_sensor_rotation()#

The set_sensor_rotation() method sets the sensor rotation of the GPS Sensor with respect to the robot. This allows heading and rotation methods to return angles relative to the robot rather than the GPS Sensor itself.

参数

描述

价值

GPS 传感器相对于机器人的角度。

单位

Optional. A valid RotationUnit. The default is DEGREES.

**返回:**无

changed()#

The changed() method registers a function to be called when the value of the GPS Sensor’s heading changes. This is not particularly useful as GPS Sensor’s heading is not stable and will cause many events.

参数

描述

打回来

当 GPS 传感器航向的值发生变化时调用的函数。

arg

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

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

# Define the function to occur when the GPS Sensor's
# heading changes.
def heading_changed():
    brain.screen.print("heading changed")

# Set the function to occur when the GPS Sensor's
# heading changes.
gps_1.changed(heading_changed())

set_turn_type()#

The set_turn_type(turntype) method sets the direction that returns positive values for heading.

参数

描述

转盘式

有效的 TurnType

**返回:**无

get_turn_type()#

The get_turn_type() method gets the direction that returns positive values for heading.

Returns: The current TurnType, LEFT or RIGHT.

installed()#

The installed() method checks for device connection.

返回: True 或 False

timestamp()#

The timestamp() method requests the timestamp of the last received status packet from the GPS Sensor.

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