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.
Dependents: ball_snatch NHK2020-arm-sub NHK2020-arm-sub NHK2020-arm-sub2 ... more
EC.cpp
00001 #include "mbed.h" 00002 #include "EC.h" 00003 00004 00005 Ec::Ec(int res,int multi): 00006 count_(0),pre_count_(0),resolution_(res),multiplication_(multi) 00007 { 00008 timer_.start(); 00009 } 00010 00011 int Ec::getCount ()const 00012 { 00013 return count_; 00014 //return pre_count_; 00015 } 00016 00017 double Ec::getRad()const 00018 { 00019 return count_*2.0f*M_PI/(multiplication_*resolution_); 00020 } 00021 00022 void Ec::calOmega() 00023 { 00024 double t=timer_.read(); 00025 omega_=(count_-pre_count_)*2.0f*M_PI/(multiplication_*resolution_*(t-ptw_)); 00026 // omega_=(getCount()-pre_count_)*2.0f*M_PI/(multiplication_*resolution_*(t-ptw_)); 00027 //pre_count_=getCount(); 00028 pre_count_=count_; 00029 ptw_=t; 00030 } 00031 00032 double Ec::getOmega()const 00033 { 00034 return omega_; 00035 } 00036 void Ec::setResolution(int res) 00037 { 00038 resolution_=res; 00039 } 00040 00041 /*reset関数の定義*/ 00042 /*エンコーダを初期状態に戻すことができる*/ 00043 void Ec::reset() 00044 { 00045 count_=0; 00046 pre_count_=0,omega_=0; 00047 ptw_=0; 00048 timer_.stop(); 00049 timer_.reset(); 00050 timer_.start(); 00051 } 00052 00053 00054 ////////////////////////////////////////////////////1逓倍////////////////////////////////////////////////////////////////// 00055 Ec1multi::Ec1multi(PinName signalA,PinName signalB,int res) : Ec(res,1),signalA_(signalA),signalB_(signalB) 00056 { 00057 signalA_.rise(callback(this,&Ec1multi::upA)); 00058 } 00059 00060 //ピン変化割り込み関数の定義 00061 void Ec1multi::upA() 00062 { 00063 if(signalB_.read())count_++; 00064 else count_--; 00065 } 00066 ////////////////////////////////////////////////////2逓倍////////////////////////////////////////////////////////////////// 00067 Ec2multi::Ec2multi(PinName signalA,PinName signalB,int res) : Ec(res,2),signalA_(signalA),signalB_(signalB) 00068 { 00069 signalA_.rise(callback(this,&Ec2multi::upA)); 00070 signalA_.fall(callback(this,&Ec2multi::downA)); 00071 } 00072 00073 //ピン変化割り込み関数の定義 00074 void Ec2multi::upA() 00075 { 00076 if(signalB_.read())count_++; 00077 else count_--; 00078 } 00079 void Ec2multi::downA() 00080 { 00081 if(signalB_.read())count_--; 00082 else count_++; 00083 } 00084 00085 ////////////////////////////////////////////////////4逓倍////////////////////////////////////////////////////////////////// 00086 Ec4multi::Ec4multi(PinName signalA,PinName signalB,int res) : Ec(res,4),signalA_(signalA),signalB_(signalB),pa_(0),pb_(0) 00087 { 00088 signalA_.rise(callback(this,&Ec4multi::upA)); 00089 signalA_.fall(callback(this,&Ec4multi::downA)); 00090 signalB_.rise(callback(this,&Ec4multi::upB)); 00091 signalB_.fall(callback(this,&Ec4multi::downB)); 00092 } 00093 void Ec4multi::upA() 00094 { 00095 pa_=1; 00096 if(pb_==1)count_++; 00097 else count_--; 00098 } 00099 void Ec4multi::downA() 00100 { 00101 pa_=0; 00102 if(pb_==1)count_--; 00103 else count_++; 00104 } 00105 void Ec4multi::upB() 00106 { 00107 pb_=1; 00108 if(pa_==1)count_--; 00109 else count_++; 00110 } 00111 void Ec4multi::downB() 00112 { 00113 pb_=0; 00114 if(pa_==1)count_++; 00115 else count_--; 00116 }
Generated on Tue Jul 12 2022 20:44:20 by
1.7.2