惯性#
介绍#
The IQ (2nd gen) Brain includes a built-in Inertial Sensor. This sensor measures how the Brain is moving and turning.
惯性传感器由两部分组成。陀螺仪测量转向,例如航向、旋转、陀螺仪速率和方向。加速度计测量运动变化,例如沿 x 轴、y 轴和 z 轴的加速或减速。
本页面使用 brain_inertial 作为惯性传感器的示例名称。请根据需要将其替换为您自己配置的名称。
以下是所有可用方法的列表:
方向控制 — 读取和控制航向、旋转和传感器校准。
heading— 返回传感器朝向的方向,从 0 到 359.99 度。rotation— 返回传感器旋转了多远。calibrate— 校准惯性传感器。set_heading— 将惯性传感器的当前航向设置为新的航向值。set_rotation— 将惯性传感器的当前旋转设置为新的旋转值。is_calibrating— 返回惯性传感器当前是否正在校准。reset_heading— 将惯性传感器的当前航向重置为 0 度。reset_rotation— 将惯性传感器的当前旋转重置为 0 度。changed— 注册一个函数,当惯性传感器的航向发生变化时运行。
运动学——测量加速度、旋转速度和倾斜度。
acceleration— 返回传感器在选定轴上加速或减速的速度。gyro_rate— 返回传感器在选定轴上的旋转速度。orientation— 返回惯性传感器的横滚角、俯仰角或偏航角。
构造函数 — 手动初始化和配置惯性传感器。
Inertial— 创建一个惯性传感器。
方向#
heading#
航向是指传感器朝向的方向,以度为单位,范围从 0 到 359.99。0 航向 heading 当前航向。
初始航向为0度。如果传感器旋转超过359.99度,航向将回落至0度。
Usage:
brain_inertial.heading(units)
参数 |
描述 |
|---|---|
|
可选。航向单位: |
# Display the heading after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print(brain_inertial.heading())
rotation#
旋转角度是指传感器转动的程度,以度为单位。与航向不同,旋转角度可以超过 359.99 度 rotation 返回惯性传感器的当前旋转角度。
向右转动会增加旋转角度,向左转动会减少旋转角度。例如,向右转动两圈将旋转 720 度。从 0 度向左转动一圈将旋转 -360 度。
Usage:
brain_inertial.rotation(units)
参数 |
描述 |
|---|---|
|
可选。旋转单位: |
# Display the rotation value after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print(brain_inertial.rotation())
calibrate#
calibrate 用于校准惯性传感器。所有后续代码行将等待校准完成后再运行。校准通过测量和补偿指定时间段内的传感器噪声和漂移,帮助传感器正确测量转弯。在此期间,大脑 必须完全静止在稳定的表面上。校准期间的移动会导致结果不准确。
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#
航向是指传感器朝向的方向,以度为单位,范围从 0 到 359.99。 set_heading() 将惯性传感器的当前航向更改为新的航向值。
例如,如果传感器已转向右侧,将航向设置为 0 度,则该右侧位置将成为新的 0 度。然后,传感器可以根据该新方向跟踪其他航向。
Usage:
brain_inertial.set_heading(value, units)
参数 |
描述 |
|---|---|
|
要设置的传感器航向值,单位为度。该值可以介于 0 到 359.99 之间。 |
|
可选。航向单位: |
# Turn the robot around
brain_inertial.set_heading(180)
drivetrain.turn_to_heading(0)
set_rotation#
旋转角度是指传感器转动的程度,以度为单位。与航向不同,旋转角度可以超过 359.99 度,也可以低于 0 度。项目开始时,旋转角度值设置为 0 度。`set_rotation() set_rotation() 用于将惯性传感器的当前旋转角度更改为新值。
例如,如果传感器向右旋转了两圈,其旋转角度将为 720 度。将旋转角度设置为 0 度会将旋转角度从 720 度重置为 0 度。然后,传感器可以根据这个新值跟踪旋转。
Usage:
brain_inertial.set_rotation(value, units)
参数 |
描述 |
|---|---|
|
要设置的传感器旋转值,单位为度。可以是整数或浮点数(十进制数)。 |
|
可选。旋转单位: |
# 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 度。
使用此方法后,传感器的当前方向将变为新的 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 度。
使用这种方法后,传感器会从新的 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)
参数 |
描述 |
|---|---|
|
当惯性传感器航向发生变化时运行的函数。 |
|
可选。传递给回调函数的参数元组。 |
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 返回所选轴上惯性传感器的加速度值,范围从 -4.0 G 到 4.0 G,以浮点数形式表示。
G 相当于你静止坐着时感受到的重力加速度。
该值可以是正数或负数,具体取决于所选轴上的加速度方向。轴选项为 XAXIS YAXISZAXIS ZAXIS
Usage:
brain_inertial.acceleration(axis)
参数 |
描述 |
|---|---|
轴 |
测量加速度的轴:
|
# 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 为 gyro_rate ,返回惯性传感器在所选轴上的当前旋转速度。
该值可以是正数也可以是负数,具体取决于传感器绕该轴旋转的方向。
Usage:
brain_inertial.gyro_rate(axis, units)
参数 |
描述 |
|---|---|
|
返回陀螺仪速率的轴:
|
|
可选。陀螺仪速率单位:
|
# 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)
参数 |
描述 |
|---|---|
|
返回方向角:
|
|
可选。方向单位: |
# 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)
构造函数#
构造函数用于手动创建 Inertial 对象,例如在 VEXcode 之外配置惯性传感器时。
Inertial#
Inertial 创建一个惯性传感器。
Usage:
Inertial(smartport)
范围 |
描述 |
|---|---|
|
可选。如果使用 IQ(第二代)大脑 内置的惯性传感器,则无需智能端口。如果连接外部惯性传感器,请指定惯性传感器所连接的智能端口,格式为 |
# 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)


