障害物回避走行プログラム

Files at this revision

API Documentation at this revision

Comitter:
nishimura_taku_pet
Date:
Mon Jul 27 01:28:51 2020 +0000
Parent:
7:e00a84257aa2
Child:
9:5f197f0fa9bd
Commit message:
commit

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Jul 27 00:50:37 2020 +0000
+++ b/main.cpp	Mon Jul 27 01:28:51 2020 +0000
@@ -57,50 +57,46 @@
 }
 
 /* サーボ制御スレッド */
-void watchsurrounding(void const *n){
-    while(1){
-        Thread::signal_wait(0x1);       // シグナル待ち
-        SC = watch();
-        if(SC < limit){                 // 正面20cm以内に障害物がある場合
-            run = STOP;                 // 停止
-        }
-        servo.pulsewidth_us(1925);      // サーボを左に40度回転
-        thread_sleep_for(25);           // 250ms待つ
-        SLD = watch();
-        if(SLD < limit){                // 左前20cm以内に障害物がある場合
-            run = STOP;                 // 停止
-        }
-        servo.pulsewidth_us(2400);      // サーボを左に90度回転
-        thread_sleep_for(25);           // 250ms待つ
-        SL = watch();
-        if(SL < limit){                 // 左20cm以内に障害物がある場合
-            run = STOP;                 // 停止
-        }
-        servo.pulsewidth_us(1450);
-        thread_sleep_for(10);
-        servo.pulsewidth_us(925);       // サーボを右に40度回転
-        thread_sleep_for(25);           // 250ms待つ
-        SRD = watch();
-        if(SRD < limit){                // 右前20cm以内に障害物がある場合
-            run = STOP;                 // 停止
-        }
-        servo.pulsewidth_us(500);       // サーボを右に90度回転
-        thread_sleep_for(25);           // 250ms待つ
-        SR = watch();
-        if(SR < limit){                 // 右20cm以内に障害物がある場合
-            run = STOP;                 // 停止
-        }
-        servo.pulsewidth_us(1450);      // サーボを中央位置に戻す
-        thread_sleep_for(10);           // 100ms待つ
-        if(SC < limit || SLD < limit || SL < limit || SRD < limit || SR < limit){ // 20cm以内に障害物を検知した場合
-            flag = 1;                   // フラグに1をセット
-        }
+void watchsurrounding(){
+    SC = watch();
+    if(SC < limit){                 // 正面20cm以内に障害物がある場合
+        run = STOP;                 // 停止
+    }
+    servo.pulsewidth_us(1925);      // サーボを左に40度回転
+    thread_sleep_for(25);           // 250ms待つ
+    SLD = watch();
+    if(SLD < limit){                // 左前20cm以内に障害物がある場合
+        run = STOP;                 // 停止
+    }
+    servo.pulsewidth_us(2400);      // サーボを左に90度回転
+    thread_sleep_for(25);           // 250ms待つ
+    SL = watch();
+    if(SL < limit){                 // 左20cm以内に障害物がある場合
+        run = STOP;                 // 停止
+    }
+    servo.pulsewidth_us(1450);
+    thread_sleep_for(10);
+    servo.pulsewidth_us(925);       // サーボを右に40度回転
+    thread_sleep_for(25);           // 250ms待つ
+    SRD = watch();
+    if(SRD < limit){                // 右前20cm以内に障害物がある場合
+        run = STOP;                 // 停止
+    }
+    servo.pulsewidth_us(500);       // サーボを右に90度回転
+    thread_sleep_for(25);           // 250ms待つ
+    SR = watch();
+    if(SR < limit){                 // 右20cm以内に障害物がある場合
+        run = STOP;                 // 停止
+    }
+    servo.pulsewidth_us(1450);      // サーボを中央位置に戻す
+    thread_sleep_for(10);           // 100ms待つ
+    if(SC < limit || SLD < limit || SL < limit || SRD < limit || SR < limit){ // 20cm以内に障害物を検知した場合
+        flag = 1;                   // フラグに1をセット
     }
 }
 
 /* 障害物回避走行 */
 void avoidance(){
-    Thread ws_thread(watchsurrounding);     // watchsurroundingをスレッド化
     while(1){
         if(flag == 0){                      // 障害物がない場合
             run = ADVANCE;                  // 前進
@@ -179,7 +175,6 @@
             }
         }
         flag = 0;                   // フラグを0にセット
-        ws_thread.signal_set(0x1);  // watchsurroundingのシグナルをセット
     }
 }