update, sampling timenya kecilin

Committer:
sarlilouis
Date:
Wed Oct 25 15:04:27 2017 +0000
Revision:
0:a074f8e4001e
update :)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sarlilouis 0:a074f8e4001e 1 /********************************************************/
sarlilouis 0:a074f8e4001e 2 /* Library untuk pembacaan Encoder */
sarlilouis 0:a074f8e4001e 3 /* Adopsi dari QEI */
sarlilouis 0:a074f8e4001e 4 /* Bismillahirahmanirrahim */
sarlilouis 0:a074f8e4001e 5 /* Encoder yang sudah dicoba : */
sarlilouis 0:a074f8e4001e 6 /* 1. Autonics */
sarlilouis 0:a074f8e4001e 7 /* 2. Encoder bawaan Motor */
sarlilouis 0:a074f8e4001e 8 /* */
sarlilouis 0:a074f8e4001e 9 /* ______________________ */
sarlilouis 0:a074f8e4001e 10 /* |______Autonics______| */
sarlilouis 0:a074f8e4001e 11 /* | Out A = Input 1 | */
sarlilouis 0:a074f8e4001e 12 /* | Out B = Input 2 | */
sarlilouis 0:a074f8e4001e 13 /* | 5V | */
sarlilouis 0:a074f8e4001e 14 /* |_Gnd________________| */
sarlilouis 0:a074f8e4001e 15 /* */
sarlilouis 0:a074f8e4001e 16 /********************************************************/
sarlilouis 0:a074f8e4001e 17 //////////////////////////////////////////
sarlilouis 0:a074f8e4001e 18 // Library
sarlilouis 0:a074f8e4001e 19 /////////////////////////////////////////
sarlilouis 0:a074f8e4001e 20 #include "mbed.h"
sarlilouis 0:a074f8e4001e 21 #include "rotaryEncoder.h"
sarlilouis 0:a074f8e4001e 22
sarlilouis 0:a074f8e4001e 23 rotaryEncoder::rotaryEncoder(PinName channelA,
sarlilouis 0:a074f8e4001e 24 PinName channelB,
sarlilouis 0:a074f8e4001e 25 int pulsesPerRev,
sarlilouis 0:a074f8e4001e 26 Encoding encoding) : channelA_(channelA), channelB_(channelB)
sarlilouis 0:a074f8e4001e 27 {
sarlilouis 0:a074f8e4001e 28 pulses_ = 0;
sarlilouis 0:a074f8e4001e 29 revolutions_ = 0;
sarlilouis 0:a074f8e4001e 30 pulsesPerRev_ = pulsesPerRev;
sarlilouis 0:a074f8e4001e 31 encoding_ = encoding;
sarlilouis 0:a074f8e4001e 32
sarlilouis 0:a074f8e4001e 33 //Workout what the current state is.
sarlilouis 0:a074f8e4001e 34 int chanA = channelA_.read();
sarlilouis 0:a074f8e4001e 35 int chanB = channelB_.read();
sarlilouis 0:a074f8e4001e 36
sarlilouis 0:a074f8e4001e 37 //2-bit state.
sarlilouis 0:a074f8e4001e 38 currState_ = (chanA << 1) | (chanB);
sarlilouis 0:a074f8e4001e 39 prevState_ = currState_;
sarlilouis 0:a074f8e4001e 40
sarlilouis 0:a074f8e4001e 41 //X2 encoding uses interrupts on only channel A.
sarlilouis 0:a074f8e4001e 42 //X4 encoding uses interrupts on channel A,
sarlilouis 0:a074f8e4001e 43 //and on channel B.
sarlilouis 0:a074f8e4001e 44 channelA_.rise(this, &rotaryEncoder::encode);
sarlilouis 0:a074f8e4001e 45 channelA_.fall(this, &rotaryEncoder::encode);
sarlilouis 0:a074f8e4001e 46
sarlilouis 0:a074f8e4001e 47 //If we're using X4 encoding, then attach interrupts to channel B too.
sarlilouis 0:a074f8e4001e 48 if (encoding == X4_ENCODING) {
sarlilouis 0:a074f8e4001e 49 channelB_.rise(this, &rotaryEncoder::encode);
sarlilouis 0:a074f8e4001e 50 channelB_.fall(this, &rotaryEncoder::encode);
sarlilouis 0:a074f8e4001e 51 }
sarlilouis 0:a074f8e4001e 52 }
sarlilouis 0:a074f8e4001e 53
sarlilouis 0:a074f8e4001e 54 void rotaryEncoder::reset(void) {
sarlilouis 0:a074f8e4001e 55
sarlilouis 0:a074f8e4001e 56 pulses_ = 0;
sarlilouis 0:a074f8e4001e 57 revolutions_ = 0;
sarlilouis 0:a074f8e4001e 58
sarlilouis 0:a074f8e4001e 59 }
sarlilouis 0:a074f8e4001e 60
sarlilouis 0:a074f8e4001e 61 int rotaryEncoder::getPulses(void) {
sarlilouis 0:a074f8e4001e 62
sarlilouis 0:a074f8e4001e 63 return pulses_;
sarlilouis 0:a074f8e4001e 64
sarlilouis 0:a074f8e4001e 65 }
sarlilouis 0:a074f8e4001e 66
sarlilouis 0:a074f8e4001e 67 int rotaryEncoder::getRevolutions(void) {
sarlilouis 0:a074f8e4001e 68
sarlilouis 0:a074f8e4001e 69 revolutions_ = pulses_ / pulsesPerRev_;
sarlilouis 0:a074f8e4001e 70 return revolutions_;
sarlilouis 0:a074f8e4001e 71
sarlilouis 0:a074f8e4001e 72 }
sarlilouis 0:a074f8e4001e 73
sarlilouis 0:a074f8e4001e 74 /***************************************
sarlilouis 0:a074f8e4001e 75 * Perhitungan Pulse Encoder
sarlilouis 0:a074f8e4001e 76 ***************************************/
sarlilouis 0:a074f8e4001e 77
sarlilouis 0:a074f8e4001e 78 void rotaryEncoder::encode(void) {
sarlilouis 0:a074f8e4001e 79
sarlilouis 0:a074f8e4001e 80 int change = 0;
sarlilouis 0:a074f8e4001e 81 int chanA = channelA_.read();
sarlilouis 0:a074f8e4001e 82 int chanB = channelB_.read();
sarlilouis 0:a074f8e4001e 83
sarlilouis 0:a074f8e4001e 84 //2-bit state.
sarlilouis 0:a074f8e4001e 85 currState_ = (chanA << 1) | (chanB);
sarlilouis 0:a074f8e4001e 86
sarlilouis 0:a074f8e4001e 87 if (encoding_ == X2_ENCODING) {
sarlilouis 0:a074f8e4001e 88
sarlilouis 0:a074f8e4001e 89 //11->00->11->00 is counter clockwise rotation or "forward".
sarlilouis 0:a074f8e4001e 90 if ((prevState_ == 0x3 && currState_ == 0x0) ||
sarlilouis 0:a074f8e4001e 91 (prevState_ == 0x0 && currState_ == 0x3)) {
sarlilouis 0:a074f8e4001e 92
sarlilouis 0:a074f8e4001e 93 pulses_++;
sarlilouis 0:a074f8e4001e 94
sarlilouis 0:a074f8e4001e 95 }
sarlilouis 0:a074f8e4001e 96 //10->01->10->01 is clockwise rotation or "backward".
sarlilouis 0:a074f8e4001e 97 else if ((prevState_ == 0x2 && currState_ == 0x1) ||
sarlilouis 0:a074f8e4001e 98 (prevState_ == 0x1 && currState_ == 0x2)) {
sarlilouis 0:a074f8e4001e 99
sarlilouis 0:a074f8e4001e 100 pulses_--;
sarlilouis 0:a074f8e4001e 101
sarlilouis 0:a074f8e4001e 102 }
sarlilouis 0:a074f8e4001e 103
sarlilouis 0:a074f8e4001e 104 } else if (encoding_ == X4_ENCODING) {
sarlilouis 0:a074f8e4001e 105
sarlilouis 0:a074f8e4001e 106 //Entered a new valid state.
sarlilouis 0:a074f8e4001e 107 if (((currState_ ^ prevState_) != INVALID) && (currState_ != prevState_)) {
sarlilouis 0:a074f8e4001e 108 //2 bit state. Right hand bit of prev XOR left hand bit of current
sarlilouis 0:a074f8e4001e 109 //gives 0 if clockwise rotation and 1 if counter clockwise rotation.
sarlilouis 0:a074f8e4001e 110 change = (prevState_ & PREV_MASK) ^ ((currState_ & CURR_MASK) >> 1);
sarlilouis 0:a074f8e4001e 111
sarlilouis 0:a074f8e4001e 112 if (change == 0) {
sarlilouis 0:a074f8e4001e 113 change = -1;
sarlilouis 0:a074f8e4001e 114 }
sarlilouis 0:a074f8e4001e 115
sarlilouis 0:a074f8e4001e 116 pulses_ -= change;
sarlilouis 0:a074f8e4001e 117 }
sarlilouis 0:a074f8e4001e 118
sarlilouis 0:a074f8e4001e 119 }
sarlilouis 0:a074f8e4001e 120
sarlilouis 0:a074f8e4001e 121 prevState_ = currState_;
sarlilouis 0:a074f8e4001e 122
sarlilouis 0:a074f8e4001e 123 }
sarlilouis 0:a074f8e4001e 124