陀螺仪#

初始化陀螺仪类#

使用以下构造函数创建陀螺仪传感器:

陀螺仪(端口)

此构造函数使用一个参数:

范围

描述

端口

陀螺仪传感器所连接的 3 线端口,无论是 Brain 上的端口,还是 3 线扩展器 上的端口。

必须先创建 Brain3-Wire Expander,然后才能使用 Gyro 类构造函数创建对象。

# Create the Brain.
brain = Brain()
# Construct a Gyro Sensor "gyro" with the
# Gyro class.
gyro = Gyro(brain.three_wire_port.a)

当引用 Gyro 类方法时,此“gyro”对象将在整个 API 文档的所有后续示例中使用。

类方法#

calibrate()#

calibrate() 方法用于校准陀螺仪传感器。校准应在陀螺仪传感器静止不动时进行。

**返回:**无。

# 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()#

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

返回: 如果陀螺仪传感器正在校准,则返回 True。否则,返回 False

# 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()#

reset_heading() 方法将陀螺仪传感器的航向重置为 0。**返回:**无。

reset_rotation()#

reset_rotation() 方法将陀螺仪传感器的旋转度重置为 0。

**返回:**无。

set_heading()#

set_heading(value, units) 方法将陀螺仪传感器的航向设置为特定值。

参数

描述

价值

用于新标题的值。

单位

**可选。**有效的 RotationUnits 类型。默认值为 DEGREES

**返回:**无。

# Set the value of the Gyro Sensor's heading to 180 degrees.
gyro.set_heading(180)

set_rotation()#

set_rotation(value, units) 方法设置陀螺仪传感器的旋转。

参数

描述

价值

要设置的旋转值。

单位

**可选。**有效的 RotationUnits 类型。默认值为 DEGREES

**返回:**无。

# Set the value of the Gyro Sensor's rotation to 180 degrees.
gyro.set_rotation(180)

heading()#

heading(units) 方法返回陀螺仪传感器的当前航向。

参数

描述

单位

**可选。**有效的 RotationUnits 类型。默认值为 DEGREES

**返回:**陀螺仪传感器的当前航向(以指定的单位表示)。

# Print the value of the current heading of the Gyro Sensor
# to the Brain's screen.
brain.screen.print(gyro.heading())

rotation()#

rotation(units) 方法返回陀螺仪传感器的当前旋转。

参数

描述

单位

**可选。**有效的 RotationUnits 类型。默认值为 DEGREES

**返回:**陀螺仪传感器以指定单位表示的当前旋转。

# Print the value of the current rotation of the Gyro Sensor
# to the Brain's screen.
brain.screen.print(gyro.rotation())

set_turn_type()#

set_turn_type(turntype) 方法设置返回正值的方向。

参数

描述

转盘式

有效的 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()#

get_turn_type() 方法返回的是返回正值的方向。

**返回:**指定单位的陀螺仪传感器的转动类型。

changed()#

changed(callback, arg) 方法注册陀螺仪传感器航向改变时的回调函数。

参数

描述

打回来

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

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)