惯性#

介绍#

第二代 IQ 大脑包含一个内置惯性传感器。该传感器测量大脑Sensing/Brain.md运动和转动情况。

惯性传感器由两部分组成。陀螺仪测量转向,例如航向、旋转、陀螺仪速率和方向。加速度计测量运动变化,例如沿 x 轴、y 轴和 z 轴的加速或减速。

This page uses brain_inertial as the example Inertial Sensor name. Replace it with your own configured name as needed.

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

方向控制 — 读取和控制航向、旋转和传感器校准。

  • heading — Returns the direction the sensor is facing, from 0 to 359.99 degrees.

  • rotation — Returns how far the sensor has turned.

  • calibrate — Calibrates the Inertial Sensor.

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

  • set_rotation — Sets the Inertial Sensor’s current rotation to a new rotation value.

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

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

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

  • changed — Registers a function to run when the Inertial Sensor’s heading changes.

运动学——测量加速度、旋转速度和倾斜度。

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

  • gyro_rate — Returns how fast the sensor is rotating on the selected axis.

  • orientation — Returns the Inertial Sensor’s roll, pitch, or yaw angle.

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

  • Inertial — Creates an Inertial Sensor.

方向#

heading#

A heading is the direction the sensor is facing, measured in degrees from 0 to 359.99. heading returns the sensor’s current heading.

初始航向为0度。如果传感器旋转超过359.99度,航向将回落至0度。

Usage:
brain_inertial.heading(units)

参数

描述

units

Optional. The heading unit: DEGREES (default) or TURNS

# Display the heading after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print(brain_inertial.heading())

rotation#

Rotation is how much the sensor 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. rotation returns the Inertial Sensor’s current rotation.

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

Usage:
brain_inertial.rotation(units)

参数

描述

units

Optional. The rotation unit: DEGREES (default) or TURNS

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

calibrate#

calibrate calibrates the Inertial Sensor. All subsequent lines will wait for calibration to complete before running. Calibration helps the sensor measure turns correctly by measuring and compensating for sensor noise and drift over a specified period. During this time, the Brain must remain completely still on a stable surface. Movement during calibration can produce inaccurate results.

IQ Brain 的屏幕向右倾斜,x、y 和 z 轴分别用从屏幕中心向外延伸的箭头表示。标有 x 的红色箭头指向右侧,标有 y 的绿色箭头向前延伸,标有 z 的蓝色箭头向下延伸。与上一张图片相同的图像,现在大脑屏幕向左倾斜,x、y、z 箭头保持其方向不变。与前一张图相同的图像,现在大脑屏幕朝上,就像平放在一个平面上一样。x、y、z方向相同。

VEX Brain 会在每个项目开始时自动尝试校准内置惯性传感器。但是,如果在项目启动过程中机器人被搬运或移动,传感器可能无法正确校准或校准结果不准确。

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#

A heading is the direction the sensor is facing, measured in degrees from 0 to 359.99. set_heading() changes the Inertial Sensor’s current heading to a new heading value.

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

Usage:
brain_inertial.set_heading(value, units)

参数

描述

value

要设置的传感器航向值,单位为度。该值可以介于 0 到 359.99 之间。

units

Optional. The heading unit: DEGREES (default) or TURNS

# Turn the robot around
brain_inertial.set_heading(180)
drivetrain.turn_to_heading(0)

set_rotation#

Rotation is how much the sensor 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. set_rotation() changes the Inertial Sensor’s current rotation to a new value.

例如,如果传感器向右旋转了两圈,其旋转角度将为 720 度。将旋转角度设置为 0 度会将旋转角度从 720 度重置为 0 度。然后,传感器可以根据这个新值跟踪旋转。

Usage:
brain_inertial.set_rotation(value, units)

参数

描述

value

要设置的传感器旋转值,单位为度。可以是整数或浮点数(十进制数)。

units

Optional. The rotation unit: DEGREES (default) or TURNS

# Turn the robot around
brain_inertial.set_rotation(-180)
drivetrain.turn_to_rotation(0)

is_calibrating#

is_calibrating returns whether the Inertial Sensor is currently calibrating.

  • True — The Inertial Sensor is calibrating.

  • False — The Inertial Sensor is not calibrating.

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 resets the Inertial Sensor’s current heading to 0 degrees.

使用此方法后,传感器的当前方向将变为新的 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 resets the Inertial Sensor’s current rotation to 0 degrees.

使用这种方法后,传感器会从新的 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 registers a function to be called when the Inertial Sensor’s heading changes.

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)

运动#

acceleration#

Acceleration is how quickly the sensor is speeding up or slowing down. acceleration returns the acceleration of the Inertial Sensor on the selected axis, from -4.0 G to 4.0 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. The axis options are XAXIS, YAXIS, and ZAXIS.

Usage:
brain_inertial.acceleration(axis)

参数

描述

The axis to measure acceleration on:

  • 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 is how fast the Inertial Sensor is rotating. gyro_rate returns the current rotation speed of the Inertial Sensor on the selected axis.

该值可以是正数也可以是负数,具体取决于传感器绕该轴旋转的方向。

Usage:
brain_inertial.gyro_rate(axis, units)

参数

描述

axis

The axis to return the gyro rate from:

  • XAXIS
  • YAXIS
  • ZAXIS

units

Optional. The gyro rate unit:

  • DPS (default) — degrees per second
  • RPM — rotations per minute
  • 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 is the Inertial Sensor’s current angle on a selected turning axis. orientation returns the roll, pitch, or yaw of the Inertial Sensor.

横滚、俯仰和偏航描述了传感器倾斜或旋转的不同方式。

Usage:
brain_inertial.orientation(axis, units)

参数

描述

axis

The orientation angle to return:

  • ROLL
  • PITCH
  • YAW

units

Optional. The orientation unit: DEGREES (default) or 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)

构造函数#

Constructors are used to manually create Inertial objects, such as when configuring an Inertial Sensor outside of VEXcode.

Inertial#

Inertial creates an Inertial Sensor.

Usage:
Inertial(smartport)

范围

描述

smartport

Optional. If using the IQ (2nd gen) Brain’s built-in Inertial Sensor, a Smart Port is not needed. If connecting an external Inertial Sensor, specify the Smart Port that the Inertial Sensor is connected to, written as PORTx, where x is the port number.

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