ec
Fork of EC by
EC.cpp@10:216d5a573dc7, 2016-11-29 (annotated)
- Committer:
- jack0325suzu
- Date:
- Tue Nov 29 12:45:45 2016 +0000
- Revision:
- 10:216d5a573dc7
- Parent:
- 9:a919aa92e65e
- Child:
- 11:b96009f8b9fd
fff
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jack0325suzu | 0:20fc96400ca3 | 1 | #include "mbed.h" |
jack0325suzu | 0:20fc96400ca3 | 2 | #include "EC.h" |
jack0325suzu | 0:20fc96400ca3 | 3 | |
jack0325suzu | 0:20fc96400ca3 | 4 | int Ec::defsolution; |
jack0325suzu | 0:20fc96400ca3 | 5 | double Ec::deftime; |
jack0325suzu | 0:20fc96400ca3 | 6 | //ピン変化割り込み関数の定義 |
jack0325suzu | 0:20fc96400ca3 | 7 | void Ec::upA(){ |
jack0325suzu | 0:20fc96400ca3 | 8 | stateA=1; |
jack0325suzu | 0:20fc96400ca3 | 9 | if(stateB==0&&S==0) { |
jack0325suzu | 0:20fc96400ca3 | 10 | S=1; |
jack0325suzu | 0:20fc96400ca3 | 11 | } else if(stateB==1&&S==3) { |
jack0325suzu | 0:20fc96400ca3 | 12 | S=2; |
jack0325suzu | 0:20fc96400ca3 | 13 | } |
jack0325suzu | 0:20fc96400ca3 | 14 | } |
jack0325suzu | 0:20fc96400ca3 | 15 | void Ec::downA(){ |
jack0325suzu | 0:20fc96400ca3 | 16 | stateA=0; |
jack0325suzu | 0:20fc96400ca3 | 17 | if(stateB==1&&S==2) { |
jack0325suzu | 0:20fc96400ca3 | 18 | S=3; |
jack0325suzu | 0:20fc96400ca3 | 19 | } else if(stateB==0&&S==1) { |
jack0325suzu | 0:20fc96400ca3 | 20 | S=0; |
jack0325suzu | 0:20fc96400ca3 | 21 | count--; |
jack0325suzu | 0:20fc96400ca3 | 22 | } |
jack0325suzu | 0:20fc96400ca3 | 23 | } |
jack0325suzu | 0:20fc96400ca3 | 24 | void Ec::upB(){ |
jack0325suzu | 0:20fc96400ca3 | 25 | stateB=1; |
jack0325suzu | 0:20fc96400ca3 | 26 | if(stateA==1&&S==1) { |
jack0325suzu | 0:20fc96400ca3 | 27 | S=2; |
jack0325suzu | 0:20fc96400ca3 | 28 | } else if(stateA==0&&S==0) { |
jack0325suzu | 0:20fc96400ca3 | 29 | S=3; |
jack0325suzu | 0:20fc96400ca3 | 30 | } |
jack0325suzu | 0:20fc96400ca3 | 31 | } |
jack0325suzu | 0:20fc96400ca3 | 32 | void Ec::downB(){ |
jack0325suzu | 0:20fc96400ca3 | 33 | stateB=0; |
jack0325suzu | 0:20fc96400ca3 | 34 | if(stateA==0&&S==3) { |
jack0325suzu | 0:20fc96400ca3 | 35 | count++; |
jack0325suzu | 0:20fc96400ca3 | 36 | S=0; |
jack0325suzu | 0:20fc96400ca3 | 37 | } else if(stateA==1&&S==2) { |
jack0325suzu | 0:20fc96400ca3 | 38 | S=1; |
jack0325suzu | 0:20fc96400ca3 | 39 | } |
jack0325suzu | 0:20fc96400ca3 | 40 | } |
jack0325suzu | 0:20fc96400ca3 | 41 | |
jack0325suzu | 5:4abba4f54406 | 42 | void Ec::upZ(){ |
jack0325suzu | 5:4abba4f54406 | 43 | if(first==false){ |
jack0325suzu | 5:4abba4f54406 | 44 | rev++; |
jack0325suzu | 5:4abba4f54406 | 45 | first=true; |
jack0325suzu | 5:4abba4f54406 | 46 | now_time=timer.read(); |
jack0325suzu | 5:4abba4f54406 | 47 | RPM=60/(now_time-old_time); |
jack0325suzu | 10:216d5a573dc7 | 48 | if((RPM_old-RPM)>RPM_th){ |
jack0325suzu | 5:4abba4f54406 | 49 | printf("\r\n CAUTION : speed downed drastically\r\n"); |
jack0325suzu | 5:4abba4f54406 | 50 | NVIC_SystemReset(); |
jack0325suzu | 5:4abba4f54406 | 51 | } |
jack0325suzu | 5:4abba4f54406 | 52 | RPM_old=RPM; |
jack0325suzu | 5:4abba4f54406 | 53 | old_time=timer.read(); |
jack0325suzu | 5:4abba4f54406 | 54 | } |
jack0325suzu | 5:4abba4f54406 | 55 | } |
jack0325suzu | 5:4abba4f54406 | 56 | |
jack0325suzu | 5:4abba4f54406 | 57 | void Ec::downZ(){ |
jack0325suzu | 5:4abba4f54406 | 58 | first=false; |
jack0325suzu | 5:4abba4f54406 | 59 | } |
jack0325suzu | 5:4abba4f54406 | 60 | |
jack0325suzu | 5:4abba4f54406 | 61 | |
jack0325suzu | 0:20fc96400ca3 | 62 | //コンストラクタの定義 |
jack0325suzu | 0:20fc96400ca3 | 63 | //main関数の前に必ず一度宣言する |
jack0325suzu | 0:20fc96400ca3 | 64 | //第一・第二引数はエンコーダのA・B相のピン名 |
jack0325suzu | 0:20fc96400ca3 | 65 | //第三院数はエンコーダの分解能 |
jack0325suzu | 0:20fc96400ca3 | 66 | |
jack0325suzu | 5:4abba4f54406 | 67 | Ec::Ec(PinName signalA , PinName signalB , PinName signalZ , int s=defsolution , double t=deftime) : signalA_(signalA),signalB_(signalB),signalZ_(signalZ) |
jack0325suzu | 0:20fc96400ca3 | 68 | { |
jack0325suzu | 5:4abba4f54406 | 69 | |
jack0325suzu | 5:4abba4f54406 | 70 | if((signalA!=NC)&&(signalB!=NC)){ |
jack0325suzu | 5:4abba4f54406 | 71 | S=0;stateA=0;stateB=0;count=0;pre_count=0.0; |
jack0325suzu | 5:4abba4f54406 | 72 | signalA_.rise(this,&Ec::upA); |
jack0325suzu | 5:4abba4f54406 | 73 | signalA_.fall(this,&Ec::downA); |
jack0325suzu | 5:4abba4f54406 | 74 | signalB_.rise(this,&Ec::upB); |
jack0325suzu | 5:4abba4f54406 | 75 | signalB_.fall(this,&Ec::downB); |
jack0325suzu | 5:4abba4f54406 | 76 | } |
jack0325suzu | 5:4abba4f54406 | 77 | if(signalZ!=NC){ |
jack0325suzu | 5:4abba4f54406 | 78 | first=false; rev=0; old_time=0; RPM=0; RPM_old=0; |
jack0325suzu | 5:4abba4f54406 | 79 | timer.start(); |
jack0325suzu | 5:4abba4f54406 | 80 | signalZ_.rise(this,&Ec::upZ); |
jack0325suzu | 5:4abba4f54406 | 81 | signalZ_.fall(this,&Ec::downZ); |
jack0325suzu | 5:4abba4f54406 | 82 | } |
jack0325suzu | 0:20fc96400ca3 | 83 | dt=t; |
jack0325suzu | 0:20fc96400ca3 | 84 | solution=s; |
jack0325suzu | 0:20fc96400ca3 | 85 | defsolution=s; |
jack0325suzu | 10:216d5a573dc7 | 86 | RPM_th=250; |
jack0325suzu | 0:20fc96400ca3 | 87 | } |
jack0325suzu | 0:20fc96400ca3 | 88 | |
jack0325suzu | 0:20fc96400ca3 | 89 | int Ec::getCount(){ |
jack0325suzu | 0:20fc96400ca3 | 90 | return count; |
jack0325suzu | 0:20fc96400ca3 | 91 | } |
jack0325suzu | 0:20fc96400ca3 | 92 | |
jack0325suzu | 0:20fc96400ca3 | 93 | void Ec::CalOmega(){ |
jack0325suzu | 0:20fc96400ca3 | 94 | omega=(count-pre_count)*2*M_pi/(solution*dt); |
jack0325suzu | 0:20fc96400ca3 | 95 | pre_count=count; |
jack0325suzu | 0:20fc96400ca3 | 96 | } |
jack0325suzu | 0:20fc96400ca3 | 97 | |
jack0325suzu | 0:20fc96400ca3 | 98 | double Ec::getOmega(){ |
jack0325suzu | 0:20fc96400ca3 | 99 | return omega; |
jack0325suzu | 0:20fc96400ca3 | 100 | } |
jack0325suzu | 0:20fc96400ca3 | 101 | |
jack0325suzu | 0:20fc96400ca3 | 102 | double Ec::getPreCount(){ |
jack0325suzu | 0:20fc96400ca3 | 103 | precount=count+S/4.0; |
jack0325suzu | 0:20fc96400ca3 | 104 | return precount; |
jack0325suzu | 0:20fc96400ca3 | 105 | } |
jack0325suzu | 0:20fc96400ca3 | 106 | /*reset関数の定義*/ |
jack0325suzu | 0:20fc96400ca3 | 107 | /*エンコーダを初期状態に戻すことができる*/ |
jack0325suzu | 0:20fc96400ca3 | 108 | void Ec::reset(){ |
jack0325suzu | 0:20fc96400ca3 | 109 | S=0;stateA=0;stateB=0;count=0;pre_count=0.0,omega=0; |
jack0325suzu | 8:833757a1df66 | 110 | rev=0;now_time=0;old_time=0;RPM=0;RPM_old=0; |
jack0325suzu | 0:20fc96400ca3 | 111 | } |
jack0325suzu | 0:20fc96400ca3 | 112 | /*setTime関数の定義*/ |
jack0325suzu | 0:20fc96400ca3 | 113 | /*自分で好きなように角速度計算の間隔を決めることができる(デフォルトは0.05秒)*/ |
jack0325suzu | 0:20fc96400ca3 | 114 | void Ec::setTime(double t){ |
jack0325suzu | 0:20fc96400ca3 | 115 | dt=t; |
jack0325suzu | 0:20fc96400ca3 | 116 | } |
jack0325suzu | 0:20fc96400ca3 | 117 | |
jack0325suzu | 5:4abba4f54406 | 118 | double Ec::getRPM(){ |
jack0325suzu | 5:4abba4f54406 | 119 | return RPM; |
jack0325suzu | 5:4abba4f54406 | 120 | } |
jack0325suzu | 5:4abba4f54406 | 121 | |
jack0325suzu | 8:833757a1df66 | 122 | int Ec::getRev(){ |
jack0325suzu | 5:4abba4f54406 | 123 | return rev; |
jack0325suzu | 5:4abba4f54406 | 124 | } |
jack0325suzu | 5:4abba4f54406 | 125 | |
jack0325suzu | 10:216d5a573dc7 | 126 | void Ec::changeRPM_th(int th){ |
jack0325suzu | 10:216d5a573dc7 | 127 | RPM_th=th; |
jack0325suzu | 10:216d5a573dc7 | 128 | } |