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