智能灭火机器人设计第程序.doc

上传人:文库蛋蛋多 文档编号:2390962 上传时间:2023-02-17 格式:DOC 页数:24 大小:81KB
返回 下载 相关 举报
智能灭火机器人设计第程序.doc_第1页
第1页 / 共24页
智能灭火机器人设计第程序.doc_第2页
第2页 / 共24页
智能灭火机器人设计第程序.doc_第3页
第3页 / 共24页
智能灭火机器人设计第程序.doc_第4页
第4页 / 共24页
智能灭火机器人设计第程序.doc_第5页
第5页 / 共24页
点击查看更多>>
资源描述

《智能灭火机器人设计第程序.doc》由会员分享,可在线阅读,更多相关《智能灭火机器人设计第程序.doc(24页珍藏版)》请在三一办公上搜索。

1、附录1 main.c#include wei.h#defineP_IOA_Data(volatile unsigned int *)0x7000#defineP_IOA_Dir(volatile unsigned int *)0x7002#defineP_IOA_Attrib(volatile unsigned int *)0x7003#defineP_IOB_Data(volatile unsigned int *)0x7005#defineP_IOB_Dir(volatile unsigned int *)0x7007#defineP_IOB_Attrib(volatile unsigne

2、d int *)0x7008#defineP_TimerB_Data(volatile unsigned int *)0x700C#defineP_TimerB_Ctrl(volatile unsigned int *)0x700D#defineP_INT_Ctrl(volatile unsigned int *)0x7010#defineP_INT_Clear(volatile unsigned int *)0x7011#defineP_ADC(volatile unsigned int *)0x7014#defineP_ADC_Ctrl(volatile unsigned int *)0x

3、7015#defineP_ADC_MUX_Ctrl(volatile unsigned int *)0x702B#defineP_ADC_MUX_Data(volatile unsigned int *)0x702C#defineC_FIQ_PWM0x8000/ P_INT_Ctrl#defineC_FIQ_TMA0x2000/ P_INT_Ctrl#defineC_FIQ_TMB0x0800/ P_INT_Ctrl#defineC_IRQ4_1KHz0x0010/ P_INT_Ctrl#defineC_IRQ4_2KHz0x0020/ P_INT_Ctrl#defineC_IRQ4_4KHz

4、0x0040/ P_INT_Ctrl#defineC_IRQ5_2Hz0x0004/ P_INT_Ctrl#defineC_IRQ5_4Hz0x0008/ P_INT_Ctrl#defineC_TMB_32KHz0x0002/ P_TimerB_Ctrl#defineC_TMB_PWM_OFF0x0000/ P_TimerB_Ctrl#defineP_Watchdog_Clear(volatile unsigned int *)0x7012unsigned int mm,min,cny,Delaynumber,xp=255,flag,dd; unsigned int left,right; u

5、nsigned int pulse_x,pulse_y;unsigned int paixu7; /排序unsigned int chdata7; /火焰传感器检测通道unsigned int workstate=0;/进入检测范围unsigned int sucessfire=0;/灭火成功标记void PWM_left(unsigned int high_time,unsigned int cyclical_time);void PWM_right(unsigned int high_time,unsigned int cyclical_time);void forward(unsigne

6、d int m,unsigned int n) ;void forward_s(unsigned int m,unsigned int n);void forward_s2(unsigned int m,unsigned int n); void backward(unsigned int m,unsigned int n);void turn_left (unsigned int m);void turn_right(unsigned int m);void stop(void);void get_AD(void); /AD转换void system_init(void); /系统初始化vo

7、id delay_ms(unsigned int t ); /ms级延时程序void delay_s(unsigned int t ); /s级延时程序void adjust(void); /调整void adjust_s(void); /粗调void seekfire(void); /寻找火源void fire(void); /灭火void hf(void);int abs(int m);void bizhan(void ) ; /避障void biya(void ); /避崖void Stage0(void);/阶段0void Stage1(void); /阶段1void Stage2(v

8、oid); /阶段2void Stage3(void); /阶段3void revison();/修正函数/+主函数+/int main(void) system_init(); delay_s(3); while(1) seekfire(); fire(); delay_s(8); /*系统初始化*/void system_init(void) _asm(INT OFF); *P_IOA_Dir=0xE700; / *P_IOA_Attrib=0xE7FF; *P_IOA_Data=0x1800; *P_IOB_Dir = 0x0FFF; *P_IOB_Attrib = 0x0FFF; *P

9、_IOB_Data = 0xF000; *P_TimerB_Ctrl=(C_TMB_32KHz|C_TMB_PWM_OFF); *P_TimerB_Data=(65536-0.1*0.001*32768); *P_INT_Ctrl |=(C_FIQ_TMB + C_IRQ4_1KHz + C_IRQ5_2Hz + C_IRQ5_4Hz); /TMB用来控制2个电机,1KHZ控制避崖, _asm(INT IRQ,FIQ); /2HZ用来控制AD采集 4HZ用来避障/*寻找火源*/void seekfire(void)Stage0(); /阶段0 按一定的路径走Stage1(); /阶段1 有发现

10、火源,做初步调整Stage2(); /阶段2 接近火源,边走边调整Stage3(); /阶段3 到达火源前面,做最后的调整 void Stage0(void) /阶段0 flag=0;while(1) turn_left(120); /转360度 扫描有没有火源 forward(250,250); if(workstate=1 & flag=0) break; void Stage1(void) /阶段1 flag=1;Set_IOB_Data(0,1); / 开启蜂鸣器报警adjust_s(); switch(mm) case 1: forward(60,60); break; case 2

11、: forward(55,55); break; case 3: forward(50,50); break; case 4: forward(45,45); break; case 5: forward(35,35); break; case 6: forward(30,30); break; case 7: forward(20,20); break; case 8: forward(10,10); break; case 9: forward(5,5); break; case 10: forward(2,2); break; case 11: forward_s(1,1); break

12、; default: break; void Stage2(void) /阶段2 flag=2; while(1) adjust(); switch(mm) case 1: forward(40,40); break; case 2: forward(30,30); break; case 3: forward(25,25); break; case 4: forward(20,20); break; case 5: forward(18,18); break; case 6: forward(15,15); break; case 7: forward(12,12); break; case

13、 8: forward(10,10); break; case 9: forward(5,5); break; case 10: forward(2,2); break; case 11: forward_s(1,1); break; default: break; if(chdata3=0x05 | chdata4= 0x05)break; void Stage3(void) /阶段3 灭火前最后调整 unsigned int i; flag=3;for(i=0;i20;i+) if(chdata3=0x05 & chdata4=0x05 & abs(chdata3-chdata4)=2 &

14、abs(chdata2-chdata5)=8 ) break;if(chdata30x05 ) turn_left(1) ; delay_ms(10); if(chdata30x05 & chdata4 =0x05) turn_right(1); delay_ms(10); stop(); /*灭火*/void fire() unsigned int n; while(chdata3=0x7f | chdata4=1)revison();/如果1次吹不灭则调用修正 Set_IOA_Data(15,1); / 灭火风扇开启 delay_s(8); Set_IOA_Data(15,0); / 灭火

15、风扇关闭 delay_s(3); n+; sucessfire=1;/ 1 灭火成功 workstate=0; flag=0; Set_IOB_Data(0,0); / 开启蜂鸣器报警 xp=255; mm=0; min=255;/*修正子程序*/void revison() forward(8,8); adjust(); /*调整*/void adjust_s(void) if(min=chdata1)turn_left(21); if(min=chdata2)turn_left(12); if(min=chdata3 & abs(chdata3-chdata4)30 )turn_left(

16、5) ; if(min=chdata4 & abs(chdata3-chdata4)30 )turn_right(5); if(min=chdata5) turn_right(15); if(min=chdata6) turn_right(30); void adjust(void) while(min20 )turn_left(1); delay_ms(20); if( min=chdata4 & abs(chdata3-chdata4)20 )turn_right(1); delay_ms(20); if( min=chdata3 & abs(chdata3-chdata4)=20 ) b

17、reak; if( min=chdata4 & abs(chdata3-chdata4)=20 ) break; if(min=chdata5) turn_right(5); delay_ms(20); if(min=chdata6) turn_right(30); delay_ms(20); /*前进*/void forward(unsigned int m,unsigned int n) pulse_x=0; pulse_y=0; while(1) if(pulse_x=m & pulse_ym & pulse_y=n) PWM_right(13,213); if(pulse_xn) PW

18、M_left(17,217); if(pulse_xm & pulse_yn) pulse_x=0;pulse_y=0 ; break; if(workstate=1 & flag=0) pulse_x=0;pulse_y=0 ; break; void forward_s(unsigned int m,unsigned int n) pulse_x=0; pulse_y=0; while(1) if(pulse_x=m & pulse_ym & pulse_y=n) PWM_right(13,413); if(pulse_xn) PWM_left(17,417); if(pulse_xm &

19、 pulse_yn) pulse_x=0;pulse_y=0 ; break; if(workstate=1 & flag=0) pulse_x=0;pulse_y=0 ; break; void forward_s2(unsigned int m,unsigned int n) pulse_x=0; pulse_y=0; while(1) if(pulse_x=m & pulse_ym & pulse_y=n) PWM_right(15,215); if(pulse_xn) PWM_left(15,215); if(pulse_xm & pulse_yn) pulse_x=0;pulse_y

20、=0 ; break; if(workstate=1 & flag=0) pulse_x=0;pulse_y=0 ; break; /*后退*/void backward(unsigned int m,unsigned int n) pulse_x=0; pulse_y=0; while(1) if(pulse_x=m & pulse_ym & pulse_y=n) PWM_right(17,217); if(pulse_xn) PWM_left(13,213); if(pulse_xm & pulse_yn) pulse_x=0;pulse_y=0 ; break; if(workstate

21、=1 & flag=0) pulse_x=0;pulse_y=0 ; break; /*左转*/void turn_left(unsigned int m) pulse_x=0; pulse_y=0; while(1) if(pulse_x=m & pulse_ym & pulse_y=m) PWM_right(13,413); if(pulse_xm) PWM_left(13,413) ; if(pulse_xm & pulse_ym) pulse_x=0;pulse_y=0 ; break; if(workstate=1 & flag=0) pulse_x=0;pulse_y=0 ; br

22、eak; /*右转*/void turn_right(unsigned int m) pulse_x=0; pulse_y=0;while(1) if(pulse_x=m & pulse_ym & pulse_y=m) PWM_right(17,417); if(pulse_xm) PWM_left(17,417) ; if(pulse_xm & pulse_ym) pulse_x=0;pulse_y=0 ; break; if(workstate=1 & flag=0) pulse_x=0;pulse_y=0 ; break; /*停止*/void stop(void)*P_IOA_Data

23、 &=(bit13 + bit14); /将IOA13、IOA14清0 pulse_x=0; pulse_y=0;/*控制小车左轮*/void PWM_left(unsigned int high_time,unsigned int cyclical_time) if(left cyclical_time)left=0;pulse_x +; *P_Watchdog_Clear=0x0001;/*控制小车右轮*/void PWM_right(unsigned int high_time,unsigned int cyclical_time)if(right cyclical_time)right

24、=0;pulse_y +; *P_Watchdog_Clear=0x0001;/*IRQ4中断*/void IRQ4(void) _attribute_ (ISR);void IRQ4(void)if(*P_INT_Ctrl & C_IRQ4_1KHz) *P_INT_Clear |=C_IRQ4_1KHz ; dd+; if(dd=100) /100ms检测一次 dd=0; biya(); if(*P_INT_Ctrl & C_IRQ4_2KHz) *P_INT_Clear |=C_IRQ4_2KHz ;else *P_INT_Clear |=C_IRQ4_4KHz; /*IRQ5中断*/v

25、oid IRQ5(void) _attribute_ (ISR);void IRQ5(void)if(*P_INT_Ctrl & C_IRQ5_4Hz) /IRQ5_4HZ *P_INT_Clear |=C_IRQ5_4Hz ; bizhan(); /避障 else /IRQ5_2HZ *P_INT_Clear |=C_IRQ5_2Hz; hf(); /比较检测到AD的最小值 if(xp=0xc8)workstate=1;/ AD值达到200以内就认为发现火源。workstate=1进入检测范围 /*A/D转换*/void get_AD(void) unsigned int ak15; uns

26、igned int i,j,temp,k,sum; unsigned int Line,AD_Data; *P_ADC_Ctrl=0x01; /允许转换 for(Line=1;Line7;Line+) *P_ADC_MUX_Ctrl=Line;/选择通道 AD_Data=*P_ADC_MUX_Data;/启动转换 for(i=0;i8 ; /右移8位,只取8高位数据 aki=AD_Data; for(i=0;i15;i+) /从小到大排序 for(j=i+1;jakj)temp=aki;aki=akj;akj=temp; sum=0; for(k=5;k10;k+) sum=sum+akk;

27、chdataLine =sum/5; *P_Watchdog_Clear=0x01; /*/ void hf(void) unsigned int i,j,temp; get_AD();for(i=1;i=6;i+)paixui=chdatai;for(i=1;i=6;i+) for(j=i+1;jpaixuj)temp=paixui;paixui=paixuj;paixuj=temp; min=paixu1; /min 为单前这次检测到的最低值xp =xpmin ? xp:min ; /xp 为检测到的历史最低值if(0xC9xp & xp=0xFF) mm=1; if(0xA0xp & x

28、p=0xC9) mm=2; if(0x90xp & xp=0xA0) mm=3;if(0x7Fxp & xp=0x90) mm=4; if(0x67xp & xp=0x7F) mm=5;if(0x50xp & xp=0x67) mm=6; if(0x3Fxp & xp=0x50) mm=7;if(0x0Fxp & xp=0x3F) mm=8; if(0x0Cxp & xp=0x0F) mm=9; if(0x08xp & xp=0x0C) mm=10; if(0x00xp & xp=0x08) mm=11;/*避崖程序*/ void biya(void ) if(!(*P_IOB_Data &

29、0x8000) /q1 1000 stop(); backward(20,20); turn_left(21); if(!(*P_IOB_Data & 0x4000) /q2 0100 stop(); backward(20,20); turn_left(21); if(!(*P_IOB_Data & 0xc000) /q12 1100 stop(); backward(20,20); turn_left(21); if(!(*P_IOB_Data & 0x2000) /h1 0010 stop(); forward(15,15); turn_left(10); if(!(*P_IOB_Dat

30、a & 0x1000) /h2 0001 stop(); forward(15,15); turn_left(10); if(!(*P_IOB_Data & 0x3000) /h1 2 0011 stop(); forward(15,15); turn_left(10); else; /*避障程序*/ void bizhan(void ) if(Get_IOA_Data(12)=0 & Get_IOA_Data(11) =1 ) /zuobingbackward(15,15);while(Get_IOA_Data(12)=0) turn_right(30); forward(20,20); turn_left(21); if(Get_IOA_Data(11)=0 & Get_IOA_Data(12) =1 )

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 建筑/施工/环境 > 项目建议


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号