a

Dependents:   wheelchaircontrol wheelchaircontrol2 wheelchaircontrol3 wheelchaircontrol4 ... more

Committer:
jvfausto
Date:
Fri Apr 19 23:03:41 2019 +0000
Revision:
4:17d461de6a8f
Parent:
3:0035b165ecc4
a

Who changed what in which revision?

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