Demo Application for the Celeritous Breakout Board

Dependencies:   mbed

Committer:
celeritous
Date:
Fri May 18 03:55:10 2012 +0000
Revision:
0:1a3da73fe36a
Celeritous_BreakoutBoardDemo

Who changed what in which revision?

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