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.
Diff: main.cpp
- Revision:
- 0:9d2545148cbf
- Child:
- 1:bab46c2f0263
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Sep 26 13:21:13 2018 +0000 @@ -0,0 +1,78 @@ +//------------------------------------------------------------------- +//QEIライブラリ使用 +//------------------------------------------------------------------- + +//mbed用ライブラリ +#include "mbed.h" +//エンコーダ用ライブラリ +#include "QEI.h" + +//プロトタイプ宣言 +void putPWM(float); + +//作業用変数 +PwmOut Servo1(D7); //Pwm出力Servo1の初期化 +PwmOut Servo2(D6); //Pwm出力Servo2の初期化 + +//変数宣言 +//1回転でのパルス数 +#define ROTATE_PER_REVOLUTIONS 360 + +//エンコーダのチャンネルAとBの初期状態を決める +QEI wheel(P1_0, P1_1, NC, ROTATE_PER_REVOLUTIONS, QEI::X4_ENCODING); + +//メイン関数 +int main() +{ + //作業用変数 + long delt_ms = 1; + + //--------------------------------------------------------- + /*デューティー比を変更する場合は以下の数値を変える(-100%≦DUTY≦100%)*/ + float DUTY=50; + + //--------------------------------------------------------- + + float PWM=DUTY/100; + wait_ms(delt_ms); + + while(1) { + + //エンコーダ値格納用変数 + static double y; + //エンコーダの値(生)の取得 + y=(double)wheel.getPulses(); + //エンコーダの値を角度に変換 + y=y*360/(ROTATE_PER_REVOLUTIONS); + //エンコーダ値(角度)の表示(TeraTerm) + //printf("(%-10f)¥0",y); + printf("%f\r\n",y); + + putPWM(PWM); + wait(delt_ms); + + } + +} + + +/*モータの回転方向と速度を与える putPWM関数*/ +void putPWM(float u) +{ + + //回転方向と速度の条件分け + if(u > 0) { + //CCW(動かして確認) + Servo1.write(u); + Servo2.write(0); + } else if(u < 0) { + //CW(動かして確認) + u=abs(u); + Servo1.write(0); + Servo2.write(u); + } else { + Servo1.write(0); + Servo2.write(0); + } + +} \ No newline at end of file