Charles Young's development fork. Going forward I only want to push mature code to main repository.

Dependencies:   mbed

Fork of GEO_COUNTER_L432KC by Geo Electronics "Geo Counter"

Committer:
charlesdavidyoung
Date:
Sun Oct 07 13:13:53 2018 +0000
Revision:
80:5770804da1b0
Parent:
0:6d1742703713
get rid of pulldown

Who changed what in which revision?

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