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