testing encoder without function

Dependencies:   mbed

Committer:
ea78anana
Date:
Mon Nov 01 15:39:50 2021 +0000
Revision:
0:e180c98a3437
testing encoder

Who changed what in which revision?

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