Rotary Encoder library with quadrate incremental encoding that you can choose on your own. Source for Encoding: https://www.motioncontroltips.com/faq-what-do-x1-x2-and-x4-position-encoding-mean-for-incremental-encoders/

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RotaryEncoder.h Source File

RotaryEncoder.h

00001 #ifndef MBED_ROTARYENCODER_H
00002 #define MBED_ROTARYENCODER_H
00003  
00004 #include "mbed.h"
00005 /*
00006     A library for quadrate incremental encoder. There are three encoding
00007     that can be used to determine posistion in quadrate incremental encoder; X1, X2, X4. For more information,
00008     you can see in: https://www.motioncontroltips.com/faq-what-do-x1-x2-and-x4-position-encoding-mean-for-incremental-encoders/
00009     
00010     Example on using X4 encoding: 
00011     #include "mbed.h"
00012     #include "RotaryEncoder.h"
00013     
00014     RotaryEncoder pA(PC_6, PC_5, 4, 200);// PC_6 and PC_5 are the pin used for hall sensor, 4 represent the encoding, and 200 is the ppr of the rotor
00015     Serial pc(USBTX, USBRX);
00016     
00017     int main(){
00018         while(1){
00019          pc.printf("%d\n", pA.getPulse());       
00020         }
00021     }
00022     
00023     ------------------------------------------------------------------------------------------------------------------------------
00024     
00025     Library untuk quadrate incremental encoder. Terdapat tiga jenis encoding
00026     yang dapat dipakai dalam menentukan posisi pada quadrate incremental encoder.
00027     Untuk informasi lebih lanjut, dapat dilihat pada: 
00028     https://www.motioncontroltips.com/faq-what-do-x1-x2-and-x4-position-encoding-mean-for-incremental-encoders/
00029     
00030     Contoh code untuk penggunaan encoding X4:
00031     #include "mbed.h"
00032     #include "RotaryEncoder.h"
00033     
00034     RotaryEncoder pA(PC_6, PC_5, 4, 200);// PC_6 dan PC_5 adalah pin untuk input hall sensor, 4 adalah jenis encoding yang dipakai, dan 200 adalah ppr dari rotor
00035     Serial pc(USBTX, USBRX);
00036     
00037     int main(){
00038         while(1){
00039          pc.printf("%d\n", pA.getPulse());       
00040         }
00041     }
00042     
00043 
00044 */
00045 
00046 class RotaryEncoder{
00047 public:
00048     //inisialisasi
00049     RotaryEncoder(PinName cA, PinName cB, int mode=1, float ppr=200);
00050     
00051     //Ambil posisi encoder
00052     float getPulse();
00053     
00054     /*ambil kecepatan dari encoder:
00055         x(t) = ∆x //perpindahan posisi  ->    x = pulse(p)     p(t) = ∆p
00056         v(t) = ∆x/∆t //kecepatan        ->    v(t) = ∆p/∆t
00057              = x'(t) 
00058     */  
00059     float getFreq();
00060     
00061     //Ambil mode encoding
00062     int getEncoding(){return mode;}
00063     
00064     //reset pulse
00065     void resetPulse();
00066     
00067     //get degree
00068     float getDegree();
00069     
00070     //get speed of motor
00071     float getRPM();
00072     float getRadian();
00073     
00074     private:
00075     InterruptIn cA;
00076     InterruptIn cB;
00077     Timer t;
00078     protected:
00079     double pi;
00080     const int mode;
00081     const float ppr;
00082     void encoding(int val){
00083         if(val == 1){
00084             cA.rise(this, &RotaryEncoder::callback1);
00085             }
00086         else if(val == 2){
00087             cA.rise(this, &RotaryEncoder::callback1);
00088             cA.fall(this, &RotaryEncoder::callback1);
00089             }
00090         else if(val == 4){
00091             cA.rise(this, &RotaryEncoder::callback1);
00092             cA.fall(this, &RotaryEncoder::callback1);
00093     
00094             cB.rise(this, &RotaryEncoder::callback2);
00095             cB.fall(this, &RotaryEncoder::callback2);
00096             }
00097         }
00098     void callback1(void);
00099     void callback2(void);
00100     volatile float v;
00101     volatile float nowT, dt, prevT;
00102     volatile float increment, pulse;
00103 };
00104 
00105 #endif