rotary encoder with push button based on work I found elswhere.

Dependents:   Vario_101_407mini

Committer:
bomilkar
Date:
Mon Dec 18 16:54:00 2017 +0000
Revision:
0:ef2f1bbeb8e1
Initial

Who changed what in which revision?

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