惯性#
介绍#
第二代 IQ 大脑 (Sensing/Brain.md) 内置惯性传感器,配备三轴陀螺仪和三轴加速度计。这使得机器人能够跟踪自身的姿态、航向和加速度。
This page uses brain_inertial as the example Inertial Sensor name. Replace it with your own configured name as needed.
以下是所有可用方法的列表:
方向控制 — 读取和控制航向、旋转和传感器校准。
heading— Returns the current heading.rotation— Returns the cumulative rotation.calibrate— Calibrates the Inertial Sensor for stable heading tracking.set_heading— Sets the Inertial Sensor’s heading to a specified value.set_rotation— Sets the Inertial Sensor’s rotation value.is_calibrating— Returns whether or not the Inertial Sensor is calibrating.reset_heading— Sets the heading of the Inertial Sensor to 0.reset_rotation— Sets the rotation of the Inertial Sensor to 0.changed— Registers a function to be called when the Inertial Sensor detects change.
运动学——测量加速度、角速度和倾斜度。
acceleration— Returns the linear acceleration along the x-, y-, or z-axis.gyro_rate— Returns the angular velocity around the x-, y-, or z-axis.orientation— Returns the roll, pitch, or yaw based on tilt and rotation.
构造函数 — 手动初始化和配置惯性传感器。
Inertial— Creates an Inertial Sensor.
方向#
heading#
heading returns the current heading of the Inertial Sensor.
Usage:
brain_inertial.heading(units)
参数 |
描述 |
|---|---|
|
Optional. The unit used to represent the heading:
|
# Display the heading after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print(brain_inertial.heading())
rotation#
rotation returns the current rotation of the Inertial Sensor.
Usage:
brain_inertial.rotation(units)
参数 |
描述 |
|---|---|
|
Optional. The unit used to represent the rotation:
|
# 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 the calibration to complete before executing. Calibration is an internal procedure that measures and compensates for sensor noise and drift over a specified period. During this time, the Brain must remain completely still (i.e., on a stable surface without any external movement). Movement during calibration will produce inaccurate results.
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#
set_heading() sets the heading of the Inertial Sensor to a specified value.
Usage:
brain_inertial.set_heading(value, units)
参数 |
描述 |
|---|---|
|
要设置的航向值。 |
|
Optional. The unit used to represent the new heading:
|
# 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. Rotation is automatically set to 0 at the start of a project.
Usage:
brain_inertial.set_rotation(value, units)
参数 |
描述 |
|---|---|
|
要设置的旋转值。 |
|
Optional. The unit used to represent the new rotation value:
|
# Turn the robot around
brain_inertial.set_rotation(-180)
drivetrain.turn_to_rotation(0)
is_calibrating#
is_calibrating checks if 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 heading of the Inertial Sensor to 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 rotation of the Inertial Sensor to 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)
参数 |
描述 |
|---|---|
|
注册一个当惯性传感器航向发生变化时要调用的函数。 |
|
可选。传递给回调函数的参数元组。 |
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 returns the acceleration of the Inertial Sensor as a float.
Usage:
brain_inertial.acceleration(axis)
参数 |
描述 |
|---|---|
轴 |
The axis to return the acceleration from:
|
# 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 returns the gyro rate for one axis of the Inertial Sensor.
Usage:
brain_inertial.gyro_rate(axis, units)
参数 |
描述 |
|---|---|
|
The axis to return the gyro rate from:
|
|
Optional. The unit used to represent the gyro rate:
|
# 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 returns the orientation for one axis of the Inertial Sensor.
Usage:
brain_inertial.orientation(axis, units)
参数 |
描述 |
|---|---|
|
The axis to return the orientation from:
|
|
Optional. The unit used to represent the orientation:
|
# 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, which are necessary for configuring an Inertial Sensor outside of VEXcode.
Inertial#
Inertial creates an Inertial Sensor.
Usage:
Inertial(smartport)
范围 |
描述 |
|---|---|
|
可选。如果使用 IQ(第二代)大脑 内置的惯性传感器,则无需智能端口。如果连接外部惯性传感器,请指定惯性传感器所连接的智能端口,格式为 PORTx,其中 x 为端口号。 |
# 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)


