[[2005/C3/しかた]] //実際に本番で使ったプログラムです。 //見ての通り、まだ途中の段階です。 //もう少し時間があれば全部の箱を入れることが出来るプログラムができたんだろうなと思います。 //でも、カトリーヌさんも言ってたように本番が3日遅ければ、 //始めるのが3日遅かったんだろうなって思います。 //だから、僕たちの限界はやっぱり1つです。 //でもせっかくいいロボットが出来たのにもったいないなー #define BLACK 40 //黒い線 #define C90 140 //90度基準 #define BKBOX 33 //黒い匣か #define WHBOX 46 //白い匣か #define GRBOX 38 //緑い匣か #define LINE1 300 //一つ目の匣 #define LINE2 200 //二つ目の匣 #define LINE3 100 //三つ目の匣 #define A 15 //微少移動時間 #define HIRAKU 200 //掴む時間 #define SHIMARU 200 //離す時間 void c90(int x){//90度回転する関数 if(x==2){//反時計回りなら(仮) OnFwd(OUT_A); OnRev(OUT_C); Wait(C90-17); } if(x==3){//時計回りなら(仮) OnFwd(OUT_A); OnRev(OUT_C); Wait(C90+17); } if(x==0){//時計回りなら OnFwd(OUT_C); OnRev(OUT_A); Wait(C90); } else if(x==1){//半時計回りなら OnFwd(OUT_A); OnRev(OUT_C); Wait(C90); } } void hiraku(){//開く関数 OnRev(OUT_B); Wait(HIRAKU); } void shimaru(){//閉る関数 OnFwd(OUT_B); Wait(SHIMARU); OnFwd(OUT_B); } void tomaru(){//止まる関数 Off(OUT_A+OUT_C); } task main() { SetSensor(SENSOR_1, SENSOR_LIGHT);//左光センサー SetSensor(SENSOR_3, SENSOR_LIGHT);//右光センサー SetSensor(SENSOR_2, SENSOR_LIGHT);//色識別光センサー int state;//状態変数 int time;//状態遷移変数 int line;//何列目か変数 int count;//何個目の匣か変数 int greenline;//緑の線何本目か変数 int work;//作業用 int flag;//黒い線フラグ state = 0; time = 0; line = 1; count = 1; flag =0; while(true){ while(line <= 3){ state = 200; tomaru();///////////////////// hiraku(); OnFwd(OUT_A+OUT_C); Wait(100); tomaru(); shimaru(); state = 100; if(SENSOR_2 > WHBOX)state += 10; else if(SENSOR_2 < BKBOX)state += 30; else state += 20; if(line==1){//1列目なら OnRev(OUT_A+OUT_C); Wait(count*50+50); c90(1); // while(!(SENSOR_1<BLACK)){ OnFwd(OUT_C+OUT_A); Wait(50); // } c90(0); OnFwd(OUT_C+OUT_A); Wait(50); c90(1); OnFwd(OUT_C+OUT_A); Wait(50); } else if(line==2){//2列目なら while(!(SENSOR_1<BLACK && SENSOR_3<BLACK)){ OnFwd(OUT_A+OUT_C); Wait(A); } c90(0); while(!(SENSOR_1<BLACK && SENSOR_3<BLACK)){ OnFwd(OUT_A+OUT_C); Wait(A); } c90(0); while(!(SENSOR_1<BLACK)){ OnFwd(OUT_A+OUT_C); Wait(A); } c90(1); } else if(line==3){//3列目なら } tomaru();///////////////////// if(state % 100 >= 30){//黒い匣なら greenline = 3; } else if(state % 100 >= 20){//緑の匣なら greenline = 1; } else if(state % 100 >= 10){//白い匣なら greenline = 2; } work= greenline; while(!(SENSOR_3 < BLACK && SENSOR_1 < BLACK)){ //2つのセンサーが同時にセンサーを感知しない限り //ライントレースせよ if(SENSOR_1>BLACK && SENSOR_3>BLACK){ OnFwd(OUT_A+OUT_C); Wait(A); } else if(SENSOR_1< BLACK){ while(SENSOR_1< BLACK){ OnFwd(OUT_C); OnRev(OUT_A); Wait(A); } } else if(SENSOR_3< BLACK){ while(SENSOR_3< BLACK){ OnFwd(OUT_A); OnRev(OUT_C); Wait(A); } } tomaru();///////////////////// } while(work!=0){ if((SENSOR_3 < BLACK && SENSOR_1 < BLACK)){ flag++;/////黒い線を何本通過したか的なカウンタ OnFwd(OUT_A+OUT_C); Wait(30); } else if(flag==0 && SENSOR_1>BLACK && SENSOR_3>BLACK){//ライントレース OnFwd(OUT_A+OUT_C); Wait(A); } else if(flag==0 && SENSOR_1< BLACK){ while(SENSOR_1< BLACK){ OnFwd(OUT_C); Wait(A); } } else if(flag ==0 && SENSOR_3< BLACK){ while(SENSOR_3< BLACK){ OnFwd(OUT_A); Wait(A); } }//////////////////////////////ライントレースここまで if((SENSOR_3 > BLACK && SENSOR_1 > BLACK) && flag>0){ work--; flag=0; OnFwd(OUT_A+OUT_C); Wait(20); } tomaru();///////////////////// } OnFwd(OUT_A+OUT_C); Wait(15); c90(2); OnRev(OUT_A+OUT_C); // if(line==1){ // Wait(LINE1); // } // else if(line==2){ Wait(LINE2+30); // } // else if(line==3){ // Wait(LINE3); // } tomaru();///////////////////// state = 250; hiraku();////////////// shimaru(); OnFwd(OUT_A+OUT_C); // if(line==1){ // Wait(LINE1); // } // else if(line==2){ Wait(LINE2-20); // } // else if(line==3){ // Wait(LINE3); // } tomaru();///////////////////// c90(3); work= greenline-1; while(work!=0){ if((SENSOR_3 < BLACK && SENSOR_1 < BLACK)){ flag++;/////黒い線を何本通過したか的なカウンタ OnFwd(OUT_A+OUT_C); Wait(30); } else if(flag==0 && SENSOR_1>BLACK && SENSOR_3>BLACK){//ライントレース OnFwd(OUT_A+OUT_C); Wait(A); } else if(flag==0 && SENSOR_1< BLACK){ while(SENSOR_1< BLACK){ OnFwd(OUT_C); Wait(A); } } else if(flag ==0 && SENSOR_3< BLACK){ while(SENSOR_3< BLACK){ OnFwd(OUT_A); Wait(A); } }//////////////////////////////ライントレースここまで if((SENSOR_3 > BLACK && SENSOR_1 > BLACK) && flag>0){ work--; flag=0; OnFwd(OUT_A+OUT_C); Wait(20); } tomaru();///////////////////// } while(!(SENSOR_3 < BLACK && SENSOR_1 < BLACK)){ if(SENSOR_1>BLACK && SENSOR_3>BLACK){ OnFwd(OUT_A+OUT_C); Wait(A); } else if(SENSOR_1< BLACK){ while(SENSOR_1< BLACK){ OnFwd(OUT_C); OnRev(OUT_A); Wait(A); } } else if(SENSOR_3< BLACK){ while(SENSOR_3< BLACK){ OnFwd(OUT_A); OnRev(OUT_C); Wait(A); } } tomaru();///////////////////// } OnFwd(OUT_A+OUT_C); Wait(30); if(line==1){ if(count==1){ c90(1); OnFwd(OUT_A+OUT_C); Wait(LINE2-150); } if(count==2){ c90(1); OnFwd(OUT_A+OUT_C); Wait(10); } tomaru();///////////////////// } else if(line==2){ } else if(line==3){ } count++; if(count>3){ line++; count=1; } } } }