配列を使用し記憶走行を行う

Dependencies:   mbed AQM0802 CRotaryEncoder TB6612FNG

Committer:
yusaku0125
Date:
Mon Aug 26 06:31:19 2019 +0000
Revision:
3:e455433c8cae
Parent:
2:a28ca7c25ed1
Child:
4:ac9e6772ddb3
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yusaku0125 0:df99e50ed3fd 1 //エンコーダの動作確認。
yusaku0125 3:e455433c8cae 2 //左右モータの回転速度の計測プログラム
yusaku0125 3:e455433c8cae 3 #include "mbed.h"
yusaku0125 3:e455433c8cae 4 #include "CRotaryEncoder.h"
yusaku0125 0:df99e50ed3fd 5
yusaku0125 3:e455433c8cae 6 #define PULSE_TO_UM 30 //エンコーダ1パルス当たりのタイヤ移動距離[um]
yusaku0125 3:e455433c8cae 7 //実測で値を調整する。
yusaku0125 3:e455433c8cae 8 #define INTERRUPT_TIME 10000 //割りこみ周期[us]
yusaku0125 3:e455433c8cae 9
yusaku0125 3:e455433c8cae 10 Serial PC(USBTX,USBRX);
yusaku0125 3:e455433c8cae 11 CRotaryEncoder encoder_a(D1,D0); //モータAのエンコーダ
yusaku0125 3:e455433c8cae 12 CRotaryEncoder encoder_b(D11,D12); //モータBのエンコーダ
yusaku0125 3:e455433c8cae 13 Ticker timer; //タイマ割込み用
yusaku0125 3:e455433c8cae 14 int enc_count_a=0,enc_count_b=0; //エンコーダパルス数を格納
yusaku0125 3:e455433c8cae 15 int distance_a=0,distance_b=0; //タイヤ移動距離を格納[mm]
yusaku0125 3:e455433c8cae 16 int speed_a=0, speed_b=0;
yusaku0125 3:e455433c8cae 17
yusaku0125 3:e455433c8cae 18 void timer_interrupt(){
yusaku0125 3:e455433c8cae 19 enc_count_a=encoder_a.Get(); //エンコーダパルス数を取得
yusaku0125 3:e455433c8cae 20 enc_count_b=-encoder_b.Get();
yusaku0125 3:e455433c8cae 21 distance_a=(enc_count_a*PULSE_TO_UM); //移動距離をmm単位で格納
yusaku0125 3:e455433c8cae 22 distance_b=(enc_count_b*PULSE_TO_UM);
yusaku0125 3:e455433c8cae 23 speed_a=(distance_a*1000)/INTERRUPT_TIME;//走行速度演算[mm/s]
yusaku0125 3:e455433c8cae 24 speed_b=(distance_b*1000)/INTERRUPT_TIME;
yusaku0125 3:e455433c8cae 25 distance_a=0;
yusaku0125 3:e455433c8cae 26 distance_b=0;
yusaku0125 3:e455433c8cae 27 encoder_a.Set(0);
yusaku0125 3:e455433c8cae 28 encoder_b.Set(0);
yusaku0125 3:e455433c8cae 29 }
yusaku0125 3:e455433c8cae 30
yusaku0125 3:e455433c8cae 31 int main() {
yusaku0125 3:e455433c8cae 32 timer.attach_us(&timer_interrupt,INTERRUPT_TIME);//タイマ割り込みスタート
yusaku0125 3:e455433c8cae 33 while(1){
yusaku0125 3:e455433c8cae 34 wait(1);
yusaku0125 3:e455433c8cae 35 PC.printf("spd_a:%d[mm/sec] spd_b:%d[mm/sec]\r\n",speed_a,speed_b);//表示
yusaku0125 0:df99e50ed3fd 36 }
yusaku0125 3:e455433c8cae 37 }