gyro#

To make gyro commands appear in VEXcode V5, a 3-Wire Gyro Sensor must be configured in the Devices window.

For more information, refer to these articles:

Initializing the gyro Class#

A Gyro Sensor is created by using the following constructor:

The gyro constructor creates a gyro object in the specified Three Wire Port:

Parameter

Description

port

The 3-Wire Port that the Gyro Sensor is connected to, whether it’s a port on the Brain, or a 3-Wire Expander.

A Brain or 3-Wire Expander must be created first before they can be used to create an object with the gyro Class constructor.

// Create the Brain.
brain Brain;
// Construct a Gyro Sensor "Gyro" with the
// gyro class.
gyro Gyro = gyro(Brain.ThreeWirePort.A);

This Gyro object will be used in all subsequent examples throughout this API documentation when referring to gyro class methods.

Class Methods#

calibrate()#

This method is called in the following ways:

The calibrate() method calibrates the Gyro Sensor. Calibration should be done when the Gyro Sensor is not moving.

Returns: None.

// Start calibration.
Gyro.calibrate();

The calibrate(int32_t value) method calibrates the Gyro Sensor. Calibration should be done when the Gyro Sensor is not moving.

Parameters

Description

value

Sets the amount of calibration time. The default is 0.

Returns: None.

// Start calibration with the calibration time set to 3 seconds.
Gyro.calibrate(3);

isCalibrating()#

The isCalibrating() method checks if the Gyro Sensor is currently calibrating.

Returns: true if the Gyro is calibrating. false if it is not.

// Start calibration.
Gyro.calibrate();
// Print that the Gyro is calibrating while waiting for it to
// finish calibrating.
while(Gyro.isCalibrating()){
    Brain.Screen.clear();
    Brain.Screen.print("Gyro Calibrating");
    wait(50, MSEC);
}

resetAngle()#

This method is called in the following ways:

The resetAngle() method resets the angle of the Gyro Sensor to 0.

Returns: None.

The resetAngle(double value, rotationUnits units) method resets the angle of the Gyro Sensor to a specified value.

Parameters

Description

value

The value to use for the new angle.

units

A valid rotationUnit.

Returns: None.

resetHeading()#

The resetHeading() method resets the heading of the Gyro Sensor to 0.

Returns: None.

resetRotation()#

The resetHeresetRotationading() method resets the rotation of the Gyro Sensor to 0.

Returns: None.

setHeading()#

The setHeading(double value, rotationUnits 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

A valid rotationUnit.

Returns: None.

angle()#

The angle(rotationUnits units) method returns the current angle of the Gyro Sensor.

Parameters

Description

units

A valid rotationUnit. The default is degrees.

Returns: A double representing current angle of the Gyro Sensor in the specified units.

// Get the current angle for the Gyro.
double value = Gyro.angle()

heading()#

The heading(rotationUnits units) method returns the current heading of the Gyro Sensor.

Parameters

Description

units

A valid rotationUnit. The default is degrees.

Returns: A double representing current heading of the Gyro Sensor in the specified units.

// Get the current heading for the Gyro.
double value = Gyro.heading()

setRotation()#

The setRotation(double value, rotationUnits units) method sets the rotation of the Gyro Sensor to a specific value.

Parameters

Description

value

The value to use for the new rotation.

units

A valid rotationUnit.

Returns: None.

rotation()#

The rotation(rotationUnits units) method returns the current rotation of the Gyro Sensor.

Parameters

Description

units

A valid rotationUnit. The default is degrees.

Returns: A double representing current rotation of the Gyro Sensor in the specified units.

// Get the current rotation for the Gyro.
double value = Gyro.rotation()

getTurnType()#

The getTurnType() method returns the direction that returns positive heading values with the Gyro Sensor.

Returns: The turn type of the Gyro Sensor in the specified units.

changed()#

The changed(callback) 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.

Returns: An instance of the Event class.

// Define the GyroChanged function with a void return type,
// showing it doesn't return a value.
void GyroChanged() {
  // The Brain will print that the Gyro Sensor changed on the
  // Brain's screen.
  Brain.Screen.print("Gyro changed");
}

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Turn the robot right.
  Drivetrain.turn(right);

  // Run GyroChanged when the value of the
  // Gyro Sensor changes.
  Gyro.changed(GyroChanged);
}