陀螺仪#
介绍#
陀螺仪传感器是一种运动传感器,可以检测绕单轴的旋转。它可以测量机器人的旋转角度,从而追踪机器人的方向和航向。
本页面使用 drivetrain_gyro 作为陀螺仪传感器名称示例。请根据需要将其替换为您自己配置的名称。
以下是所有可用方法的列表:
操作——校准或调整陀螺仪传感器的航向和旋转角度。
calibrate— 校准陀螺仪传感器以实现稳定的航向跟踪。set_heading— 将陀螺仪传感器的航向设置为指定值。set_rotation— 设置陀螺仪传感器的旋转值。reset_heading— 将陀螺仪传感器的航向设置为 0。reset_rotation— 将陀螺仪传感器的旋转设置为 0。
获取器 — 读取陀螺仪传感器的航向、旋转和状态。
heading— 返回当前标题。rotation— 返回累积旋转。rate— 返回绕 x、y 或 z 轴的角速度。is_calibrating— 返回陀螺仪传感器是否正在校准。installed— 返回陀螺仪传感器是否连接到大脑。
回调函数——当陀螺仪传感器检测到运动或变化时运行代码。
changed— 注册一个当陀螺仪传感器检测到变化时要调用的函数。
构造函数——手动初始化和配置陀螺仪传感器。
Gyro— 创建一个陀螺仪传感器。
行动#
calibrate#
calibrate 校准陀螺仪传感器。所有后续代码行将等待校准完成后再执行。校准是一个内部程序,用于测量和补偿传感器在指定时间段内的噪声和漂移。在此期间,陀螺仪传感器必须保持完全静止(即,放置在稳定的表面上,没有任何外部移动)。校准期间的移动会导致结果不准确。
Usage:
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 将陀螺仪传感器的航向设置为特定值。
Usage:
set_heading(value, units)
参数 |
描述 |
|---|---|
价值 |
要设置的航向值。 |
单位 |
可选。用于表示新航向的单位:
|
# Turn the robot around
drivetrain_gyro.set_heading(180)
drivetrain.turn_to_heading(0)
set_rotation#
set_rotation 将陀螺仪传感器的旋转角度设置为特定值。
Usage:
set_rotation(value, units)
参数 |
描述 |
|---|---|
|
要设置的航向值。 |
|
可选。用于表示新旋转值的单位:
|
# Turn the robot around
drivetrain_gyro.set_rotation(-180)
drivetrain.turn_to_rotation(0)
reset_heading#
reset_heading 将陀螺仪传感器的航向重置为 0。
Usage:
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#
reset_rotation 将陀螺仪传感器的旋转重置为 0。
Usage:
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)
吸气剂#
heading#
heading 返回陀螺仪传感器的当前航向。
Usage:
heading(units)
参数 |
描述 |
|---|---|
|
可选。用于表示航向的单位:
|
# Display the heading after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print(drivetrain_gyro.heading())
rotation#
rotation 返回陀螺仪传感器的当前旋转角度。
Usage:
rotation(units)
参数 |
描述 |
|---|---|
|
可选。旋转单位:
|
# Display the rotation after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print(drivetrain_gyro.rotation())
rate#
rate 返回陀螺仪传感器旋转的当前变化率,单位为指定单位。
Usage:
rate(units)
参数 |
描述 |
|---|---|
|
可选。陀螺仪速率的单位:
|
# 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#
is_calibrating 检查陀螺仪传感器当前是否正在校准。
True— 陀螺仪传感器正在校准。False— 陀螺仪传感器未校准。
Usage:
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!")
installed#
installed 返回一个布尔值,指示陀螺仪传感器是否连接到大脑。
True— 陀螺仪传感器与大脑相连。False— 陀螺仪传感器未连接到大脑。
Usage:
installed()
参数 |
描述 |
|---|---|
该方法没有参数。 |
# Display a message if the Gyro Sensor is
# installed.
if drivetrain_gyro.installed():
brain.screen.print("Installed!")
打回来#
changed#
changed 注册一个回调函数,用于在陀螺仪传感器的航向发生变化时执行此操作。
Usage:
changed(callback, 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)
构造函数#
构造函数用于手动创建 Gyro 对象,这是在 VEXcode 之外配置陀螺仪传感器所必需的。
Gyro#
Gyro 创建一个陀螺仪传感器。
Usage:
Gyro(smartport)
范围 |
描述 |
|---|---|
|
陀螺仪传感器连接的智能端口,记为 PORTx,其中 x 是端口号。 |
# 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)