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