Gyro Sensor#

Introduction#

The gyro class is used with a Gyro Sensor to measure rotational movement around a single axis. It can track which direction the robot is facing and how far the robot has turned.

Class Constructor#

gyro(
    int32_t index );

Class Destructor#

Destroys the gyro object and releases associated resources.

virtual ~gyro();

Parameters#

Parameter

Type

Description

index

int32_t

The Smart Port that the Gyro Sensor is connected to, written as PORTx, where x is the port number (for example, PORT1).

Example#

// Create a gyro instance in Port 1
gyro Gyro1 = gyro(PORT1);

Member Functions#

The gyro class includes the following member functions:

  • calibrate — Calibrates the Gyro Sensor.

  • setHeading — Sets the Gyro Sensor’s current heading to a new heading value.

  • setRotation — Sets the Gyro Sensor’s current rotation to a new rotation value.

  • heading — Returns the direction the Gyro Sensor is facing.

  • rotation — Returns how far the Gyro Sensor has turned.

  • rate — Returns how fast the Gyro Sensor is rotating.

Before calling any gyro member functions, a gyro instance must be created, as shown below:

/* This constructor is required when using VS Code.
Gyro Sensor configuration is generated automatically
in VEXcode using the Device Menu. Replace the values
as needed. */

// Create a gyro instance in Port 1
gyro Gyro1 = gyro(PORT1);

calibrate#

Calibrates the Gyro Sensor.

Available Functions
void calibrate();

Parameters

This function does not accept any parameters.

Return Values

This function does not return a value.

Notes
  • Calibration helps the Gyro Sensor measure turns correctly. Keep the sensor still during calibration. If the sensor moves during calibration, heading, rotation, and rate values may not measure turns correctly.

Examples
// Calibrate the Gyro Sensor before turning.
Gyro1.calibrate();

setHeading#

Sets the Gyro Sensor’s current heading to a specific value.

A heading is the direction the Gyro Sensor is facing, measured from 0 to 359.99 degrees. Setting the heading changes the sensor’s current direction reference.

Available Functions
void setHeading(
  double        value,
  rotationUnits units );

Parameters

Type

Description

value

double

The value to use for the new heading.

units

rotationUnits

The unit that represents the heading:

  • degrees
  • turns

Return Values

This function does not return a value.

setRotation#

Sets the Gyro Sensor’s current rotation to a specific value.

Rotation is how much the Gyro Sensor has turned. Unlike heading, rotation can increase past 359.99 degrees or decrease below 0 degrees.

Available Functions
void setRotation(
  double        value,
  rotationUnits units );

Parameters

Parameters

Type

Description

value

double

The value to use for the new rotation value.

units

rotationUnits

The unit that represents the rotation value:

  • degrees
  • turns

Return Values

This function does not return a value.

heading#

Returns the current heading of the Gyro Sensor.

A heading is the direction the Gyro Sensor is facing, measured from 0 to 359.99 degrees. If the sensor turns past 359.99 degrees, the heading wraps back to 0 degrees.

Available Functions
double heading(
  rotationUnits units = degrees );

Parameters

Parameter

Type

Description

units

rotationUnits

The unit that represents the heading:

  • degrees (default)
  • turns

Return Values

Returns a double representing the current heading of the Gyro Sensor in the specified units, defaulting to degrees.

Examples
// Get the current heading for the Gyro.
double value = Gyro1.heading();
Brain.Screen.print(value);

rotation#

Returns the current rotation of the Gyro Sensor.

Rotation is how much the Gyro Sensor has turned. Unlike heading, rotation can increase past 359.99 degrees or decrease below 0 degrees. For example, making two full turns to the right returns a rotation of 720 degrees. Turning one full turn to the left from 0 degrees returns a rotation of -360 degrees.

Available Functions
double rotation(
  rotationUnits units = degrees );

Parameters

Parameter

Type

Description

units

rotationUnits

The unit that represents the rotation value:

  • degrees (default)
  • turns

Return Values

Returns a double representing the current rotation value of the Gyro Sensor in the specified units, defaulting to degrees.

Examples
// Get the current rotation for the Gyro.
double value = Gyro1.rotation();
Brain.Screen.print(value);

rate#

Returns the turning rate of the Gyro Sensor.

Turning rate is how fast the Gyro Sensor is rotating. The value can be positive or negative depending on the direction the sensor is rotating.

Available Functions
double rate(
  rateUnits units = rateUnits::dps );

Parameters

| Parameter | Type | Description | | :—————-: | :————————————– | | units | rateUnits | The unit that represents the rate:

  • rateUnits::dps (default) — degrees per second
  • rateUnits::rpm — rotations per minute
|

Return Values

Returns a double representing the current rate of change of the Gyro Sensor in the specified units, defaulting to rateUnits::dps.