Support » Pololu AVR Library Command Reference »
18. Wheel Encoders
The PololuWheelEncoders class and the associated C functions provide an easy interface for using the Pololu Wheel Encoders, which allow a robot to know exactly how far its motors have turned at any point in time.
This section of the library makes uses of pin-change interrupts to quickly detect and record each transition on the encoder. Specifically, it uses the AVR’s PCINT0, PCINT1, and PCINT2 (and PCINT3 on the Orangutan SVP and X2) interrupts, and it will conflict with any other code that uses pin-change interrupts (e.g. OrangutanPulseIn).
For a higher level overview of this library and example programs that show how this library can be used, please see Section 3.l of the Pololu AVR C/C++ Library User’s Guide.
Reference
C++ and Arduino methods are shown in red.
C functions are shown in green.
static void PololuWheelEncoders::init(unsigned char m1a, unsigned char m1b, unsigned char m2a, unsigned char m2b)
void encoders_init(unsigned char m1a, unsigned char m1b, unsigned char m2a, unsigned char m2b)
Initializes the wheel encoders. The four arguments are the four pins that the wheel encoders are connected to. Each pin is specified using the IO_* keywords provided by the library (e.g. IO_D1 for an encoder input on pin PD1). The arguments are named m1a, m1b, etc. with the intention that when motor M1 is spinning forward, pin m1a will change before pin m1b. However, it is difficult to get them all correct on the first try, and you might have to experiment.
static int getCountsM1()
static int getCountsM2()
int encoders_get_counts_m1()
int encoders_get_counts_m2()
Returns the number of counts measured on M1 or M2. For the Pololu wheel encoders, the resolution is about 3mm/count, so this allows a maximum distance of 32767 × 3mm or about 100m. For longer distances, you will need to occasionally reset the counts using the functions below.
static int getCountsAndResetM1()
static int getCountsAndResetM2()
int encoders_get_counts_and_reset_m1()
int encoders_get_counts_and_reset_m2()
Returns the number of counts measured on M1 or M2, and resets the stored value to zero.
static unsigned char checkErrorM1()
static unsigned char checkErrorM2()
unsigned char encoders_check_error_m1()
unsigned char encoders_check_error_m2()
These functions check whether there has been an error on M1 or M2; that is, if both m1a/m2b or m2a/m2b changed simultaneously. They return 1 if there was an error, then reset the error flag automatically.