首页 智能循迹小车详细源代码程序MSPID

智能循迹小车详细源代码程序MSPID

举报
开通vip

智能循迹小车详细源代码程序MSPIDLastrevisiondate:13December2020.智能循迹小车详细源代码程序MSPID巡线车程序(完整版)1#ifndef_Macro.h_2#define_Macro.h_3#include4#include5#defineucharunsignedchar6#defineuintunsignedint7#defineone11.118#defineLMAX19999#defineRMAX399910#defineCPU_F((double)8000000)11#definedelay_us(x)_...

智能循迹小车详细源代码程序MSPID
Lastrevisiondate:13December2020.智能循迹小车详细源代码程序MSPID巡线车程序(完整版)1#ifndef_Macro.h_2#define_Macro.h_3#include4#include5#defineucharunsignedchar6#defineuintunsignedint7#defineone11.118#defineLMAX19999#defineRMAX399910#defineCPU_F((double)8000000)11#definedelay_us(x)__delay_cycles((long)(CPU_F*(double)x/1000000.0))1213#definedelay_ms(x)__delay_cycles((long)(CPU_F*(double)x/1000.0))14#definePC20//比例放大系数15#defineIC0//积分放大系数16#defineDC85//大系数17#defineLEFTOUTTACCR118#defineRIGHTOUTTACCR219#defineSensorInP5IN20#defineF5000//5000hz21#definePeriod(8000000/F)22#defineEnableLeftPosP3OUT|=BIT123#defineUnenableLeftPosP3OUT&=~BIT12425#defineEnableLeftNegP3OUT|=BIT026#defineUnenableLeftNegP3OUT&=~BIT02728#defineEnableRightPosP3OUT|=BIT229#defineUnenableRightPosP3OUT&=~BIT23031#defineEnableRightNegP3OUT|=BIT332#defineUnenableRightNegP3OUT&=~BIT33334#defineBasic_Left100//百分之八十35#defineBasic_Right100//Basic_Left36#defineMAX(100)37#defineMIN(-100)38#defineforeward139#definebackward040#definemax_speed10041#definemin_speed-10042#definekey0434445#defineleft_1146#defineleft_2247#defineleft_3348#defineleft_4449#defineleft_5550#defineleft_6651#defineleft_77//右直角5253#defineright_1-154#defineright_2-255#defineright_3-356#defineright_4-457#defineright_5-558#defineright_6-659#defineright_7-7//左直角60#endif[cpp]viewplaincopy61#include"Macro.h"62#include"sensor.h"63voidMotorstop()64{65LEFTOUT=0;66RIGHTOUT=0;67}68voidMotorLeft(intspeed,intdirection)69{70if(speed>max_speed)speed=max_speed;71if(direction==backward)//反转72{73EnableLeftNeg;74UnenableLeftPos;75}76elseif(direction==foreward)//正转77{78EnableLeftPos;79UnenableLeftNeg;80}81LEFTOUT=Period/100*speed;82}83voidMotorRight(intspeed,intdirection)84{85if(speed>max_speed)speed=max_speed;8687if(direction==backward)//反转88{89EnableRightNeg;90UnenableRightPos;91}92elseif(direction==foreward)//正转93{94EnableRightPos;95UnenableRightNeg;96}97RIGHTOUT=Period/100*speed;98}99voidMotorDrive(intPIDout)100{101intspeedleft,speedright;102speedleft=Basic_LeftPIDout;103speedright=Basic_Right-PIDout;104105if(speedleft<0)106MotorLeft(speedleft,backward);//反转107elseMotorLeft(speedleft,foreward);//正转108109if(speedright<0)110MotorRight(speedright,backward);//反转111elseMotorRight(speedright,foreward);//正转112}113voidRangle(floatangle)114{115//TBCTL|=TBCLR;116TBCCR1=LMAX(unsignedint)(angle*one);117}[cpp]viewplaincopy118//下面是小车的程序。。用定时器A来输出两路PWM波。119//选用输出模式7(首先输出高电平。在TAR=CCRX时,电平自动变低。这样可以输出任意占空比的PWM波)120//传感器接在P5口,电机的驱动接在P3口。121#include"Macro.h"122#include"motor.h"123typedefstructp124{125floaterror_1;//上次的误差126floatsumerror;127}PID;128PIDPid;129PID*pid=&Pid;130uchardetection,sample=0,corner,k,flag,tt=0;131intweight1[]={-8,-5,-3,-2,0,2,3,5,8};//传感器的权值132floatsensorin;133intnum,tag=1;134voidIO_inti()//io口初始化135{136P1SEL|=BIT2BIT3;//p1为定时器输出端。137P1DIR|=BIT2BIT3;//设置为输出模式。P5为传感器输入。138P3DIR|=BIT0BIT1BIT2BIT3BIT4;//0,1,2,3,作为电机控制的输出端,其他的作为传感器的输入端139P5DIR=0X00;//P5为传感器输入端。设置P5为输入端。普通IO口。140P5SEL=0x00;141142143P2DIR=0X00;//输入144145P2IE|=BIT3;146P2IES|=BIT3;147148P6DIR=0XFF;//纯粹是为了调试用的。149P6OUT=0X00;150151}152voiddelay(intt)153{154unsignedinti;155while(t--)156{157i=65535;158while(i--);159}160}161voidCLK_inti()//时钟初始化162{163BCSCTL1=0X00;//打开XT2164do{165IFG1&=~OFIFG;166for(inti=0x20;i>0;i--);167}while((IFG1&OFIFG)==OFIFG);//如果起震失败。。继续起震。知道成功为止168BCSCTL2=0X00;169BCSCTL2|=SELM_2|SELS;//mclk,sMCLK的时钟源为XT2,0分频。170}171172voidPID_inti()//PID初始化173{174pid->error_1=0;175pid->sumerror=0;176}177178voidPWM_inti()//PWM初始化179{180TACTL|=TASSEL_2|ID_0|MC_1|TACLR;//时钟源采用SMCLK,增计数模式。清空TAR,0分频181TACCR0=8000000/F;//0分频。所以Period=8000000/5000182TACCTL1|=OUTMOD_7;//两个PWM输出口的输出模式。183TACCTL2|=OUTMOD_7;//184LEFTOUT=0;185RIGHTOUT=0;186}187188floatabs(floata)189{190returna<0-a:a;191}192193voidtimer_inti()//用定时器B1来确定采样周期194{195TBCTL|=TBSSEL_2|CNTL_0|ID_3|MC_1|TBCLR;//定时器B:SMCLK,16位,8分频,增计数196TBCCTL0|=CCIE;//使能timerb1的中断。197TBCCR0=5000;//八分频,1MHz,5000表示5ms。198_EINT();//打开总中断。199}200voidnum_inti()201{202sensorin=0;203num=0;204}205voidinti()//总初始化函数206{207PID_inti();208CLK_inti();209PWM_inti();210IO_inti();211timer_inti();212num_inti();213}214floatReadSensor2()215{216intstate=0,i,num=0;217floatsum=0;218staticfloatoutput=0;219state=P3IN&0X80;220state<<=1;221state|=P5IN;222for(i=0;i<9;i)223if(((1<0&&num<7)//不是全白或者全黑229{230corner=0;231if(num>=3)232{233corner=1;234if(sum>0)235output=10;236elseoutput=-10;237returnoutput;238}239output=sum/num;240}241elseif(num>=7)//如果是全黑。242{243flag;244returnoutput=0;245}246returnoutput;247}248voidAvoid(inttime,intlspeed,intldirection,intrspeed,intrdirection)249{250intstate=0;251MotorLeft(lspeed,ldirection);//左转出去,n252delay(time);253MotorRight(lspeed,ldirection);254MotorLeft(rspeed,rdirection);255do{//右转,直到传感器压线。256state=P3IN&0X80;257state<<=1;258state|=P5IN;259}while(state==0X1FF);260PID_inti();//PID初始化261}262intPIDCal(floaterror)//PID计算。位置式。263{264floatoutput,derror;265266pid->sumerror=error;267268derror=(error-pid->error_1);269270output=error*PCderror*DC;271272pid->error_1=error;273return(int)(output0.5);274}275voidmain(void)276{277WDTCTL=WDTHOLDWDTPW;//closethewatchdog;278inti();279sample=0;280corner=0;281flag=0;282detection=0;283while(1)284{285if(sample)286{287sensorin=ReadSensor2();288if(flag==1)//下坡,用定时器定时。到坡底后减速,只有第一次扫到黑线后才要减速。289{290//P6OUT=0X81;291WDTCTL=(WDTPWWDTTMSELWDTCNTCL);292IE1|=WDTIE;293flag;294continue;295}296if(corner)//如果是直角,297{298intstate=0;299MotorLeft(100,1);//向前冲一段时间,然后再转。避免误判300MotorRight(100,1);301delay_ms(20);302303if(sensorin>0)//如果是右直角304{305MotorLeft(100,1);306MotorRight(30,0);307}308else//如果是左直角309{310MotorRight(100,1);311MotorLeft(30,0);312}313do314{315state=0;316state=P3IN&0X80;317state<<=1;318state|=P5IN;319}while(state==0x1ff);320continue;321}322MotorDrive(PIDCal(sensorin));323sample=0;324}325elseif(!(P2IN&0X10)&&!detection)//壁障326{327detection=1;328P6OUT=0XF0;329MotorLeft(100,0);330MotorRight(100,0);331delay_ms(50);332if(pid->sumerror<0)333Avoid(5,60,0,100,1);//先左转,再右转334elseAvoid(5,100,1,60,0);//先右转,再左转335}336}337}338#pragmavector=TIMERB0_VECTOR339__interruptvoidTimerB()//控制采样周期,大概10ms,中断正常。。340{341sample=1;342}343#pragmavector=PORT2_VECTOR//用外部中断来判断并且处理避障344__interruptvoidOutside()//最后的抓取。用定时器B才给舵机输出PWM波345{346if(P2IFG&BIT3)347{348P2IFG&=~BIT3;349//P6OUT=0X07;350TBCTL|=TBSSEL_2|CNTL_0|ID_3|MC_1|TBCLR|TBIE;//定时器B:SMCLK,16位,4分频,增计数351TBCCR0=39999;//0分频。所以Period=8000000/500=16000352TBCCTL1|=OUTMOD_7;//两个PWM输出口的输出模式。353TBCCR1=0;354P4DIR|=BIT1;355P4SEL|=BIT1;356MotorLeft(0,1);//停止357MotorRight(0,1);358delay_ms(50);//延迟359Rangle(0);//夹住东西360delay_ms(300);361MotorLeft(100,0);//后退362MotorRight(100,0);363delay_ms(200);//延迟364MotorLeft(0,0);365MotorRight(0,0);366while(1);//死循环,结束。367}368}369#pragmavector=WDT_VECTOR370__interruptvoidwdtch()371{372staticintxx=0;373if(xx==300)374{375//P6OUT=0x01;376MotorLeft(100,0);377MotorRight(100,0);378delay_ms(160);379WDTCTL=WDTHOLDWDTPW;380IE1&=~WDTIE;381xx=0;382tag=0;383}384}
本文档为【智能循迹小车详细源代码程序MSPID】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: ¥18.0 已有0 人下载
最新资料
资料动态
专题动态
个人认证用户
is_483825
暂无简介~
格式:doc
大小:374KB
软件:Word
页数:0
分类:企业经营
上传时间:2021-09-15
浏览量:62