[[2006a/C3/課題3左]]

今回、最初に光センサーを一つ使用して光を追うプログラムを作り、そこに光を見失ったとき、音を鳴らしながら旋回する動作を組み込むプログラムを作っていました。
が、途中行き詰ってしまったので、光センサーを二つ使用して光を追うプログラムを作り直していました。
しかし、結局コンパイルしてもうまく作動せず、期限までに完成することができませんでした。
以下に、失敗したプログラムを載せます。

 #define turn_right OnFwd(OUT_A);OnRev(OUT_C); 
 #define turn_left OnRev(OUT_A);OnFwd(OUT_C);
 #define Stop_turning Off(OUT_A+OUT_C);
 #define TURN_TIME 50
 #define go_straight OnFwd(OUT_A+OUT_C);Wait(30);Off(OUT_A+OUT_C);Wait(50);
 sub serch_light()
-曲を演奏するプログラム(以下のプログラム3つ共に使用)


 #define Do 523           //ドを定義
 #define Re 587           //レを定義
 #define Mi 659           //ミを定義
 #define Fa 698           //ファを定義
 #define So 784           //ソを定義
 #define Ra 880           //ラを定義
 task play_music()
 {
    while (true)
    {
        PlayTone(Do,30); Wait(40);  //ドを0,3秒鳴らし、次の音にいくまで0,4秒待つ
        PlayTone(Do,30); Wait(40);  //ド
        PlayTone(So,30); Wait(40);    //ソ 
        PlayTone(So,30); Wait(40);    //ソ 
        PlayTone(Ra,30); Wait(40);    //ラ
        PlayTone(Ra,30); Wait(40);    //ラ
        PlayTone(So,70); Wait(80);    //ソを0,7秒鳴らし、次の音に行くまで0,8秒待つ
        PlayTone(Fa,30); Wait(40);    //ファ 
        PlayTone(Fa,30); Wait(40);    //ファ
        PlayTone(Mi,30); Wait(40);    //ミ
        PlayTone(Mi,30); Wait(40);    //ミ 
        PlayTone(Re,30); Wait(40);    //レ
        PlayTone(Re,30); Wait(40);    //レ
        PlayTone(Do,70); Wait(80);    //ド     
    }
 } 


-光センサーを一つ使用

失敗プログラム1

 #define turn_right OnFwd(OUT_A);OnRev(OUT_C);     //右回りを定義
 #define turn_left OnRev(OUT_A);OnFwd(OUT_C);     //左回りを定義
 #define Stop_turning Off(OUT_A+OUT_C);        //旋回の停止を定義
 #define TURN_TIME 50                 //旋回する時間を0,5秒に設定
 #define go_straight OnFwd(OUT_A+OUT_C);Wait(30);Off(OUT_A+OUT_C);Wait(50);    //直進を定義
 sub serch_light()     //光を探す
 {
    ClearTimer(0);
    while(FastTimer(0)<400){
       if(SENSOR_2>=light_max){
           light_max=SENSOR_2;
           time_max=FastTimer(0);
           }
      }
      Stop_turning;Wait(50);
      turn_left;     
      Wait(TURN_TIME-time_max);
      Stop_turning;
      go_straight;          
 }
 task main()
 {
    SetSensor(SENSOR_2,SENSOR_LIGHT);
    int light_max=30,time_max=0;
    SetSensor(SENSOR_2,SENSOR_LIGHT);    //センサー2は光センサー
    int light_max=30,time_max=0;      //光の最大値を30、最大値を観測した時間を0と置く
    while(true){ 
    OnFwd(OUT_C);OnRev(OUT_A);Wait(25);Off(OUT_A+OUT_C);Wait(10);
    turn_right;
    turn_left;Wait(25);Off(OUT_A+OUT_C);Wait(10);   //左に0,25秒回り、0,1秒待つ
    turn_right;              //右に回る
    ClearTimer(0);
    while(FastTimer(0)<TURN_TIME){
       if(SENSOR_2>=light_max){
           light_max=SENSOR_2;
           time_max=FastTimer(0);
       if(SENSOR_2>=light_max){      //センサー2の値が30以上のとき
           light_max=SENSOR_2;       //センサー2の値を光の最大値に置き換え
           time_max=FastTimer(0);     
           }
      }
      Stop_turning;Wait(50);
      turn_left;     
      Wait(TURN_TIME-time_max);
      Stop_turning;
      go_straight;      
       if(SENSOR_2<light_max){
           start play_music; 
           search_light();
           stop play_music;             
      Stop_turning;Wait(50);        //旋回停止、0,5秒待つ
      turn_left;              //左に回る     
      Wait(TURN_TIME-time_max);       //旋回した時間−最大値を観測した時間
      Stop_turning;            //旋回停止
      go_straight;                //直進
       if(SENSOR_2<light_max){       //センサー2の値が30より小さいとき
           start play_music;        //曲の演奏を開始
           search_light();         //光を探す
           stop play_music;               //曲の演奏を停止
          }
      light_max=30;
      light_max=30;            //光の最大値を30に置き換え
      time_max=0;
      }
 }

問題点:サブルーチンの中身がエラー。



失敗プログラム2

 #define turn_right OnFwd(OUT_A);OnRev(OUT_C); 
 #define turn_left OnRev(OUT_A);OnFwd(OUT_C);
 #define Stop_turning Off(OUT_A+OUT_C);
 #define TURN_TIME 50
 #define go_straight OnFwd(OUT_A+OUT_C);Wait(30);Off(OUT_A+OUT_C);Wait(50);
 task main()
 {
    SetSensor(SENSOR_2,SENSOR_LIGHT);
    int light_max=0,time_max=0;
    while(true){ 
    turn_left;Wait(25);Off(OUT_A+OUT_C);Wait(10);
    turn_right;
    ClearTimer(0);
    while(FastTimer(0)<TURN_TIME){
       if(SENSOR_2>=light_max){
           light_max=SENSOR_2;
           time_max=FastTimer(0);
           }
      }
       if(SENSOR_2<light_max){
          ClearTimer(0);
    while(FastTimer(0)<400){
       if(SENSOR_2>=light_max){
           light_max=SENSOR_2;
           time_max=FastTimer(0);
           }
      }
      }
      Stop_turning;Wait(50);
      turn_left;     
      Wait(TURN_TIME-time_max);
      Stop_turning;
      go_straight;  
      light_max=30;
      time_max=0;
      }
 }      

問題点:光の大きさを感知してくれなくなってしまった。



-光センサーを二つ使用

失敗プログラム3

 #define turn_right OnFwd(OUT_A);OnRev(OUT_C); 
 #define turn_left OnRev(OUT_A);OnFwd(OUT_C);
 #define go_straight OnFwd(OUT_A+OUT_C);
 task main()
 {
    SetSensor(SENSOR_1,SENSOR_LIGHT);
    SetSensor(SENSOR_3,SENSOR_LIGHT);
    while(true){
       if(SENSOR_1>=30&&SENSOR_3>=30){
           go_straight;
           }
       if(SENSOR_1<30&&SENSOR_3>=30){ 
           turn_right;
           }
       if(SENSOR_1>=30&&SENSOR_3<30){
           turn_left;
           }
       if(SENSOR_1<30&&SENSOR_3<30){
           start play_music;
           turn_right;
           stop play_music;
           }
    }    
 }

問題点:光センサーの値を決めてしまったために、ちょっとした光の大きさの変化で動作が変わってしまい、ロボットが困っていた。

 


トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS