5 #include <avr/interrupt.h>
16 static volatile bool lastLeftA;
17 static volatile bool lastLeftB;
18 static volatile bool lastRightA;
19 static volatile bool lastRightB;
21 static volatile bool errorLeft;
22 static volatile bool errorRight;
26 static volatile uint16_t countLeft;
27 static volatile uint16_t countRight;
31 bool newLeftB = FastGPIO::Pin<LEFT_B>::isInputHigh();
32 bool newLeftA = FastGPIO::Pin<LEFT_XOR>::isInputHigh() ^ newLeftB;
34 countLeft += (newLeftA ^ lastLeftB) - (lastLeftA ^ newLeftB);
36 if((lastLeftA ^ newLeftA) & (lastLeftB ^ newLeftB))
45 static void rightISR()
47 bool newRightB = FastGPIO::Pin<RIGHT_B>::isInputHigh();
48 bool newRightA = FastGPIO::Pin<RIGHT_XOR>::isInputHigh() ^ newRightB;
50 countRight += (newRightA ^ lastRightB) - (lastRightA ^ newRightB);
52 if((lastRightA ^ newRightA) & (lastRightB ^ newRightB))
57 lastRightA = newRightA;
58 lastRightB = newRightB;
61 void Encoders::init2()
64 FastGPIO::Pin<LEFT_XOR>::setInputPulledUp();
65 FastGPIO::Pin<LEFT_B>::setInputPulledUp();
66 FastGPIO::Pin<RIGHT_XOR>::setInputPulledUp();
67 FastGPIO::Pin<RIGHT_B>::setInputPulledUp();
72 PCMSK0 = (1 << PCINT4);
78 attachInterrupt(4, rightISR, CHANGE);
83 lastLeftB = FastGPIO::Pin<LEFT_B>::isInputHigh();
84 lastLeftA = FastGPIO::Pin<LEFT_XOR>::isInputHigh() ^ lastLeftB;
88 lastRightB = FastGPIO::Pin<RIGHT_B>::isInputHigh();
89 lastRightA = FastGPIO::Pin<RIGHT_XOR>::isInputHigh() ^ lastRightB;
106 int16_t counts = countLeft;
108 return flip ? -counts : counts;
116 int16_t counts = countRight;
118 return flip ? -counts : counts;
126 int16_t counts = countLeft;
129 return flip ? -counts : counts;
137 int16_t counts = countRight;
140 return flip ? -counts : counts;
147 bool error = errorLeft;
156 bool error = errorRight;
static void flipEncoders(bool flip)
Flips the direction of the encoders.
static bool checkErrorRight()
Returns true if an error was detected on the right-side encoder.
static int16_t getCountsRight()
Returns the number of counts that have been detected from the right-side encoder.
static bool checkErrorLeft()
Returns true if an error was detected on the left-side encoder.
static int16_t getCountsAndResetRight()
Returns the number of counts that have been detected from the left-side encoder and clears the counts...
static int16_t getCountsAndResetLeft()
Returns the number of counts that have been detected from the left-side encoder and clears the counts...
static int16_t getCountsLeft()
Returns the number of counts that have been detected from the left-side encoder.
static void init()
Initializes the encoders (called automatically).
Top-level namespace for the Pololu3piPlus32U4 library.