My understanding of the gyro is that a reset is not instantaneous. It takes a bit of time for the gryo to zero itself and during this time it is best not to move. If you changed your code to the following, you might be more likely to see a value closer to zero:
Code:
public void resetGyro() {
double before = getGyroAngle();
gyro.reset();
Timer.delay(2.0);
System.out.println("Gyro Before: " + before + " After: " + getGyroAngle());
}
Adding delays to wait for the gyro to initialize is undesirable. Our general approach has been:
- Avoid resetting the gyro (maybe reset it once during initialization, but avoid it during the autonomous and teleop periods).
- Make autonomous turns based off the current gyro setting. For example, if the want to rotate 90 degrees, use (90.0 + getGryoAngle()) as your destination instead of resetting and using 90.0.
- Add a smart dashboard button to reset the gyro while testing.
- Determine a gain correction for the gyro and apply this to each reading. For example, if you rotate the robot 10 times and the gryo reports 3500 degrees, the gain correction would be (3600.0 / 3500.0) and you would then multiply the raw values reported by the gyro using this gain correction factor.
Hope that helps,
Paul