a

Dependencies:   mbed

Committer:
Jagang
Date:
Sun Dec 14 17:49:01 2014 +0000
Revision:
0:85567bbcebdb
New

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jagang 0:85567bbcebdb 1 /**
Jagang 0:85567bbcebdb 2 * @author Aaron Berk
Jagang 0:85567bbcebdb 3 *
Jagang 0:85567bbcebdb 4 * @section LICENSE
Jagang 0:85567bbcebdb 5 *
Jagang 0:85567bbcebdb 6 * Copyright (c) 2010 ARM Limited
Jagang 0:85567bbcebdb 7 *
Jagang 0:85567bbcebdb 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
Jagang 0:85567bbcebdb 9 * of this software and associated documentation files (the "Software"), to deal
Jagang 0:85567bbcebdb 10 * in the Software without restriction, including without limitation the rights
Jagang 0:85567bbcebdb 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Jagang 0:85567bbcebdb 12 * copies of the Software, and to permit persons to whom the Software is
Jagang 0:85567bbcebdb 13 * furnished to do so, subject to the following conditions:
Jagang 0:85567bbcebdb 14 *
Jagang 0:85567bbcebdb 15 * The above copyright notice and this permission notice shall be included in
Jagang 0:85567bbcebdb 16 * all copies or substantial portions of the Software.
Jagang 0:85567bbcebdb 17 *
Jagang 0:85567bbcebdb 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Jagang 0:85567bbcebdb 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Jagang 0:85567bbcebdb 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Jagang 0:85567bbcebdb 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Jagang 0:85567bbcebdb 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Jagang 0:85567bbcebdb 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Jagang 0:85567bbcebdb 24 * THE SOFTWARE.
Jagang 0:85567bbcebdb 25 *
Jagang 0:85567bbcebdb 26 * @section DESCRIPTION
Jagang 0:85567bbcebdb 27 *
Jagang 0:85567bbcebdb 28 * Quadrature Encoder Interface.
Jagang 0:85567bbcebdb 29 *
Jagang 0:85567bbcebdb 30 * A quadrature encoder consists of two code tracks on a disc which are 90
Jagang 0:85567bbcebdb 31 * degrees out of phase. It can be used to determine how far a wheel has
Jagang 0:85567bbcebdb 32 * rotated, relative to a known starting position.
Jagang 0:85567bbcebdb 33 *
Jagang 0:85567bbcebdb 34 * Only one code track changes at a time leading to a more robust system than
Jagang 0:85567bbcebdb 35 * a single track, because any jitter around any edge won't cause a state
Jagang 0:85567bbcebdb 36 * change as the other track will remain constant.
Jagang 0:85567bbcebdb 37 *
Jagang 0:85567bbcebdb 38 * Encoders can be a homebrew affair, consisting of infrared emitters/receivers
Jagang 0:85567bbcebdb 39 * and paper code tracks consisting of alternating black and white sections;
Jagang 0:85567bbcebdb 40 * alternatively, complete disk and PCB emitter/receiver encoder systems can
Jagang 0:85567bbcebdb 41 * be bought, but the interface, regardless of implementation is the same.
Jagang 0:85567bbcebdb 42 *
Jagang 0:85567bbcebdb 43 * +-----+ +-----+ +-----+
Jagang 0:85567bbcebdb 44 * Channel A | ^ | | | | |
Jagang 0:85567bbcebdb 45 * ---+ ^ +-----+ +-----+ +-----
Jagang 0:85567bbcebdb 46 * ^ ^
Jagang 0:85567bbcebdb 47 * ^ +-----+ +-----+ +-----+
Jagang 0:85567bbcebdb 48 * Channel B ^ | | | | | |
Jagang 0:85567bbcebdb 49 * ------+ +-----+ +-----+ +-----
Jagang 0:85567bbcebdb 50 * ^ ^
Jagang 0:85567bbcebdb 51 * ^ ^
Jagang 0:85567bbcebdb 52 * 90deg
Jagang 0:85567bbcebdb 53 *
Jagang 0:85567bbcebdb 54 * The interface uses X2 encoding by default which calculates the pulse count
Jagang 0:85567bbcebdb 55 * based on reading the current state after each rising and falling edge of
Jagang 0:85567bbcebdb 56 * channel A.
Jagang 0:85567bbcebdb 57 *
Jagang 0:85567bbcebdb 58 * +-----+ +-----+ +-----+
Jagang 0:85567bbcebdb 59 * Channel A | | | | | |
Jagang 0:85567bbcebdb 60 * ---+ +-----+ +-----+ +-----
Jagang 0:85567bbcebdb 61 * ^ ^ ^ ^ ^
Jagang 0:85567bbcebdb 62 * ^ +-----+ ^ +-----+ ^ +-----+
Jagang 0:85567bbcebdb 63 * Channel B ^ | ^ | ^ | ^ | ^ | |
Jagang 0:85567bbcebdb 64 * ------+ ^ +-----+ ^ +-----+ +--
Jagang 0:85567bbcebdb 65 * ^ ^ ^ ^ ^
Jagang 0:85567bbcebdb 66 * ^ ^ ^ ^ ^
Jagang 0:85567bbcebdb 67 * Pulse count 0 1 2 3 4 5 ...
Jagang 0:85567bbcebdb 68 *
Jagang 0:85567bbcebdb 69 * This interface can also use X4 encoding which calculates the pulse count
Jagang 0:85567bbcebdb 70 * based on reading the current state after each rising and falling edge of
Jagang 0:85567bbcebdb 71 * either channel.
Jagang 0:85567bbcebdb 72 *
Jagang 0:85567bbcebdb 73 * +-----+ +-----+ +-----+
Jagang 0:85567bbcebdb 74 * Channel A | | | | | |
Jagang 0:85567bbcebdb 75 * ---+ +-----+ +-----+ +-----
Jagang 0:85567bbcebdb 76 * ^ ^ ^ ^ ^
Jagang 0:85567bbcebdb 77 * ^ +-----+ ^ +-----+ ^ +-----+
Jagang 0:85567bbcebdb 78 * Channel B ^ | ^ | ^ | ^ | ^ | |
Jagang 0:85567bbcebdb 79 * ------+ ^ +-----+ ^ +-----+ +--
Jagang 0:85567bbcebdb 80 * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
Jagang 0:85567bbcebdb 81 * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
Jagang 0:85567bbcebdb 82 * Pulse count 0 1 2 3 4 5 6 7 8 9 ...
Jagang 0:85567bbcebdb 83 *
Jagang 0:85567bbcebdb 84 * It defaults
Jagang 0:85567bbcebdb 85 *
Jagang 0:85567bbcebdb 86 * An optional index channel can be used which determines when a full
Jagang 0:85567bbcebdb 87 * revolution has occured.
Jagang 0:85567bbcebdb 88 *
Jagang 0:85567bbcebdb 89 * If a 4 pules per revolution encoder was used, with X4 encoding,
Jagang 0:85567bbcebdb 90 * the following would be observed.
Jagang 0:85567bbcebdb 91 *
Jagang 0:85567bbcebdb 92 * +-----+ +-----+ +-----+
Jagang 0:85567bbcebdb 93 * Channel A | | | | | |
Jagang 0:85567bbcebdb 94 * ---+ +-----+ +-----+ +-----
Jagang 0:85567bbcebdb 95 * ^ ^ ^ ^ ^
Jagang 0:85567bbcebdb 96 * ^ +-----+ ^ +-----+ ^ +-----+
Jagang 0:85567bbcebdb 97 * Channel B ^ | ^ | ^ | ^ | ^ | |
Jagang 0:85567bbcebdb 98 * ------+ ^ +-----+ ^ +-----+ +--
Jagang 0:85567bbcebdb 99 * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
Jagang 0:85567bbcebdb 100 * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
Jagang 0:85567bbcebdb 101 * ^ ^ ^ +--+ ^ ^ +--+ ^
Jagang 0:85567bbcebdb 102 * ^ ^ ^ | | ^ ^ | | ^
Jagang 0:85567bbcebdb 103 * Index ------------+ +--------+ +-----------
Jagang 0:85567bbcebdb 104 * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
Jagang 0:85567bbcebdb 105 * Pulse count 0 1 2 3 4 5 6 7 8 9 ...
Jagang 0:85567bbcebdb 106 * Rev. count 0 1 2
Jagang 0:85567bbcebdb 107 *
Jagang 0:85567bbcebdb 108 * Rotational position in degrees can be calculated by:
Jagang 0:85567bbcebdb 109 *
Jagang 0:85567bbcebdb 110 * (pulse count / X * N) * 360
Jagang 0:85567bbcebdb 111 *
Jagang 0:85567bbcebdb 112 * Where X is the encoding type [e.g. X4 encoding => X=4], and N is the number
Jagang 0:85567bbcebdb 113 * of pulses per revolution.
Jagang 0:85567bbcebdb 114 *
Jagang 0:85567bbcebdb 115 * Linear position can be calculated by:
Jagang 0:85567bbcebdb 116 *
Jagang 0:85567bbcebdb 117 * (pulse count / X * N) * (1 / PPI)
Jagang 0:85567bbcebdb 118 *
Jagang 0:85567bbcebdb 119 * Where X is encoding type [e.g. X4 encoding => X=44], N is the number of
Jagang 0:85567bbcebdb 120 * pulses per revolution, and PPI is pulses per inch, or the equivalent for
Jagang 0:85567bbcebdb 121 * any other unit of displacement. PPI can be calculated by taking the
Jagang 0:85567bbcebdb 122 * circumference of the wheel or encoder disk and dividing it by the number
Jagang 0:85567bbcebdb 123 * of pulses per revolution.
Jagang 0:85567bbcebdb 124 */
Jagang 0:85567bbcebdb 125
Jagang 0:85567bbcebdb 126 #ifndef QEI_H
Jagang 0:85567bbcebdb 127 #define QEI_H
Jagang 0:85567bbcebdb 128
Jagang 0:85567bbcebdb 129 /**
Jagang 0:85567bbcebdb 130 * Includes
Jagang 0:85567bbcebdb 131 */
Jagang 0:85567bbcebdb 132 #include "mbed.h"
Jagang 0:85567bbcebdb 133
Jagang 0:85567bbcebdb 134 /**
Jagang 0:85567bbcebdb 135 * Defines
Jagang 0:85567bbcebdb 136 */
Jagang 0:85567bbcebdb 137 #define PREV_MASK 0x1 //Mask for the previous state in determining direction
Jagang 0:85567bbcebdb 138 //of rotation.
Jagang 0:85567bbcebdb 139 #define CURR_MASK 0x2 //Mask for the current state in determining direction
Jagang 0:85567bbcebdb 140 //of rotation.
Jagang 0:85567bbcebdb 141 #define INVALID 0x3 //XORing two states where both bits have changed.
Jagang 0:85567bbcebdb 142
Jagang 0:85567bbcebdb 143 /**
Jagang 0:85567bbcebdb 144 * Quadrature Encoder Interface.
Jagang 0:85567bbcebdb 145 */
Jagang 0:85567bbcebdb 146 class QEI {
Jagang 0:85567bbcebdb 147
Jagang 0:85567bbcebdb 148 public:
Jagang 0:85567bbcebdb 149
Jagang 0:85567bbcebdb 150 typedef enum Encoding {
Jagang 0:85567bbcebdb 151
Jagang 0:85567bbcebdb 152 X2_ENCODING,
Jagang 0:85567bbcebdb 153 X4_ENCODING
Jagang 0:85567bbcebdb 154
Jagang 0:85567bbcebdb 155 } Encoding;
Jagang 0:85567bbcebdb 156
Jagang 0:85567bbcebdb 157 /**
Jagang 0:85567bbcebdb 158 * Constructor.
Jagang 0:85567bbcebdb 159 *
Jagang 0:85567bbcebdb 160 * Reads the current values on channel A and channel B to determine the
Jagang 0:85567bbcebdb 161 * initial state.
Jagang 0:85567bbcebdb 162 *
Jagang 0:85567bbcebdb 163 * Attaches the encode function to the rise/fall interrupt edges of
Jagang 0:85567bbcebdb 164 * channels A and B to perform X4 encoding.
Jagang 0:85567bbcebdb 165 *
Jagang 0:85567bbcebdb 166 * Attaches the index function to the rise interrupt edge of channel index
Jagang 0:85567bbcebdb 167 * (if it is used) to count revolutions.
Jagang 0:85567bbcebdb 168 *
Jagang 0:85567bbcebdb 169 * @param channelA mbed pin for channel A input.
Jagang 0:85567bbcebdb 170 * @param channelB mbed pin for channel B input.
Jagang 0:85567bbcebdb 171 * @param index mbed pin for optional index channel input,
Jagang 0:85567bbcebdb 172 * (pass NC if not needed).
Jagang 0:85567bbcebdb 173 * @param pulsesPerRev Number of pulses in one revolution.
Jagang 0:85567bbcebdb 174 * @param encoding The encoding to use. Uses X2 encoding by default. X2
Jagang 0:85567bbcebdb 175 * encoding uses interrupts on the rising and falling edges
Jagang 0:85567bbcebdb 176 * of only channel A where as X4 uses them on both
Jagang 0:85567bbcebdb 177 * channels.
Jagang 0:85567bbcebdb 178 */
Jagang 0:85567bbcebdb 179 QEI(PinName channelA, PinName channelB, PinName index, int pulsesPerRev, Encoding encoding = X2_ENCODING);
Jagang 0:85567bbcebdb 180
Jagang 0:85567bbcebdb 181 /**
Jagang 0:85567bbcebdb 182 * Reset the encoder.
Jagang 0:85567bbcebdb 183 *
Jagang 0:85567bbcebdb 184 * Sets the pulses and revolutions count to zero.
Jagang 0:85567bbcebdb 185 */
Jagang 0:85567bbcebdb 186 void reset(void);
Jagang 0:85567bbcebdb 187
Jagang 0:85567bbcebdb 188 /**
Jagang 0:85567bbcebdb 189 * Read the state of the encoder.
Jagang 0:85567bbcebdb 190 *
Jagang 0:85567bbcebdb 191 * @return The current state of the encoder as a 2-bit number, where:
Jagang 0:85567bbcebdb 192 * bit 1 = The reading from channel B
Jagang 0:85567bbcebdb 193 * bit 2 = The reading from channel A
Jagang 0:85567bbcebdb 194 */
Jagang 0:85567bbcebdb 195 int getCurrentState(void);
Jagang 0:85567bbcebdb 196
Jagang 0:85567bbcebdb 197 /**
Jagang 0:85567bbcebdb 198 * Read the number of pulses recorded by the encoder.
Jagang 0:85567bbcebdb 199 *
Jagang 0:85567bbcebdb 200 * @return Number of pulses which have occured.
Jagang 0:85567bbcebdb 201 */
Jagang 0:85567bbcebdb 202 int getPulses(void);
Jagang 0:85567bbcebdb 203
Jagang 0:85567bbcebdb 204 /**
Jagang 0:85567bbcebdb 205 * Read the number of revolutions recorded by the encoder on the index channel.
Jagang 0:85567bbcebdb 206 *
Jagang 0:85567bbcebdb 207 * @return Number of revolutions which have occured on the index channel.
Jagang 0:85567bbcebdb 208 */
Jagang 0:85567bbcebdb 209 int getRevolutions(void);
Jagang 0:85567bbcebdb 210
Jagang 0:85567bbcebdb 211
Jagang 0:85567bbcebdb 212
Jagang 0:85567bbcebdb 213 /**
Jagang 0:85567bbcebdb 214 * Update the pulse count.
Jagang 0:85567bbcebdb 215 *
Jagang 0:85567bbcebdb 216 * Called on every rising/falling edge of channels A/B.
Jagang 0:85567bbcebdb 217 *
Jagang 0:85567bbcebdb 218 * Reads the state of the channels and determines whether a pulse forward
Jagang 0:85567bbcebdb 219 * or backward has occured, updating the count appropriately.
Jagang 0:85567bbcebdb 220 */
Jagang 0:85567bbcebdb 221 void encode(void);
Jagang 0:85567bbcebdb 222
Jagang 0:85567bbcebdb 223 int getPulsesPerRev() {return pulsesPerRev_;}
Jagang 0:85567bbcebdb 224 private:
Jagang 0:85567bbcebdb 225 /**
Jagang 0:85567bbcebdb 226 * Called on every rising edge of channel index to update revolution
Jagang 0:85567bbcebdb 227 * count by one.
Jagang 0:85567bbcebdb 228 */
Jagang 0:85567bbcebdb 229 void index(void);
Jagang 0:85567bbcebdb 230
Jagang 0:85567bbcebdb 231 Encoding encoding_;
Jagang 0:85567bbcebdb 232
Jagang 0:85567bbcebdb 233 InterruptIn channelA_;
Jagang 0:85567bbcebdb 234 InterruptIn channelB_;
Jagang 0:85567bbcebdb 235 InterruptIn index_;
Jagang 0:85567bbcebdb 236
Jagang 0:85567bbcebdb 237 int pulsesPerRev_;
Jagang 0:85567bbcebdb 238 int prevState_;
Jagang 0:85567bbcebdb 239 int currState_;
Jagang 0:85567bbcebdb 240
Jagang 0:85567bbcebdb 241 volatile int pulses_;
Jagang 0:85567bbcebdb 242 volatile int revolutions_;
Jagang 0:85567bbcebdb 243
Jagang 0:85567bbcebdb 244 };
Jagang 0:85567bbcebdb 245
Jagang 0:85567bbcebdb 246 #endif /* QEI_H */