Gyro#
Initializing the Gyro Class#
The Gyro
constructor creates a Gyro object in the specified Smart Port:
This constructor uses one parameter:
Parameter |
Description |
---|---|
|
A valid Smart Port that the Gyro Sensor is connected to. |
# Create the Brain.
brain = Brain()
# Construct a Gyro Sensor "gyro" with the
# Gyro class.
gyro = Gyro(Ports.PORT1)
This gyro
object will be used in all subsequent examples throughout this API documentation when referring to Gyro class methods.
Class Methods#
calibrate()#
The calibrate()
method calibrates the Gyro Sensor. Calibration should be done when the Gyro Sensor is not moving.
Returns: None.
# 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.
Returns: None.
set_heading()#
The set_heading(value, units)
method sets the heading of the Gyro Sensor to a specific value.
Parameters |
Description |
---|---|
value |
The value to use for the new heading. |
units |
Optional. A valid RotationUnits type. The default is |
Returns: None.
# 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.
Parameters |
Description |
---|---|
value |
The rotation value to set. |
units |
Optional. A valid RotationUnits type. The default is |
Returns: None.
# 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.
Parameters |
Description |
---|---|
units |
Optional. A valid RotationUnits type. The default is |
Returns: The current heading of the Gyro Sensor in the specified units.
# 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.
Parameters |
Description |
---|---|
units |
Optional. A valid RotationUnits type. The default is |
Returns: The current rotation of the Gyro Sensor in the specified units.
# 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.
Parameters |
Description |
---|---|
turntype |
A valid TurnType. |
Returns: None.
# 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.
Returns: The turn type of the Gyro Sensor in the specified units.
changed()#
The changed(callback, arg)
method registers a callback function for when the Gyro Sensor’s heading changes.
Parameters |
Description |
---|---|
callback |
The callback function to be called when the Gyro Sensor’s heading changes. |
arg |
Optional. A tuple of arguments to pass to the callback function. |
Returns: An instance of the Event class.
# 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)
rate()#
The rate(units)
method returns the current rate of change of the Gyro Sensor of rotation in specified units.
Parameters |
Description |
---|---|
units |
Optional. A valid VelocityUnit. The default is |
Returns: The gyro rate of rotation in the specified units.
installed()#
The installed()
method returns if the Gyro Sensor is connected to the Brain.
Returns: True
if the Gyro Sensor is connected to the Brain. False
if it is not.