Gyro turning

I’m trying to write a simple Java code that will make our robot turn clockwise for a second and then turn counterclockwise until it reaches the original heading. I use gyro.reset() in autonomousInit() and then in autonomousPeriodic() have the following code:

if(timer.get()<1) {drivetrain.tankDrive(-0.7, 0.7);}

else if (gyro.getAngle() > 0) {drivetrain.tankDrive(0.7, -0.7); }

else {drivetrain.arcadeDrive(0, 0);}

//end code

Currently our robot will turn clockwise for a second and then keep turning counterclockwise without stopping. Is there a way to make this code work/to test if our gyro is returning the correct angles?

In the auton code, put

SmartDashboard.putNumber("Gyro angle", gyro.getAngle());

That’ll put the gyroscope’s reading on the Smart Dashboard so you can check and see if it’s correct.

How could I use what you just did with a FRC ADXRS450 Gyro Board that sits onto the roborio SPI slot? Is there a special line I put in for this specific gyro and location or can I just plug it in and paste your code and it work?

You’ll need to create the “gyro” object with something like “ADXRS450_Gyro gyro = new ADXRS450_Gyro();”

Where would I put this?

We normally make the gyro an object in our chasis/base SubSystem and provide external access to it via a public GetAngle() method.

You put it in whatever class you need to use the gyro in. It can only be instantiated in one place though. If you’re doing a very simple auto program all within, then you’d add this line in the same place that you instantiate other hardware such as your motor controllers in
We typically declare the gyro in class declaration area, then instantiate it in robotInit().