陀螺仪#

介绍#

陀螺仪传感器是一种运动传感器,可以检测绕单轴的旋转。它可以测量机器人的旋转角度,从而追踪机器人的方向和航向。

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

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

  • calibrate – 校准陀螺仪传感器以实现稳定的航向跟踪。

  • set_heading – 将陀螺仪传感器的航向设置为指定值。

  • set_rotation – 设置陀螺仪传感器的旋转值。

  • heading – 返回当前标题。

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

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

  • is_calibrating – 返回陀螺仪传感器是否正在校准。

  • reset_heading – 将陀螺仪传感器的航向设置为 0。

  • reset_rotation - 将陀螺仪传感器的旋转角度设置为 0。

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

  • installed – 返回陀螺仪传感器是否连接到大脑。

构造函数——手动初始化和配置陀螺仪传感器。

  • Gyro – 创建陀螺仪传感器。

方法#

校准#

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

用法:
calibrate()

范围

描述

该方法没有参数。

# Start Gyro Sensor calibration.
drivetrain_gyro.calibrate()
# Print after calibration
while drivetrain_gyro.is_calibrating():
    wait(0.1, SECONDS)
brain.screen.print("Done!")

设置标题#

set_heading 将陀螺仪传感器的航向设置为特定值。

用法:
set_heading(值,单位)

参数

描述

价值

要设置的航向值。

单位

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

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

设置旋转#

set_rotation 将陀螺仪传感器的旋转设置为特定值。

用法:
set_rotation(值,单位)

参数

描述

价值

要设置的航向值。

单位

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

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

标题#

heading 返回陀螺仪传感器的当前航向。

用法:
标题(单位)

参数

描述

单位

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

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

旋转#

rotation 返回陀螺仪传感器的当前旋转。

用法:
旋转(单位)

参数

描述

单位

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

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

速度#

“rate” 以指定单位返回陀螺仪传感器旋转的当前变化率。

用法:
rate(units)

参数

描述

单位

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

  • DPS(默认)
  • RPM
  • PERCENT
# Display the rate of change of rotation while turning
drivetrain.turn(RIGHT)
wait(1, SECONDS)
brain.screen.print(drivetrain_gyro.rate())
drivetrain.stop()

正在校准#

is_calibrating 检查陀螺仪传感器当前是否正在校准。

  • “True”——陀螺仪传感器正在校准。

  • “False”——陀螺仪传感器未校准。

用法:
is_calibrating()

范围

描述

该方法没有参数。

# Start Gyro Sensor calibration.
drivetrain_gyro.calibrate()
# Print after calibration
while drivetrain_gyro.is_calibrating():
    wait(0.1, SECONDS)
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)
drivetrain_gyro.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)
drivetrain_gyro.reset_rotation()
drivetrain.turn_to_rotation(-90, DEGREES)

改变#

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

用法:
changed(callback, arg)

参数

描述

打回来

当陀螺仪传感器航向改变时调用的回调函数。

arg

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

def gyro_changed():
    brain.screen.clear_screen()
    brain.screen.set_cursor(1, 1)
    brain.screen.print("Heading: ")
    brain.screen.print(drivetrain_gyro.heading()) 
# Display the heading when the gyro detects a change
drivetrain.turn_for(RIGHT, 90, DEGREES, wait=False)
drivetrain_gyro.changed(gyro_changed)

已安装#

“installed”返回一个布尔值,指示陀螺仪传感器是否连接到大脑。

  • “True”——陀螺仪传感器已连接到大脑。

  • “False”——陀螺仪传感器未连接到大脑。

用法:
installed()

参数

描述

该方法没有参数。

# Display a message if the Gyro Sensor is
# installed.
if drivetrain_gyro.installed():
    brain.screen.print("Installed!")

构造函数#

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

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

陀螺仪#

Gyro 创建陀螺仪传感器。

用法:
陀螺仪(端口)

范围

描述

端口

陀螺仪传感器连接到哪个智能端口,以“PORT”表示,后跟端口号,范围从 1 到 12。

# Create the Brain
brain = Brain()
# When constructing Gyro Sensor "drivetrain_gyro" with a 
# SmartDrive "drivetrain"
drivetrain_gyro = Gyro(Ports.PORT1)
drivetrain = SmartDrive(left_drive_smart, right_drive_smart, drivetrain_gyro, 200)

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