ec
Fork of EC by
EC.cpp@32:297384f9d261, 2018-07-04 (annotated)
- Committer:
- shimizuta
- Date:
- Wed Jul 04 04:21:37 2018 +0000
- Revision:
- 32:297384f9d261
- Parent:
- 26:45a53e3c81b1
- Child:
- 34:b34dc495b3c8
add getDistance_mm()
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 | //ピン変化割り込み関数の定義 |
shimizuta | 32:297384f9d261 | 7 | void Ec::upA() |
shimizuta | 32:297384f9d261 | 8 | { |
jack0325suzu | 0:20fc96400ca3 | 9 | stateA=1; |
jack0325suzu | 0:20fc96400ca3 | 10 | if(stateB==0&&S==0) { |
jack0325suzu | 0:20fc96400ca3 | 11 | S=1; |
jack0325suzu | 0:20fc96400ca3 | 12 | } else if(stateB==1&&S==3) { |
jack0325suzu | 0:20fc96400ca3 | 13 | S=2; |
jack0325suzu | 0:20fc96400ca3 | 14 | } |
jack0325suzu | 0:20fc96400ca3 | 15 | } |
shimizuta | 32:297384f9d261 | 16 | void Ec::downA() |
shimizuta | 32:297384f9d261 | 17 | { |
jack0325suzu | 0:20fc96400ca3 | 18 | stateA=0; |
jack0325suzu | 0:20fc96400ca3 | 19 | if(stateB==1&&S==2) { |
jack0325suzu | 0:20fc96400ca3 | 20 | S=3; |
jack0325suzu | 0:20fc96400ca3 | 21 | } else if(stateB==0&&S==1) { |
jack0325suzu | 0:20fc96400ca3 | 22 | S=0; |
jack0325suzu | 0:20fc96400ca3 | 23 | count--; |
jack0325suzu | 0:20fc96400ca3 | 24 | } |
jack0325suzu | 0:20fc96400ca3 | 25 | } |
shimizuta | 32:297384f9d261 | 26 | void Ec::upB() |
shimizuta | 32:297384f9d261 | 27 | { |
jack0325suzu | 0:20fc96400ca3 | 28 | stateB=1; |
jack0325suzu | 0:20fc96400ca3 | 29 | if(stateA==1&&S==1) { |
jack0325suzu | 0:20fc96400ca3 | 30 | S=2; |
jack0325suzu | 0:20fc96400ca3 | 31 | } else if(stateA==0&&S==0) { |
jack0325suzu | 0:20fc96400ca3 | 32 | S=3; |
jack0325suzu | 0:20fc96400ca3 | 33 | } |
jack0325suzu | 0:20fc96400ca3 | 34 | } |
shimizuta | 32:297384f9d261 | 35 | void Ec::downB() |
shimizuta | 32:297384f9d261 | 36 | { |
jack0325suzu | 0:20fc96400ca3 | 37 | stateB=0; |
jack0325suzu | 0:20fc96400ca3 | 38 | if(stateA==0&&S==3) { |
jack0325suzu | 0:20fc96400ca3 | 39 | count++; |
jack0325suzu | 0:20fc96400ca3 | 40 | S=0; |
jack0325suzu | 0:20fc96400ca3 | 41 | } else if(stateA==1&&S==2) { |
jack0325suzu | 0:20fc96400ca3 | 42 | S=1; |
jack0325suzu | 0:20fc96400ca3 | 43 | } |
jack0325suzu | 0:20fc96400ca3 | 44 | } |
jack0325suzu | 0:20fc96400ca3 | 45 | |
shimizuta | 32:297384f9d261 | 46 | void Ec::upZ() |
shimizuta | 32:297384f9d261 | 47 | { |
shimizuta | 32:297384f9d261 | 48 | if(first==false) { |
ShotaroSenzaki | 11:b96009f8b9fd | 49 | static int res_count; |
jack0325suzu | 5:4abba4f54406 | 50 | rev++; |
jack0325suzu | 5:4abba4f54406 | 51 | first=true; |
jack0325suzu | 5:4abba4f54406 | 52 | now_time=timer.read(); |
jack0325suzu | 5:4abba4f54406 | 53 | RPM=60/(now_time-old_time); |
shimizuta | 32:297384f9d261 | 54 | if((RPM_old-RPM)>RPM_th) { |
shimizuta | 32:297384f9d261 | 55 | if(res_count >= 5) { |
ShotaroSenzaki | 11:b96009f8b9fd | 56 | printf("\r\n CAUTION : speed downed drastically\r\n"); |
ShotaroSenzaki | 11:b96009f8b9fd | 57 | NVIC_SystemReset(); |
ShotaroSenzaki | 11:b96009f8b9fd | 58 | res_count=0; |
shimizuta | 32:297384f9d261 | 59 | } else { |
ShotaroSenzaki | 11:b96009f8b9fd | 60 | res_count++; |
ShotaroSenzaki | 11:b96009f8b9fd | 61 | } |
shimizuta | 32:297384f9d261 | 62 | } else { |
ShotaroSenzaki | 11:b96009f8b9fd | 63 | res_count=0; |
jack0325suzu | 5:4abba4f54406 | 64 | } |
jack0325suzu | 5:4abba4f54406 | 65 | RPM_old=RPM; |
jack0325suzu | 5:4abba4f54406 | 66 | old_time=timer.read(); |
shimizuta | 32:297384f9d261 | 67 | } |
jack0325suzu | 5:4abba4f54406 | 68 | } |
jack0325suzu | 5:4abba4f54406 | 69 | |
shimizuta | 32:297384f9d261 | 70 | void Ec::downZ() |
shimizuta | 32:297384f9d261 | 71 | { |
jack0325suzu | 5:4abba4f54406 | 72 | first=false; |
jack0325suzu | 5:4abba4f54406 | 73 | } |
jack0325suzu | 5:4abba4f54406 | 74 | |
jack0325suzu | 5:4abba4f54406 | 75 | |
jack0325suzu | 0:20fc96400ca3 | 76 | //コンストラクタの定義 |
jack0325suzu | 0:20fc96400ca3 | 77 | //main関数の前に必ず一度宣言する |
jack0325suzu | 0:20fc96400ca3 | 78 | //第一・第二引数はエンコーダのA・B相のピン名 |
jack0325suzu | 0:20fc96400ca3 | 79 | //第三院数はエンコーダの分解能 |
jack0325suzu | 0:20fc96400ca3 | 80 | |
jack0325suzu | 5:4abba4f54406 | 81 | Ec::Ec(PinName signalA , PinName signalB , PinName signalZ , int s=defsolution , double t=deftime) : signalA_(signalA),signalB_(signalB),signalZ_(signalZ) |
jack0325suzu | 0:20fc96400ca3 | 82 | { |
shimizuta | 32:297384f9d261 | 83 | |
shimizuta | 32:297384f9d261 | 84 | if((signalA!=NC)&&(signalB!=NC)) { |
shimizuta | 32:297384f9d261 | 85 | S=0; |
shimizuta | 32:297384f9d261 | 86 | stateA=0; |
shimizuta | 32:297384f9d261 | 87 | stateB=0; |
shimizuta | 32:297384f9d261 | 88 | count=0; |
shimizuta | 32:297384f9d261 | 89 | pre_count=0.0; |
jack0325suzu | 26:45a53e3c81b1 | 90 | timer.start(); |
shimizuta | 32:297384f9d261 | 91 | signalA_.rise(callback(this,&Ec::upA)); |
shimizuta | 32:297384f9d261 | 92 | signalA_.fall(callback(this,&Ec::downA)); |
shimizuta | 32:297384f9d261 | 93 | signalB_.rise(callback(this,&Ec::upB)); |
shimizuta | 32:297384f9d261 | 94 | signalB_.fall(callback(this,&Ec::downB)); |
jack0325suzu | 5:4abba4f54406 | 95 | } |
shimizuta | 32:297384f9d261 | 96 | if(signalZ!=NC) { |
shimizuta | 32:297384f9d261 | 97 | first=false; |
shimizuta | 32:297384f9d261 | 98 | rev=0; |
shimizuta | 32:297384f9d261 | 99 | old_time=0; |
shimizuta | 32:297384f9d261 | 100 | RPM=0; |
shimizuta | 32:297384f9d261 | 101 | RPM_old=0; |
shimizuta | 32:297384f9d261 | 102 | signalZ_.rise(callback(this,&Ec::upZ)); |
shimizuta | 32:297384f9d261 | 103 | signalZ_.fall(callback(this,&Ec::downZ)); |
jack0325suzu | 5:4abba4f54406 | 104 | } |
jack0325suzu | 0:20fc96400ca3 | 105 | dt=t; |
jack0325suzu | 0:20fc96400ca3 | 106 | solution=s; |
jack0325suzu | 0:20fc96400ca3 | 107 | defsolution=s; |
jack0325suzu | 10:216d5a573dc7 | 108 | RPM_th=250; |
shimizuta | 32:297384f9d261 | 109 | count_to_distance_mm_ = 0; |
shimizuta | 32:297384f9d261 | 110 | gear_rate_ = 1; |
jack0325suzu | 0:20fc96400ca3 | 111 | } |
jack0325suzu | 0:20fc96400ca3 | 112 | |
shimizuta | 32:297384f9d261 | 113 | int Ec::getCount() |
shimizuta | 32:297384f9d261 | 114 | { |
jack0325suzu | 0:20fc96400ca3 | 115 | return count; |
jack0325suzu | 0:20fc96400ca3 | 116 | } |
jack0325suzu | 0:20fc96400ca3 | 117 | |
shimizuta | 32:297384f9d261 | 118 | void Ec::CalOmega() |
shimizuta | 32:297384f9d261 | 119 | { |
jack0325suzu | 0:20fc96400ca3 | 120 | omega=(count-pre_count)*2*M_pi/(solution*dt); |
jack0325suzu | 0:20fc96400ca3 | 121 | pre_count=count; |
jack0325suzu | 0:20fc96400ca3 | 122 | } |
jack0325suzu | 0:20fc96400ca3 | 123 | |
shimizuta | 32:297384f9d261 | 124 | double Ec::getOmega() |
shimizuta | 32:297384f9d261 | 125 | { |
jack0325suzu | 0:20fc96400ca3 | 126 | return omega; |
jack0325suzu | 0:20fc96400ca3 | 127 | } |
jack0325suzu | 0:20fc96400ca3 | 128 | |
shimizuta | 32:297384f9d261 | 129 | double Ec::getPreCount() |
shimizuta | 32:297384f9d261 | 130 | { |
jack0325suzu | 0:20fc96400ca3 | 131 | precount=count+S/4.0; |
jack0325suzu | 0:20fc96400ca3 | 132 | return precount; |
jack0325suzu | 0:20fc96400ca3 | 133 | } |
jack0325suzu | 0:20fc96400ca3 | 134 | /*reset関数の定義*/ |
jack0325suzu | 0:20fc96400ca3 | 135 | /*エンコーダを初期状態に戻すことができる*/ |
shimizuta | 32:297384f9d261 | 136 | void Ec::reset() |
shimizuta | 32:297384f9d261 | 137 | { |
shimizuta | 32:297384f9d261 | 138 | S=0; |
shimizuta | 32:297384f9d261 | 139 | stateA=0; |
shimizuta | 32:297384f9d261 | 140 | stateB=0; |
shimizuta | 32:297384f9d261 | 141 | count=0; |
shimizuta | 32:297384f9d261 | 142 | pre_count=0.0,omega=0; |
shimizuta | 32:297384f9d261 | 143 | rev=0; |
shimizuta | 32:297384f9d261 | 144 | now_time=0; |
shimizuta | 32:297384f9d261 | 145 | old_time=0; |
shimizuta | 32:297384f9d261 | 146 | RPM=0; |
shimizuta | 32:297384f9d261 | 147 | RPM_old=0; |
jack0325suzu | 0:20fc96400ca3 | 148 | } |
jack0325suzu | 0:20fc96400ca3 | 149 | /*setTime関数の定義*/ |
jack0325suzu | 0:20fc96400ca3 | 150 | /*自分で好きなように角速度計算の間隔を決めることができる(デフォルトは0.05秒)*/ |
shimizuta | 32:297384f9d261 | 151 | void Ec::setTime(double t) |
shimizuta | 32:297384f9d261 | 152 | { |
jack0325suzu | 0:20fc96400ca3 | 153 | dt=t; |
jack0325suzu | 0:20fc96400ca3 | 154 | } |
jack0325suzu | 0:20fc96400ca3 | 155 | |
shimizuta | 32:297384f9d261 | 156 | double Ec::getRPM() |
shimizuta | 32:297384f9d261 | 157 | { |
jack0325suzu | 5:4abba4f54406 | 158 | return RPM; |
jack0325suzu | 5:4abba4f54406 | 159 | } |
jack0325suzu | 5:4abba4f54406 | 160 | |
shimizuta | 32:297384f9d261 | 161 | int Ec::getRev() |
shimizuta | 32:297384f9d261 | 162 | { |
jack0325suzu | 5:4abba4f54406 | 163 | return rev; |
jack0325suzu | 5:4abba4f54406 | 164 | } |
jack0325suzu | 5:4abba4f54406 | 165 | |
shimizuta | 32:297384f9d261 | 166 | void Ec::changeRPM_th(int th) |
shimizuta | 32:297384f9d261 | 167 | { |
jack0325suzu | 10:216d5a573dc7 | 168 | RPM_th=th; |
shimizuta | 32:297384f9d261 | 169 | } |
shimizuta | 32:297384f9d261 | 170 | |
shimizuta | 32:297384f9d261 | 171 | double Ec::getDistance_mm() |
shimizuta | 32:297384f9d261 | 172 | { |
shimizuta | 32:297384f9d261 | 173 | distance_mm_= count * count_to_distance_mm_; |
shimizuta | 32:297384f9d261 | 174 | return distance_mm_; |
shimizuta | 32:297384f9d261 | 175 | } |
shimizuta | 32:297384f9d261 | 176 | void Ec::setDiameter_mm(double diameter_mm) |
shimizuta | 32:297384f9d261 | 177 | { |
shimizuta | 32:297384f9d261 | 178 | diameter_mm_ = diameter_mm; |
shimizuta | 32:297384f9d261 | 179 | setCountToDistance_mm(); |
shimizuta | 32:297384f9d261 | 180 | } |
shimizuta | 32:297384f9d261 | 181 | void Ec::setGearRate(double gear_rate){ |
shimizuta | 32:297384f9d261 | 182 | gear_rate_ = gear_rate; |
shimizuta | 32:297384f9d261 | 183 | setCountToDistance_mm(); |
shimizuta | 32:297384f9d261 | 184 | } |
shimizuta | 32:297384f9d261 | 185 | void Ec::setCountToDistance_mm(){ |
shimizuta | 32:297384f9d261 | 186 | count_to_distance_mm_ = 1 / (double)solution * M_pi * diameter_mm_ * gear_rate_;//カウント分解能*2Pi=角度(rad)。角度*直径/2 距離を整理した式 |
jack0325suzu | 10:216d5a573dc7 | 187 | } |