fdlsj

Dependencies:   mbed

Fork of f3rc2 by 涼太郎 中村

Committer:
sakanakuuun
Date:
Tue Aug 30 07:05:09 2016 +0000
Revision:
2:1a2984dfac3e
Parent:
1:a1e592eca305
fe;

Who changed what in which revision?

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