Gyro Sensor#

Introduction#

The gyro class is used to measure the robot’s heading and rotational movement, allowing tracking of orientation over time.

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 for stable heading tracking.

  • setHeading — Sets the heading of the gyro.

  • setRotation — Sets the current rotation value of the gyro.

  • heading — Returns the heading of the gyro.

  • rotation — Returns the rotation of the gyro.

  • rate — Returns the turning rate of the gyro.

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
  • calibrate should only be called when the sensor is not moving.

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

setHeading#

Sets the heading of the Gyro Sensor to a specific value.

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 rotation of the Gyro Sensor to a specific value.

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.

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.

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.

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

Parameters

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

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

      Return Values

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