Minor test serial map

Committer:
Feike
Date:
Mon Oct 21 11:00:54 2019 +0000
Revision:
1:e567b2f1aebf
Nieuwe lib

Who changed what in which revision?

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