惯性#

介绍#

VEX AIM 编程机器人内置惯性传感器。该传感器用于测量机器人的运动和转向情况。

惯性传感器由两部分组成。陀螺仪测量转向参数,例如机器人的航向、旋转、偏航、横滚、俯仰和转弯速率。加速度计测量运动变化,例如加速、减速或在碰撞过程中突然停止。

惯性方法可用于跟踪转弯、测量加速度、检测碰撞、重置航向或旋转值以及校准传感器。

以下是所有方法的列表:

方向调整 — 获取并重置机器人的朝向、转向或倾斜方向。

  • get_rotation — Returns how far the robot has turned.

  • get_heading — Returns the direction the robot is facing, from 0 to 359.99 degrees.

  • get_yaw — Returns the robot’s yaw angle.

  • get_roll — Returns the robot’s roll angle.

  • get_pitch — Returns the robot’s pitch angle.

  • reset_rotation — Resets the robot’s current rotation to 0 degrees.

  • reset_heading — Resets the robot’s current heading to 0 degrees.

  • set_heading — Sets the robot’s current heading to a new heading value.

碰撞检测——检测碰撞。

  • crashed — Registers a function to run when a crash is detected.

  • set_crash_sensitivity — Sets how sensitive crash detection is to impacts.

运动——测量加速度和转弯速度。

  • get_acceleration — Returns how quickly the robot is speeding up or slowing down on the selected axis.

  • get_turn_rate — Returns how fast the robot is rotating on the selected axis.

校准 — 管理传感器校准。

  • calibrate — Calibrates the Inertial Sensor.

  • is_calibrating — Returns whether the Inertial Sensor is currently calibrating.

方向#

get_rotation#

Rotation is how much the robot has turned, measured in degrees. Unlike heading, rotation can increase past 359.99 degrees or decrease below 0 degrees. At the beginning of a project, the rotation value is set to 0 degrees. get_rotation returns the robot’s current rotation as a float.

向右转动会增加旋转角度,向左转动会减少旋转角度。例如,向右转动两圈将旋转 720 度。从 0 度向左转动一圈将旋转 -360 度。

Usage:
robot.inertial.get_rotation()

参数

描述

该方法没有参数。

# Display the robot's rotation as it rotates
while True:
    robot.screen.clear_screen()
    robot.screen.set_cursor(1, 1)
    robot.screen.print(f"Rotation: {robot.inertial.get_rotation():.2f}")
    wait(50, MSEC)

get_heading#

A heading is the direction the robot is facing, measured in degrees from 0 to 359.99. get_heading returns the robot’s current heading as a float.

初始航向角为0度。如果机器人转弯超过359.99度,航向角将回落至0度。

Usage:
robot.inertial.get_heading()

参数

描述

该方法没有参数。

# Turn right until the heading reaches 90 degrees
robot.turn(RIGHT)
while robot.inertial.get_heading() < 90:
    wait(50, MSEC)
robot.stop_all_movement()

# Display the robot's heading as it is rotated by hand
while True:
    robot.screen.clear_screen()
    robot.screen.set_cursor(1, 1)
    robot.screen.print(f"Heading: {robot.inertial.get_heading()} degrees")
    wait(50, MSEC)

get_yaw#

Yaw describes the robot turning left or right around its vertical axis. get_yaw returns the robot’s yaw angle in the range -180.00 to 180.00 degrees as a float.

下图使用箭头显示偏航的正旋转方向。

一个VEX AIM编程机器人,其中心向下伸出一个蓝色箭头。蓝色箭头周围环绕着一个顺时针方向的黑色弯曲箭头,指示正偏航方向。

Usage:
robot.inertial.get_yaw()

参数

描述

该方法没有参数。

# Display the robot's yaw angle as it is rotated by hand
while True:
    robot.screen.clear_screen()
    robot.screen.set_cursor(1, 1)
    robot.screen.print(robot.inertial.get_yaw())
    wait(50, MSEC)

get_roll#

Roll describes the robot tilting side to side. get_roll returns the robot’s roll angle in the range -180.00 to 180.00 degrees as a float.

下图使用箭头表示滚动的正向旋转方向。

VEX AIM 编码机器人前方伸出一个绿色箭头,位于 AI 视觉传感器的中心位置。围绕绿色箭头,一个黑色箭头逆时针弯曲,指示翻滚的正方向。

Usage:
robot.inertial.get_roll()

参数

描述

该方法没有参数。

# Display the robot's roll angle as it is tilted by hand
while True:
    robot.screen.clear_screen()
    robot.screen.set_cursor(1, 1)
    robot.screen.print(robot.inertial.get_roll())
    wait(50, MSEC)

get_pitch#

Pitch describes the robot tilting forward or backward. get_pitch returns the robot’s pitch angle in the range -90.00 to 90.00 degrees as a float.

下图使用箭头表示俯仰正向旋转的方向。

VEX AIM 编程机器人,其侧面 VEX 标志下方伸出一个红色箭头。围绕红色箭头,一个黑色箭头逆时针弯曲,指示俯仰的正方向。

Usage:
robot.inertial.get_pitch()

参数

描述

该方法没有参数。

# Display the robot's pitch angle as it is tilted by hand
while True:
    robot.screen.clear_screen()
    robot.screen.set_cursor(1, 1)
    robot.screen.print(robot.inertial.get_pitch())
    wait(50, MSEC)

reset_rotation#

reset_rotation resets the robot’s current rotation to 0 degrees.

使用该方法后,机器人将从新的 0 度旋转值开始跟踪未来的转弯。

Usage:
robot.inertial.reset_rotation()

参数

描述

该方法没有参数。

# Reset the robot's rotation if it exceeds 180 degrees
while True:
    if robot.inertial.get_rotation() >= 180:
        robot.inertial.reset_rotation()

    robot.screen.clear_screen()
    robot.screen.set_cursor(1, 1)
    robot.screen.print(f"Rotation: {robot.inertial.get_rotation():.2f}")

    wait(50, MSEC)

reset_heading#

reset_heading resets the robot’s current heading to 0 degrees.

使用此方法后,机器人的当前方向将变为新的 0 度航向。

Usage:
robot.inertial.reset_heading()

参数

描述

该方法没有参数。

# Turn the robot around using a new 90 degree heading
robot.turn_to(90)
wait(1, SECONDS)
robot.inertial.reset_heading()
robot.turn_to(90)

set_heading#

A heading is the direction the robot is facing, measured in degrees from 0 to 359.99. set_heading changes the robot’s current heading to a new heading value.

例如,如果机器人转向右侧,将航向设置为 0 度,则该右侧位置将成为新的 0 度方向。然后,机器人可以根据这个新的方向跟踪其他航向。

Usage:
robot.inertial.set_heading(heading)

参数

描述

heading

机器人航向角值,以度为单位。取值范围为 0 到 359.99。

# Turn the robot to 90 degrees using its new heading
robot.inertial.set_heading(45)
robot.turn_to(90)

碰撞#

crashed#

crashed registers a function to be called when the robot detects a sudden impact or collision.

用法:

robot.inertial.crashed(callback, arg)

参数

描述

callback

先前定义的在检测到碰撞时执行的 函数

arg

可选。包含要传递给回调函数的参数的元组。更多信息请参阅使用带参数的函数

# Define what happens when a crash occurs
def crash_detected():
    # Stop all movement and indicate a crash occurred
    robot.screen.print("Crash detected")
    robot.stop_all_movement()

robot.inertial.crashed(crash_detected)

# Drive forward until crash
robot.move_at(0, 100)

set_crash_sensitivity#

set_crash_sensitivity adjusts how much sudden acceleration is needed for the robot to detect a crash.

Usage:
robot.inertial.set_crash_sensitivity(sensitivity)

参数

描述

sensitivity

The crash sensitivity:

  • HIGH — Most sensitive; detects smaller impacts and triggers at 1G.
  • NORMAL — Default sensitivity; detects moderate impacts and triggers at 1.5G.
  • LOW — Least sensitive; detects stronger impacts and triggers at 2G.

def crashed_callback():
    robot.stop_all_movement()
    robot.sound.play(CRASH)

# system event handlers
robot.inertial.crashed(crashed_callback)
# add 15ms delay to make sure events are registered correctly.
wait(15, MSEC)

# Detect a crash at a slow velocity.
robot.set_move_velocity(35, PERCENT)
robot.inertial.set_crash_sensitivity(HIGH)
robot.move_at(0)

运动#

get_acceleration#

Acceleration is how quickly the robot is speeding up or slowing down. get_acceleration returns the robot’s acceleration on the selected axis, from -4.00 G to 4.00 G, as a float.

A G is a unit used to measure acceleration. 1 G is about the acceleration you feel from gravity while sitting still.

The value can be positive or negative depending on the direction of acceleration on the selected axis. On AIM, the axis options are named for directions on the robot: FORWARD, RIGHTWARD, and DOWNWARD.

一个VEX AIM编程机器人,其中心延伸出方向箭头。一个绿色箭头(向前)从机器人前方延伸,一个蓝色箭头(向下)从机器人底部延伸,一个红色箭头(向右)从机器人侧面VEX标志下方延伸。

用法:

robot.inertial.get_acceleration(type)

参数

描述

type

The axis to measure acceleration on:

  • DOWNWARD — Measures acceleration along the robot’s vertical direction.
  • FORWARD — Measures acceleration along the robot’s front-to-back direction.
  • RIGHTWARD — Measures acceleration along the robot’s side-to-side direction.

# Display the acceleration as the robot begins to move
robot.screen.set_cursor(4,1)
sitting_accel = robot.inertial.get_acceleration(RIGHTWARD)
robot.screen.print(f"Resting: {sitting_accel:.2f}")
wait(0.5, SECONDS)
robot.screen.next_row()

robot.move_at(90, 100)
wait(0.1, SECONDS)

robot.screen.print(f"Startup: {robot.inertial.get_acceleration(RIGHTWARD):.2f}")

get_turn_rate#

Turn rate is how fast the robot is rotating. get_turn_rate returns the robot’s turning rate in degrees per second \(dps\) as a float, from -1000.00 to 1000.00 dps.

该值可以是正数也可以是负数,具体取决于机器人绕所选轴的旋转方向。下图使用箭头表示横滚、俯仰和偏航的正旋转方向。

一个VEX AIM编码机器人,其中心延伸出方向箭头。一个标有“滚动”的绿色箭头从机器人前方延伸,其周围环绕着一个逆时针方向的黑色弯曲箭头。一个标有“偏航”的蓝色箭头从机器人底部延伸,其周围环绕着一个顺时针方向的黑色弯曲箭头。一个标有“俯仰”的红色箭头从机器人侧面VEX标志下方延伸,其周围环绕着一个逆时针方向的黑色弯曲箭头。

Usage:
robot.inertial.get_turn_rate(axis)

参数

描述

axis

Which orientation to return:

  • YAW
  • ROLL
  • PITCH
# Display the gyro rate as the robot is rotated by hand
while True:
    robot.screen.clear_screen()
    robot.screen.set_cursor(1, 1)
    robot.screen.print(robot.inertial.get_turn_rate(YAW))
    wait(50, MSEC)

校准#

校准过程通过测量和补偿传感器噪声和漂移,帮助惯性传感器准确测量转弯角度,校准周期为2秒。在此期间,机器人必须保持静止在稳定的表面上。校准过程中的任何移动都可能导致结果不准确。

VEX 机器人启动后会尝试自动校准,直到检测到无运动为止。但是,如果在启动过程中搬运或移动机器人,传感器可能无法正确校准或产生错误的校准结果。

If your project relies heavily on having an accurate heading, or if you need consistent and repeatable turns, calling calibrate at the beginning of your code can help. It’s good practice to display a message like “Calibrating…” on the robot’s screen during calibration, then update it to “Calibration complete.” afterward to remind you (and anyone else using the robot) that the robot must remain motionless during this period.

calibrate#

calibrate calibrates the gyro. Calibration helps the Inertial Sensor measure turns correctly by measuring and compensating for sensor noise and drift over a 2-second period. During this time, the robot must remain completely still on a stable surface. Movement during calibration can produce inaccurate results.

VEX 机器人启动后会尝试自动校准,直到检测到无运动为止。但是,如果在启动过程中搬运或移动机器人,传感器可能无法正确校准或产生错误的校准结果。

Usage:
robot.inertial.calibrate()

参数

描述

该方法没有参数。

# Calibrate the gyro before moving
robot.inertial.calibrate()
robot.screen.show_emoji(THINKING)
wait(2, SECONDS)
robot.screen.show_emoji(PROUD)
robot.move_for(50, 90)

is_calibrating#

is_calibrating returns a Boolean indicating whether the Inertial Sensor is calibrating.

  • True — The Inertial Sensor is calibrating.

  • False — The Inertial Sensor is not calibrating.

Usage:
robot.inertial.is_calibrating()

参数

描述

该方法没有参数。

# Move after the calibration is completed
robot.inertial.calibrate()
while robot.inertial.is_calibrating():
    robot.screen.show_emoji(THINKING)
    wait(50, MSEC)
robot.screen.show_emoji(PROUD)
robot.move_for(50, 90)