gyro#

Introduction#

The VEX IQ Gyro Sensor allows for measuring rotational movement and detecting changes in motion. These sensors allow the robot to track its orientation and heading.

Below is a list of all available methods:

Methods – Measure rotational movement and detect changes in motion.

  • calibrate – Calibrates the Gyro Sensor for stable heading tracking.

  • setHeading – Sets the Gyro Sensor’s heading to a specified value.

  • setRotation – Sets the Gyro Sensor’s rotation value.

  • heading – Returns the current heading.

  • rotation – Returns the cumulative rotation.

  • rate – Returns the angular velocity around the x, y, or z axis.

  • isCalibrating – Returns whether or not the Gyro Sensor is calibrating.

  • resetHeading – Sets the heading of the Gyro Sensor to 0.

  • resetRotation – Sets the rotation of the Gyro Sensor to 0.

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

  • installed – Returns whether or not a Gyro Sensor is connected to the Brain.

Constructors – Manually initialize and configure a Gyro Sensor.

  • gyro – Creates a Gyro Sensor.

Methods#

calibrate#

calibrate calibrates the Gyro Sensor. Calibration should be done when the Gyro Sensor is not moving.

Default Usage:
calibrate(type)

Parameters

Description

type

One of the valid calibration types:

  • calExtended – An extended calibration.
  • calNormal – Normal calibration speed.
  • calSlow – Slow calibration speed.
// Start calibration.
DrivetrainGyro.calibrate();
Brain.Screen.print("Done calibrating");

Overload Usage:
calibrate(type, wait)

Parameters

Description

type

One of the valid calibration types:

  • calExtended – An extended calibration.
  • calNormal – Normal calibration speed.
  • calSlow – Slow calibration speed.

wait

Whether or not to wait before executing the next command:

  • true – Wait before executing the next command.
  • false – Do not wait before executing the next command.

// Do not wait after starting calibration
DrivetrainGyro.calibrate(calNormal, false);
Brain.Screen.print("Done calibrating");

setHeading#

setHeading sets the heading of the Gyro Sensor to a specific value.

Usage:
setHeading(value, units)

Parameters

Description

value

The heading value to set.

units

The unit that represents the new heading:

  • degrees
  • turns
// Turn the robot around
DrivetrainGyro.setHeading(180, degrees);
Drivetrain.turnToHeading(0, degrees);

setRotation#

setRotation sets the rotation of the Gyro Sensor to a specific value.

Usage:
setRotation(value, units)

Parameters

Description

value

The rotation value to set.

units

The unit that represents the new rotation:

  • degrees
  • turns
// Turn the robot around
DrivetrainGyro.setRotation(-180, degrees);
Drivetrain.turnToRotation(0, degrees);

heading#

heading returns the current heading of the Gyro Sensor in degrees.

Usage:
heading()

Parameters

Description

This method has no parameters.

// Display the heading after turning
Drivetrain.turnFor(right, 450, degrees);
Brain.Screen.print("%f", DrivetrainGyro.heading());

Overload:
heading(units)

Parameters

Description

units

The unit used to represent the heading:

  • turns
// Display the heading after turning
Drivetrain.turnFor(right, 450, degrees);
Brain.Screen.print("%f", DrivetrainGyro.heading(turns));

rotation#

rotation returns the current rotation of the Gyro Sensor in degrees.

Usage:
rotation()

Parameters

Description

This method has no parameters.

// Display the rotation after turning
Drivetrain.turnFor(right, 450, degrees);
Brain.Screen.print("%f", DrivetrainGyro.rotation());

Overload:
rotation(units)

Parameters

Description

units

The unit used to represent the rotation:

  • turns
// Display the rotation after turning
Drivetrain.turnFor(right, 450, degrees);
Brain.Screen.print("%f", DrivetrainGyro.rotation(turns));

rate#

rate returns the current rate of change of the Gyro Sensor’s rotation in dps.

Usage:
rate()

Parameters

Description

This method has no parameters.

// Display the rate of change of rotation while turning
Drivetrain.turn(right);
wait(1, seconds);
Brain.Screen.print("%f", DrivetrainGyro.rate());
Drivetrain.stop();

Overload:
rate(units)

Parameters

Description

units

The unit used to represent the gyro rate:

  • rateUnits::rps
// Display the rate of change of rotation while turning
Drivetrain.turn(right);
wait(1, seconds);
Brain.Screen.print("%f", DrivetrainGyro.rate(rateUnits::rps));
Drivetrain.stop();

isCalibrating#

isCalibrating checks if the Gyro Sensor is currently calibrating.

  • 1 - The Gyro Sensor is calibrating.

  • 0 - The Gyro Sensor is not calibrating.

Parameters

Description

This method has no parameters.

wait(1, seconds);
// Calibrate without waiting
DrivetrainGyro.calibrate(0);
// Print that the Gyro is calibrating while waiting for it to
// finish calibrating
while(DrivetrainGyro.isCalibrating()){
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Gyro Calibrating");
  wait(50, msec);
}
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
Brain.Screen.print("Done!");

resetHeading#

resetHeading resets the heading of the Gyro Sensor to 0.

Usage:
resetHeading()

Parameters

Description

This method has no parameters.

// Turn the robot before and after resetting the heading
Drivetrain.turnToHeading(90, degrees);
wait(0.5, seconds);
DrivetrainGyro.resetHeading();
Drivetrain.turnToHeading(90, degrees);

resetRotation#

resetRotation resets the rotation of the Gyro Sensor to 0.

Usage:
resetRotation()

Parameters

Description

This method has no parameters.

// Turn the robot before and after resetting the rotation
Drivetrain.turnToRotation(-90, degrees);
wait(0.5, seconds);
DrivetrainGyro.resetRotation();
Drivetrain.turnToRotation(-90, degrees);

changed#

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

Usage:
changed()

Parameters

Description

callback

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

Returns: None.

// Callback function to print the heading
void gyroChanged(){
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);
  Brain.Screen.print("Heading: ");
  Brain.Screen.print("%f", DrivetrainGyro.heading());
}
    
int main() {
  vexcodeInit();
  // Display the heading when the gyro detects a change
  Drivetrain.turnFor(90, rotationUnits::deg, 50, velocityUnits::pct, false);
  DrivetrainGyro.changed(gyroChanged);
}

installed#

installed returns a Boolean indicating whether the Gyro Sensor is connected to the Brain.

  • 1 - The Gyro Sensor is connected to the Brain.

  • 0 - 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 (DrivetrainGyro.installed()){
  Brain.Screen.print("Installed!");
}

Constructors#

Constructors are used to manually create gyro objects, which are necessary for configuring an Gyro Sensor outside of VEXcode.

For the examples below, the configured Gyro Sensor will be named DrivetrainGyro and will be used in all subsequent examples throughout this API documentation when referring to gyro class methods.

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.

// When constructing Gyro Sensor "drivetrain_gyro" with a 
// SmartDrive "drivetrain"
motor LeftDriveSmart = motor(PORT2, 1, false);
motor RightDriveSmart = motor(PORT3, 1, true);
gyro DrivetrainGyro = gyro(PORT1, true);
smartdrive Drivetrain = smartdrive(LeftDriveSmart, RightDriveSmart, DrivetrainGyro, 200);

DrivetrainGyro.setHeading(180, degrees);
Drivetrain.turnToHeading(0, degrees);