|
傳感器接法
紅外傳感器接法
前紅外: 數(shù)字9
左紅外: 數(shù)字15
左45度角紅外: 數(shù)字10
右45度角紅外: 數(shù)字8
右紅外: 數(shù)字14
火焰?zhèn)鞲衅鹘臃?br />
左火焰: 模擬3
中火焰: 模擬5
右火焰: 模擬4
(底部)灰度傳感器: 模擬2
聲控傳感器: 模擬6
程序說明
- #define p 120 //定義火焰?zhèn)鞲衅鳈z測到火焰的返回值
- int k,j,i=0,n,b=1,c=1; //程序控制變量,不必更改
- int m=i;
- int pro,end=1;
- void main() //主程序
- {
- while(analog(6)>100) //聲控啟動
- {
- }
- while(!(analog(2)>100)) //走出白色超始區(qū)
- {
- motor(0,80);
- motor(1,80);
- }
- pro=start_process(test()); //啟動地面標(biāo)志線檢測進(jìn)程
- while(1) //滅火與迷宮程序切換
- {
- if (analog(3) {
- fire();
- }
- else //沒有發(fā)現(xiàn)火焰,進(jìn)入迷宮子程序
- {
- migong();
- }
- }
- }
- void migong() //迷宮子程序
- {
- if(digital(8)==0 II digital(9)==0) //如果前方 或 右45度角紅外檢測到障礙物,左轉(zhuǎn)
- {
- motor(0,-70); //根據(jù)情況,調(diào)節(jié)功率參數(shù),以下雷同
- motor(1,70);
- }
- else if(digital(14)==0 && digital(9)==1 && digital(8)==1) //如果只右方有障礙物,直行
- {
- motor(0,100);
- motor(1,100);
- }
- else //如果沒有障礙物,右轉(zhuǎn)
- {
- motor(0,100);
- motor(1,-100);
- motor(1,10);
- motor(0,90);
- }
- if((i>2)&&(i>m)) //如果標(biāo)志線數(shù)大于2且標(biāo)志線有變化(針對1、2、3號房間)
- {
- stop();
- while(analog(3)>150 && analog(5)>150 && analog(4)>150) //如果沒有檢測到火焰
- {
- motor(0,70); //右轉(zhuǎn)
- motor(1,-70);
- if(digital(14)==0) //右紅外檢測到障礙物,停止轉(zhuǎn)動
- break;
- }
- m=i+1; //更改標(biāo)志線的對比變量
- }
- if(i==1 && b && (analog(3)>150 && analog(5)>150 && analog(4)>150)) //4號房間,檢測到第1條標(biāo)志線,且沒有火焰
- {
- while(digital(9)==0 II digital(8)==0 II digital(14)==0) //任意右手紅外傳感器有障礙物,右轉(zhuǎn)
- {
- motor(0,70);
- motor(1,-70);
- }
- while(digital(9)==1) //前方傳感器沒有障礙物,右前進(jìn)
- {
- motor(0,100);
- motor(1,70);
- }
- b=0; //控制變量
- }
- }
- void fire() //滅火子程序
- {
- if((i>4 II i<8) && c) //如果在2號房間
- {
- while(analog(3) {
- motor(0,-60);
- motor(1,60);
- }
- motor(0,60); //強(qiáng)制直行一段
- motor(1,60); //主要是解決2號房間右手法則滅火時的碰墻的情況
- sleep(0.6);
- c=0;
- }
- while(analog(5)<10 && analog(2)<60) //如果火焰距離合適 且 檢測到地面標(biāo)志線,滅火
- {
- motor(0,-10); //后退一段
- motor(1,-10);
- sleep(0.15);
- stop();
- motor(2,100); //滅火
- sleep(2.000);
- stop();
- motor(0,-70); //滅完火,后退一段
- motor(1,-100);
- sleep(0.2);
- kill_process(pro); //關(guān)掉地面標(biāo)志線檢測進(jìn)程
- select(); //進(jìn)入回家子程序選擇子程序
- }
- while(analog(3)-analog(4)> 3) //向右調(diào)整,如果調(diào)整較頻繁,修改最后的3
- {
- motor(0,50);
- motor(1,-50);
- }
- while(analog(4)-analog(3)> 3) //向左調(diào)整,同上
- {
- motor(0,-50);
- motor(1,50);
- }
- motor(0,40); //直行
- motor(1,40);
- }
- void test() //地面標(biāo)志線檢測子程序
- {
- while(1)
- {
- k=analog(2);
- if(k<30 II k>150) //30 和 150 分別為底部傳感器檢測到的的標(biāo)志線和黑地面
- j=k; //的值,請根據(jù)實(shí)際測試值適當(dāng)修改
- if((j<30) && (n>150)) //如果更改,此處也一并更改
- {
- i++;
- beep();
- }
- n=j;
- }
- }
- void select() //回家程序選擇子程序
- {
- if(i<3) //如果地面標(biāo)志線條數(shù)小于3(即在4號房間)
- gohome_4(); //進(jìn)入4號房間回家子程序,下同
- else if(i<5)
- gohome_3();
- else if(i<8)
- gohome_2();
- else
- gohome_1();
- }
- void gohome_4() //4號房間回家子程序
- {
- start_process(endstop()); //開啟終(起)點(diǎn)停止進(jìn)程序,下同
- mg_right(); //右手法則回家
- stop(); //停止
- sleep(100.0);
- }
- void gohome_3() //3號房間回家子程序
- {
- start_process(endstop());
- mg_left(); //左手法則回家
- stop();
- sleep(100.0);
- }
- void gohome_2() //2號房間的特殊回家程序
- {
- motor(0,60); //滅完火后右轉(zhuǎn)一段
- motor(1,-60);
- sleep(0.2);
- reset_system_time(); //系統(tǒng)時間復(fù)位
- while(mseconds()<2100L) //運(yùn)行左手法則走迷宮2.1秒(時間長短為可以走出房間門口且不到3號房間的走廊)
- {
- while(digital(9)==0 II digital(10)==0)
- {
- motor(0,60);
- motor(1,-50);
- }
- while(digital(15)==0 && digital(9)==1 && digital(10)==1)
- {
- motor(0,50);
- motor(1,50);
- }
- while(digital(15)==1 && digital(9)==1 && digital(10)==1)
- {
- motor(0,-80);
- motor(1,80);
- motor(0,45);
- motor(1,80);
- }
- }
- stop();
- sleep(0.1);
- while(digital(9)==0) //如果前方有障礙物,右轉(zhuǎn)(調(diào)整姿勢)
- {
- motor(0,60);
- motor(1,-60);
- }
- while(digital(9) == 1) //如果前方?jīng)]有障礙物,左、右手法則并進(jìn),基本上直行到另一端(3號房間旁)
- {
- if(digital(8)==0 ) //右邊45度有障礙物,左轉(zhuǎn)
- {
- motor(0,-50);
- motor(1,50);
- }
- if(digital(14)==0 && digital(8)==1) //右邊有障礙物且右45度角無障礙物,直行
- {
- motor(0,50);
- motor(1,50);
- }
- if(digital(10)==0) //左邊45度有障礙物,右轉(zhuǎn)
- {
- motor(0,50);
- motor(1,-50);
- }
- if(digital(15)==0 && digital(10)==1) //左邊有障礙物且左45度角無障礙物,直行
- {
- motor(0,50);
- motor(1,50);
- }
- motor(0,50); //直行
- motor(1,50);
- }
- start_process(endstop()); //前方有障礙物時,退出循環(huán),開啟終點(diǎn)停止進(jìn)程
- while(end) //左手法則走迷宮
- {
- if(digital(9)==0 II digital(10)==0)
- {
- motor(0,60);
- motor(1,-50);
- }
- else if (digital(15)==0 && digital(9)==1 && digital(10)==1)
- {
- motor(0,70);
- motor(1,70);
- }
- else
- {
- motor(0,-80);
- motor(1,80);
- motor(0,40);
- motor(1,100);
- }
- }
- motor(0,-100); //終點(diǎn)剎車
- motor(1,-100);
- sleep(0.1);
- stop(); //停止
- sleep(100.0);
- }
- void gohome_1() //1號房間回家
- {
- start_process(endstop()); //開啟終點(diǎn)停止進(jìn)程
- mg_right(); //右手法則走迷宮
- stop();
- sleep(100.0);
- }
- void mg_right() //右手法則走迷宮子程序
- {
- while(end)
- {
- if(digital(8)==0 II digital(9)==0)
- {
- motor(0,-70);
- motor(1,70);
- }
- else if(digital(14)==0 && digital(9)==1 && digital(8)==1)
- {
- motor(0,100);
- motor(1,100);
- }
- else
- {
- motor(0,100);
- motor(1,-100);
- motor(1,20);
- motor(0,100);
- }
- }
- }
- void mg_left() //左手法則走迷宮子程序
- {
- while(end)
- {
- if(digital(9)==0 II digital(10)==0 )
- {
- motor(0,60);
- motor(1,-50);
- }
- else if(digital(15)==0 && digital(9)==1 && digital(10)==1)
- {
- motor(0,70);
- motor(1,70);
- }
- else
- {
- motor(0,-100);
- motor(1,100);
- motor(0,30);
- motor(1,100);
- }
- }
- }
- void endstop() //終點(diǎn)停止進(jìn)程子程序
- {
- while(1)
- {
- k=analog(2); //檢測1次
- sleep(0.02); //間隔0.02秒,再檢測 //可適當(dāng)縮短或延長時間
- if(k<50 && analog(2)<50) //如果2次都是白色,終點(diǎn)到,停止
- {
- end=0;
- stop();
- sleep(1.0);
- }
- }
- }
- //全部程序結(jié)束
復(fù)制代碼
|
|