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.
EC.h@0:23ae7be73daf, 2020-03-18 (annotated)
- Committer:
- oshin1030
- Date:
- Wed Mar 18 13:00:13 2020 +0000
- Revision:
- 0:23ae7be73daf
tweaked a little;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| oshin1030 | 0:23ae7be73daf | 1 | #ifndef _INCLUDED_EC_H_ |
| oshin1030 | 0:23ae7be73daf | 2 | #define _INCLUDED_EC_H_ |
| oshin1030 | 0:23ae7be73daf | 3 | |
| oshin1030 | 0:23ae7be73daf | 4 | #ifndef M_PI |
| oshin1030 | 0:23ae7be73daf | 5 | #define M_PI 3.14159265359f |
| oshin1030 | 0:23ae7be73daf | 6 | #endif |
| oshin1030 | 0:23ae7be73daf | 7 | |
| oshin1030 | 0:23ae7be73daf | 8 | /** @section SAMPLE |
| oshin1030 | 0:23ae7be73daf | 9 | * @code |
| oshin1030 | 0:23ae7be73daf | 10 | * //プログラム例 |
| oshin1030 | 0:23ae7be73daf | 11 | * #include "mbed.h" |
| oshin1030 | 0:23ae7be73daf | 12 | * #include "EC.h" |
| oshin1030 | 0:23ae7be73daf | 13 | * |
| oshin1030 | 0:23ae7be73daf | 14 | * #define RESOLUTION 500 |
| oshin1030 | 0:23ae7be73daf | 15 | * Ticker ticker; |
| oshin1030 | 0:23ae7be73daf | 16 | * Serial pc(USBTX,USBRX); |
| oshin1030 | 0:23ae7be73daf | 17 | * |
| oshin1030 | 0:23ae7be73daf | 18 | * // (A層,B層,分解能) |
| oshin1030 | 0:23ae7be73daf | 19 | * Ec1multi EC(p7,p8,RESOLUTION); //1逓倍用class |
| oshin1030 | 0:23ae7be73daf | 20 | * // or |
| oshin1030 | 0:23ae7be73daf | 21 | * //Ec2multi EC(p7,p8,RESOLUTION); //2逓倍用class |
| oshin1030 | 0:23ae7be73daf | 22 | * // or |
| oshin1030 | 0:23ae7be73daf | 23 | * //Ec4multi EC(p7,p8,RESOLUTION); //4逓倍用class |
| oshin1030 | 0:23ae7be73daf | 24 | * void calcOmega(); |
| oshin1030 | 0:23ae7be73daf | 25 | * |
| oshin1030 | 0:23ae7be73daf | 26 | * int main() |
| oshin1030 | 0:23ae7be73daf | 27 | * { |
| oshin1030 | 0:23ae7be73daf | 28 | * int count=0; |
| oshin1030 | 0:23ae7be73daf | 29 | * double omega; |
| oshin1030 | 0:23ae7be73daf | 30 | * ticker.attach(&calcOmega,0.05); |
| oshin1030 | 0:23ae7be73daf | 31 | * |
| oshin1030 | 0:23ae7be73daf | 32 | * while(1) { |
| oshin1030 | 0:23ae7be73daf | 33 | * count=EC.getCount(); |
| oshin1030 | 0:23ae7be73daf | 34 | * omega=EC.getOmega(); |
| oshin1030 | 0:23ae7be73daf | 35 | * pc.printf("count=%d,",count); |
| oshin1030 | 0:23ae7be73daf | 36 | * pc.printf("omega=%f\r\n",omega); |
| oshin1030 | 0:23ae7be73daf | 37 | * } |
| oshin1030 | 0:23ae7be73daf | 38 | * } |
| oshin1030 | 0:23ae7be73daf | 39 | * void calcOmega() |
| oshin1030 | 0:23ae7be73daf | 40 | * { |
| oshin1030 | 0:23ae7be73daf | 41 | * EC.calOmega(); |
| oshin1030 | 0:23ae7be73daf | 42 | * } |
| oshin1030 | 0:23ae7be73daf | 43 | * @endcode |
| oshin1030 | 0:23ae7be73daf | 44 | */ |
| oshin1030 | 0:23ae7be73daf | 45 | |
| oshin1030 | 0:23ae7be73daf | 46 | /** |
| oshin1030 | 0:23ae7be73daf | 47 | * @brief increment型エンコーダ用class |
| oshin1030 | 0:23ae7be73daf | 48 | * @details 1逓倍:Ec1multi |
| oshin1030 | 0:23ae7be73daf | 49 | * 2逓倍:Ec2multi |
| oshin1030 | 0:23ae7be73daf | 50 | * 4逓倍:Ec4multi |
| oshin1030 | 0:23ae7be73daf | 51 | **/ |
| oshin1030 | 0:23ae7be73daf | 52 | class Ec |
| oshin1030 | 0:23ae7be73daf | 53 | { |
| oshin1030 | 0:23ae7be73daf | 54 | protected: |
| oshin1030 | 0:23ae7be73daf | 55 | int count_; //カウント数 |
| oshin1030 | 0:23ae7be73daf | 56 | double omega_; //角速度(rad/s) |
| oshin1030 | 0:23ae7be73daf | 57 | int pre_count_; //一つ前のカウント |
| oshin1030 | 0:23ae7be73daf | 58 | int resolution_; //分解能 |
| oshin1030 | 0:23ae7be73daf | 59 | int multiplication_; |
| oshin1030 | 0:23ae7be73daf | 60 | double ptw_; |
| oshin1030 | 0:23ae7be73daf | 61 | |
| oshin1030 | 0:23ae7be73daf | 62 | public: |
| oshin1030 | 0:23ae7be73daf | 63 | /*コンストラクタの定義 |
| oshin1030 | 0:23ae7be73daf | 64 | 1,2,4逓倍の元となるclass |
| oshin1030 | 0:23ae7be73daf | 65 | |
| oshin1030 | 0:23ae7be73daf | 66 | @param res エンコーダの分解能 |
| oshin1030 | 0:23ae7be73daf | 67 | @param multi エンコーダの逓倍 |
| oshin1030 | 0:23ae7be73daf | 68 | |
| oshin1030 | 0:23ae7be73daf | 69 | @remarks このclassは各逓倍のclassに継承されるため、使用者が宣言する必要はない |
| oshin1030 | 0:23ae7be73daf | 70 | */ |
| oshin1030 | 0:23ae7be73daf | 71 | Ec(int res,int multi); |
| oshin1030 | 0:23ae7be73daf | 72 | /** @details エンコーダのcountを返す関数 |
| oshin1030 | 0:23ae7be73daf | 73 | * 1周のcount=分解能×逓倍 |
| oshin1030 | 0:23ae7be73daf | 74 | * @return count |
| oshin1030 | 0:23ae7be73daf | 75 | */ |
| oshin1030 | 0:23ae7be73daf | 76 | int getCount()const; |
| oshin1030 | 0:23ae7be73daf | 77 | /** |
| oshin1030 | 0:23ae7be73daf | 78 | * 軸の回転角度を返す関数 |
| oshin1030 | 0:23ae7be73daf | 79 | * @return θ[rad] |
| oshin1030 | 0:23ae7be73daf | 80 | */ |
| oshin1030 | 0:23ae7be73daf | 81 | double getRad()const; |
| oshin1030 | 0:23ae7be73daf | 82 | /** |
| oshin1030 | 0:23ae7be73daf | 83 | * 軸の角速度を返す関数 |
| oshin1030 | 0:23ae7be73daf | 84 | * @return ω [rad/s] |
| oshin1030 | 0:23ae7be73daf | 85 | */ |
| oshin1030 | 0:23ae7be73daf | 86 | |
| oshin1030 | 0:23ae7be73daf | 87 | double getOmega()const; |
| oshin1030 | 0:23ae7be73daf | 88 | /** |
| oshin1030 | 0:23ae7be73daf | 89 | * 角速度を計算するための関数 |
| oshin1030 | 0:23ae7be73daf | 90 | * 微分を微小時間の変位として計算しているので、タイマー割込などで回さなければいけない |
| oshin1030 | 0:23ae7be73daf | 91 | */ |
| oshin1030 | 0:23ae7be73daf | 92 | void calOmega(); |
| oshin1030 | 0:23ae7be73daf | 93 | /** |
| oshin1030 | 0:23ae7be73daf | 94 | * 分解能を指定するための関数 |
| oshin1030 | 0:23ae7be73daf | 95 | * @param res エンコーダの分解能 |
| oshin1030 | 0:23ae7be73daf | 96 | */ |
| oshin1030 | 0:23ae7be73daf | 97 | void setResolution(int res); |
| oshin1030 | 0:23ae7be73daf | 98 | ///エンコーダのcountやωをリセットするための関数 |
| oshin1030 | 0:23ae7be73daf | 99 | void reset(); |
| oshin1030 | 0:23ae7be73daf | 100 | Timer timer_; |
| oshin1030 | 0:23ae7be73daf | 101 | }; |
| oshin1030 | 0:23ae7be73daf | 102 | ///@brief increment型エンコーダ用class(1逓倍) |
| oshin1030 | 0:23ae7be73daf | 103 | class Ec1multi : public Ec |
| oshin1030 | 0:23ae7be73daf | 104 | { |
| oshin1030 | 0:23ae7be73daf | 105 | private: |
| oshin1030 | 0:23ae7be73daf | 106 | InterruptIn signalA_; |
| oshin1030 | 0:23ae7be73daf | 107 | DigitalIn signalB_; |
| oshin1030 | 0:23ae7be73daf | 108 | void upA(); |
| oshin1030 | 0:23ae7be73daf | 109 | public: |
| oshin1030 | 0:23ae7be73daf | 110 | /** |
| oshin1030 | 0:23ae7be73daf | 111 | * @brief コンストラクタの定義 |
| oshin1030 | 0:23ae7be73daf | 112 | * @details main関数の前に必ず一度宣言する |
| oshin1030 | 0:23ae7be73daf | 113 | * 使うエンコーダの数だけ設定する必要がある |
| oshin1030 | 0:23ae7be73daf | 114 | |
| oshin1030 | 0:23ae7be73daf | 115 | @ param signalA エンコーダのA相のピン名 |
| oshin1030 | 0:23ae7be73daf | 116 | @ param signalB エンコーダのB相のピン名 |
| oshin1030 | 0:23ae7be73daf | 117 | @ param res エンコーダの分解能 |
| oshin1030 | 0:23ae7be73daf | 118 | @ remark 2,4逓倍も同様 |
| oshin1030 | 0:23ae7be73daf | 119 | */ |
| oshin1030 | 0:23ae7be73daf | 120 | Ec1multi(PinName signalA,PinName signalB,int res); |
| oshin1030 | 0:23ae7be73daf | 121 | }; |
| oshin1030 | 0:23ae7be73daf | 122 | ///@brief increment型エンコーダ用class(2逓倍) |
| oshin1030 | 0:23ae7be73daf | 123 | class Ec2multi : public Ec |
| oshin1030 | 0:23ae7be73daf | 124 | { |
| oshin1030 | 0:23ae7be73daf | 125 | private: |
| oshin1030 | 0:23ae7be73daf | 126 | InterruptIn signalA_; |
| oshin1030 | 0:23ae7be73daf | 127 | DigitalIn signalB_; |
| oshin1030 | 0:23ae7be73daf | 128 | void upA(); |
| oshin1030 | 0:23ae7be73daf | 129 | void downA(); |
| oshin1030 | 0:23ae7be73daf | 130 | public: |
| oshin1030 | 0:23ae7be73daf | 131 | /** |
| oshin1030 | 0:23ae7be73daf | 132 | * @brief コンストラクタの定義 |
| oshin1030 | 0:23ae7be73daf | 133 | * @details main関数の前に必ず一度宣言する |
| oshin1030 | 0:23ae7be73daf | 134 | * 使うエンコーダの数だけ設定する必要がある |
| oshin1030 | 0:23ae7be73daf | 135 | |
| oshin1030 | 0:23ae7be73daf | 136 | @ param signalA エンコーダのA相のピン名 |
| oshin1030 | 0:23ae7be73daf | 137 | @ param signalB エンコーダのB相のピン名 |
| oshin1030 | 0:23ae7be73daf | 138 | @ param res エンコーダの分解能 |
| oshin1030 | 0:23ae7be73daf | 139 | */ |
| oshin1030 | 0:23ae7be73daf | 140 | Ec2multi(PinName signalA,PinName signalB,int res); |
| oshin1030 | 0:23ae7be73daf | 141 | }; |
| oshin1030 | 0:23ae7be73daf | 142 | ///@brief increment型エンコーダ用class(4逓倍) |
| oshin1030 | 0:23ae7be73daf | 143 | class Ec4multi : public Ec |
| oshin1030 | 0:23ae7be73daf | 144 | { |
| oshin1030 | 0:23ae7be73daf | 145 | private: |
| oshin1030 | 0:23ae7be73daf | 146 | InterruptIn signalA_; |
| oshin1030 | 0:23ae7be73daf | 147 | InterruptIn signalB_; |
| oshin1030 | 0:23ae7be73daf | 148 | void upA(); |
| oshin1030 | 0:23ae7be73daf | 149 | void downA(); |
| oshin1030 | 0:23ae7be73daf | 150 | void upB(); |
| oshin1030 | 0:23ae7be73daf | 151 | void downB(); |
| oshin1030 | 0:23ae7be73daf | 152 | int pa_,pb_; |
| oshin1030 | 0:23ae7be73daf | 153 | public: |
| oshin1030 | 0:23ae7be73daf | 154 | /** |
| oshin1030 | 0:23ae7be73daf | 155 | * @brief コンストラクタの定義 |
| oshin1030 | 0:23ae7be73daf | 156 | * @details main関数の前に必ず一度宣言する |
| oshin1030 | 0:23ae7be73daf | 157 | * 使うエンコーダの数だけ設定する必要がある |
| oshin1030 | 0:23ae7be73daf | 158 | |
| oshin1030 | 0:23ae7be73daf | 159 | @ param signalA エンコーダのA相のピン名 |
| oshin1030 | 0:23ae7be73daf | 160 | @ param signalB エンコーダのB相のピン名 |
| oshin1030 | 0:23ae7be73daf | 161 | @ param res エンコーダの分解能 |
| oshin1030 | 0:23ae7be73daf | 162 | */ |
| oshin1030 | 0:23ae7be73daf | 163 | Ec4multi(PinName signalA,PinName signalB,int res); |
| oshin1030 | 0:23ae7be73daf | 164 | }; |
| oshin1030 | 0:23ae7be73daf | 165 | |
| oshin1030 | 0:23ae7be73daf | 166 | |
| oshin1030 | 0:23ae7be73daf | 167 | |
| oshin1030 | 0:23ae7be73daf | 168 | #endif |