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 |
|---|---|---|
|
|
The Smart Port that the Gyro Sensor is connected to, written as |
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 Functionsvoid calibrate();
This function does not accept any parameters.
Return ValuesThis function does not return a value.
NotesCalibration 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.
// 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 Functionsvoid setHeading(
double value,
rotationUnits units );
Parameters |
Type |
Description |
|---|---|---|
|
|
The value to use for the new heading. |
|
|
The unit that represents the heading:
|
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 Functionsvoid setRotation(
double value,
rotationUnits units );
Parameters |
Type |
Description |
|---|---|---|
|
|
The value to use for the new rotation value. |
|
|
The unit that represents the rotation value:
|
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 Functionsdouble heading(
rotationUnits units = degrees );
Parameter |
Type |
Description |
|---|---|---|
|
|
The unit that represents the heading:
|
Returns a double representing the current heading of the Gyro Sensor in the specified units, defaulting to degrees.
// 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 Functionsdouble rotation(
rotationUnits units = degrees );
Parameter |
Type |
Description |
|---|---|---|
|
|
The unit that represents the rotation value:
|
Returns a double representing the current rotation value of the Gyro Sensor in the specified units, defaulting to degrees.
// 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 Functionsdouble rate(
rateUnits units = rateUnits::dps );
| Parameter | Type | Description |
| :—————-: | :————————————– |
| units | rateUnits | The unit that represents the rate:
rateUnits::dps(default) — degrees per secondrateUnits::rpm— rotations per minute
Returns a double representing the current rate of change of the Gyro Sensor in the specified units, defaulting to rateUnits::dps.