Gyro#

Introduction#

The Gyro Sensor is a motion sensor that detects rotation around a single axis. It can measure how much the robot has turned, allowing it to track orientation and heading.

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.

Below is a list of all available methods:

Actions – Calibrate or adjust the Gyro Sensor’s heading and rotation.

  • 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 – Read the Gyro Sensor’s heading, rotation, and status.

  • 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.

Callback – Run code when the Gyro Sensor detects movement or change.

  • changed – Registers a function to call when the Gyro Sensor detects change.

Constructors – Manually initialize and configure a Gyro Sensor.

  • Gyro – Creates a Gyro Sensor.

Actions#

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

Parameter

Description

This method has no parameters.

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

Parameters

Description

value

The heading value to set.

units

Optional. The unit used to represent the new heading:

  • DEGREES (default)
  • TURNS
# 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)

Parameters

Description

value

The heading value to set.

units

Optional. The unit used to represent the new rotation value:

  • DEGREES (default)
  • TURNS
# 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()

Parameters

Description

This method has no parameters.

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

Parameters

Description

This method has no parameters.

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

Getters#

heading#

heading returns the current heading of the Gyro Sensor.

Usage:
heading(units)

Parameters

Description

units

Optional. The unit used to represent the heading:

  • DEGREES (default)
  • TURNS
# 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)

Parameters

Description

units

Optional. The unit used to represent the rotation:

  • DEGREES (default)
  • TURNS
# 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)

Parameters

Description

units

Optional. The unit used to represent the gyro rate:

  • DPS (default)
  • RPM
  • PERCENT
# 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()

Parameter

Description

This method has no parameters.

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

Parameters

Description

This method has no parameters.

# Display a message if the Gyro Sensor is
# installed.
if drivetrain_gyro.installed():
    brain.screen.print("Installed!")

Callback#

changed#

changed registers a callback function for when the Gyro Sensor’s heading changes.

Usage:
changed(callback, arg)

Parameters

Description

callback

The callback function to be called when the Gyro Sensor heading changes.

arg

Optional. A tuple of arguments to pass to the callback function.

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#

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)

Parameter

Description

port

Which Smart Port that the Gyro Sensor is connected to as PORT followed by the port number, ranging from 1 to 12.

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