All mbed code for control over dive planes, pump motor, valve motor, BCUs, UART interface, etc.

Dependencies:   mbed ESC mbed MODDMA

Committer:
juansal12
Date:
Tue Jan 14 19:17:05 2020 +0000
Revision:
0:c3a329a5b05d
Sofi7 mbed code;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
juansal12 0:c3a329a5b05d 1 /**
juansal12 0:c3a329a5b05d 2 * @author Aaron Berk
juansal12 0:c3a329a5b05d 3 *
juansal12 0:c3a329a5b05d 4 * @section LICENSE
juansal12 0:c3a329a5b05d 5 *
juansal12 0:c3a329a5b05d 6 * Copyright (c) 2010 ARM Limited
juansal12 0:c3a329a5b05d 7 *
juansal12 0:c3a329a5b05d 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
juansal12 0:c3a329a5b05d 9 * of this software and associated documentation files (the "Software"), to deal
juansal12 0:c3a329a5b05d 10 * in the Software without restriction, including without limitation the rights
juansal12 0:c3a329a5b05d 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
juansal12 0:c3a329a5b05d 12 * copies of the Software, and to permit persons to whom the Software is
juansal12 0:c3a329a5b05d 13 * furnished to do so, subject to the following conditions:
juansal12 0:c3a329a5b05d 14 *
juansal12 0:c3a329a5b05d 15 * The above copyright notice and this permission notice shall be included in
juansal12 0:c3a329a5b05d 16 * all copies or substantial portions of the Software.
juansal12 0:c3a329a5b05d 17 *
juansal12 0:c3a329a5b05d 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
juansal12 0:c3a329a5b05d 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
juansal12 0:c3a329a5b05d 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
juansal12 0:c3a329a5b05d 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
juansal12 0:c3a329a5b05d 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
juansal12 0:c3a329a5b05d 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
juansal12 0:c3a329a5b05d 24 * THE SOFTWARE.
juansal12 0:c3a329a5b05d 25 *
juansal12 0:c3a329a5b05d 26 * @section DESCRIPTION
juansal12 0:c3a329a5b05d 27 *
juansal12 0:c3a329a5b05d 28 * Quadrature Encoder Interface.
juansal12 0:c3a329a5b05d 29 *
juansal12 0:c3a329a5b05d 30 * A quadrature encoder consists of two code tracks on a disc which are 90
juansal12 0:c3a329a5b05d 31 * degrees out of phase. It can be used to determine how far a wheel has
juansal12 0:c3a329a5b05d 32 * rotated, relative to a known starting position.
juansal12 0:c3a329a5b05d 33 *
juansal12 0:c3a329a5b05d 34 * Only one code track changes at a time leading to a more robust system than
juansal12 0:c3a329a5b05d 35 * a single track, because any jitter around any edge won't cause a state
juansal12 0:c3a329a5b05d 36 * change as the other track will remain constant.
juansal12 0:c3a329a5b05d 37 *
juansal12 0:c3a329a5b05d 38 * Encoders can be a homebrew affair, consisting of infrared emitters/receivers
juansal12 0:c3a329a5b05d 39 * and paper code tracks consisting of alternating black and white sections;
juansal12 0:c3a329a5b05d 40 * alternatively, complete disk and PCB emitter/receiver encoder systems can
juansal12 0:c3a329a5b05d 41 * be bought, but the interface, regardless of implementation is the same.
juansal12 0:c3a329a5b05d 42 *
juansal12 0:c3a329a5b05d 43 * +-----+ +-----+ +-----+
juansal12 0:c3a329a5b05d 44 * Channel A | ^ | | | | |
juansal12 0:c3a329a5b05d 45 * ---+ ^ +-----+ +-----+ +-----
juansal12 0:c3a329a5b05d 46 * ^ ^
juansal12 0:c3a329a5b05d 47 * ^ +-----+ +-----+ +-----+
juansal12 0:c3a329a5b05d 48 * Channel B ^ | | | | | |
juansal12 0:c3a329a5b05d 49 * ------+ +-----+ +-----+ +-----
juansal12 0:c3a329a5b05d 50 * ^ ^
juansal12 0:c3a329a5b05d 51 * ^ ^
juansal12 0:c3a329a5b05d 52 * 90deg
juansal12 0:c3a329a5b05d 53 *
juansal12 0:c3a329a5b05d 54 * The interface uses X2 encoding by default which calculates the pulse count
juansal12 0:c3a329a5b05d 55 * based on reading the current state after each rising and falling edge of
juansal12 0:c3a329a5b05d 56 * channel A.
juansal12 0:c3a329a5b05d 57 *
juansal12 0:c3a329a5b05d 58 * +-----+ +-----+ +-----+
juansal12 0:c3a329a5b05d 59 * Channel A | | | | | |
juansal12 0:c3a329a5b05d 60 * ---+ +-----+ +-----+ +-----
juansal12 0:c3a329a5b05d 61 * ^ ^ ^ ^ ^
juansal12 0:c3a329a5b05d 62 * ^ +-----+ ^ +-----+ ^ +-----+
juansal12 0:c3a329a5b05d 63 * Channel B ^ | ^ | ^ | ^ | ^ | |
juansal12 0:c3a329a5b05d 64 * ------+ ^ +-----+ ^ +-----+ +--
juansal12 0:c3a329a5b05d 65 * ^ ^ ^ ^ ^
juansal12 0:c3a329a5b05d 66 * ^ ^ ^ ^ ^
juansal12 0:c3a329a5b05d 67 * Pulse count 0 1 2 3 4 5 ...
juansal12 0:c3a329a5b05d 68 *
juansal12 0:c3a329a5b05d 69 * This interface can also use X4 encoding which calculates the pulse count
juansal12 0:c3a329a5b05d 70 * based on reading the current state after each rising and falling edge of
juansal12 0:c3a329a5b05d 71 * either channel.
juansal12 0:c3a329a5b05d 72 *
juansal12 0:c3a329a5b05d 73 * +-----+ +-----+ +-----+
juansal12 0:c3a329a5b05d 74 * Channel A | | | | | |
juansal12 0:c3a329a5b05d 75 * ---+ +-----+ +-----+ +-----
juansal12 0:c3a329a5b05d 76 * ^ ^ ^ ^ ^
juansal12 0:c3a329a5b05d 77 * ^ +-----+ ^ +-----+ ^ +-----+
juansal12 0:c3a329a5b05d 78 * Channel B ^ | ^ | ^ | ^ | ^ | |
juansal12 0:c3a329a5b05d 79 * ------+ ^ +-----+ ^ +-----+ +--
juansal12 0:c3a329a5b05d 80 * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
juansal12 0:c3a329a5b05d 81 * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
juansal12 0:c3a329a5b05d 82 * Pulse count 0 1 2 3 4 5 6 7 8 9 ...
juansal12 0:c3a329a5b05d 83 *
juansal12 0:c3a329a5b05d 84 * It defaults
juansal12 0:c3a329a5b05d 85 *
juansal12 0:c3a329a5b05d 86 * An optional index channel can be used which determines when a full
juansal12 0:c3a329a5b05d 87 * revolution has occured.
juansal12 0:c3a329a5b05d 88 *
juansal12 0:c3a329a5b05d 89 * If a 4 pules per revolution encoder was used, with X4 encoding,
juansal12 0:c3a329a5b05d 90 * the following would be observed.
juansal12 0:c3a329a5b05d 91 *
juansal12 0:c3a329a5b05d 92 * +-----+ +-----+ +-----+
juansal12 0:c3a329a5b05d 93 * Channel A | | | | | |
juansal12 0:c3a329a5b05d 94 * ---+ +-----+ +-----+ +-----
juansal12 0:c3a329a5b05d 95 * ^ ^ ^ ^ ^
juansal12 0:c3a329a5b05d 96 * ^ +-----+ ^ +-----+ ^ +-----+
juansal12 0:c3a329a5b05d 97 * Channel B ^ | ^ | ^ | ^ | ^ | |
juansal12 0:c3a329a5b05d 98 * ------+ ^ +-----+ ^ +-----+ +--
juansal12 0:c3a329a5b05d 99 * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
juansal12 0:c3a329a5b05d 100 * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
juansal12 0:c3a329a5b05d 101 * ^ ^ ^ +--+ ^ ^ +--+ ^
juansal12 0:c3a329a5b05d 102 * ^ ^ ^ | | ^ ^ | | ^
juansal12 0:c3a329a5b05d 103 * Index ------------+ +--------+ +-----------
juansal12 0:c3a329a5b05d 104 * ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
juansal12 0:c3a329a5b05d 105 * Pulse count 0 1 2 3 4 5 6 7 8 9 ...
juansal12 0:c3a329a5b05d 106 * Rev. count 0 1 2
juansal12 0:c3a329a5b05d 107 *
juansal12 0:c3a329a5b05d 108 * Rotational position in degrees can be calculated by:
juansal12 0:c3a329a5b05d 109 *
juansal12 0:c3a329a5b05d 110 * (pulse count / X * N) * 360
juansal12 0:c3a329a5b05d 111 *
juansal12 0:c3a329a5b05d 112 * Where X is the encoding type [e.g. X4 encoding => X=4], and N is the number
juansal12 0:c3a329a5b05d 113 * of pulses per revolution.
juansal12 0:c3a329a5b05d 114 *
juansal12 0:c3a329a5b05d 115 * Linear position can be calculated by:
juansal12 0:c3a329a5b05d 116 *
juansal12 0:c3a329a5b05d 117 * (pulse count / X * N) * (1 / PPI)
juansal12 0:c3a329a5b05d 118 *
juansal12 0:c3a329a5b05d 119 * Where X is encoding type [e.g. X4 encoding => X=44], N is the number of
juansal12 0:c3a329a5b05d 120 * pulses per revolution, and PPI is pulses per inch, or the equivalent for
juansal12 0:c3a329a5b05d 121 * any other unit of displacement. PPI can be calculated by taking the
juansal12 0:c3a329a5b05d 122 * circumference of the wheel or encoder disk and dividing it by the number
juansal12 0:c3a329a5b05d 123 * of pulses per revolution.
juansal12 0:c3a329a5b05d 124 */
juansal12 0:c3a329a5b05d 125
juansal12 0:c3a329a5b05d 126 /**
juansal12 0:c3a329a5b05d 127 * Includes
juansal12 0:c3a329a5b05d 128 */
juansal12 0:c3a329a5b05d 129 #include "QEI.h"
juansal12 0:c3a329a5b05d 130
juansal12 0:c3a329a5b05d 131 QEI::QEI(PinName channelA,
juansal12 0:c3a329a5b05d 132 PinName channelB,
juansal12 0:c3a329a5b05d 133 PinName index,
juansal12 0:c3a329a5b05d 134 int pulsesPerRev,
juansal12 0:c3a329a5b05d 135 Encoding encoding) : channelA_(channelA), channelB_(channelB),
juansal12 0:c3a329a5b05d 136 index_(index) {
juansal12 0:c3a329a5b05d 137
juansal12 0:c3a329a5b05d 138 pulses_ = 0;
juansal12 0:c3a329a5b05d 139 revolutions_ = 0;
juansal12 0:c3a329a5b05d 140 pulsesPerRev_ = pulsesPerRev;
juansal12 0:c3a329a5b05d 141 encoding_ = encoding;
juansal12 0:c3a329a5b05d 142
juansal12 0:c3a329a5b05d 143 //Workout what the current state is.
juansal12 0:c3a329a5b05d 144 int chanA = channelA_.read();
juansal12 0:c3a329a5b05d 145 int chanB = channelB_.read();
juansal12 0:c3a329a5b05d 146
juansal12 0:c3a329a5b05d 147 //2-bit state.
juansal12 0:c3a329a5b05d 148 currState_ = (chanA << 1) | (chanB);
juansal12 0:c3a329a5b05d 149 prevState_ = currState_;
juansal12 0:c3a329a5b05d 150
juansal12 0:c3a329a5b05d 151 //X2 encoding uses interrupts on only channel A.
juansal12 0:c3a329a5b05d 152 //X4 encoding uses interrupts on channel A,
juansal12 0:c3a329a5b05d 153 //and on channel B.
juansal12 0:c3a329a5b05d 154 channelA_.rise(this, &QEI::encode);
juansal12 0:c3a329a5b05d 155 channelA_.fall(this, &QEI::encode);
juansal12 0:c3a329a5b05d 156
juansal12 0:c3a329a5b05d 157 //If we're using X4 encoding, then attach interrupts to channel B too.
juansal12 0:c3a329a5b05d 158 if (encoding == X4_ENCODING) {
juansal12 0:c3a329a5b05d 159 channelB_.rise(this, &QEI::encode);
juansal12 0:c3a329a5b05d 160 channelB_.fall(this, &QEI::encode);
juansal12 0:c3a329a5b05d 161 }
juansal12 0:c3a329a5b05d 162 //Index is optional.
juansal12 0:c3a329a5b05d 163 if (index != NC) {
juansal12 0:c3a329a5b05d 164 index_.rise(this, &QEI::index);
juansal12 0:c3a329a5b05d 165 }
juansal12 0:c3a329a5b05d 166
juansal12 0:c3a329a5b05d 167 }
juansal12 0:c3a329a5b05d 168
juansal12 0:c3a329a5b05d 169 void QEI::reset(void) {
juansal12 0:c3a329a5b05d 170
juansal12 0:c3a329a5b05d 171 pulses_ = 0;
juansal12 0:c3a329a5b05d 172 revolutions_ = 0;
juansal12 0:c3a329a5b05d 173
juansal12 0:c3a329a5b05d 174 }
juansal12 0:c3a329a5b05d 175
juansal12 0:c3a329a5b05d 176 int QEI::getCurrentState(void) {
juansal12 0:c3a329a5b05d 177
juansal12 0:c3a329a5b05d 178 return currState_;
juansal12 0:c3a329a5b05d 179
juansal12 0:c3a329a5b05d 180 }
juansal12 0:c3a329a5b05d 181
juansal12 0:c3a329a5b05d 182 int QEI::getPulses(void) {
juansal12 0:c3a329a5b05d 183
juansal12 0:c3a329a5b05d 184 return pulses_;
juansal12 0:c3a329a5b05d 185
juansal12 0:c3a329a5b05d 186 }
juansal12 0:c3a329a5b05d 187
juansal12 0:c3a329a5b05d 188 int QEI::getRevolutions(void) {
juansal12 0:c3a329a5b05d 189
juansal12 0:c3a329a5b05d 190 return revolutions_;
juansal12 0:c3a329a5b05d 191
juansal12 0:c3a329a5b05d 192 }
juansal12 0:c3a329a5b05d 193
juansal12 0:c3a329a5b05d 194 // +-------------+
juansal12 0:c3a329a5b05d 195 // | X2 Encoding |
juansal12 0:c3a329a5b05d 196 // +-------------+
juansal12 0:c3a329a5b05d 197 //
juansal12 0:c3a329a5b05d 198 // When observing states two patterns will appear:
juansal12 0:c3a329a5b05d 199 //
juansal12 0:c3a329a5b05d 200 // Counter clockwise rotation:
juansal12 0:c3a329a5b05d 201 //
juansal12 0:c3a329a5b05d 202 // 10 -> 01 -> 10 -> 01 -> ...
juansal12 0:c3a329a5b05d 203 //
juansal12 0:c3a329a5b05d 204 // Clockwise rotation:
juansal12 0:c3a329a5b05d 205 //
juansal12 0:c3a329a5b05d 206 // 11 -> 00 -> 11 -> 00 -> ...
juansal12 0:c3a329a5b05d 207 //
juansal12 0:c3a329a5b05d 208 // We consider counter clockwise rotation to be "forward" and
juansal12 0:c3a329a5b05d 209 // counter clockwise to be "backward". Therefore pulse count will increase
juansal12 0:c3a329a5b05d 210 // during counter clockwise rotation and decrease during clockwise rotation.
juansal12 0:c3a329a5b05d 211 //
juansal12 0:c3a329a5b05d 212 // +-------------+
juansal12 0:c3a329a5b05d 213 // | X4 Encoding |
juansal12 0:c3a329a5b05d 214 // +-------------+
juansal12 0:c3a329a5b05d 215 //
juansal12 0:c3a329a5b05d 216 // There are four possible states for a quadrature encoder which correspond to
juansal12 0:c3a329a5b05d 217 // 2-bit gray code.
juansal12 0:c3a329a5b05d 218 //
juansal12 0:c3a329a5b05d 219 // A state change is only valid if of only one bit has changed.
juansal12 0:c3a329a5b05d 220 // A state change is invalid if both bits have changed.
juansal12 0:c3a329a5b05d 221 //
juansal12 0:c3a329a5b05d 222 // Clockwise Rotation ->
juansal12 0:c3a329a5b05d 223 //
juansal12 0:c3a329a5b05d 224 // 00 01 11 10 00
juansal12 0:c3a329a5b05d 225 //
juansal12 0:c3a329a5b05d 226 // <- Counter Clockwise Rotation
juansal12 0:c3a329a5b05d 227 //
juansal12 0:c3a329a5b05d 228 // If we observe any valid state changes going from left to right, we have
juansal12 0:c3a329a5b05d 229 // moved one pulse clockwise [we will consider this "backward" or "negative"].
juansal12 0:c3a329a5b05d 230 //
juansal12 0:c3a329a5b05d 231 // If we observe any valid state changes going from right to left we have
juansal12 0:c3a329a5b05d 232 // moved one pulse counter clockwise [we will consider this "forward" or
juansal12 0:c3a329a5b05d 233 // "positive"].
juansal12 0:c3a329a5b05d 234 //
juansal12 0:c3a329a5b05d 235 // We might enter an invalid state for a number of reasons which are hard to
juansal12 0:c3a329a5b05d 236 // predict - if this is the case, it is generally safe to ignore it, update
juansal12 0:c3a329a5b05d 237 // the state and carry on, with the error correcting itself shortly after.
juansal12 0:c3a329a5b05d 238 void QEI::encode(void) {
juansal12 0:c3a329a5b05d 239
juansal12 0:c3a329a5b05d 240 int change = 0;
juansal12 0:c3a329a5b05d 241 int chanA = channelA_.read();
juansal12 0:c3a329a5b05d 242 int chanB = channelB_.read();
juansal12 0:c3a329a5b05d 243
juansal12 0:c3a329a5b05d 244 //2-bit state.
juansal12 0:c3a329a5b05d 245 currState_ = (chanA << 1) | (chanB);
juansal12 0:c3a329a5b05d 246
juansal12 0:c3a329a5b05d 247 if (encoding_ == X2_ENCODING) {
juansal12 0:c3a329a5b05d 248
juansal12 0:c3a329a5b05d 249 //11->00->11->00 is counter clockwise rotation or "forward".
juansal12 0:c3a329a5b05d 250 if ((prevState_ == 0x3 && currState_ == 0x0) ||
juansal12 0:c3a329a5b05d 251 (prevState_ == 0x0 && currState_ == 0x3)) {
juansal12 0:c3a329a5b05d 252
juansal12 0:c3a329a5b05d 253 pulses_++;
juansal12 0:c3a329a5b05d 254
juansal12 0:c3a329a5b05d 255 }
juansal12 0:c3a329a5b05d 256 //10->01->10->01 is clockwise rotation or "backward".
juansal12 0:c3a329a5b05d 257 else if ((prevState_ == 0x2 && currState_ == 0x1) ||
juansal12 0:c3a329a5b05d 258 (prevState_ == 0x1 && currState_ == 0x2)) {
juansal12 0:c3a329a5b05d 259
juansal12 0:c3a329a5b05d 260 pulses_--;
juansal12 0:c3a329a5b05d 261
juansal12 0:c3a329a5b05d 262 }
juansal12 0:c3a329a5b05d 263
juansal12 0:c3a329a5b05d 264 } else if (encoding_ == X4_ENCODING) {
juansal12 0:c3a329a5b05d 265
juansal12 0:c3a329a5b05d 266 //Entered a new valid state.
juansal12 0:c3a329a5b05d 267 if (((currState_ ^ prevState_) != INVALID) && (currState_ != prevState_)) {
juansal12 0:c3a329a5b05d 268 //2 bit state. Right hand bit of prev XOR left hand bit of current
juansal12 0:c3a329a5b05d 269 //gives 0 if clockwise rotation and 1 if counter clockwise rotation.
juansal12 0:c3a329a5b05d 270 change = (prevState_ & PREV_MASK) ^ ((currState_ & CURR_MASK) >> 1);
juansal12 0:c3a329a5b05d 271
juansal12 0:c3a329a5b05d 272 if (change == 0) {
juansal12 0:c3a329a5b05d 273 change = -1;
juansal12 0:c3a329a5b05d 274 }
juansal12 0:c3a329a5b05d 275
juansal12 0:c3a329a5b05d 276 pulses_ -= change;
juansal12 0:c3a329a5b05d 277 }
juansal12 0:c3a329a5b05d 278
juansal12 0:c3a329a5b05d 279 }
juansal12 0:c3a329a5b05d 280
juansal12 0:c3a329a5b05d 281 prevState_ = currState_;
juansal12 0:c3a329a5b05d 282
juansal12 0:c3a329a5b05d 283 }
juansal12 0:c3a329a5b05d 284
juansal12 0:c3a329a5b05d 285 void QEI::index(void) {
juansal12 0:c3a329a5b05d 286
juansal12 0:c3a329a5b05d 287 revolutions_++;
juansal12 0:c3a329a5b05d 288
juansal12 0:c3a329a5b05d 289 }