惯性#

介绍#

IQ(第二代)大脑内置惯性传感器,配备 3 轴陀螺仪和 3 轴加速度计。这使得机器人能够追踪其方向、航向和加速度。

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

方法——测量旋转运动并检测运动变化。

  • calibrate – 校准惯性传感器以实现稳定的航向跟踪。

  • set_heading – 将惯性传感器的航向设置为指定值。

  • set_rotation – 设置惯性传感器的旋转值。

  • heading – 返回当前标题。

  • rotation – 返回累积旋转角度。

  • 加速度 – 返回沿 x、y 或 z 轴的线性加速度。

  • gyro_rate – 返回围绕 x、y 或 z 轴的角速度。

  • orientation – 根据倾斜和旋转返回滚动、俯仰或偏航。

  • is_calibrating – 返回惯性传感器是否正在校准。

  • reset_heading – 将惯性传感器的航向设置为 0。

  • reset_rotation - 将惯性传感器的旋转角度设置为 0。

  • 已更改 – 注册一个函数,当惯性传感器检测到变化时调用。

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

方法#

校准#

“calibrate” 用于校准惯性传感器。所有后续代码行都将等待校准完成后再执行。校准是一个内部过程,用于测量并补偿指定时间段内的传感器噪声和漂移。在此期间,Brain 必须保持完全静止(即,在稳定的表面上,没有任何外部运动)。校准期间的移动会导致结果不准确。

../../_images/IQ_right_orientation.png../../_images/IQ_left_orientation.png../../_images/IQ_top_orientation.png

VEX 机器人会在每次项目开始时尝试自动校准。但是,如果在项目启动期间搬运或移动机器人,传感器可能无法正确校准或产生错误的校准结果。

用法:
calibrate()

范围

描述

该方法没有参数。

# Start calibration
brain_inertial.calibrate()
# Print after calibration
while brain_inertial.is_calibrating():
    brain.screen.clear_screen()
    brain.screen.set_cursor(1, 1)
    brain.screen.print("Inertial Sensor")
    brain.screen.next_row()
    brain.screen.print("Calibrating")
    wait(50, MSEC)
brain.screen.clear_screen()
brain.screen.set_cursor(1, 1)
brain.screen.print("Done!")

设置标题#

set_heading() 将惯性传感器的航向设置为指定值。

用法:
set_heading(值,单位)

参数

描述

价值

要设置的航向值。

单位

可选。用于表示新航向的单位:

  • (默认)
  • `圈
# Turn the robot around
brain_inertial.set_heading(180)
drivetrain.turn_to_heading(0)

设置旋转#

set_rotation() 设置惯性传感器的旋转。

用法:
set_rotation(值,单位)

参数

描述

价值

要设置的旋转值。

单位

可选。用于表示新旋转值的单位:

  • DEGREES(默认)
  • TURNS
# Turn the robot around
brain_inertial.set_rotation(-180)
drivetrain.turn_to_rotation(0)

标题#

heading 返回惯性传感器的当前航向。

用法:
标题(单位)

参数

描述

单位

可选。用于表示航向的单位:

  • (默认)
  • `圈
# Display the heading after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print(brain_inertial.heading())

旋转#

rotation 返回惯性传感器的当前旋转。

用法:
旋转(单位)

参数

描述

单位

可选。用于表示旋转的单位:

  • DEGREES(默认)
  • TURNS
# Display the rotation value after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print(brain_inertial.rotation())

加速度#

acceleration 返回惯性传感器的加速度。

用法:
加速度(轴)

参数

描述

返回加速度的轴:

  • XAXIS
  • YAXIS
  • ZAXIS
# Display acceleration after moving
vexcode_brain_precision = 2
drivetrain.set_drive_velocity(100,PERCENT)
brain.screen.print("Resting: ")
brain.screen.next_row()
brain.screen.print(brain_inertial.acceleration(XAXIS))
brain.screen.next_row()
wait(1, SECONDS)
drivetrain.drive_for(FORWARD, 500, MM, wait=False)
wait(0.01,SECONDS)
brain.screen.print("Startup: ")
brain.screen.next_row()
brain.screen.print(brain_inertial.acceleration(XAXIS))

陀螺仪速率#

gyro_rate 返回惯性传感器一个轴的陀螺仪速率。

用法:
gyro_rate(axis, unit)

参数

描述

返回陀螺仪速率的轴:

  • XAXIS
  • YAXIS
  • ZAXIS

单位

可选。用于表示陀螺仪速率的单位:

  • DPS(默认)
  • RPM
  • PERCENT
# Display the z-axis gyro rate
drivetrain.turn(RIGHT)
wait(1, SECONDS)
brain.screen.print(brain_inertial.gyro_rate(ZAXIS, RPM))
drivetrain.stop()

方向#

orientation 返回惯性传感器一个轴的方向。

用法:
方向(轴,单位)

参数

描述

返回方向的轴:

  • ROLL
  • PITCH
  • YAW

单位

可选。用于表示方向的单位:

  • DEGREES(默认)
  • TURNS
# Display the roll, pitch, and yaw of the Brain as it
# is rotated by hand
while True:
    brain.screen.clear_screen()
    brain.screen.print(brain_inertial.orientation(OrientationType.ROLL))
    brain.screen.next_row()
    brain.screen.print(brain_inertial.orientation(OrientationType.PITCH))
    brain.screen.next_row()
    brain.screen.print(brain_inertial.orientation(OrientationType.YAW))
    brain.screen.next_row()
    brain.screen.set_cursor(1, 1)
    wait(0.1,SECONDS)

正在校准#

is_calibrating 检查惯性传感器当前是否正在校准。

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

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

用法:
is_calibrating()

范围

描述

该方法没有参数。

# Start calibration
brain_inertial.calibrate()
# Print while waiting for calibration
while brain_inertial.is_calibrating():
    brain.screen.clear_screen()
    brain.screen.set_cursor(1, 1)
    brain.screen.print("Inertial Sensor")
    brain.screen.next_row()
    brain.screen.print("Calibrating")
    wait(50, MSEC)
brain.screen.clear_screen()
brain.screen.set_cursor(1, 1)
brain.screen.print("Done!")

重置标题#

reset_heading 将惯性传感器的航向重置为 0。

用法:
reset_heading()

参数

描述

该方法没有参数。

# Turn the robot before and after resetting the heading
drivetrain.turn_to_heading(90, DEGREES)
wait(0.5,SECONDS)
brain_inertial.reset_heading()
drivetrain.turn_to_heading(90, DEGREES)

重置旋转#

reset_rotation 将惯性传感器的旋转重置为 0。

用法:
reset_rotation()

参数

描述

该方法没有参数。

# Turn the robot before and after resetting the rotation
drivetrain.turn_to_rotation(-90, DEGREES)
wait(0.5,SECONDS)
brain_inertial.reset_rotation()
drivetrain.turn_to_rotation(-90, DEGREES)

改变#

当惯性传感器的航向发生变化时,changed 注册一个回调函数。

用法:
changed(callback, arg)

参数

描述

打回来

当惯性传感器航向改变时调用的回调函数。

arg

可选。传递给回调函数的参数元组。

def heading_changed():
    brain.screen.set_cursor(1, 1)
    brain.screen.clear_screen()
    brain.screen.print("my heading ")
    brain.screen.next_row()
    brain.screen.print("has changed!")
    wait(0.1, SECONDS)
# Call the function when the inertial heading is changed
wait(1, SECONDS)
drivetrain.turn_for(RIGHT, 90, DEGREES, wait=False)
brain_inertial.changed(heading_changed)
wait(15, MSEC)

构造函数#

构造函数用于手动创建“惯性”对象,这对于在 VEXcode 之外配置惯性传感器是必需的。

对于下面的示例,配置的惯性传感器将被命名为“brain_inertial”,并且在整个 API 文档的所有后续示例中引用“Inertial”类方法时将使用它。

惯性#

“Inertial”创建一个惯性传感器。

用法:
惯性(端口)

范围

描述

端口

可选。如果使用 IQ(第二代)Brain 的内置惯性传感器,则无需智能端口。如果连接外部惯性传感器,请将惯性传感器连接到的智能端口指定为“PORT”,后接端口号,范围从 1 到 12。

# Create a new object "brain_inertial" with the
# Inertial class
brain_inertial = Inertial()
left_drive_smart = Motor(Ports.PORT1, 1.0, False)
right_drive_smart = Motor(Ports.PORT6, 1.0, True)

drivetrain = SmartDrive(left_drive_smart, right_drive_smart, brain_inertial, 200)

brain_inertial.set_heading(180)
drivetrain.turn_to_heading(0)