DECS_YoungCHA / Mbed 2 deprecated BTS_dc_motor

Dependencies:   mbed

Committer:
ohdoyoel
Date:
Tue Aug 23 08:55:01 2022 +0000
Revision:
1:fa0730bf53ef
hello~

Who changed what in which revision?

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