Updated: Example Gyro Interface Code

I’ve updated the example gyro interface code with a much improved way to calculate the gyro bias. The code can be found here: http://kevin.org/frc. Here’s the readme.txt:


The source code in gyro.c/.h contains a driver and supporting 
software to interface a variety of inexpensive gyros to your
robot controller. This software was tested with Analog Devices'
ADXRS150EB and ADXRS300EB gyros. Data sheets for these devices 
are included. Support for the ADXRS401EB is also included. By
default, this software is configured to work with a ADXRS150EB
gyro, sampling at 400Hz, downconverting to an update rate of
50Hz by averaging eight samples per update. These parameters
can be changed by editing gyro.h.
This software makes the assumption that when it's running, it 
solely controls the analog to digital conversion hardware and 
that only the gyro, on analog input one, is being sampled.
Calling Disable_Gyro() will disable the gyro software and
allow you to use the ADC for other purposes.
This source code will work with the Robovation (A/K/A EDU-RC) 
robot controller and the FIRST Robotics robot controller.
Wiring-up the ADXRS401EB, ADXRS150EB and ADXRS300EB gyro
evaluation boards is straightforward. Grab a PWM cable and cut
off the male end and strip the three wires back a centimeter 
or so. With a low wattage soldering iron, solder the white wire 
to the RATEOUT pin, solder the black wire to the AGND pin, the 
red wire to the AVCC pin, and finally connect a jumper wire 
between the AVCC and PDD pins. Plug the female end into the 
robot controller's analog input 1.
For optimum performance, you'll need to calibrate the scaling
factor to match that of your gyro's. One way to calibrate your 
gyro is to mount it very securely to a hefty, square or 
rectangular object. Mounting the gyro to a hefty object will 
help dampen higher frequency vibrations that can adversly 
effect your measurements. Place the mounted gyro against 
another square object and start the included demonstration 
software. To get good results, the mount must be absolutely 
still when the "Calibrating Gyro Bias..." message appears on 
the terminal screen. After a few seconds, the gyro angular rate 
and angle will be sent to the terminal screen. If the angle 
drifts rapidly while the mounted gyro is motonless, you need 
to restart the software to acquire a new gyro bias measurement. 
Again, gyros are very sensitive and must be still while the bias 
is calculated. Once the gyro is running with little drift, 
rotate the mount 180 degrees and note the reported angle. If the 
angular units are set to tenths of a degree, the ideal reported 
angle is 1800. If set to milliradians, the ideal angle 1s 3142 
(Pi times a thousand). For every tenth of a percent that the 
angle is high, decrease the GYRO_CAL_FACTOR numerator by one.
Conversly, for every tenth of a percent low, increase the 
numerator by one. Repeat until you're satisfied with the 
accuracy.
The included project files were built with MPLAB version 6.60.
If your version of MPLAB complains about the project version, 
the best thing to do is just create a new project with MPLAB's 
project wizard. Include every file except: FRC_alltimers.lib 
and ifi_alltimers.lib and you should be able to build the code.
****************************************************************
Here's a description of the functions in gyro.c:
 
Initialize_Gyro()
This function initializes the gyro software. It should be called 
from user_routines.c/User_Initialization().
 
Get_Gyro_Rate()
This function returns the current angular rate of change in 
units of milliradians per second. If desired, the angular unit 
can be changed to tenths of a degree per second by modifying
the angular units #define entry in gyro.h
 
Get_Gyro_Angle()
This function returns the change in heading angle, in 
milliradians, since the software was initialized or 
Reset_Gyro_Angle() was called. If desired, the angular unit
can be changed to tenths of a degree by modifying the angular
units #define entry in gyro
 
Start_Gyro_Bias_Calc()
Stop_Gyro_Bias_Calc()
These functions start/stop a new gyro bias calculation. For best 
results, Start_Gyro_Bias_Calc() should be called about a second 
after the robot powers-up and while the robot is perfectly still 
with all vibration sources turned off (e.g., compressor). After
at least a second (longer is better), call Stop_Gyro_Bias_Calc()
to terminate the calibration. While a calibration is taking
place, gyro rate and angle are not updated. Once calibrated, a
call to Reset_Gyro_Angle() should take place.
 
Get_Gyro_Bias()
This function returns the current calculated gyro bias. For
extra precision, this software internally maintains a gyro bias
value that is the sum of GYRO_SAMPLES_PER_UPDATE samples of the 
gyro's analog output. By default, GYRO_SAMPLES_PER_UPDATE is
set to a value of eight in gyro.h
 
Set_Gyro_Bias()
This function can be called to manually set the gyro bias. For
extra precision, this software internally maintains a gyro bias
value that is the sum of GYRO_SAMPLES_PER_UPDATE samples of the 
gyro's analog output. By default, GYRO_SAMPLES_PER_UPDATE is
set to a value of eight in gyro.h
 
Reset_Gyro_Angle()
This function resets the integrated gyro heading angle to zero.
 
Disable_Gyro()
This function should be called when the gyro functionality is
no longer needed, thus reclaiming the time spent servicing the
timer 2 interrupts.
 
Initialize_ADC()
This function initializes the analog to digital conversion
hardware. This is called by Initialize_Gyro() above and 
shouldn't be called directly.
 
Initialize_Timer_2()
This function initializes and starts timer2. This is called by
Initialize_Gyro() above and shouldn't be called directly.
 
Timer_2_Int_Handler()
This function is automatically called when timer 2 causes an
interrupt and shouldn't be called directly.
 
****************************************************************
Seven things must be done before this software will work 
correctly on the FRC-RC:
1) The gyro's rate output is wired to analog input 1.
2) A #include statement for the gyro.h header file must be 
included at the beginning of each source file that calls the 
functions in this source file. The statement should look like 
this: #include "gyro.h".
3) Initialize_Gyro() must be called from user_routines.c/
User_Initialization().
4) The timer 2 interrupt handler, Timer_2_Int_Handler(), must
be installed in User_Routines_Fast.c/InterruptHandlerLow().
See the accompanying copy of User_Routines_Fast.c to see how
this is done.
5) You must select the gyro you're using from a list in gyro.h
and if needed, remove the // in front of it's #define.
6) A gyro bias calculation must take place using the functions 
Start_Gyro_Bias_Calc() & Stop_Gyro_Bias_Calc() described above.
This must be done several hundred milliseconds after the gyro 
powers-up and is allowed to stabilize.
7) For optimal performance, you'll need to calibrate the gyro 
scaling factor using the instructions above or those included
in gyro.h.	
 
Nine things must be done before this software will work 
correctly on the EDU-RC:
1) The gyro's rate output is wired to analog input 1.
2) IO1 must be configured as an "INPUT" in user_routines.c/
User_Initialization().
3) The call to Set_Number_of_Analog_Channels() must configure
at least one analog channel.
4) A #include statement for the gyro.h header file must be 
included at the beginning of each source file that calls the 
functions in this source file. The statement should look like 
this: #include "gyro.h".
5) Initialize_Gyro() must be called from user_routines.c/
User_Initialization().
6) The timer 2 interrupt handler, Timer_2_Int_Handler(), must
be installed in User_Routines_Fast.c/InterruptHandlerLow().
See the accompanying copy of User_Routines_Fast.c to see how
this is done.
7) You must select the gyro you're using from a list in gyro.h
and if needed, remove the // in front of it's #define.
8) A gyro bias calculation must take place using the functions 
Start_Gyro_Bias_Calc() & Stop_Gyro_Bias_Calc() described above.
This must be done several hundred milliseconds after the gyro 
powers-up and is allowed to stabilize.
9) For optimal performance, you'll need to calibrate the gyro 
scaling factor using the instructions above or those included
in gyro.h.
 
Kevin Watson
[email="[email protected]"][email protected][/email]

-Kevin

Thanks for this amazing software Kevin.

We were really happy to see that it worked right out of the box.

But we wanted to use the self test function of our gyro so we created our entire routine.

I wanna have this code but I can’t go to the web http://kevin.org/frc to download. So can any body tell me where to get it or send me an attach file via email. My email address is : [email protected]
Thanks alot !