Buggy bois / Mbed 2 deprecated HEATS_1

Dependencies:   mbed

Committer:
mazdo25
Date:
Sun Mar 03 00:55:10 2019 +0000
Revision:
2:730ccfbf08d5
buggy project;

Who changed what in which revision?

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