《2022年飞思卡尔智能车智能车源代码光电组 .pdf》由会员分享,可在线阅读,更多相关《2022年飞思卡尔智能车智能车源代码光电组 .pdf(5页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、智能车源代码光电组(有注解 ) #include /* common defines and macros */ #include /* derivative information */ #include math.h #include PWM.h #include A TD.h #include LQprintp.h #pragma LINK_INFO DERIVATIVE mc9s12dg256b unsigned int i = 0; unsigned int j = 0; unsigned int t = 0; byte ad_value13; uchar data13; int s
2、um = 0; uchar start_flag = 0; uchar num = 0; uchar lw=0; unsigned int per = 65530; int SPWM = 0; int L_SPWM = 0; unsigned int SPmax = 1000; int MPWM = 0; uchar current_corrd = 0; static unsigned int mem_num = 0; /*PID* static unsigned int Kp=25; static unsigned int Kp2=60; static unsigned int Ki=9;
3、static unsigned int Kd=30; static unsigned int rKp=100; static unsigned int rKp2=60; static unsigned int rKi=0; static unsigned int rKd=60; unsigned short E = 5; unsigned char q = 1; int Mp = 0; int Mi = 0; int Md = 0; int Mp2 = 0; int P_Speed = 0; int L_u3; /* /*舵 机PID变量* static unsigned int s_sKp=
4、35; /直道 PID 的 P值static unsigned int s_sKp2=0; /直道 PID的二阶 P 值static unsigned int s_sKi=0; /直道 PID 的 I 值static unsigned int s_sKd=10; /直道 PID 的 D 值/* 用于防止 PID 溢出 * unsigned short s_sE = 5; unsigned char s_sq = 1; /* /* 分别存放 P I D 值* int s_sMp = 0; int s_sMi = 0; int s_sMd = 0; int s_sMp2 = 0; /* int s
5、L_u3; /存放前 3 次理论速度与实际速度的差值int last_corrd310 =0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0; /* /*PID调试中断* unsigned char cp = 0; unsigned char ci = 0; unsigned char cd = 0; unsigned int search_PACN10; unsigned int np = 0; unsigned int sp500; /* /*存 储 前20点 的
6、数 据* int L_num10 = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0; int sum_corrd = 0; int wb = 0; /记录当前状态黑为 0,白为 1;/* /*红外滤波* int corrd10 = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0; int T_corrd = 0; int is_white = 0; int numb = 0; /* /*数据统计* int corrd_sate23 = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
7、int SPWM_sate15 = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0; int MPWM_sate11 = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0; uchar corrd_time512; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 5 页 - - - - - - - - - uchar SPWM_time512; uchar MPWM_time512; uchar PACN_time51
8、2; /* void setbusclock(void) CLKSEL=0X00; /disengage PLL to system PLLCTL_PLLON=1; /turn on PLL SYNR=1; REFDV=1; /pllclock=2*osc*(1+SYNR)/(1+REFDV)=32MHz; _asm(nop); /BUS CLOCK=16M _asm(nop); while(!(CRGFLG_LOCK=1); /when pll is steady ,then use it; CLKSEL_PLLSEL =1; /engage PLL to system; void Dly_
9、ms(int ms) int ii,jj; if (ms1) ms=1; for(ii=0;iims;ii+) for(jj=0;jj E)|(Ki * L_u0) 1000) MPWM = 1000; if(MPWM E)|(rKi * L_u0) 1000) MPWM = 1000; if(MPWM -1000) MPWM = -1000; Set_MPWM(MPWM); /*舵 机PID控 制 函 数* void s_PID_SPWM() 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - -
10、 - 第 2 页,共 5 页 - - - - - - - - - sL_u2 = sL_u1; sL_u1 = sL_u0; sL_u0 = (current_corrd+last_corrd01) - (last_corrd02+last_corrd03)/2; /*计算 PID 值* if(sL_u0-20) s_sMp = s_sKp * sL_u0; s_sMi = s_sMi + s_sKi * sL_u0; s_sMd = s_sKd * (sL_u0 - 2 * sL_u1 + sL_u2); s_sMp2 = s_sKp2 * (sL_u0 - sL_u1); /* /*I项溢
11、出防止 * if(s_sKi * sL_u0) s_sE)|(s_sKi * sL_u0) 70) SPWM = 70; if(SPWM -70) SPWM = -70; /* else sL_u0 = sL_u1; Set_SPWM(SPWM); /* void show_SPWM_data() for(j = 0; j 15; j+) printp(%10d, SPWM_satej); void show_MPWM_data() for(j = 0; j 11; j+) printp(%10d, MPWM_satej); void show_corrd_data() for(j = 0;
12、j 23; j+) printp(%6d, corrd_satej); void show_corrd_time() for(j = 0; j 512; j+) printp(%c, corrd_timej); void show_SPWM_time() for(j = 0; j 512; j+) printp(%c, SPWM_timej); void show_MPWM_time() for(j = 0; j 512; j+) printp(%c, MPWM_timej); void show_PACN_time() for(j = 0; j 512; j+) printp(%c, PAC
13、N_timej); void main(void) /* put your own code here */ setbusclock(); SCI_Init(); AD_Init(); IOC_Init(); Ini_PWM(); EnableInterrupts; for(;) Dly_ms(10); /*红外滤波* for(j=0; j10; j+) adc_get(ad_value); for(i = 1; i=160) datai = 1; num = num + 1; else datai = 0; sum = sum + datai * 2*i; /printp(%10d, dat
14、ai); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 5 页 - - - - - - - - - if(num = 0) corrdj = -1; else corrdj = sum/num; sum = 0; num = 0; is_white = 0; for(i=0;i10;i+) if(corrdi 0) is_white+; for(j=i;j 6) numb = 0; sum = 0; j = 0; for(i=3; i -1) sum += corrdi
15、; j+; if(j!=0) current_corrd = sum/j; numb = j; L_num9 = L_num8; L_num8 = L_num7; L_num7 = L_num6; L_num6 = L_num5; L_num5 = L_num4; L_num4 = L_num3; L_num3 = L_num2; L_num2 = L_num1; L_num1 = L_num0; L_num0 = numb; /* wb = L_num1|L_num2|L_num3|L_num4|L_num5|L_num6|L_num7|L_num8|L_num9; if(numb = 0)
16、 DDRB = 0 x80; if(search_PACN10 10 & wb0) P_Speed = 20; if(lw = 0) SPWM = -70; else SPWM = 72; / if(sum_corrd-220) Set_MPWM(0); / else / Set_MPWM(700); else P_Speed = 20; if(lw = 0) SPWM = -70; else SPWM = 72; Set_MPWM(1000); /Set_MPWM(0); else DDRB = 0 x00; / printp(%d , current_corrd); / if(PTJ &
17、0 x80) Set_MPWM(0); /else if(wb = 0) Set_MPWM(1000); else switch(current_corrd) case 2: SPWM = -70;P_Speed = 32;r_PID_MPWM();lw = 0;break; case 3: SPWM = -60;P_Speed = 32;r_PID_MPWM();lw = 0;break; case 4: SPWM = -50;P_Speed = 32;r_PID_MPWM();lw = 0;break; case 5: SPWM = -40;P_Speed = 名师资料总结 - - -精品
18、资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 5 页 - - - - - - - - - 32;r_PID_MPWM();lw = 0;break; case 6: SPWM = -30;P_Speed = 32;r_PID_MPWM();lw = 0;break; case 7: SPWM = -25;P_Speed = 32;r_PID_MPWM();lw = 0;break; case 8: SPWM = -20;P_Speed = 32;r_PID_MPWM();lw = 0;break;
19、 case 9: SPWM = -15;P_Speed = 32;s_PID_MPWM();lw = 0;break; case 10: SPWM = -10; P_Speed = 32;s_PID_MPWM();lw = 0;break; case 11: SPWM = -5; P_Speed = 32;s_PID_MPWM();lw = 0;break; case 12: SPWM = 0; P_Speed = 32;s_PID_MPWM();break; case 13: SPWM = 5; P_Speed = 32;s_PID_MPWM();lw = 1;break; case 14:
20、 SPWM = 10; P_Speed = 32;s_PID_MPWM();lw = 1;break; case 15: SPWM = 15; P_Speed = 32;s_PID_MPWM();lw = 1;break; case 16: SPWM = 20; P_Speed = 32;r_PID_MPWM();lw = 1;break; case 17: SPWM = 25; P_Speed = 32;r_PID_MPWM();lw = 1;break; case 18: SPWM = 30; P_Speed = 32;r_PID_MPWM();lw = 1;break; case 19:
21、 SPWM = 40; P_Speed = 32;r_PID_MPWM();lw = 1;break; case 20: SPWM = 50; P_Speed = 32;r_PID_MPWM();lw = 1;break; case 21: SPWM = 60; P_Speed = 32;r_PID_MPWM();lw = 1;break; case 22: SPWM = 70; P_Speed = 32;r_PID_MPWM();lw = 1;break; default: break; /Set_MPWM(500); last_corrd03 = last_corrd02; last_co
22、rrd02 = last_corrd01; last_corrd01 = last_corrd00; last_corrd00 = current_corrd; corrd_satecurrent_corrd+; SPWM_sateSPWM/10+7+; MPWM_sateMPWM/10+; corrd_timet=current_corrd; SPWM_timet=SPWM+70; MPWM_timet=(MPWM+1000)/10; s_PID_SPWM(); /Set_SPWM(SPWM); /Set_MPWM(500); search_PACN10 = PACN10; /printp(
23、%10d, search_PACN10); PACN_timet=search_PACN10; PACN10 = 0; sum_corrd = 0; sum = 0; start_flag = 0; num = 0; t+; if(t=512) MPWM=0; for(;); /* wait forever */ /* please make sure that you never leave this function */ void interrupt 20 SCI0RX(void) byte result,temp; DisableInterrupts; temp=SCI0SR1; /*
24、clear flag*/ result=SCI0DRL; if(result=s|result=S) show_SPWM_data(); if(result=M|result=m) show_MPWM_data(); if(result=c|result=C) show_corrd_data(); if(result=1|result=!) show_corrd_time(); if(result=2|result=) show_SPWM_time(); if(result=3|result=#) show_MPWM_time(); if(result=4|result=$) show_PACN_time(); if(result=a|result=A) MPWM=0; for(;)EnableInterrupts; EnableInterrupts; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 5 页 - - - - - - - - -