Quadrature encoder interface by Aaron Berk
Fork of QEI by
Revision 1:5f28ceec8280, committed 2016-05-04
- Comitter:
- aminomar
- Date:
- Wed May 04 18:47:39 2016 +0000
- Parent:
- 0:5c2ad81551aa
- Commit message:
- test encoder;
Changed in this revision
QEI.cpp | Show annotated file Show diff for this revision Revisions of this file |
QEI.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 5c2ad81551aa -r 5f28ceec8280 QEI.cpp --- a/QEI.cpp Thu Sep 02 16:48:55 2010 +0000 +++ b/QEI.cpp Wed May 04 18:47:39 2016 +0000 @@ -1,139 +1,9 @@ -/** - * @author Aaron Berk - * - * @section LICENSE - * - * Copyright (c) 2010 ARM Limited - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - * @section DESCRIPTION - * - * Quadrature Encoder Interface. - * - * A quadrature encoder consists of two code tracks on a disc which are 90 - * degrees out of phase. It can be used to determine how far a wheel has - * rotated, relative to a known starting position. - * - * Only one code track changes at a time leading to a more robust system than - * a single track, because any jitter around any edge won't cause a state - * change as the other track will remain constant. - * - * Encoders can be a homebrew affair, consisting of infrared emitters/receivers - * and paper code tracks consisting of alternating black and white sections; - * alternatively, complete disk and PCB emitter/receiver encoder systems can - * be bought, but the interface, regardless of implementation is the same. - * - * +-----+ +-----+ +-----+ - * Channel A | ^ | | | | | - * ---+ ^ +-----+ +-----+ +----- - * ^ ^ - * ^ +-----+ +-----+ +-----+ - * Channel B ^ | | | | | | - * ------+ +-----+ +-----+ +----- - * ^ ^ - * ^ ^ - * 90deg - * - * The interface uses X2 encoding by default which calculates the pulse count - * based on reading the current state after each rising and falling edge of - * channel A. - * - * +-----+ +-----+ +-----+ - * Channel A | | | | | | - * ---+ +-----+ +-----+ +----- - * ^ ^ ^ ^ ^ - * ^ +-----+ ^ +-----+ ^ +-----+ - * Channel B ^ | ^ | ^ | ^ | ^ | | - * ------+ ^ +-----+ ^ +-----+ +-- - * ^ ^ ^ ^ ^ - * ^ ^ ^ ^ ^ - * Pulse count 0 1 2 3 4 5 ... - * - * This interface can also use X4 encoding which calculates the pulse count - * based on reading the current state after each rising and falling edge of - * either channel. - * - * +-----+ +-----+ +-----+ - * Channel A | | | | | | - * ---+ +-----+ +-----+ +----- - * ^ ^ ^ ^ ^ - * ^ +-----+ ^ +-----+ ^ +-----+ - * Channel B ^ | ^ | ^ | ^ | ^ | | - * ------+ ^ +-----+ ^ +-----+ +-- - * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ - * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ - * Pulse count 0 1 2 3 4 5 6 7 8 9 ... - * - * It defaults - * - * An optional index channel can be used which determines when a full - * revolution has occured. - * - * If a 4 pules per revolution encoder was used, with X4 encoding, - * the following would be observed. - * - * +-----+ +-----+ +-----+ - * Channel A | | | | | | - * ---+ +-----+ +-----+ +----- - * ^ ^ ^ ^ ^ - * ^ +-----+ ^ +-----+ ^ +-----+ - * Channel B ^ | ^ | ^ | ^ | ^ | | - * ------+ ^ +-----+ ^ +-----+ +-- - * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ - * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ - * ^ ^ ^ +--+ ^ ^ +--+ ^ - * ^ ^ ^ | | ^ ^ | | ^ - * Index ------------+ +--------+ +----------- - * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ - * Pulse count 0 1 2 3 4 5 6 7 8 9 ... - * Rev. count 0 1 2 - * - * Rotational position in degrees can be calculated by: - * - * (pulse count / X * N) * 360 - * - * Where X is the encoding type [e.g. X4 encoding => X=4], and N is the number - * of pulses per revolution. - * - * Linear position can be calculated by: - * - * (pulse count / X * N) * (1 / PPI) - * - * Where X is encoding type [e.g. X4 encoding => X=44], N is the number of - * pulses per revolution, and PPI is pulses per inch, or the equivalent for - * any other unit of displacement. PPI can be calculated by taking the - * circumference of the wheel or encoder disk and dividing it by the number - * of pulses per revolution. - */ -/** - * Includes - */ #include "QEI.h" -QEI::QEI(PinName channelA, - PinName channelB, - PinName index, - int pulsesPerRev, - Encoding encoding) : channelA_(channelA), channelB_(channelB), - index_(index) { +QEI::QEI(PinName channelA, PinName channelB, PinName index, int pulsesPerRev, Encoding encoding) : + channelA_(channelA), channelB_(channelB), index_(index) + { pulses_ = 0; revolutions_ = 0; @@ -246,9 +116,9 @@ if (encoding_ == X2_ENCODING) { - //11->00->11->00 is counter clockwise rotation or "forward". + //11->00->11->00 is counter clockwise rotation or "forward". If it follows the sequence, then pulses ++ if ((prevState_ == 0x3 && currState_ == 0x0) || - (prevState_ == 0x0 && currState_ == 0x3)) { + (prevState_ == 0x0 && currState_ == 0x3)) { pulses_++;
diff -r 5c2ad81551aa -r 5f28ceec8280 QEI.h --- a/QEI.h Thu Sep 02 16:48:55 2010 +0000 +++ b/QEI.h Wed May 04 18:47:39 2016 +0000 @@ -1,158 +1,24 @@ -/** - * @author Aaron Berk - * - * @section LICENSE - * - * Copyright (c) 2010 ARM Limited - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - * @section DESCRIPTION - * - * Quadrature Encoder Interface. - * - * A quadrature encoder consists of two code tracks on a disc which are 90 - * degrees out of phase. It can be used to determine how far a wheel has - * rotated, relative to a known starting position. - * - * Only one code track changes at a time leading to a more robust system than - * a single track, because any jitter around any edge won't cause a state - * change as the other track will remain constant. - * - * Encoders can be a homebrew affair, consisting of infrared emitters/receivers - * and paper code tracks consisting of alternating black and white sections; - * alternatively, complete disk and PCB emitter/receiver encoder systems can - * be bought, but the interface, regardless of implementation is the same. - * - * +-----+ +-----+ +-----+ - * Channel A | ^ | | | | | - * ---+ ^ +-----+ +-----+ +----- - * ^ ^ - * ^ +-----+ +-----+ +-----+ - * Channel B ^ | | | | | | - * ------+ +-----+ +-----+ +----- - * ^ ^ - * ^ ^ - * 90deg - * - * The interface uses X2 encoding by default which calculates the pulse count - * based on reading the current state after each rising and falling edge of - * channel A. - * - * +-----+ +-----+ +-----+ - * Channel A | | | | | | - * ---+ +-----+ +-----+ +----- - * ^ ^ ^ ^ ^ - * ^ +-----+ ^ +-----+ ^ +-----+ - * Channel B ^ | ^ | ^ | ^ | ^ | | - * ------+ ^ +-----+ ^ +-----+ +-- - * ^ ^ ^ ^ ^ - * ^ ^ ^ ^ ^ - * Pulse count 0 1 2 3 4 5 ... - * - * This interface can also use X4 encoding which calculates the pulse count - * based on reading the current state after each rising and falling edge of - * either channel. - * - * +-----+ +-----+ +-----+ - * Channel A | | | | | | - * ---+ +-----+ +-----+ +----- - * ^ ^ ^ ^ ^ - * ^ +-----+ ^ +-----+ ^ +-----+ - * Channel B ^ | ^ | ^ | ^ | ^ | | - * ------+ ^ +-----+ ^ +-----+ +-- - * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ - * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ - * Pulse count 0 1 2 3 4 5 6 7 8 9 ... - * - * It defaults - * - * An optional index channel can be used which determines when a full - * revolution has occured. - * - * If a 4 pules per revolution encoder was used, with X4 encoding, - * the following would be observed. - * - * +-----+ +-----+ +-----+ - * Channel A | | | | | | - * ---+ +-----+ +-----+ +----- - * ^ ^ ^ ^ ^ - * ^ +-----+ ^ +-----+ ^ +-----+ - * Channel B ^ | ^ | ^ | ^ | ^ | | - * ------+ ^ +-----+ ^ +-----+ +-- - * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ - * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ - * ^ ^ ^ +--+ ^ ^ +--+ ^ - * ^ ^ ^ | | ^ ^ | | ^ - * Index ------------+ +--------+ +----------- - * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ - * Pulse count 0 1 2 3 4 5 6 7 8 9 ... - * Rev. count 0 1 2 - * - * Rotational position in degrees can be calculated by: - * - * (pulse count / X * N) * 360 - * - * Where X is the encoding type [e.g. X4 encoding => X=4], and N is the number - * of pulses per revolution. - * - * Linear position can be calculated by: - * - * (pulse count / X * N) * (1 / PPI) - * - * Where X is encoding type [e.g. X4 encoding => X=44], N is the number of - * pulses per revolution, and PPI is pulses per inch, or the equivalent for - * any other unit of displacement. PPI can be calculated by taking the - * circumference of the wheel or encoder disk and dividing it by the number - * of pulses per revolution. - */ #ifndef QEI_H #define QEI_H -/** - * Includes - */ #include "mbed.h" -/** - * Defines - */ -#define PREV_MASK 0x1 //Mask for the previous state in determining direction -//of rotation. -#define CURR_MASK 0x2 //Mask for the current state in determining direction -//of rotation. +#define PREV_MASK 0x1 //Mask for the previous state in determining direction of rotation. +#define CURR_MASK 0x2 //Mask for the current state in determining direction of rotation. #define INVALID 0x3 //XORing two states where both bits have changed. -/** - * Quadrature Encoder Interface. - */ + class QEI { public: - typedef enum Encoding { - - X2_ENCODING, + typedef enum Encoding + { + X2_ENCODING, X4_ENCODING - - } Encoding; + } + Encoding; /** * Constructor.