Eurobot2012_Primary

Dependencies:   mbed Eurobot_2012_Primary

Committer:
narshu
Date:
Wed Oct 17 22:22:47 2012 +0000
Revision:
26:0995f61cb7b8
Parent:
25:143b19c1fb05
Eurobot 2012 Primary;

Who changed what in which revision?

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