Demo Application for the Celeritous Breakout Board

Dependencies:   mbed

Committer:
celeritous
Date:
Fri May 18 03:55:10 2012 +0000
Revision:
0:1a3da73fe36a
Celeritous_BreakoutBoardDemo

Who changed what in which revision?

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