陀螺仪#
初始化陀螺仪类#
使用以下构造函数创建陀螺仪传感器:
Gyro(port)
此构造函数使用一个参数:
范围 |
描述 |
---|---|
|
必须先创建 Brain 或 3-Wire Expander,然后才能使用 Gyro 类构造函数创建对象。
# Create the Brain.
brain = Brain()
# Construct a Gyro Sensor "gyro" with the
# Gyro class.
gyro = Gyro(brain.three_wire_port.a)
This gyro
object will be used in all subsequent examples throughout this API documentation when referring to Gyro class methods.
类方法#
calibrate()#
The calibrate()
method calibrates the Gyro Sensor. Calibration should be done when the Gyro Sensor is not moving.
**返回:**无。
# Start Gyro Sensor calibration.
gyro.calibrate()
# Print that the Gyro Sensor is calibrating while waiting for it to finish calibrating.
while gyro.is_calibrating():
brain.screen.clear()
brain.screen.print("Gyro Sensor is calibrating.")
wait(50, MSEC)
is_calibrating()#
The is_calibrating()
method checks if the Gyro Sensor is currently calibrating.
Returns: True
if the Gyro Sensor is calibrating. False
if it is not.
# Start Gyro Sensor calibration.
gyro.calibrate()
# Print that the Gyro Sensor is calibrating while waiting for it to finish calibrating.
while gyro.is_calibrating():
brain.screen.clear()
brain.screen.print("Gyro Sensor is calibrating.")
wait(50, MSEC)
reset_heading()#
The reset_heading()
method resets the heading of the Gyro Sensor to 0.
Returns: None.
reset_rotation()#
The reset_rotation()
method resets the rotation of the Gyro Sensor to 0.
**返回:**无。
set_heading()#
The set_heading(value, units)
method sets the heading of the Gyro Sensor to a specific value.
参数 |
描述 |
---|---|
价值 |
用于新标题的值。 |
单位 |
Optional. A valid RotationUnits type. The default is |
**返回:**无。
# Set the value of the Gyro Sensor's heading to 180 degrees.
gyro.set_heading(180)
set_rotation()#
The set_rotation(value, units)
method sets the rotation of the Gyro Sensor.
参数 |
描述 |
---|---|
价值 |
要设置的旋转值。 |
单位 |
Optional. A valid RotationUnits type. The default is |
**返回:**无。
# Set the value of the Gyro Sensor's rotation to 180 degrees.
gyro.set_rotation(180)
heading()#
The heading(units)
method returns the current heading of the Gyro Sensor.
参数 |
描述 |
---|---|
单位 |
Optional. A valid RotationUnits type. The default is |
**返回:**陀螺仪传感器的当前航向(以指定的单位表示)。
# Print the value of the current heading of the Gyro Sensor
# to the Brain's screen.
brain.screen.print(gyro.heading())
rotation()#
The rotation(units)
method returns the current rotation of the Gyro Sensor.
参数 |
描述 |
---|---|
单位 |
Optional. A valid RotationUnits type. The default is |
**返回:**陀螺仪传感器以指定单位表示的当前旋转。
# Print the value of the current rotation of the Gyro Sensor
# to the Brain's screen.
brain.screen.print(gyro.rotation())
set_turn_type()#
The set_turn_type(turntype)
method sets the direction that returns positive values for the heading.
参数 |
描述 |
---|---|
转盘式 |
有效的 TurnType。 |
**返回:**无。
# Set the turn type to left.
gyro.set_turn_type(LEFT)
# Set the turn type to right.
gyro.set_turn_type(RIGHT)
get_turn_type()#
The get_turn_type()
method returns the direction that returns positive values for heading.
**返回:**指定单位的陀螺仪传感器的转动类型。
changed()#
The changed(callback, arg)
method registers a callback function for when the Gyro Sensor’s heading changes.
参数 |
描述 |
---|---|
打回来 |
当陀螺仪传感器的航向改变时调用的回调函数。 |
arg |
**可选。**传递给回调函数的参数元组。 |
**返回:**事件类的一个实例。
# Define a function gyro_changed.
def gyro_changed():
# The Brain will print that the heading changed on
# the Brain's screen.
brain.screen.print("heading changed")
# Run gyro_changed() when the Gyro Sensor's heading changes.
gyro.changed(gyro_changed)