View Full Version : Programming SPI Gyro
Hello Chief Delphi,
Our team recently ordered new gyros. We decided to go with the EVAL-ADXRS453Z boards. (http://www.analog.com/en/products/mems/mems-gyroscopes/adxrs453.html#product-overview).
The gyroscopes run on the SPI bus, and do not use an analog input like the KOP gyros. We have never programmed using SPI before, and we were hoping we could get some help. We have looked through the WPILib class documentation, and searched for examples, but we are having a hard time finding any. Also, it seems like there are no built in SPI examples this year in Eclipse.
We have written some base initialization code such as,
SPI spiGyro = new SPI(Port.kOnboardCS0) ;
spiGyro.setClockRate(4000000);
spiGyro.setClockActiveHigh();
spiGyro.setChipSelectActiveLow();
spiGyro.setMSBFirst();
However, we do not know where to go from here. From the class documentation, it seems like we could have to use a combination of the read, write and transaction methods in order to receive data from the sensor.
If any of you have used SPI in the past, have example code we could look at, or just help us out, that would be greatly appreciated.
We considered doing this immediately following bag-and-tag when our next-to-last analog gyro died. We had hoped to extend the AnalogChannel class and get the integration done for us by using the Gyro(AnalogChannel) constructor. We learned that the integration was done in hardware, so we would have to do it manually. A gyroscope gives you a rotation rate-of-change, so the computer must integrate that rate to determine the actual orientation. At that point, we switched to encoders instead, and trusted that the wheels didn't slip. For more info, see this thread on usfirst (http://forums.usfirst.org/showthread.php?24192-Java-non-analog-gyroscope-support). We may tackle this as an off-season project, or get one of those integrated solutions (e.g. the AndyMark NavX MXP board) that are out of stock for next year.
We considered doing this immediately following bag-and-tag when our next-to-last analog gyro died. We had hoped to extend the AnalogChannel class and get the integration done for us by using the Gyro(AnalogChannel) constructor. We learned that the integration was done in hardware, so we would have to do it manually. A gyroscope gives you a rotation rate-of-change, so the computer must integrate that rate to determine the actual orientation. At that point, we switched to encoders instead, and trusted that the wheels didn't slip. For more info, see this thread on usfirst (http://forums.usfirst.org/showthread.php?24192-Java-non-analog-gyroscope-support). We may tackle this as an off-season project, or get one of those integrated solutions (e.g. the AndyMark NavX MXP board) that are out of stock for next year.
The navX MXP's are in stock now at both AndyMark and the Kauai Labs store (http://www.kauailabs.com/store/index.php?route=product/product&product_id=56), feel free to private message me if you have any questions.
On the navX MXP wiki (http://code.google.com/p/navxmxp/wiki/TableOfContents?tm=6) in the Application Example section, there are several robot examples for features including Field-centric drive, auto-balancing, motion detection and auto-rotate to angle. Team 2465's mecanum drive featured field-centric drive and auto-rotate to angle based on the navX MXP and it worked very well - and you can find the open-source code for this here (http://code.google.com/p/kauaibotsfirst2010/source/browse/trunk/Source/2015/GizmoBot/src/org/usfirst/frc2465/GizmoBot/subsystems/Drive.java).
otherguy
14-04-2015, 08:19
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:
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:
//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 (https://github.com/Team2168/FRC2014_Main_Robot/blob/master/src/org/team2168/utils/Debouncer.java).
If you have any questions please shoot an e-mail to kevin[at]team2168.org or myself james[at]team2168.org
vBulletin® v3.6.4, Copyright ©2000-2017, Jelsoft Enterprises Ltd.