陀螺仪#
介绍#
陀螺仪传感器是一种运动传感器,可以检测绕单轴的旋转。它可以测量机器人的旋转角度,从而追踪机器人的方向和航向。
For the examples below, the configured Gyro Sensor will be named drivetrain_gyro
and will be used in all subsequent examples throughout this API documentation when referring to Gyro
class methods.
以下是所有可用方法的列表:
操作——校准或调整陀螺仪传感器的航向和旋转。
calibrate
– Calibrates the Gyro Sensor for stable heading tracking.set_heading
– Sets the Gyro Sensor’s heading to a specified value.set_rotation
– Sets the Gyro Sensor’s rotation value.reset_heading
– Sets the heading of the Gyro Sensor to 0.reset_rotation
– Sets the rotation of the Gyro Sensor to 0.
Getters——读取陀螺仪传感器的航向、旋转和状态。
heading
– Returns the current heading.rotation
– Returns the cumulative rotation.rate
– Returns the angular velocity around the x, y, or z axis.is_calibrating
– Returns whether or not the Gyro Sensor is calibrating.installed
– Returns whether or not a Gyro Sensor is connected to the Brain.
回调——当陀螺仪传感器检测到运动或变化时运行代码。
changed
– Registers a function to call when the Gyro Sensor detects change.
构造函数——手动初始化和配置陀螺仪传感器。
Gyro
– Creates a Gyro Sensor.
行动#
calibrate#
calibrate
calibrates the Gyro 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 Gyro Sensor must remain completely still (i.e., on a stable surface without any external movement). Movement during calibration will produce inaccurate results.
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
sets the heading of the Gyro Sensor to a specific value.
Usage:
set_heading(value, units)
参数 |
描述 |
---|---|
价值 |
要设置的航向值。 |
单位 |
Optional. The unit used to represent the new heading:
|
# Turn the robot around
drivetrain_gyro.set_heading(180)
drivetrain.turn_to_heading(0)
set_rotation#
set_rotation
sets the rotation of the Gyro Sensor to a specific value.
Usage:
set_rotation(value, units)
参数 |
描述 |
---|---|
价值 |
要设置的航向值。 |
单位 |
Optional. The unit used to represent the new rotation value:
|
# Turn the robot around
drivetrain_gyro.set_rotation(-180)
drivetrain.turn_to_rotation(0)
reset_heading#
reset_heading
resets the heading of the Gyro Sensor to 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
resets the rotation of the Gyro Sensor to 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
returns the current heading of the Gyro Sensor.
Usage:
heading(units)
参数 |
描述 |
---|---|
单位 |
Optional. The unit used to represent the heading:
|
# Display the heading after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print(drivetrain_gyro.heading())
rotation#
rotation
returns the current rotation of the Gyro Sensor.
Usage:
rotation(units)
参数 |
描述 |
---|---|
单位 |
Optional. The unit used to represent the rotation:
|
# Display the rotation after turning
drivetrain.turn_for(RIGHT, 450, DEGREES)
brain.screen.print(drivetrain_gyro.rotation())
rate#
rate
returns the current rate of change of the Gyro Sensor’s rotation in specified units.
Usage:
rate(units)
参数 |
描述 |
---|---|
单位 |
Optional. The unit used to represent the gyro rate:
|
# 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
checks if the Gyro Sensor is currently calibrating.
True
- The Gyro Sensor is calibrating.False
- The Gyro Sensor is not calibrating.
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
returns a Boolean indicating whether the Gyro Sensor is connected to the Brain.
True
- The Gyro Sensor is connected to the Brain.False
- The Gyro Sensor is not connected to the Brain.
Usage:
installed()
参数 |
描述 |
---|---|
该方法没有参数。 |
# Display a message if the Gyro Sensor is
# installed.
if drivetrain_gyro.installed():
brain.screen.print("Installed!")
打回来#
changed#
changed
registers a callback function for when the Gyro Sensor’s heading changes.
Usage:
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)
构造函数#
Constructors are used to manually create Gyro
objects, which are necessary for configuring an Gyro Sensor outside of VEXcode.
Gyro#
Gyro
creates a Gyro Sensor.
Usage:
Gyro(port)
范围 |
描述 |
---|---|
|
Which Smart Port that the Gyro Sensor is connected to as |
# 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)