惯性传感器#

简介#

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

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

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

以下是所有方法的列表:

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

  • [get_rotation] (#get_rotation) —返回机器人转向的距离。

  • [get_heading] (#get_heading) —返回机器人朝向的方向,从0到359.99度。

  • [get_yaw] (#get_yaw) —返回机器人的偏航角。

  • get_roll — 返回机器人的翻滚角。

  • [get_pitch] (#get_pitch) —返回机器人的俯仰角。

  • [reset_rotation] (#reset_rotation) —将机器人当前的旋转重置为0度。

  • [reset_heading] (#reset_heading) —将机器人当前航向重置为0度。

  • [set_heading] (#set_heading) —将机器人的当前航向设置为新的航向值。

Crash — Detect collisions.

  • [crashed] (#crashed) —注册一个在检测到崩溃时运行的函数。

  • [set_crash_sensitivity] (#set_crash_sensitivity) —设置碰撞检测对碰撞的敏感程度。

Motion — Measure acceleration and turning speed.

  • [get_acceleration] (#get_acceleration) —返回机器人在所选轴上加速或减速的速度。

  • [get_turn_rate] (#get_turn_rate) —返回机器人在所选轴上旋转的速度。

校准 — 管理传感器校准。

  • calibrate — 校准惯性传感器。

  • [is_calibrating] (#is_calibrating) —返回惯性传感器当前是否正在校准。

方向#

get_rotation#

Rotation 是指机器人已转动的角度,以度为单位。与 heading 不同,rotation 的值可以超过 359.99 度,也可以降到 0 度以下。项目开始时,rotation 的初始值为 0 度。get_rotation 以浮点数形式返回机器人当前的 rotation 值。

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

用法:
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#

航向是机器人面向的方向,以度为单位,范围从0到359.99。get_heading 以浮点数形式返回机器人当前的航向值。

初始航向角为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#

偏航描述机器人绕其垂直轴向左或向右转动。get_yaw 以浮点数形式返回机器人的偏航角,范围在 -180.00 到 180.00 度之间。

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

一个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#

翻滚描述机器人左右倾斜。get_roll 以浮点数形式返回机器人的翻滚角,范围在 -180.00 到 180.00 度之间。

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

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#

俯仰描述机器人向前或向后倾斜。get_pitch 以浮点数形式返回机器人的俯仰角,范围在 -90.00 到 90.00 度之间。

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

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 将机器人的当前旋转重置为0度。

使用该方法后,机器人将从新的 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#

航向是机器人面向的方向,以度为单位,范围从 0 到 359.99。set_heading 将机器人的当前航向更改为一个新的航向值。

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

用法:
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 注册一个在机器人检测到突然撞击或碰撞时调用的函数。

用法:

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 调整机器人检测碰撞所需的突然加速度。

用法:
robot.inertial.set_crash_sensitivity(sensitivity)

参数

描述

sensitivity

碰撞灵敏度:

  • HIGH — 最灵敏;检测较小的撞击,触发阈值为 1G。
  • NORMAL — 默认灵敏度;检测中等撞击,触发阈值为 1.5G。
  • LOW — 最不灵敏;检测较强的撞击,触发阈值为 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#

加速度是机器人加速或减速的快慢程度。get_acceleration 以浮点数形式返回机器人在选定轴上的加速度,范围从 -4.00 G 到 4.00 G。

G 是用于测量加速度的单位。1 G是关于您坐着不动的时候从重力中感受到的加速度。

值可以是正数或负数,这取决于所选轴的加速度方向。 AIM上的坐标轴选项被命名为机器人上的方向: FORWARDRIGHTWARDDOWNWARD

一张 VEX AIM 编程机器人图片,从机器人中心向外伸出方向箭头。一个标记为FORWARD的绿色箭头从机器人前部伸出,一个标记为DOWNWARD的蓝色箭头从机器人底部伸出,一个标记为RIGHTWARD的红色箭头从机器人侧面 VEX 标志下方伸出。

用法:

robot.inertial.get_acceleration(type)

参数

描述

type

测量加速度的轴:

  • DOWNWARD — 测量机器人垂直方向的加速度。
  • FORWARD — 测量机器人前后方向的加速度。
  • RIGHTWARD — 测量机器人左右方向的加速度。

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

旋转速率是机器人旋转的速度。get_turn_rate 以每秒度( dps )为单位返回机器人的旋转速率,从-1000.00到1000.00 dps。

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

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

Usage:
robot.inertial.get_turn_rate(axis)

参数

描述

axis

要返回的姿态:

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

若你的项目高度依赖精确的航向,或需要稳定且可复现的转向,在代码开头调用 calibrate 会很有帮助。建议在校准期间,在机器人屏幕上显示类似“Calibrating…”的消息,待校准完成后再更新为“Calibration complete.”,以提醒你(及其他使用该机器人的人员)在此期间务必保持机器人静止。

calibrate#

calibrate 用于校准陀螺仪。校准通过在 2 秒内测量并补偿传感器噪声和漂移,帮助惯性传感器正确测量转向。在此期间,机器人必须在一块稳定的表面上保持完全静止。校准期间若发生移动,可能导致结果不准确。

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 返回一个布尔值,指示惯性传感器是否正在校准。

  • True — 惯性传感器正在校准。

  • False — 惯性传感器未校准。

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)