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: harurobo1006 harurobo_1026
Fork of EC by
Diff: EC.h
- Revision:
- 29:b8de333facd4
- Parent:
- 28:172a316ab863
- Child:
- 30:087ad7703445
--- a/EC.h Thu Aug 10 05:28:47 2017 +0000
+++ b/EC.h Tue Aug 15 07:26:28 2017 +0000
@@ -149,47 +149,135 @@
};
-///速度制御用class
-///Ec classを継承している
-/** @section Example
- * @code
- * #include "mbed.h"
- * #include "EC.h" //ヘッダファイルをインクルード
- *
- * SpeedControl motor1(PA_0,PA_1,NC,1024,0.05,PB_8,PB_9); //エンコーダ分解能1024、角速度計算間隔を0.05秒に設定
- * Ticker ticker; //角速度計算用ticker
- * DigitalIn button(USER_BUTTON);
- * Serial pc(USBTX,USBRX);
- *
- * void calOmega() //角速度計算関数
- * {
- * motor1.CalOmega();
- * }
- *
- * int main()
- * {
- * ticker.attach(&calOmega,0.05); //0.05秒間隔で角速度を計算
- * motor1.setPDparam(0.01,0); //PDパラメータを設定
- * motor1.setDOconstant(46.3); //duty比と角速度の変換係数を46.3に設定
- *
- * int kai=0;
- *
- * while(1) {
- * motor1.Sc(-10); //角速度10rad/sで逆回転
- * if(kai>=500){ //ループ500回ごとに角速度・出力duty比を表示
- * pc.printf("omega=%f duty=%f \r\n",motor1.getOmega(),motor1.duty);
- * kai=0;
- * }
- * if(pc.readable()) {
- * char sel=pc.getc();
- * if(sel=='q') break; //qを押したらリセット
- * }
- * kai++;
- * }
- * motor1.stop();
- * }
- * @endcode
- */
+
+/*!
+ * 速度制御用ライブラリ
+ *
+ * @section 基本的な速度制御の実用までの流れ
+ *
+ * 1. モーター・エンコーダ・モータードライバ・マイコン・バッテリーを適切につなげる
+ *
+ * 注意!!! リポ用のスイッチと電圧計を必ずつけること!!!
+ *
+ *
+ *
+ * 2. 実際にモーターを回転させてみて不備なく動作するか確認する
+ *
+ * 確認事項
+ *
+ * ・モーターが回るか、duty比によって加速・減速するか
+ *
+ * ・エンコーダの値が取れているか
+ *
+ * ・モーターの回転方向とエンコーダの加算減算が対応しているか (プログラム内で定義した正・負回転とエンコーダの増減が対応しているかということ)
+ *
+ * などなど。コードは下の一つ目のものを使えばいい
+ *
+ * 3. Cの値を見つける
+ *
+ * このライブラリでは、「モーターにかかる電圧(duty比)と回転速度はある程度比例の関係にあるだろう」という考え方をしている(もちろん実際のモデルはもっと複雑である)。
+ *
+ * このライブラリにおける速度制御のアルゴリズムとしては、あらかじめ求めたこの比例係数を用いて、目標角速度からだいたいのduty比を算出し、そこからpd制御をかけるという形をとっている。
+ *
+ * Cとはこの比例係数のことである。一応式としてあらわすと、
+ *
+ * (回転速度)=C×(duty比)
+ *
+ * となる。このCの値はモーターやギアユニットによって固有の値であり、それぞれのモーターにおいて求める必要がある。
+ *
+ * 具体的なこの係数の求め方は、duty比を0.05くらいずつで変化させ、その時のduty比と回転角速度を記録し、excelからグラフの傾きを求めればよい。
+ *
+ * @image html https://www.fastpic.jp/images.php?file=2416798781.gif
+ *
+ * コードは下の一つ目のものを使えばいい
+ *
+ * @code
+ * //2,3で使えるサンプルコード
+ * //入力はTera Termで行う
+ * #include "mbed.h"
+ * #include "EC.h"
+ *
+ * SpeedControl motor(PF_0,PB_1,NC,1048,0.05,PA_8,PB_5);
+ * Serial pc(USBTX,USBRX);
+ * Ticker motor_tick;
+ *
+ * void calOmega(){
+ * motor.CalOmega(); //角速度計算用
+ * }
+ *
+ * int main(void){
+ * motor_tick.attach(calOmega,0.05);
+ * int loop_time=0;
+ * float in_duty=0;
+ * bool print=false;
+ *
+ * while(1){
+ * loop_time++;
+ *
+ * if(in_duty>0) motor.turnF(in_duty); //in_dutyが正のとき正回転(このプログラムではPA_8で駆動)
+ * else motor.turnB((-1)*in_duty); //in_dutyが負のとき逆回転(このプログラムではPB_5で駆動)
+ *
+ *
+ * if(pc.readable()){
+ * char sel=pc.getc();
+ * if(sel=='s'){
+ * motor.stop();
+ * } else if(sel=='f'){
+ * in_duty+=(float)0.05; //fを押すとduty比がo.o5ずつあがる
+ * pc.printf("duty=%f\r\n",in_duty);
+ * } else if(sel=='b'){
+ * in_duty-=(float)0.05; //bを押すとduty比がo.o5ずつさがる
+ * pc.printf("duty=%f\r\n",in_duty);
+ * } else if(sel=='p'){
+ * print=!print; //pを押すと表示を停止/再開する
+ * }
+ * }
+ *
+ * if(loop_time%1000==0){
+ * if(print) pc.printf("count=%d omega=%f dutyF=%f dutyB=%f\r\n",motor.getCount(),motor.getOmega(),(double)motor.pwm_F_,(double)motor.pwm_B_);
+ * } //左からエンコーダのカウント、角速度、正方向出力duty比、負方向出力duty比を表示
+ * }
+ * }
+ * @endcode
+ *
+ * @code
+ * #include "mbed.h"
+ * #include "EC.h" //ヘッダファイルをインクルード
+ *
+ * SpeedControl motor1(PA_0,PA_1,NC,1024,0.05,PB_8,PB_9); //エンコーダ分解能1024、角速度計算間隔を0.05秒に設定
+ * Ticker ticker; //角速度計算用ticker
+ * DigitalIn button(USER_BUTTON);
+ * Serial pc(USBTX,USBRX);
+ *
+ * void calOmega() //角速度計算関数
+ * {
+ * motor1.CalOmega();
+ * }
+ *
+ * int main()
+ * {
+ * ticker.attach(&calOmega,0.05); //0.05秒間隔で角速度を計算
+ * motor1.setPDparam(0.01,0); //PDパラメータを設定
+ * motor1.setDOconstant(46.3); //duty比と角速度の変換係数を46.3に設定
+ *
+ * int kai=0;
+ *
+ * while(1) {
+ * motor1.Sc(-10); //角速度10rad/sで逆回転
+ * if(kai>=500){ //ループ500回ごとに角速度・出力duty比を表示
+ * pc.printf("omega=%f duty=%f \r\n",motor1.getOmega(),motor1.duty);
+ * kai=0;
+ * }
+ * if(pc.readable()) {
+ * char sel=pc.getc();
+ * if(sel=='q') break; //qを押したらリセット
+ * }
+ * kai++;
+ * }
+ * motor1.stop();
+ * }
+ * @endcode
+ */
class SpeedControl : public Ec{
