I’ve significantly updated my example quadrature encoder code with additional functionality and documentation. Here’s an excerpt from the readme.txt:
This source code in this file implements an interface to two
quadrature output encoders with the assumption that they are
coupled to the robot's drive train, one on the left side, the
other on the right. Used with suitable PID control software,
encoders can be used to control the position and velocity of
of your robot. This software was tested with Grayhill 63R256
and 61K128 quadrature output optical encoders. Data sheets for
these devices are included.
This source code will work with the Robovation (A/K/A EDU-RC)
robot controller and the FIRST Robotics robot controller.
On a 40MHz PIC18F8520, this software can track peak encoder
count rates as high as a few thousand counts per second, which
should be more than adequate for most applications. To meet
your performance expectations, selecting the proper Counts Per
Revolution (CPR) parameter of your encoder is very important.
If the CPR is too high, the robot controller will spend too
much time counting encoder "ticks" and not enough time on
other tasks. At the extreme, you will see very wacky behavior
in your robot controller including corrupted data, the red-
light-of-death or the controller may even think the robot is
traveling in a direction that it isn't. Selecting a CPR that
is too low will not give you the resolution you desire. The
CPR should be optimized to minimize the number of interrupts
your robot controller will have to service yet meet your
resolution expectations (yes, millimeter position resolution
to too much to ask for).
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.
This file is best viewed with tabs set to four characters.
****************************************************************
Here's a description of the functions in encoder.c:
Initialize_Encoders()
This function initializes the encoder software. It should be
called from user_routines.c/User_Initialization().
Get_Left_Encoder_Count()
Get_Right_Encoder_Count()
These functions will return the current number of encoder
counts or "ticks".
Set_Left_Encoder_Count()
Set_Right_Encoder_Count()
These functions are used to reset or change the encoder count.
Left_Encoder_Int_Handler()
Right_Encoder_Int_Handler()
When the phase-A signal of an encoder transitions from a zero
to a one, the microcontroller will automatically call these
functions to sample the phase-B signal and increment/decrement
the encoder count variables. You shouldn't have to call these
functions yourself.
****************************************************************
Five things must be done before this software will work
correctly on the FRC-RC:
1) The left encoder's phase-A output is wired to digital input
one and the phase-B output is wired to digital I/O six.
2) The right encoder's phase-A output is wired to digital input
two and the phase-B output is wired to digital I/O 8.
3) Digital I/O pins one, two, six and eight are configured as
inputs in user_routines.c/User_Initialization(). If you notice
that the encoder only counts in one direction, you forgot to
do this step.
4) A #include statement for the encoder.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 "encoder.h".
5) Initialize_Encoders() must be called from user_routines.c/
User_Initialization().
Five things must be done before this software will work
correctly on the EDU-RC:
1) The left encoder's phase-A output is wired to interrupt
one and the phase-B output is wired to digital I/O six.
2) The right encoder's phase-A output is wired to interrupt
two and the phase-B output is wired to digital I/O eight.
3) Digital I/O pins six and eight are configured as inputs in
user_routines.c/User_Initialization(). If you notice that the
encoder only counts in one direction, you forgot to do this
step.
4) A #include statement for the encoder.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 "encoder.h".
5) Initialize_Encoders() must be called from user_routines.c/
User_Initialization().
Kevin Watson
[email="[email protected]"][email protected][/email]
The code can be found here: http://kevin.org/frc
-Kevin