PRAWIRA
EncoderMotor.h@0:cd1a027f038d, 2019-09-16 (annotated)
- Committer:
- alienbernamaihsan
- Date:
- Mon Sep 16 10:06:23 2019 +0000
- Revision:
- 0:cd1a027f038d
PRAWIRA
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
alienbernamaihsan | 0:cd1a027f038d | 1 | #ifndef ENCODERMOTOR_H |
alienbernamaihsan | 0:cd1a027f038d | 2 | #define ENCODERMOTOR_H |
alienbernamaihsan | 0:cd1a027f038d | 3 | |
alienbernamaihsan | 0:cd1a027f038d | 4 | /** |
alienbernamaihsan | 0:cd1a027f038d | 5 | * Includes |
alienbernamaihsan | 0:cd1a027f038d | 6 | */ |
alienbernamaihsan | 0:cd1a027f038d | 7 | #include "mbed.h" |
alienbernamaihsan | 0:cd1a027f038d | 8 | |
alienbernamaihsan | 0:cd1a027f038d | 9 | /** |
alienbernamaihsan | 0:cd1a027f038d | 10 | * Defines |
alienbernamaihsan | 0:cd1a027f038d | 11 | */ |
alienbernamaihsan | 0:cd1a027f038d | 12 | #define PREV_MASK 0x1 //Mask for the previous state in determining direction |
alienbernamaihsan | 0:cd1a027f038d | 13 | //of rotation. |
alienbernamaihsan | 0:cd1a027f038d | 14 | #define CURR_MASK 0x2 //Mask for the current state in determining direction |
alienbernamaihsan | 0:cd1a027f038d | 15 | //of rotation. |
alienbernamaihsan | 0:cd1a027f038d | 16 | #define INVALID 0x3 //XORing two states where both bits have changed. |
alienbernamaihsan | 0:cd1a027f038d | 17 | |
alienbernamaihsan | 0:cd1a027f038d | 18 | /** |
alienbernamaihsan | 0:cd1a027f038d | 19 | * Quadrature Encoder Interface. |
alienbernamaihsan | 0:cd1a027f038d | 20 | */ |
alienbernamaihsan | 0:cd1a027f038d | 21 | class EncoderMotor { |
alienbernamaihsan | 0:cd1a027f038d | 22 | |
alienbernamaihsan | 0:cd1a027f038d | 23 | public: |
alienbernamaihsan | 0:cd1a027f038d | 24 | |
alienbernamaihsan | 0:cd1a027f038d | 25 | typedef enum Encoding |
alienbernamaihsan | 0:cd1a027f038d | 26 | { |
alienbernamaihsan | 0:cd1a027f038d | 27 | X2_ENCODING, |
alienbernamaihsan | 0:cd1a027f038d | 28 | X4_ENCODING |
alienbernamaihsan | 0:cd1a027f038d | 29 | } |
alienbernamaihsan | 0:cd1a027f038d | 30 | Encoding; |
alienbernamaihsan | 0:cd1a027f038d | 31 | |
alienbernamaihsan | 0:cd1a027f038d | 32 | /** Membuat interface dari encoder |
alienbernamaihsan | 0:cd1a027f038d | 33 | * |
alienbernamaihsan | 0:cd1a027f038d | 34 | * @param inA DigitalIn, out A dari encoder |
alienbernamaihsan | 0:cd1a027f038d | 35 | * @param inB DigitalIn, out B dari encoder |
alienbernamaihsan | 0:cd1a027f038d | 36 | */ |
alienbernamaihsan | 0:cd1a027f038d | 37 | EncoderMotor(PinName channelA, PinName channelB, float pulsesPerRev, Encoding encoding = X2_ENCODING); |
alienbernamaihsan | 0:cd1a027f038d | 38 | |
alienbernamaihsan | 0:cd1a027f038d | 39 | /** |
alienbernamaihsan | 0:cd1a027f038d | 40 | * Reset encoder. |
alienbernamaihsan | 0:cd1a027f038d | 41 | * |
alienbernamaihsan | 0:cd1a027f038d | 42 | * Menset pulse dan revolusi/putaran menjadi 0 |
alienbernamaihsan | 0:cd1a027f038d | 43 | */ |
alienbernamaihsan | 0:cd1a027f038d | 44 | void reset(void); |
alienbernamaihsan | 0:cd1a027f038d | 45 | |
alienbernamaihsan | 0:cd1a027f038d | 46 | /** |
alienbernamaihsan | 0:cd1a027f038d | 47 | * Membaca pulse yang didapat oleh encoder |
alienbernamaihsan | 0:cd1a027f038d | 48 | * |
alienbernamaihsan | 0:cd1a027f038d | 49 | * @return Nilai pulse yang telah dilalui. |
alienbernamaihsan | 0:cd1a027f038d | 50 | */ |
alienbernamaihsan | 0:cd1a027f038d | 51 | int getPulses(void); |
alienbernamaihsan | 0:cd1a027f038d | 52 | |
alienbernamaihsan | 0:cd1a027f038d | 53 | /** |
alienbernamaihsan | 0:cd1a027f038d | 54 | * Membaca putaran yang didapat oleh encoder |
alienbernamaihsan | 0:cd1a027f038d | 55 | * |
alienbernamaihsan | 0:cd1a027f038d | 56 | * @return Nilai revolusi/putaran yang telah dilalui. |
alienbernamaihsan | 0:cd1a027f038d | 57 | */ |
alienbernamaihsan | 0:cd1a027f038d | 58 | float getRevolutions(void); |
alienbernamaihsan | 0:cd1a027f038d | 59 | |
alienbernamaihsan | 0:cd1a027f038d | 60 | /** |
alienbernamaihsan | 0:cd1a027f038d | 61 | * Membaca pulse yang didapat oleh encoder |
alienbernamaihsan | 0:cd1a027f038d | 62 | * |
alienbernamaihsan | 0:cd1a027f038d | 63 | * @return Nilai pulse yang telah dilalui. |
alienbernamaihsan | 0:cd1a027f038d | 64 | */ |
alienbernamaihsan | 0:cd1a027f038d | 65 | //int readDeltaPulses(void); |
alienbernamaihsan | 0:cd1a027f038d | 66 | |
alienbernamaihsan | 0:cd1a027f038d | 67 | /** |
alienbernamaihsan | 0:cd1a027f038d | 68 | * Membaca putaran yang didapat oleh encoder |
alienbernamaihsan | 0:cd1a027f038d | 69 | * |
alienbernamaihsan | 0:cd1a027f038d | 70 | * @return Nilai revolusi/putaran yang telah dilalui. |
alienbernamaihsan | 0:cd1a027f038d | 71 | */ |
alienbernamaihsan | 0:cd1a027f038d | 72 | //int readDeltaRevolutions(void); |
alienbernamaihsan | 0:cd1a027f038d | 73 | |
alienbernamaihsan | 0:cd1a027f038d | 74 | /** |
alienbernamaihsan | 0:cd1a027f038d | 75 | * Disable encoder interrupt pins |
alienbernamaihsan | 0:cd1a027f038d | 76 | * |
alienbernamaihsan | 0:cd1a027f038d | 77 | * @return Disable interrupt pins |
alienbernamaihsan | 0:cd1a027f038d | 78 | */ |
alienbernamaihsan | 0:cd1a027f038d | 79 | void disableInterrupts(void); |
alienbernamaihsan | 0:cd1a027f038d | 80 | |
alienbernamaihsan | 0:cd1a027f038d | 81 | /** |
alienbernamaihsan | 0:cd1a027f038d | 82 | * Enable encoder interrupt pins |
alienbernamaihsan | 0:cd1a027f038d | 83 | * |
alienbernamaihsan | 0:cd1a027f038d | 84 | * @return Enable interrupt pins |
alienbernamaihsan | 0:cd1a027f038d | 85 | */ |
alienbernamaihsan | 0:cd1a027f038d | 86 | void enableInterrupts(void); |
alienbernamaihsan | 0:cd1a027f038d | 87 | |
alienbernamaihsan | 0:cd1a027f038d | 88 | |
alienbernamaihsan | 0:cd1a027f038d | 89 | private: |
alienbernamaihsan | 0:cd1a027f038d | 90 | |
alienbernamaihsan | 0:cd1a027f038d | 91 | /** |
alienbernamaihsan | 0:cd1a027f038d | 92 | * Menghitung pulse |
alienbernamaihsan | 0:cd1a027f038d | 93 | * |
alienbernamaihsan | 0:cd1a027f038d | 94 | * Digunakan setiap rising/falling edge baik channel A atau B |
alienbernamaihsan | 0:cd1a027f038d | 95 | * Membaca putaran CW atau CCW => mengakibatkan pertambahan/pengurangan pulse |
alienbernamaihsan | 0:cd1a027f038d | 96 | */ |
alienbernamaihsan | 0:cd1a027f038d | 97 | void encode(void); |
alienbernamaihsan | 0:cd1a027f038d | 98 | |
alienbernamaihsan | 0:cd1a027f038d | 99 | /** |
alienbernamaihsan | 0:cd1a027f038d | 100 | * Indeks setiap rising edge untuk menghitung putaran |
alienbernamaihsan | 0:cd1a027f038d | 101 | * Nilai bertambah 1 |
alienbernamaihsan | 0:cd1a027f038d | 102 | */ |
alienbernamaihsan | 0:cd1a027f038d | 103 | //void index(void); |
alienbernamaihsan | 0:cd1a027f038d | 104 | |
alienbernamaihsan | 0:cd1a027f038d | 105 | Encoding encoding_; |
alienbernamaihsan | 0:cd1a027f038d | 106 | |
alienbernamaihsan | 0:cd1a027f038d | 107 | InterruptIn channelA_; |
alienbernamaihsan | 0:cd1a027f038d | 108 | InterruptIn channelB_; |
alienbernamaihsan | 0:cd1a027f038d | 109 | //InterruptIn index_; |
alienbernamaihsan | 0:cd1a027f038d | 110 | |
alienbernamaihsan | 0:cd1a027f038d | 111 | float pulsesPerRev_; |
alienbernamaihsan | 0:cd1a027f038d | 112 | int prevState_; |
alienbernamaihsan | 0:cd1a027f038d | 113 | int currState_; |
alienbernamaihsan | 0:cd1a027f038d | 114 | |
alienbernamaihsan | 0:cd1a027f038d | 115 | volatile int pulses_; |
alienbernamaihsan | 0:cd1a027f038d | 116 | volatile float revolutions_; |
alienbernamaihsan | 0:cd1a027f038d | 117 | |
alienbernamaihsan | 0:cd1a027f038d | 118 | }; |
alienbernamaihsan | 0:cd1a027f038d | 119 | |
alienbernamaihsan | 0:cd1a027f038d | 120 | #endif /* ENCODERMOTOR_H */ |