ec
Fork of EC by
EC.h@32:297384f9d261, 2018-07-04 (annotated)
- Committer:
- shimizuta
- Date:
- Wed Jul 04 04:21:37 2018 +0000
- Revision:
- 32:297384f9d261
- Parent:
- 31:05e37fea072b
- Child:
- 33:5fe65de5684c
add getDistance_mm()
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jack0325suzu | 0:20fc96400ca3 | 1 | #ifndef __INCLUDED_EC_H_ |
jack0325suzu | 0:20fc96400ca3 | 2 | #define __INCLUDED_EC_H_ |
jack0325suzu | 0:20fc96400ca3 | 3 | |
jack0325suzu | 0:20fc96400ca3 | 4 | #ifndef M_pi |
jack0325suzu | 0:20fc96400ca3 | 5 | #define M_pi 3.141592 |
jack0325suzu | 0:20fc96400ca3 | 6 | #endif |
shimizuta | 32:297384f9d261 | 7 | /**@class Ec |
shimizuta | 32:297384f9d261 | 8 | @brief increment型encoder用class. |
shimizuta | 32:297384f9d261 | 9 | Z相(1周につき1回立ち上がる)の機能あり. |
shimizuta | 32:297384f9d261 | 10 | 普通に使う分には不必要な機能ですが、回転回数が欲しい場合や、回転が非常に早い物の回転速度が欲しい場合などで |
shimizuta | 32:297384f9d261 | 11 | A,B相での処理だとマイコンが追いつかない場合などに使ってください |
shimizuta | 32:297384f9d261 | 12 | */ |
shimizuta | 32:297384f9d261 | 13 | class Ec |
shimizuta | 32:297384f9d261 | 14 | { |
shimizuta | 32:297384f9d261 | 15 | protected: |
shimizuta | 32:297384f9d261 | 16 | int S; //A相B相のパターン(1~4) |
shimizuta | 32:297384f9d261 | 17 | bool stateA,stateB; //A・B相の状態 |
shimizuta | 32:297384f9d261 | 18 | int count; //カウント数 分解能分で一周 |
shimizuta | 32:297384f9d261 | 19 | int pre_count; //一つ前のカウント |
shimizuta | 32:297384f9d261 | 20 | double precount; //4倍精度カウント |
shimizuta | 32:297384f9d261 | 21 | int solution; //分解能 |
shimizuta | 32:297384f9d261 | 22 | double dt; //角速度の計算間隔 |
shimizuta | 32:297384f9d261 | 23 | double distance_mm_;//距離[mm] |
shimizuta | 32:297384f9d261 | 24 | double diameter_mm_;//測定輪半径 |
shimizuta | 32:297384f9d261 | 25 | double count_to_distance_mm_;//カウントを距離に変換する係数 |
shimizuta | 32:297384f9d261 | 26 | double gear_rate_;//ギア比(エンコーダタイムプーリー/測定輪タイムプーリー) |
shimizuta | 32:297384f9d261 | 27 | //z相用 |
shimizuta | 32:297384f9d261 | 28 | bool first; |
shimizuta | 32:297384f9d261 | 29 | int rev; |
shimizuta | 32:297384f9d261 | 30 | double now_time,old_time; |
shimizuta | 32:297384f9d261 | 31 | double RPM,RPM_old; |
shimizuta | 32:297384f9d261 | 32 | int RPM_th; |
shimizuta | 32:297384f9d261 | 33 | |
shimizuta | 32:297384f9d261 | 34 | InterruptIn signalA_; |
shimizuta | 32:297384f9d261 | 35 | InterruptIn signalB_; |
shimizuta | 32:297384f9d261 | 36 | InterruptIn signalZ_; |
jack0325suzu | 8:833757a1df66 | 37 | |
shimizuta | 32:297384f9d261 | 38 | void upA(); |
shimizuta | 32:297384f9d261 | 39 | void downA(); |
shimizuta | 32:297384f9d261 | 40 | void upB(); |
shimizuta | 32:297384f9d261 | 41 | void downB(); |
shimizuta | 32:297384f9d261 | 42 | void upZ(); |
shimizuta | 32:297384f9d261 | 43 | void downZ(); |
shimizuta | 32:297384f9d261 | 44 | void setCountToDistance_mm();//countからdistanceに変換する係数を計算する。 |
shimizuta | 32:297384f9d261 | 45 | public: |
shimizuta | 32:297384f9d261 | 46 | double omega; //角速度 |
shimizuta | 32:297384f9d261 | 47 | /** コンストラクタの定義 |
shimizuta | 32:297384f9d261 | 48 | * |
shimizuta | 32:297384f9d261 | 49 | * ***Z相の機能を追加したことで引数が増えました!!!!*** |
shimizuta | 32:297384f9d261 | 50 | * |
shimizuta | 32:297384f9d261 | 51 | * main関数の前に必ず一度宣言する |
shimizuta | 32:297384f9d261 | 52 | * |
shimizuta | 32:297384f9d261 | 53 | * 使うエンコーダの数だけ設定する必要がある |
shimizuta | 32:297384f9d261 | 54 | * |
shimizuta | 32:297384f9d261 | 55 | * @param signalA エンコーダA相のピン名 |
shimizuta | 32:297384f9d261 | 56 | * @param signalB エンコーダB相のピン名 |
shimizuta | 32:297384f9d261 | 57 | * @param signalZ エンコーダZ相のピン名 |
shimizuta | 32:297384f9d261 | 58 | * @param s エンコーダの分解能(省略可) |
shimizuta | 32:297384f9d261 | 59 | * @param t 角速度計算の間隔(省略可) |
shimizuta | 32:297384f9d261 | 60 | */ |
shimizuta | 32:297384f9d261 | 61 | /** @section CAUTION |
shimizuta | 32:297384f9d261 | 62 | * 今まで以下のように定義していたものは |
shimizuta | 32:297384f9d261 | 63 | * @code |
shimizuta | 32:297384f9d261 | 64 | * #include "mbed.h" |
shimizuta | 32:297384f9d261 | 65 | * #include "EC.h" |
shimizuta | 32:297384f9d261 | 66 | * |
shimizuta | 32:297384f9d261 | 67 | * Ec Ec1(PA_0,PA_1,1024,0.05); |
shimizuta | 32:297384f9d261 | 68 | * @endcode |
shimizuta | 32:297384f9d261 | 69 | * 次のようにZ相の引数の部分に、NCと入れれば今までの様に使える |
shimizuta | 32:297384f9d261 | 70 | * @code |
shimizuta | 32:297384f9d261 | 71 | * #include "mbed.h" |
shimizuta | 32:297384f9d261 | 72 | * #include "EC.h" |
shimizuta | 32:297384f9d261 | 73 | * |
shimizuta | 32:297384f9d261 | 74 | * Ec Ec1(PA_0,PA_1,NC,1024,0.05); |
shimizuta | 32:297384f9d261 | 75 | * @endcode |
shimizuta | 32:297384f9d261 | 76 | */ |
shimizuta | 32:297384f9d261 | 77 | Ec(PinName signalA,PinName signalB,PinName signalZ,int s,double t); |
shimizuta | 32:297384f9d261 | 78 | |
shimizuta | 32:297384f9d261 | 79 | ///countの値を返す関数(int型) |
shimizuta | 32:297384f9d261 | 80 | int getCount(); |
jack0325suzu | 0:20fc96400ca3 | 81 | |
shimizuta | 32:297384f9d261 | 82 | /** omega(角速度 rad/s)の値を計算する関数 |
shimizuta | 32:297384f9d261 | 83 | * @section CAUTION |
shimizuta | 32:297384f9d261 | 84 | * CalOmega関数は、一定時間ごと(dtごと)に計算される必要があるので、main関数内でタイマー割り込みを設定する必要がある。 |
shimizuta | 32:297384f9d261 | 85 | * @code |
shimizuta | 32:297384f9d261 | 86 | * #include "mbed.h" |
shimizuta | 32:297384f9d261 | 87 | * #include "EC.h" //ライブラリをインクルード |
shimizuta | 32:297384f9d261 | 88 | * |
shimizuta | 32:297384f9d261 | 89 | * Ec Ec1(PA_0,PA_1,NC,1024,0.05); //分解能1024、計算間隔0.05秒に設定、Z相は使わない |
shimizuta | 32:297384f9d261 | 90 | * Ticker ticker; |
shimizuta | 32:297384f9d261 | 91 | * DigitalIn button(USER_BUTTON); |
shimizuta | 32:297384f9d261 | 92 | * Serial pc(USBTX,USBRX); |
shimizuta | 32:297384f9d261 | 93 | * |
shimizuta | 32:297384f9d261 | 94 | * void calOmega(){ |
shimizuta | 32:297384f9d261 | 95 | * Ec1.CalOmega(); |
shimizuta | 32:297384f9d261 | 96 | * } |
shimizuta | 32:297384f9d261 | 97 | * |
shimizuta | 32:297384f9d261 | 98 | * int main(){ |
shimizuta | 32:297384f9d261 | 99 | * ticker.attach(&calOmega,0.05); //0.05秒間隔で角速度を計算 |
shimizuta | 32:297384f9d261 | 100 | * while(1){ |
shimizuta | 32:297384f9d261 | 101 | * pc.printf(" count1=%d ",Ec1.getCount()); |
shimizuta | 32:297384f9d261 | 102 | * pc.printf(" omega1=%f\r\n ",Ec1.getOmega()); |
shimizuta | 32:297384f9d261 | 103 | * if(pc.readable()) { |
shimizuta | 32:297384f9d261 | 104 | * char sel=pc.getc(); |
shimizuta | 32:297384f9d261 | 105 | * if(sel=='r') Ec1.reset(); //rを押したらリセット |
shimizuta | 32:297384f9d261 | 106 | * } |
shimizuta | 32:297384f9d261 | 107 | * } |
shimizuta | 32:297384f9d261 | 108 | * } |
shimizuta | 32:297384f9d261 | 109 | * @endcode |
shimizuta | 32:297384f9d261 | 110 | */ |
shimizuta | 32:297384f9d261 | 111 | void CalOmega(); |
jack0325suzu | 8:833757a1df66 | 112 | |
shimizuta | 32:297384f9d261 | 113 | ///omega(角速度 rad/s)の値を返す関数(double型) |
shimizuta | 32:297384f9d261 | 114 | double getOmega(); |
shimizuta | 32:297384f9d261 | 115 | ///四倍精度のcountの値を返す関数(double型) |
shimizuta | 32:297384f9d261 | 116 | double getPreCount(); |
shimizuta | 32:297384f9d261 | 117 | ///エンコーダを初期状態に戻す関数 countやomegaの値を0にする |
shimizuta | 32:297384f9d261 | 118 | virtual void reset(); |
shimizuta | 32:297384f9d261 | 119 | ///角速度計算の間隔dtを決めることができる(デフォルトは0.05秒) |
shimizuta | 32:297384f9d261 | 120 | void setTime(double t); |
shimizuta | 32:297384f9d261 | 121 | ///(Z相を使用する場合)回転回数を返す関数(int型) |
shimizuta | 32:297384f9d261 | 122 | int getRev(); |
shimizuta | 32:297384f9d261 | 123 | |
shimizuta | 32:297384f9d261 | 124 | ///(Z相を使用する場合)回転速度(rpm)の値を返す関数(double型) |
shimizuta | 32:297384f9d261 | 125 | /** @section SAMPLE |
shimizuta | 32:297384f9d261 | 126 | * z相を使う場合のプログラムの例 |
shimizuta | 32:297384f9d261 | 127 | * @code |
shimizuta | 32:297384f9d261 | 128 | * #include "mbed.h" |
shimizuta | 32:297384f9d261 | 129 | * #include "EC.h" //ライブラリをインクルード |
shimizuta | 32:297384f9d261 | 130 | * |
shimizuta | 32:297384f9d261 | 131 | * Ec Ec1(NC,NC,PA_0,1024,0.05); //A相B相が不必要な場合も、このようにNCと入れればよい |
shimizuta | 32:297384f9d261 | 132 | * DigitalIn button(USER_BUTTON); |
shimizuta | 32:297384f9d261 | 133 | * Serial pc(USBTX,USBRX); |
shimizuta | 32:297384f9d261 | 134 | * |
shimizuta | 32:297384f9d261 | 135 | * int main(){ |
shimizuta | 32:297384f9d261 | 136 | * while(1){ |
shimizuta | 32:297384f9d261 | 137 | * pc.printf(" rev1=%d ",Ec1.getRev()); |
shimizuta | 32:297384f9d261 | 138 | * pc.printf(" rpm1=%f\r\n ",Ec1.getRPM()); |
shimizuta | 32:297384f9d261 | 139 | * if(!button) Ec1.reset(); //USERボタンを押したらリセット |
shimizuta | 32:297384f9d261 | 140 | * } |
shimizuta | 32:297384f9d261 | 141 | * } |
shimizuta | 32:297384f9d261 | 142 | * @endcode |
shimizuta | 32:297384f9d261 | 143 | */ |
shimizuta | 32:297384f9d261 | 144 | double getRPM(); |
shimizuta | 32:297384f9d261 | 145 | void changeRPM_th(int th); |
shimizuta | 32:297384f9d261 | 146 | |
shimizuta | 32:297384f9d261 | 147 | /** @caution 先にsetDiameter_mmをしないと正しい値とならない |
shimizuta | 32:297384f9d261 | 148 | @return 現在距離. |
shimizuta | 32:297384f9d261 | 149 | */ |
shimizuta | 32:297384f9d261 | 150 | double getDistance_mm(); |
shimizuta | 32:297384f9d261 | 151 | |
shimizuta | 32:297384f9d261 | 152 | /** @param diameter_mm 測定輪半径 |
shimizuta | 32:297384f9d261 | 153 | */ |
shimizuta | 32:297384f9d261 | 154 | void setDiameter_mm(double diameter_mm); |
shimizuta | 32:297384f9d261 | 155 | /** @param gear比(エンコーダタイムプーリー/測定輪タイムプーリー) |
shimizuta | 32:297384f9d261 | 156 | */ |
shimizuta | 32:297384f9d261 | 157 | void setGearRate(double gear_rate); |
shimizuta | 32:297384f9d261 | 158 | static double deftime;//角速度計算の間隔 |
shimizuta | 32:297384f9d261 | 159 | static int defsolution;//エンコーダの分解能のデフォルト値 |
shimizuta | 32:297384f9d261 | 160 | Timer timer; |
jack0325suzu | 0:20fc96400ca3 | 161 | }; |
jack0325suzu | 0:20fc96400ca3 | 162 | |
jack0325suzu | 29:b8de333facd4 | 163 | |
jack0325suzu | 0:20fc96400ca3 | 164 | #endif |