您好,欢迎访问三七文档
基于ATmeg16单片机的简易直线单级倒立摆系统设计姓名:专业班级:指导教师:倒立摆系统整体方案图ATmeg16单片机Nokia5110液晶显示器BTS7960驱动器直流电动机摆杆小车WDD35D-4型角位移传感器干扰OMRON编码器PID控制器倒立摆简化物理模型示意图倒立摆的实物模型示意图单级倒立摆的控制算法设计PID公式:其中,t为时间参数,u(t)为输出量,r(t)输入量;err(t)=u(t)-r(t)PID增量式算法(离散化):1()()(()())DTdeertutkperrterrtdtTdt122(1)(1)kkkkTTdTdTduKpeKpeKpeTiTTT12kkkAeBeCeC语言实现:typedefstructPID{intSetPoint;//设定目标DesiredValuelongSumError;//误差累计doubleProportion;//比例常数ProportionalConsdoubleIntegral;//积分常数IntegralConstdoubleDerivative;//微分常数DerivativeConstintLastError;//Error[-1]intPrevError;//Error[-2]}PID;//函数描述:增量式PID控制计算intIncPIDCalc(intNextPoint){intiError,iIncpid;//当前误差iError=sptr-SetPoint-NextPoint;//增量计算iIncpid=sptr-Proportion*iError//E[k]项-sptr-Integral*sptr-LastError//E[k-1]项+sptr-Derivative*sptr-PrevError;//E[k-2]项//存储误差,用于下次计算sptr-PrevError=sptr-LastError;sptr-LastError=iError;//返回增量值return(iIncpid);}PID控制系统LabVIEW程序方框图纯比例调节前面板框图PID调节作用前面板框图ICCV7forAVR编程界面ATmeg16单片机程序烧录界面Main函数清单如下:#includeiom16v.h/*commondefinesandmacros*/#includemacros.h#includeAD.h#includeUSART.h#includePWM.h#include5110.h#typedefuintunsignedint#typedefucharunsignedchar#defineYou_value338#defineZuo_value738#defineheadstand_value688//设定AD值目标DesiredValueintSetPoint=headstand_value;//设定AD值目标intvariation;//AD值变化量intProportion0=3;//完成intProportion1=2;//intProportion2=5;//5intProportion3=3;//3intProportion4=4;//2-8intProportion5=2;intProportion6=8;//2-4intProportion7=8;//2-8intProportion8=10;intProportion9=4;intIntegral=0;//积分常数IntegralintIntegral1=2;intIntegral2=2;intIntegral3=0;intIntegral4=0;intIntegral5=0;intIntegral6=0;intIntegral7=0;intIntegral8=0;intDerivative0=1;//微分常数IntegralConstintDerivative1=1;intDerivative2=1;intDerivative3=0;intDerivative4=0;intDerivative5=5;intDerivative6=0;intDerivative7=0;intDerivative8=0;intLastError=0;//Error[-1]角度intPrevError=0;ucharinversion_flag;//电机反转标志ucharreciprocate_flag=0,delay_flag=0;ucharpulse_flag=0;//往复摆标志charpulse_temp=-1;uintreciprocate_sum=0;uintpulse,Error,a,b,c;uinttemp;intAD0_value,AD0_last,value_deal,speed_value;intLastError=0;//Error[-1]角度intPrevError=0;ucharinversion_flag;//电机反转标志ucharreciprocate_flag=0,delay_flag=0;ucharpulse_flag=0;//往复摆标志charpulse_temp=-1;uintreciprocate_sum=0;uintpulse,Error,a,b,c;uinttemp;intAD0_value,AD0_last,value_deal,speed_value;voidspeed_deal(uintAD_value){variation=SetPoint-AD_value;if(variation=0)inversion_flag=1;variation=-variation;}elseinversion_flag=0;Error=variation;speed_value=a*variation-b*variation*variation/c;//P=4角度0-300(speed_value=0-120)//抛物线speed_value=IncPIDCalc0(speed_value);if(speed_value=2400)speed_value=2400;if(variation300)speed_value=0;/*if(Error160)speed_value=0;*/}voidAD0_transform(){while(!ATD0STAT2L_CCF0);AD0_value=(ATD0DR0H8)+ATD0DR0L;}//-----------------------------------------//增量式PID控制设计速度//-----------------------------------------intIncPIDCalc0(intNextPoint){intiError,iIncpid;//当前误差iError=NextPoint;//增量计算if((0=Error)&&(Error=10)){a=10;//TOP=1500b=1;c=60;Proportion=Proportion0;Integral=Integral0;Derivative=Derivative0;}elseif((10Error)&&(Error=20)){a=10;//TOP=1500b=1;c=60;Proportion=Proportion00;Integral=Integral0;Derivative=Derivative0;}elseif((20Error)&&(Error=30)){//TOP=1200a=8;b=1;c=75;Proportion=Proportion1;Integral=Integral1;Derivative=Derivative1;}elseif((30Error)&&(Error=42)){//TOP=1200a=8;b=1;c=75;Proportion=Proportion10;Integral=Integral10;Derivative=Derivative1;}elseif((45Error)&&(Error=70)){//TOP=600-900a=6;b=1;c=100;Proportion=Proportion2;Integral=Integral2;Derivative=Derivative2;}elseif((70Error)&&(Error=90)){//TOP=900a=6;b=1;c=100;Proportion=Proportion3;Integral=Integral3;Derivative=Derivative3;}elseif((90Error)&&(Error=120)){//TOP=900a=6;b=1;c=100;Proportion=Proportion4;Integral=Integral4;Derivative=Derivative4;}elseif((120Error)&&(Error=140)){a=6;b=1;c=100;Proportion=Proportion5;Integral=Integral5;Derivative=Derivative5;}elseif((140Error)&&(Error=160)){a=6;b=1;c=100;Proportion=Proportion50;Integral=Integral5;Derivative=Derivative5;}elseif((160=Error)&&(Error=200)){Proportion=Proportion6;Integral=Integral6;Derivative=Derivative6;}elseif((200=Error)&&(Error=240)){a=8;b=1;c=75;Proportion=Proportion7;Integral=Integral7;Derivative=Derivative7;}elseif((240=Error)&&(Error=280)){a=10;//TOP=1500b=1;c=60;Proportion=Proportion8;Integral=Integral8;Derivative=Derivative8;}else{a=10;b=1;c=60;Proportion=Proportion9;Integral=Integral8;Derivative=Derivative8;}iIncpid=(Proportion*iError)+(Integral*LastError)-(Derivative*PrevError);//E[k-1]项//E[k]项PrevError=LastError;LastError=iError;return(iIncpid);//返回增量值}voidAD0_transform(){while(!ATD0STAT2L_CCF0);AD0_value=(ATD0DR0H8)+ATD0DR0L;}//-----------------------------------------//增量式PID控制设计速度//-----------------------------------------intIncPIDCalc0(intNextPoint){intiError,iIncpid;//当前误差iError=NextPoint;//增量计算if((0=Error)&&(Error=10)){a=10;//TOP=1500b=1;c=60;Proportion=Proportion0;Integral=Integral0;Derivative=Derivative0;voidmain(void){/*putyourowncodehere*/USART_Init();PWM_init();ATD
本文标题:小车倒立摆答辩
链接地址:https://www.777doc.com/doc-3208394 .html