Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
EncoderMotor.h
00001 #ifndef ENCODERMOTOR_H 00002 #define ENCODERMOTOR_H 00003 00004 /** 00005 * Includes 00006 */ 00007 #include "mbed.h" 00008 00009 /** 00010 * Defines 00011 */ 00012 #define PREV_MASK 0x1 //Mask for the previous state in determining direction 00013 //of rotation. 00014 #define CURR_MASK 0x2 //Mask for the current state in determining direction 00015 //of rotation. 00016 #define INVALID 0x3 //XORing two states where both bits have changed. 00017 00018 /** 00019 * Quadrature Encoder Interface. 00020 */ 00021 class EncoderMotor { 00022 00023 public: 00024 00025 typedef enum Encoding 00026 { 00027 X2_ENCODING, 00028 X4_ENCODING 00029 } 00030 Encoding; 00031 00032 /** Membuat interface dari encoder 00033 * 00034 * @param inA DigitalIn, out A dari encoder 00035 * @param inB DigitalIn, out B dari encoder 00036 */ 00037 EncoderMotor(PinName channelA, PinName channelB, float pulsesPerRev, Encoding encoding = X2_ENCODING); 00038 00039 /** 00040 * Reset encoder. 00041 * 00042 * Menset pulse dan revolusi/putaran menjadi 0 00043 */ 00044 void reset(void); 00045 00046 /** 00047 * Membaca pulse yang didapat oleh encoder 00048 * 00049 * @return Nilai pulse yang telah dilalui. 00050 */ 00051 int getPulses(void); 00052 00053 /** 00054 * Membaca putaran yang didapat oleh encoder 00055 * 00056 * @return Nilai revolusi/putaran yang telah dilalui. 00057 */ 00058 float getRevolutions(void); 00059 00060 /** 00061 * Membaca pulse yang didapat oleh encoder 00062 * 00063 * @return Nilai pulse yang telah dilalui. 00064 */ 00065 //int readDeltaPulses(void); 00066 00067 /** 00068 * Membaca putaran yang didapat oleh encoder 00069 * 00070 * @return Nilai revolusi/putaran yang telah dilalui. 00071 */ 00072 //int readDeltaRevolutions(void); 00073 00074 /** 00075 * Disable encoder interrupt pins 00076 * 00077 * @return Disable interrupt pins 00078 */ 00079 void disableInterrupts(void); 00080 00081 /** 00082 * Enable encoder interrupt pins 00083 * 00084 * @return Enable interrupt pins 00085 */ 00086 void enableInterrupts(void); 00087 00088 00089 private: 00090 00091 /** 00092 * Menghitung pulse 00093 * 00094 * Digunakan setiap rising/falling edge baik channel A atau B 00095 * Membaca putaran CW atau CCW => mengakibatkan pertambahan/pengurangan pulse 00096 */ 00097 void encode(void); 00098 00099 /** 00100 * Indeks setiap rising edge untuk menghitung putaran 00101 * Nilai bertambah 1 00102 */ 00103 //void index(void); 00104 00105 Encoding encoding_; 00106 00107 InterruptIn channelA_; 00108 InterruptIn channelB_; 00109 //InterruptIn index_; 00110 00111 float pulsesPerRev_; 00112 int prevState_; 00113 int currState_; 00114 00115 volatile int pulses_; 00116 volatile float revolutions_; 00117 00118 }; 00119 00120 #endif /* ENCODERMOTOR_H */
Generated on Wed Jul 27 2022 10:06:28 by
1.7.2