Derivative QEI library to add an error counter and the ability to set the count and revolutions, and reset the error counter.

Committer:
WiredHome
Date:
Wed Jan 13 20:12:03 2021 +0000
Revision:
1:cecfd2d4d286
Parent:
0:5c2ad81551aa
Functional version suitable for testing

Who changed what in which revision?

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