惯性#

介绍#

The IQ (2nd gen) Brain includes a built-in inertial sensor with a 3-axis gyroscope and 3-axis accelerometer. This allows the robot to track its orientation, heading, and acceleration.

For the examples below, the configured Inertial Sensor will be named brain_inertial and will be used in all subsequent examples throughout this API documentation when referring to Inertial class methods.

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

Orientation – Read and control heading, rotation, and sensor calibration.

  • heading – 返回当前标题。

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

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

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

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

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

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

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

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

Motion – Measure acceleration, angular velocity, and tilt.

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

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

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

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

Orientation#

heading#

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

Usage:
brain_inertial.heading(units)

参数

描述

units

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

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

rotation#

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

Usage:
brain_inertial.rotation(units)

参数

描述

units

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

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

calibrate#

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

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

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

Usage:
brain_inertial.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() sets the heading of the Inertial Sensor to a specified value.

Usage:
brain_inertial.set_heading(value, units)

参数

描述

value

要设置的航向值。

units

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

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

set_rotation#

set_rotation() sets the rotation of the Inertial Sensor.

Usage:
brain_inertial.set_rotation(value, units)

参数

描述

value

要设置的旋转值。

units

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

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

is_calibrating#

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

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

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

Usage:
brain_inertial.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#

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

Usage:
brain_inertial.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#

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

Usage:
brain_inertial.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 注册一个回调函数。

Usage:
brain_inertial.changed(callback, arg)

参数

描述

callback

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

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)

Motion#

acceleration#

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

Usage:
brain_inertial.acceleration(axis)

参数

描述

axis

返回加速度的轴:

  • 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 返回惯性传感器一个轴的陀螺仪速率。

Usage:
brain_inertial.gyro_rate(axis, units)

参数

描述

axis

返回陀螺仪速率的轴:

  • XAXIS
  • YAXIS
  • ZAXIS

units

Optional. The unit used to represent the gyro rate:

  • DPS (default)
  • 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#

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

Usage:
brain_inertial.orientation(axis, units)

参数

描述

axis

返回方向的轴:

  • ROLL
  • PITCH
  • YAW

units

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

  • 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)

构造函数#

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

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)