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