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/
RotaryEncoder.cpp
00001 #include "RotaryEncoder.h" 00002 #include "mbed.h" 00003 00004 RotaryEncoder::RotaryEncoder(PinName channelA, PinName channelB, int mode, float ppr) : cA(channelA), cB(channelB), mode(mode), ppr(ppr){ 00005 pulse = 0; 00006 prevT = 0; 00007 nowT = 0; 00008 dt = 0; 00009 increment = 0; 00010 encoding(mode); 00011 pi = 3.14159265358979323846; 00012 t.reset(); 00013 t.start(); 00014 } 00015 00016 void RotaryEncoder::callback1(){ 00017 nowT = t; 00018 if(cB.read() != cA.read()){ 00019 increment = 1; 00020 } 00021 else{ 00022 increment = -1; 00023 } 00024 pulse = pulse + increment; 00025 dt = nowT - prevT; 00026 v = increment/dt; 00027 prevT = nowT; 00028 } 00029 00030 void RotaryEncoder::callback2(){ 00031 nowT = t; 00032 if(cB.read() != cA.read()){ 00033 increment = -1; 00034 } 00035 else{ 00036 increment = 1; 00037 } 00038 pulse = pulse + increment; 00039 dt = nowT - prevT; 00040 v = increment/dt; 00041 prevT = nowT; 00042 } 00043 00044 float RotaryEncoder::getPulse(){ 00045 return pulse; 00046 } 00047 00048 float RotaryEncoder::getFreq(){ 00049 if(pulse == 0){return 0;} 00050 return v; 00051 } 00052 00053 void RotaryEncoder::resetPulse(){ 00054 pulse = 0; 00055 } 00056 00057 float RotaryEncoder::getDegree(){ 00058 return pulse/(ppr*mode)*360; 00059 } 00060 00061 float RotaryEncoder::getRPM(){ 00062 return v/(ppr*mode)*60; 00063 } 00064 00065 float RotaryEncoder::getRadian(){ 00066 return (2*pi*v)/(ppr*dt); 00067 }
Generated on Wed Oct 19 2022 02:07:36 by
1.7.2
noze akgae