fdlsj

Dependencies:   mbed

Fork of f3rc2 by Tk A

Committer:
sakanakuuun
Date:
Tue Aug 30 07:05:09 2016 +0000
Revision:
2:1a2984dfac3e
Parent:
1:a1e592eca305
fe;

Who changed what in which revision?

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