mencari respon motor launcher dengan fungsi step

Dependencies:   Motor mbed millis

Fork of transfer_function_motor by KRAI 2017

Committer:
Najib_irvani
Date:
Thu Feb 16 16:24:30 2017 +0000
Revision:
0:be14d75c8037
mencari fungsi transfer motor dengan fungsi step

Who changed what in which revision?

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