This version is outdated by a newer approved version.DiffThis version (24 Oct 2018 18:07) is a draft.
Approvals: 0/1

This is an old revision of the document!

Using the ADXRS450 Gyro for FRC in Java

This guide will walk you through the various features of the WPI Library for the ADXRS450 FRC Gyro Board and how to use it in your Java robot code. If you're looking for a guide in another language, click here for C++ or here for LabVIEW.

If you need help getting started with the basics of programming your robot in java, you may want to check out the WPI Screensteps documentation, which can be found here. Another useful resource can be found here.

Definition and Instantiation

Before you can read data from the gyro, you'll need to define and instantiate an ADXRS450_Gyro object in your code as part of the Robot class. Where exactly you decide to put it will depend on how your team organizes your robot code, but as long as it is accessible by the Robot class, you will be able to use it.

When you instantiate your gyro, you will need to define which SPI port the device will be using. Depending on how you have set up your gyro (see notes on the general FRC Gyro page), this will vary, but should usually be set to CS0. Alternatively, you can leave the port out of the instantiation and the robot will default to CS0.

Sensor Initialization and Calibration

Once your gyro is defined and instantiated, you will need to initialize it with a calibration where it will execute as soon as the robot is powered on, typically robotInit(). To do this, you will need to call the calibrate() method. Calibration is necessary for proper operation and MUST be performed before the gyro can be used. For more information, please see the general FRC Gyro page.

Using getAngle() and getRate()

Now that your sensor is calibrated you can start reading data from the gyro. You can use getAngle() to obtain the robot's current heading as a double. If you ever need it you can also request the current rotation rate using getRate(), though this is typically very rare. You'll typically use these in either autonomousPeriodic() and teleopPeriodic().

As a general note, the GetAngle() method will count continuously, meaning when it reaches 360 degrees, it will continue to 361, not zero. This is to make any functionality in your code using the gyro angle easier to implement without having to keep track of where in the 0-360 range your robot is or how many rotations have happened.

Checking Communications

If you ever need to check that the gyro is responding to the RoboRIO, you can do this by using isConnected(). It will return a Boolean indicating whether it is connected or not. You usually will not need to use this, but it's available if you need to.

Resetting the Gyro

Sometimes it may be necessary to reset the gyro's “zero degrees” position. All gyros will have some amount of drift over time and it's physically impossible to calibrate out all sources of drift. In this case, you can use reset() to reset the current gyro heading to 0 degrees. When doing this, your robot should be facing the direction you want zero degrees to be, particularly if you are driving with field-oriented drive using an omnidirectional drive base. Otherwise, your robot may begin to behave incorrectly. For this reason, you should never automate this routine. Note that this *does not* recalibrate the gyro, so you don't have to be sitting still to perform this method properly.

Drive Straight Example

To show you how you would use the gyro in your code, we'll take a look at the gyro example that comes packaged in the WPI library and modify it to use the ADXRS450 gyro instead of the analog gyro. When you open the example, it should look like this.

old open

First we will need to tell the program to import the correct package for using the ADXRS450. Change your import statement from AnalogGyro to ADXRS450_Gyro like below.


Now we need to fix all of the variable definitions. You'll need to fix the gyro port variable as well as the gyro itself, and delete the kVoltsPerDegrePerSecond variable and its related comments. In case you're using a port other than CS0, I've explicitly defined my gyro port below. If you're using CS0, you don't have to include the port.


Next you'll need to fix the method called in robotInit() to be calibrate() instead of setSensitivity(). You can also delete the argument from setSensitivity(), since calibrate does not take any arguments.


You can leave teleopPeriodic() alone since nothing is different here between using data from an analog gyro and the ADXRS450. When you're done, your code should now look like this.

new full
first/adxrs450_gyro_board_frc/java.1540397278.txt.gz · Last modified: 24 Oct 2018 18:07 by Kristen Chong