Mbed基板エンコーダ読み取り基板

Dependencies:   mbed

Committer:
Waaaaat
Date:
Wed Aug 22 13:41:41 2018 +0000
Revision:
0:e2d42e66534c
encoder board mbed;

Who changed what in which revision?

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