follow wall 1

Dependencies:   mbed

Committer:
khaledelmadawi
Date:
Tue Apr 08 13:23:06 2014 +0000
Revision:
0:b703833f6795
follow wall 1

Who changed what in which revision?

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