a

Dependencies:   mbed

Committer:
Jagang
Date:
Sun Dec 14 17:49:01 2014 +0000
Revision:
0:85567bbcebdb
New

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jagang 0:85567bbcebdb 1 /**
Jagang 0:85567bbcebdb 2 * @author Aaron Berk
Jagang 0:85567bbcebdb 3 *
Jagang 0:85567bbcebdb 4 * @section LICENSE
Jagang 0:85567bbcebdb 5 *
Jagang 0:85567bbcebdb 6 * Copyright (c) 2010 ARM Limited
Jagang 0:85567bbcebdb 7 *
Jagang 0:85567bbcebdb 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
Jagang 0:85567bbcebdb 9 * of this software and associated documentation files (the "Software"), to deal
Jagang 0:85567bbcebdb 10 * in the Software without restriction, including without limitation the rights
Jagang 0:85567bbcebdb 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Jagang 0:85567bbcebdb 12 * copies of the Software, and to permit persons to whom the Software is
Jagang 0:85567bbcebdb 13 * furnished to do so, subject to the following conditions:
Jagang 0:85567bbcebdb 14 *
Jagang 0:85567bbcebdb 15 * The above copyright notice and this permission notice shall be included in
Jagang 0:85567bbcebdb 16 * all copies or substantial portions of the Software.
Jagang 0:85567bbcebdb 17 *
Jagang 0:85567bbcebdb 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Jagang 0:85567bbcebdb 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Jagang 0:85567bbcebdb 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Jagang 0:85567bbcebdb 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Jagang 0:85567bbcebdb 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Jagang 0:85567bbcebdb 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Jagang 0:85567bbcebdb 24 * THE SOFTWARE.
Jagang 0:85567bbcebdb 25 *
Jagang 0:85567bbcebdb 26 * @section DESCRIPTION
Jagang 0:85567bbcebdb 27 *
Jagang 0:85567bbcebdb 28 * Quadrature Encoder Interface.
Jagang 0:85567bbcebdb 29 *
Jagang 0:85567bbcebdb 30 * A quadrature encoder consists of two code tracks on a disc which are 90
Jagang 0:85567bbcebdb 31 * degrees out of phase. It can be used to determine how far a wheel has
Jagang 0:85567bbcebdb 32 * rotated, relative to a known starting position.
Jagang 0:85567bbcebdb 33 *
Jagang 0:85567bbcebdb 34 * Only one code track changes at a time leading to a more robust system than
Jagang 0:85567bbcebdb 35 * a single track, because any jitter around any edge won't cause a state
Jagang 0:85567bbcebdb 36 * change as the other track will remain constant.
Jagang 0:85567bbcebdb 37 *
Jagang 0:85567bbcebdb 38 * Encoders can be a homebrew affair, consisting of infrared emitters/receivers
Jagang 0:85567bbcebdb 39 * and paper code tracks consisting of alternating black and white sections;
Jagang 0:85567bbcebdb 40 * alternatively, complete disk and PCB emitter/receiver encoder systems can
Jagang 0:85567bbcebdb 41 * be bought, but the interface, regardless of implementation is the same.
Jagang 0:85567bbcebdb 42 *
Jagang 0:85567bbcebdb 43 * +-----+ +-----+ +-----+
Jagang 0:85567bbcebdb 44 * Channel A | ^ | | | | |
Jagang 0:85567bbcebdb 45 * ---+ ^ +-----+ +-----+ +-----
Jagang 0:85567bbcebdb 46 * ^ ^
Jagang 0:85567bbcebdb 47 * ^ +-----+ +-----+ +-----+
Jagang 0:85567bbcebdb 48 * Channel B ^ | | | | | |
Jagang 0:85567bbcebdb 49 * ------+ +-----+ +-----+ +-----
Jagang 0:85567bbcebdb 50 * ^ ^
Jagang 0:85567bbcebdb 51 * ^ ^
Jagang 0:85567bbcebdb 52 * 90deg
Jagang 0:85567bbcebdb 53 *
Jagang 0:85567bbcebdb 54 * The interface uses X2 encoding by default which calculates the pulse count
Jagang 0:85567bbcebdb 55 * based on reading the current state after each rising and falling edge of
Jagang 0:85567bbcebdb 56 * channel A.
Jagang 0:85567bbcebdb 57 *
Jagang 0:85567bbcebdb 58 * +-----+ +-----+ +-----+
Jagang 0:85567bbcebdb 59 * Channel A | | | | | |
Jagang 0:85567bbcebdb 60 * ---+ +-----+ +-----+ +-----
Jagang 0:85567bbcebdb 61 * ^ ^ ^ ^ ^
Jagang 0:85567bbcebdb 62 * ^ +-----+ ^ +-----+ ^ +-----+
Jagang 0:85567bbcebdb 63 * Channel B ^ | ^ | ^ | ^ | ^ | |
Jagang 0:85567bbcebdb 64 * ------+ ^ +-----+ ^ +-----+ +--
Jagang 0:85567bbcebdb 65 * ^ ^ ^ ^ ^
Jagang 0:85567bbcebdb 66 * ^ ^ ^ ^ ^
Jagang 0:85567bbcebdb 67 * Pulse count 0 1 2 3 4 5 ...
Jagang 0:85567bbcebdb 68 *
Jagang 0:85567bbcebdb 69 * This interface can also use X4 encoding which calculates the pulse count
Jagang 0:85567bbcebdb 70 * based on reading the current state after each rising and falling edge of
Jagang 0:85567bbcebdb 71 * either channel.
Jagang 0:85567bbcebdb 72 *
Jagang 0:85567bbcebdb 73 * +-----+ +-----+ +-----+
Jagang 0:85567bbcebdb 74 * Channel A | | | | | |
Jagang 0:85567bbcebdb 75 * ---+ +-----+ +-----+ +-----
Jagang 0:85567bbcebdb 76 * ^ ^ ^ ^ ^
Jagang 0:85567bbcebdb 77 * ^ +-----+ ^ +-----+ ^ +-----+
Jagang 0:85567bbcebdb 78 * Channel B ^ | ^ | ^ | ^ | ^ | |
Jagang 0:85567bbcebdb 79 * ------+ ^ +-----+ ^ +-----+ +--
Jagang 0:85567bbcebdb 80 * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
Jagang 0:85567bbcebdb 81 * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
Jagang 0:85567bbcebdb 82 * Pulse count 0 1 2 3 4 5 6 7 8 9 ...
Jagang 0:85567bbcebdb 83 *
Jagang 0:85567bbcebdb 84 * It defaults
Jagang 0:85567bbcebdb 85 *
Jagang 0:85567bbcebdb 86 * An optional index channel can be used which determines when a full
Jagang 0:85567bbcebdb 87 * revolution has occured.
Jagang 0:85567bbcebdb 88 *
Jagang 0:85567bbcebdb 89 * If a 4 pules per revolution encoder was used, with X4 encoding,
Jagang 0:85567bbcebdb 90 * the following would be observed.
Jagang 0:85567bbcebdb 91 *
Jagang 0:85567bbcebdb 92 * +-----+ +-----+ +-----+
Jagang 0:85567bbcebdb 93 * Channel A | | | | | |
Jagang 0:85567bbcebdb 94 * ---+ +-----+ +-----+ +-----
Jagang 0:85567bbcebdb 95 * ^ ^ ^ ^ ^
Jagang 0:85567bbcebdb 96 * ^ +-----+ ^ +-----+ ^ +-----+
Jagang 0:85567bbcebdb 97 * Channel B ^ | ^ | ^ | ^ | ^ | |
Jagang 0:85567bbcebdb 98 * ------+ ^ +-----+ ^ +-----+ +--
Jagang 0:85567bbcebdb 99 * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
Jagang 0:85567bbcebdb 100 * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
Jagang 0:85567bbcebdb 101 * ^ ^ ^ +--+ ^ ^ +--+ ^
Jagang 0:85567bbcebdb 102 * ^ ^ ^ | | ^ ^ | | ^
Jagang 0:85567bbcebdb 103 * Index ------------+ +--------+ +-----------
Jagang 0:85567bbcebdb 104 * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
Jagang 0:85567bbcebdb 105 * Pulse count 0 1 2 3 4 5 6 7 8 9 ...
Jagang 0:85567bbcebdb 106 * Rev. count 0 1 2
Jagang 0:85567bbcebdb 107 *
Jagang 0:85567bbcebdb 108 * Rotational position in degrees can be calculated by:
Jagang 0:85567bbcebdb 109 *
Jagang 0:85567bbcebdb 110 * (pulse count / X * N) * 360
Jagang 0:85567bbcebdb 111 *
Jagang 0:85567bbcebdb 112 * Where X is the encoding type [e.g. X4 encoding => X=4], and N is the number
Jagang 0:85567bbcebdb 113 * of pulses per revolution.
Jagang 0:85567bbcebdb 114 *
Jagang 0:85567bbcebdb 115 * Linear position can be calculated by:
Jagang 0:85567bbcebdb 116 *
Jagang 0:85567bbcebdb 117 * (pulse count / X * N) * (1 / PPI)
Jagang 0:85567bbcebdb 118 *
Jagang 0:85567bbcebdb 119 * Where X is encoding type [e.g. X4 encoding => X=44], N is the number of
Jagang 0:85567bbcebdb 120 * pulses per revolution, and PPI is pulses per inch, or the equivalent for
Jagang 0:85567bbcebdb 121 * any other unit of displacement. PPI can be calculated by taking the
Jagang 0:85567bbcebdb 122 * circumference of the wheel or encoder disk and dividing it by the number
Jagang 0:85567bbcebdb 123 * of pulses per revolution.
Jagang 0:85567bbcebdb 124 */
Jagang 0:85567bbcebdb 125
Jagang 0:85567bbcebdb 126 /**
Jagang 0:85567bbcebdb 127 * Includes
Jagang 0:85567bbcebdb 128 */
Jagang 0:85567bbcebdb 129 #include "QEI.h"
Jagang 0:85567bbcebdb 130
Jagang 0:85567bbcebdb 131 QEI::QEI(PinName channelA,
Jagang 0:85567bbcebdb 132 PinName channelB,
Jagang 0:85567bbcebdb 133 PinName index,
Jagang 0:85567bbcebdb 134 int pulsesPerRev,
Jagang 0:85567bbcebdb 135 Encoding encoding) : channelA_(channelA), channelB_(channelB),
Jagang 0:85567bbcebdb 136 index_(index) {
Jagang 0:85567bbcebdb 137
Jagang 0:85567bbcebdb 138 pulses_ = 0;
Jagang 0:85567bbcebdb 139 revolutions_ = 0;
Jagang 0:85567bbcebdb 140 pulsesPerRev_ = pulsesPerRev;
Jagang 0:85567bbcebdb 141 encoding_ = encoding;
Jagang 0:85567bbcebdb 142
Jagang 0:85567bbcebdb 143 //Workout what the current state is.
Jagang 0:85567bbcebdb 144 int chanA = channelA_.read();
Jagang 0:85567bbcebdb 145 int chanB = channelB_.read();
Jagang 0:85567bbcebdb 146
Jagang 0:85567bbcebdb 147 //2-bit state.
Jagang 0:85567bbcebdb 148 currState_ = (chanA << 1) | (chanB);
Jagang 0:85567bbcebdb 149 prevState_ = currState_;
Jagang 0:85567bbcebdb 150
Jagang 0:85567bbcebdb 151 //X2 encoding uses interrupts on only channel A.
Jagang 0:85567bbcebdb 152 //X4 encoding uses interrupts on channel A,
Jagang 0:85567bbcebdb 153 //and on channel B.
Jagang 0:85567bbcebdb 154 channelA_.rise(this, &QEI::encode);
Jagang 0:85567bbcebdb 155 channelA_.fall(this, &QEI::encode);
Jagang 0:85567bbcebdb 156
Jagang 0:85567bbcebdb 157 //If we're using X4 encoding, then attach interrupts to channel B too.
Jagang 0:85567bbcebdb 158 if (encoding == X4_ENCODING) {
Jagang 0:85567bbcebdb 159 channelB_.rise(this, &QEI::encode);
Jagang 0:85567bbcebdb 160 channelB_.fall(this, &QEI::encode);
Jagang 0:85567bbcebdb 161 }
Jagang 0:85567bbcebdb 162 //Index is optional.
Jagang 0:85567bbcebdb 163 if (index != NC) {
Jagang 0:85567bbcebdb 164 index_.rise(this, &QEI::index);
Jagang 0:85567bbcebdb 165 }
Jagang 0:85567bbcebdb 166
Jagang 0:85567bbcebdb 167 }
Jagang 0:85567bbcebdb 168
Jagang 0:85567bbcebdb 169 void QEI::reset(void) {
Jagang 0:85567bbcebdb 170
Jagang 0:85567bbcebdb 171 pulses_ = 0;
Jagang 0:85567bbcebdb 172 revolutions_ = 0;
Jagang 0:85567bbcebdb 173
Jagang 0:85567bbcebdb 174 }
Jagang 0:85567bbcebdb 175
Jagang 0:85567bbcebdb 176 int QEI::getCurrentState(void) {
Jagang 0:85567bbcebdb 177
Jagang 0:85567bbcebdb 178 return currState_;
Jagang 0:85567bbcebdb 179
Jagang 0:85567bbcebdb 180 }
Jagang 0:85567bbcebdb 181
Jagang 0:85567bbcebdb 182 int QEI::getPulses(void) {
Jagang 0:85567bbcebdb 183
Jagang 0:85567bbcebdb 184 return pulses_;
Jagang 0:85567bbcebdb 185
Jagang 0:85567bbcebdb 186 }
Jagang 0:85567bbcebdb 187
Jagang 0:85567bbcebdb 188 int QEI::getRevolutions(void) {
Jagang 0:85567bbcebdb 189
Jagang 0:85567bbcebdb 190 return revolutions_;
Jagang 0:85567bbcebdb 191
Jagang 0:85567bbcebdb 192 }
Jagang 0:85567bbcebdb 193
Jagang 0:85567bbcebdb 194 // +-------------+
Jagang 0:85567bbcebdb 195 // | X2 Encoding |
Jagang 0:85567bbcebdb 196 // +-------------+
Jagang 0:85567bbcebdb 197 //
Jagang 0:85567bbcebdb 198 // When observing states two patterns will appear:
Jagang 0:85567bbcebdb 199 //
Jagang 0:85567bbcebdb 200 // Counter clockwise rotation:
Jagang 0:85567bbcebdb 201 //
Jagang 0:85567bbcebdb 202 // 10 -> 01 -> 10 -> 01 -> ...
Jagang 0:85567bbcebdb 203 //
Jagang 0:85567bbcebdb 204 // Clockwise rotation:
Jagang 0:85567bbcebdb 205 //
Jagang 0:85567bbcebdb 206 // 11 -> 00 -> 11 -> 00 -> ...
Jagang 0:85567bbcebdb 207 //
Jagang 0:85567bbcebdb 208 // We consider counter clockwise rotation to be "forward" and
Jagang 0:85567bbcebdb 209 // counter clockwise to be "backward". Therefore pulse count will increase
Jagang 0:85567bbcebdb 210 // during counter clockwise rotation and decrease during clockwise rotation.
Jagang 0:85567bbcebdb 211 //
Jagang 0:85567bbcebdb 212 // +-------------+
Jagang 0:85567bbcebdb 213 // | X4 Encoding |
Jagang 0:85567bbcebdb 214 // +-------------+
Jagang 0:85567bbcebdb 215 //
Jagang 0:85567bbcebdb 216 // There are four possible states for a quadrature encoder which correspond to
Jagang 0:85567bbcebdb 217 // 2-bit gray code.
Jagang 0:85567bbcebdb 218 //
Jagang 0:85567bbcebdb 219 // A state change is only valid if of only one bit has changed.
Jagang 0:85567bbcebdb 220 // A state change is invalid if both bits have changed.
Jagang 0:85567bbcebdb 221 //
Jagang 0:85567bbcebdb 222 // Clockwise Rotation ->
Jagang 0:85567bbcebdb 223 //
Jagang 0:85567bbcebdb 224 // 00 01 11 10 00
Jagang 0:85567bbcebdb 225 //
Jagang 0:85567bbcebdb 226 // <- Counter Clockwise Rotation
Jagang 0:85567bbcebdb 227 //
Jagang 0:85567bbcebdb 228 // If we observe any valid state changes going from left to right, we have
Jagang 0:85567bbcebdb 229 // moved one pulse clockwise [we will consider this "backward" or "negative"].
Jagang 0:85567bbcebdb 230 //
Jagang 0:85567bbcebdb 231 // If we observe any valid state changes going from right to left we have
Jagang 0:85567bbcebdb 232 // moved one pulse counter clockwise [we will consider this "forward" or
Jagang 0:85567bbcebdb 233 // "positive"].
Jagang 0:85567bbcebdb 234 //
Jagang 0:85567bbcebdb 235 // We might enter an invalid state for a number of reasons which are hard to
Jagang 0:85567bbcebdb 236 // predict - if this is the case, it is generally safe to ignore it, update
Jagang 0:85567bbcebdb 237 // the state and carry on, with the error correcting itself shortly after.
Jagang 0:85567bbcebdb 238 void QEI::encode(void) {
Jagang 0:85567bbcebdb 239
Jagang 0:85567bbcebdb 240 int change = 0;
Jagang 0:85567bbcebdb 241 int chanA = channelA_.read();
Jagang 0:85567bbcebdb 242 int chanB = channelB_.read();
Jagang 0:85567bbcebdb 243
Jagang 0:85567bbcebdb 244 //2-bit state.
Jagang 0:85567bbcebdb 245 currState_ = (chanA << 1) | (chanB);
Jagang 0:85567bbcebdb 246
Jagang 0:85567bbcebdb 247 if (encoding_ == X2_ENCODING) {
Jagang 0:85567bbcebdb 248
Jagang 0:85567bbcebdb 249 //11->00->11->00 is counter clockwise rotation or "forward".
Jagang 0:85567bbcebdb 250 if ((prevState_ == 0x3 && currState_ == 0x0) ||
Jagang 0:85567bbcebdb 251 (prevState_ == 0x0 && currState_ == 0x3)) {
Jagang 0:85567bbcebdb 252
Jagang 0:85567bbcebdb 253 pulses_++;
Jagang 0:85567bbcebdb 254
Jagang 0:85567bbcebdb 255 }
Jagang 0:85567bbcebdb 256 //10->01->10->01 is clockwise rotation or "backward".
Jagang 0:85567bbcebdb 257 else if ((prevState_ == 0x2 && currState_ == 0x1) ||
Jagang 0:85567bbcebdb 258 (prevState_ == 0x1 && currState_ == 0x2)) {
Jagang 0:85567bbcebdb 259
Jagang 0:85567bbcebdb 260 pulses_--;
Jagang 0:85567bbcebdb 261
Jagang 0:85567bbcebdb 262 }
Jagang 0:85567bbcebdb 263
Jagang 0:85567bbcebdb 264 } else if (encoding_ == X4_ENCODING) {
Jagang 0:85567bbcebdb 265
Jagang 0:85567bbcebdb 266 //Entered a new valid state.
Jagang 0:85567bbcebdb 267 if (((currState_ ^ prevState_) != INVALID) && (currState_ != prevState_)) {
Jagang 0:85567bbcebdb 268 //2 bit state. Right hand bit of prev XOR left hand bit of current
Jagang 0:85567bbcebdb 269 //gives 0 if clockwise rotation and 1 if counter clockwise rotation.
Jagang 0:85567bbcebdb 270 change = (prevState_ & PREV_MASK) ^ ((currState_ & CURR_MASK) >> 1);
Jagang 0:85567bbcebdb 271
Jagang 0:85567bbcebdb 272 if (change == 0) {
Jagang 0:85567bbcebdb 273 change = -1;
Jagang 0:85567bbcebdb 274 }
Jagang 0:85567bbcebdb 275
Jagang 0:85567bbcebdb 276 pulses_ -= change;
Jagang 0:85567bbcebdb 277 }
Jagang 0:85567bbcebdb 278
Jagang 0:85567bbcebdb 279 }
Jagang 0:85567bbcebdb 280
Jagang 0:85567bbcebdb 281 prevState_ = currState_;
Jagang 0:85567bbcebdb 282
Jagang 0:85567bbcebdb 283 }
Jagang 0:85567bbcebdb 284
Jagang 0:85567bbcebdb 285 void QEI::index(void) {
Jagang 0:85567bbcebdb 286
Jagang 0:85567bbcebdb 287 revolutions_++;
Jagang 0:85567bbcebdb 288
Jagang 0:85567bbcebdb 289 }