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