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

上传人:豆**** 文档编号:17241427 上传时间:2022-05-22 格式:DOC 页数:18 大小:175KB
返回 下载 相关 举报
智能灭火机器人第程序.doc_第1页
第1页 / 共18页
智能灭火机器人第程序.doc_第2页
第2页 / 共18页
点击查看更多>>
资源描述

《智能灭火机器人第程序.doc》由会员分享,可在线阅读,更多相关《智能灭火机器人第程序.doc(18页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

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 *)

2、0x7007#defineP_IOB_Attrib(volatile unsigned 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#d

3、efineP_ADC_Ctrl(volatile unsigned int *)0x7015#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_IRQ

4、4_2KHz0x0020/ P_INT_Ctrl#defineC_IRQ4_4KHz0x0040/ 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,

5、xp=255,flag,dd; unsigned int left,right; unsigned 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,unsign

6、ed int cyclical_time);void forward(unsigned 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(vo

7、id); /AD转换void system_init(void); /系统初始化void 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

8、);/阶段0void Stage1(void); /阶段1void Stage2(void); /阶段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_

9、Dir = 0x0FFF; *P_IOB_Attrib = 0x0FFF; *P_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)

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

11、ase 1: forward(60,60); break; case 2: 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); b

12、reak; case 11: forward_s(1,1); break; default: break; void Stage2(void) /阶段2flag=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;

13、case 7: forward(12,12); 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; 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 & chdata

14、4=0x05 & abs(chdata3-chdata4)=2 &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); / 灭火风扇开启 dela

15、y_s(8); Set_IOA_Data(15,0); / 灭火风扇关闭 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 & ab

16、s(chdata3-chdata4)30 )turn_left(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=chdata

17、3 & abs(chdata3-chdata4)=20 ) break; 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

18、_right(13,213); if(pulse_xn) PWM_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) P

19、WM_left(17,417); 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_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

20、& pulse_yn) pulse_x=0;pulse_y=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;pul

21、se_y=0 ; break; if(workstate=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=

22、0) pulse_x=0;pulse_y=0 ; break;/*右转*/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;/*停止*/v

23、oid stop(void)*P_IOA_Data &=(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(

24、right cyclical_time)right=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 |=

25、C_IRQ4_4KHz; /*IRQ5中断*/void 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

26、) unsigned int ak15; unsigned 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

27、=5;k10;k+) sum=sum+akk; 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

28、=1; if(0xA0xp & xp=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

29、(!(*P_IOB_Data & 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)

30、; if(!(*P_IOB_Data & 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);

31、forward(20,20); turn_left(21);if(Get_IOA_Data(11)=0 & Get_IOA_Data(12) =1 ) /youbingbackward(15,15);while(Get_IOA_Data(11)=0) turn_left(21); forward(20,20); turn_right(30);if(Get_IOA_Data(11)=0 & Get_IOA_Data(12) =0)backward(15,15); turn_left(21); forward(20,20); turn_right(30);/*延时子程序*/ void delay_

32、s(unsigned int t ) / s级延时程序 unsigned int i; for(i=0;it;i+) delay_ms(1000); void delay_ms(unsigned int t) / ms级延时程序Delaynumber=0; while(Delaynumber=10*t)*P_Watchdog_Clear=0x01; /没有延时完则继续等待 附录2 FIQ.asm.DEFINE P_INT_Ctrl 0x7010 / Control port for interrupt source.DEFINE P_INT_Clear 0x7011 / Clear inter

33、rupt source.DEFINE C_FIQ_TMB 0x0800 / Timer B FIQ.DEFINE C_FIQ_TMA 0x2000 / Timer A FIQ.DEFINE C_FIQ_PWM 0x8000 / PWM FIQ.external _left,_right,_Delaynumber.PUBLIC _IRQ0,_IRQ1,_IRQ2,_IRQ3,_IRQ6,_IRQ7,_BREAK.TEXT.PUBLIC _FIQ_FIQ:push r1,r5 to spr1 = C_FIQ_TMAtest r1,P_INT_Ctrljnz L_FIQ_TimerAr1 = C_F

34、IQ_TMBtest r1,P_INT_Ctrljnz L_FIQ_TimerBL_FIQ_PWM:r1 = C_FIQ_PWMP_INT_Clear = r1pop r1,r5 from spretiL_FIQ_TimerA:P_INT_Clear = r1pop r1,r5 from spretiL_FIQ_TimerB:P_INT_Clear = r1 r2=_left r2 +=1 _left=r2 r3=_right r3 +=1 _right=r3 r4=_Delaynumber r4 +=1 _Delaynumber=r4pop r1,r5 from spreti_IRQ0:reti_IRQ1: reti_IRQ2: reti_IRQ3: reti_IRQ6: reti_IRQ7: reti_BREAK: reti附录3 wei.h#defineP_IOA_Data(volatile uns

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 小学资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁