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