View Single Post
  #4   Spotlight this post!  
Unread 14-04-2015, 08:19
otherguy's Avatar
otherguy otherguy is offline
sparkE
AKA: James
FRC #2168 (The Aluminum Falcons)
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2009
Location: CT
Posts: 429
otherguy is a splendid one to beholdotherguy is a splendid one to beholdotherguy is a splendid one to beholdotherguy is a splendid one to beholdotherguy is a splendid one to beholdotherguy is a splendid one to beholdotherguy is a splendid one to behold
Re: Programming SPI Gyro

We used the gyro you mentioned in the original post all season with good results. The other programming mentor on our team (kevin[at]team2168.org) spent time getting this class up and running.

Our source code to interface with this sensor is available here: https://gist.github.com/jcorcoran/6d721cf6570f772b51f8

Once the sensor is wired up, the code is pretty easy to use.

We instantiate it in our drivetrain susbsystem:
Code:
public ADXRS453Gyro gyroSPI;
gyroSPI = new ADXRS453Gyro();
gyroSPI.startThread();
You can call gyroSPI.getAngle(); to get the current heading relative to the position that the robot was at when it turned on.


There's also a feature built in which allows you to recalibrate the gyro. Calibration will occur automatically on startup. It's a 10 second calibration sequence, and should only be called when you expect the robot to be stationary for this duration of time (disabled before a match).

This feature is there to allow the robot to re-calibrate itself before a match if the robot was say, turned on while it was being placed on the field, or moved around while it was calibrating. This affects the static drift rate, so it will throw off all subsequent calculations. Our recalibration code looks something like this in our disabledPeriodic method:

Code:
//Check to see if the gyro is drifting, if it is re-initialize it.
//Thanks FRC254 for orig. idea.
curAngle = drivetrain.getHeading();
gyroCalibrating = drivetrain.isGyroCalibrating(); //this is a call into ADXRS453Gyro.isCalibrating();

if (lastGyroCalibrating && !gyroCalibrating) {
	//if we've just finished calibrating the gyro, reset
	gyroDriftDetector.reset();
	curAngle = drivetrain.getHeading();
	System.out.println("Finished auto-reinit gyro");
} else if (gyroDriftDetector.update(Math.abs(curAngle - lastAngle) > (0.75 / 50.0))
		&& !matchStarted && !gyroCalibrating) {
	//&& gyroReinits < 3) {
	gyroReinits++;
	System.out.println("!!! Sensed drift, about to auto-reinit gyro ("+ gyroReinits + ")");
	drivetrain.calibrateGyro();
}

lastAngle = curAngle;
lastGyroCalibrating = gyroCalibrating;

It's possible that a match will start while the gyro is calibrating. So if you're going to use the recalibration code, you should ensure that you add a call to ADXRS453Gyro.stopCalibrating() inside your autonomousInit() method, so that the gyro completes its calibration sequence prior to match start. For your information, if you call stopCalibrating, while a calibration sequence is active, it will fail gracefully with a partial calibrated zero drift value. See implementation at line 325 of ADXRS453Gyro.java.

In the above code gyroDriftDetector is an instance of the Debouncer class.

If you have any questions please shoot an e-mail to kevin[at]team2168.org or myself james[at]team2168.org
__________________
http://team2168.org

Last edited by otherguy : 14-04-2015 at 08:22.
Reply With Quote