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