算机控制技术与系统课程设计报告--DDC单回路PID闭环控制系统的设计及实时仿真.docx
课程设计报告(20132014年度第2学期)名称:计算机控制技术与系统题目:DDC单回路PID闭环控制系统的设计及实时仿真PID飞升曲线绘制程序#include<graphics.h>#include<stdio.h>#include<dos.h>#include<conio.h>*理想PID参数定义*/*比例增益1.0*/*采样周期1.0$*/*积分时间10.0s*/*微分时间5.0s*/*惯性时间0.0s*/*增量型PID运算系数*/#defineKpl1.0#defineTl1#defineTil10.0#defineTdl5.0#defineTfl0#defineCll(Tfl(Tl+Tfl)NdefineC21(Kpl*Tl*(l+Tl/TiI+TdlTl)(Tl+Tfl)#defineC31(0-Kpl*Tl*(l+2*TdlTl)(Tl+Tfl)#defineC41(Kpl*Tdl(Tl+Tfl)/*实际PID参数定义*/*比例增益1.0*/*采样周期LOs*/*积分时间IOQs*/*微分时间5.0s*/*惯性时间2.0s*/*增量型PlD运算系数*/defineKp21.0#defineT21.0#defineTi210.0#defineTd25.0#defineTf22ttdefineC12(Tf2(T2+Tf2)defineC22(Kp2*T2*(l+T2Ti2+Td2T2)(T2+Tf2)#defineC32(O-KP2*T2*(l+2*Td2T2)(T2+Tf2)#defineC42(Kp2*Td2(T2+Tf2)/*坐标轴定义*/defineox40#defineoy440#definexx620ttdefinexy440#defineIenx580#defineIeny400#defineyx40ttdefineyy20*定义作图区域,/#defineleft20Adefinetop20Sdefineright620definebottom460*坐标轴注释*/Adefinexte×tlx450Sdefinexte×tly450defineytextlx10ftdefineytextIy65Adefinexte×t2×610#definextext2y450#defineytext2x10#defineytext2y20*坐标子程序/voidDrawAxis(Void)i11ti;setbkcolor(7);setcolor(5);Iine(OX,oy,xx,xy);line(xx-5,xy-5,xx,×y);Iine(XX,xy,xx-5,xy+5);Iine(OX,oy,yx,yy);line(y×-5,yy+10,yx,yy);line(yx+5,yy+10,yx,yy);*原点横坐标*/*原点纵坐标*/*x轴顶点横坐标*/*x轴顶点纵坐标*/*横轴长*/*纵轴长*/*y轴顶点横坐标*/*y轴顶点纵坐标*/*背景颜色*/*线条颜色*/*x-axis*/*画X轴箭头"/*y-a×is*/*画y轴箭头*/for(i=0;i<51;i+)(line(o×+10*i,oy,o×+10*i,oy-10);line(ox+10*i+5,o½ox+10*i+5,oy-5);)for(i=l;i<=10;i+)line(ox,oy-40*i,o×+10,oy-40*i);/*标X轴刻度值*/outtextxy(ox+50*0-7,oy+20,"0");/*刻度值值表示点*/outtextxy(ox+50*l-7,oy+20,"5");outtext×y(ox+50*2-7,oy+20,"10");outtext×y(o×+50*3-7,oy+20,15");outte×t×y(ox+50*4-7,oy+20,20");outtext×y(ox+50*5-7zoy+20,"25");outtext×y(ox+50*6-7,oy+20,"30");outtext×y(o×+50*7-7,oy+20,"35");outtext×y(o×+50*8-7,oy+20,"40");outte×t×y(ox+50*9-7,oy+20,"45");outtext×y(ox+50*10-7,oy+20,"50");/*标y轴刻度值*/outte×t×y(o×-20,oy-401,1");outtext×y(o×-20,oy-40*2,2");outtext×y(o×-20,oy-403,"3");outtext×y(o×-20,oy-40*4,"4");outtext×y(o×-20,oy-40*5,"5");outtext×y(o×-20,oy-40*6,"6");outtextxy(ox-20,oy-407,"7");outtext×y(o×-20,oy-408,"8");outte×t×y(o×-20,oy-40*9,"9");outtextxy(ox-20,oy-40*10,10");settextstyle(SMALL_FONTHORIZ_DIR,5);/*坐标轴坐标量注释*/OUtteXtXy(XteXtlX,XtextlyJTime");outtextxy(xtext2x,xte×t2½"ts");SetteXtStyIe(SMALL_F0N1VERT_DIR,5);oUttextxy(ytextlx,ytextly,"Theoutput(Response)");outtextxy(ytext2xzyte×t2½"UV");)voidlnitial_Sys(void)*初始化*/intgdriver=DETECTzgmode;initgraph(&gdriver,&gmode,"C:TC20HtcBGI");/*pD运算*L*/floatPIDl(floatNew,floatSp)*理想*/staticfloatel3=0.0,0.0,0.0,ul=0.0,dul=0.0;el2=el(l);*数据更新*/ell=el(01;elO=Sp-New;dul=Cll*dul+C21*el0+C31*ell+C41*el2;*PID运算*/ul=ul+dul;returnul;floatPID2(floatNewJIoatSP)*实际*/staticfloate23=0.0,0.0,0.0,u2=0.0,du2=0.0;e22=e2l);*数据更新*/e2l=e20;e20=Sp-New;du2=C12*du2+C22*e20+C32e2l+C42*e22;u2=u2+du2;returnu2;)*绘图*/voiddraw(floata)intk=5;*x轴间距*/int×l,yl,x,y;inti;xO=O;y0=(int)(a0*25);*规整*/for(i=l;i<100;i+)xl=x+k;yl=(int)(ai*25);delay(100);setcolor(14);/*标注*/settextstyle(SMALL_F0NT,H0RIZ_DIR,5);outtextxy(600,20,"");Iine(ox+x0,oy-y0,ox+xlzoy-y0);line(ox+xlzoy-yO,ox+xl,oy-yl);setcolor(14);outtextxy(620,20,Sp");line(ox+xO,oy-25,ox+xl,oy-25);×O=xl;/*数据更新*/y=yi;)voiddraw2(floatb)intk=5;*x轴间距*/intxlzyl,x,y;inti;xO=O;y0=(int)(b0*25);for(i=l;i<100;i+)×l=x+k;yl=(int)(bi*25);delay(100);setcolor(6);OUttextXy(600,40,"U'");Iine(ox+x0,oy-y0,o×+xl,oy-y0);line(ox+xl,oy-yO,o×+×l,oy-yl);setcolor(6);OUtteXtXy(620,40,"Sp"');line(o×+x0,oy-25,ox+xl,oy-25);×O=xl;*数据更新*/y=yi;)*主程序*/voidmain(void)floata100;floatb100;inti;for(i=0;i<100;i+)(ai=PIDl(O.O,lO);)lnitial-Sys();DrawAxis();draw(a);for(i=0;i<100;i+)(bi=PID2(0.040);)draw2(b);)getch();实际采样显示控制程序/*头文件定义/#include"stdio.h"#include"math.h"#include"graphics.ht,*forgraphdriverinstalling,onlycanbecalledinTurboC*/#include"string.h"#include"dos.h"#include"bios.h"#include"conio.h"*中断程序头文件*/#include"stdlib.h"#include"io.h"#include"math.h"*按键地址定义7doublekey_ESC=0x011b;*definecannotsuitthelengthofbioskey*/doublekey-E=0xl265;doublekey-A=0xle61;doublekey-H=0×2368;doublekey-U=0×1675;doublekey-D=0×2064;doublekey_l=0x1769;doublekey-P=0xl970;doublekey_up=0x4800;doublekey_down=0x5000;doublekeyJeft=0×4b00;doublekey-right=0×4d00;doublekey-pgup=0x49;doublekey_pgdown=0x5100;*PLCD780基地址定义*/# defineBASE0x220*pleaselookupinPCL812PGdocument*/# defineREG1/定义绘图坐标*/# defineox40*原点横坐标*/# defineoy240/*原点纵坐标*/# definexx600/*X轴顶点横坐标一*/define×y240/*×轴顶点纵坐标一*/# defineyx40/*y轴顶点横坐标*/defineyy40/*y轴顶点纵坐标-*/*PID参数定义*/floatKp=I.2;floatTi=30.0;*9,0.67floatTd=4.5;floatTf0=5;floatTf=5;*理想PID参数*/floatT=0.2;/采样时间间隔*/floatad,e;floatu=0.0;floatpv=0.0;floatsp=0.0;charA-H='H*;*PID手自动状态标志/charmanu;intkey=0;inttime_counter=0;/*中断次数*/intSpt=O;/*-采样计数intst_time=500;/*采样次数总和(X轴范围)*/intround=0;voidinterrupt(*faddlC)(void);/*申明中断向量faddlC*/voidinterruptINT_lC(void);/*8259中断控制函数*/intscankey();/*绘图初始化*/voidlnitial_Sys(void);voida×is(introundl);voidDrawline(introundl,intcj,floatpvl,floatspl,floatul,floatel);*PID设置及运算»/voidPlDsetfvoid);floatPID(floatspl,floatpvl,floatKpl,floatTilJIoatTdlJIoatTfl,charA_H!,floatTl);*程序实现负增益三阶惯性环节*/floatObject(floatu!,floatTl);float AD-convert(unsigned char channal); void DA_convert(float u);*A/D采样子函数/*D/A采样子函数*/floatDelayAction(floatyO);*displayanddrawing*/initiategraphdisplay*/voidlnitial_Sys(void)(intGraphDriver;intGraphMode;detectgraph(&GraphDriver,&GraphMode);*确定图形驱动程序和模式*/initgraph(&GraphDriver,&GraphMode,"C:TC201EBGI");/*初始化图形系统(前两项为指针,后一项为路径)/)*drawbasiccoordinateaxis*/voida×is(introundl)(inti;chars3;/*注意刻度位置*/lnitial_Sys();setbkcolor(15);*whiteblackl5*/setcolor(9);*linghtblue*/printf("nnnnnnnnnnnnnnnnnnnnnnnnnnnnn");printf("NOTEPID-Pl,Mode-AH,spu-UD,Kp-updnJi-leftright,Td-pguppgdn");rectangle(10,35,630,465);/*绘制矩形,对角顶点坐标(10,35),(630,465)*/line(ox,oy,xx,xy);/*画y轴,始点坐标(OX,oy),终点坐标(xx,xy)*/Iine(XX-5,xy-5,xx,xy);Iine(XX,xy,xx-5,xy+5);line(ox,oy+200,yx,yy);line(yx-5,y+5,yx,yy);Iine(yx+5,yy+5,y×,yy);settextstyle(2,l,5);/*设置文本属性:字体Smallfont,横排,大小5*/OUtteXtXy(20,150,"TheOutput(Response)");Outtextxy(20,40,"U(t)V")z输出文本-*/settextstyle(2,0,5);outtext×y(300,455,"Time");outtextxy(590,455,"tsec");setlinestyle(l,O,l);/*设置画线类型(dotline,none,width)*/for(i=-5;i<=5;i+)/*-eachinportstartingposition*/line(ox,oy-40*i,o×+500,oy-40*i);for(i=-5;i<=5;i+)(sprintf(s,"%d",i);oUttextxy(ox-8,oy-40*i,s);)for(i=l;i<=500/50;i+)/*scale*/line(o×+50*i,oy+200,ox+50*i,oy-200);*lT=10,thatisIOpx=Isec*/for(i=l;i<=500/10;i+)line(ox+10*i,oy,ox+10*i,oy-10);setlinestyle(O,O,l);*realline,none,width*/for(i=0;i<=10;i+)(sprintf(s,"%d",10*i+100*roundl);if(roundl=0)outtextxy(ox-8+i*50,oy+2,s);elseif(roundl>0)outtextxy(ox-12+i*50,oy+2,s);*line-drawingandnotes*/voidDrawlinefintroundl,intcjl,floatpvl,floatspljloatul,floatel)(staticintcj-O=O,cj-l=O,pv-O=O.O,pv-l=O.O,sp-O=O.O,sp-l=O.O,u-O=O.O,u-l=O.O,e-O=O.O,e-l=O.O;cj-l=cjl-500*roundl;*Xcoordinate*/if(cj_l=O)cj_O=O;pv_l=(int)(pvl*40);sp_l=(int)(spl*40);u_l=(int)(ul*40);e_l=(int)(el*40);setcolor(2);/*green*/line(ox+cj_0,oy-pv_0,ox+cj_l,oy-pv_l);outtextxy(590,40,"pv");setcolor(8);*darkgray*/Iine(OX+cj_O,Oy-SP_0,ox+cj_l,Oy-SP_1);outtextxy(590,55,"sp");setcolor(3);/*cyan*/line(ox+cj_0,oy-u_0,ox+cj_l,oy-u_l);outtext×y(590,70,"u");setcolor(4);/*red*/line(ox+cj_0,oy-e_0,ox+cj_l,oy-e_l);outte×txy(590,85,"e");CLO=CL1;pv_0=pv_l;sp_O=sp_l;u_0=u_l;e_0=e_l;)*resetinterruptcontroller*/voidinterruptINT_lC(void)(time-counter+;outportb(0x20,0×20);*重置8259中断控制器,表明中断结束*/)*keyboardinputdetection*/intscankey(void)(intkey;keyO=bioskey(l);/*查询是否按下按犍,按下则返回非零值*/if(keyO!=O)key0=bioskey(0);/*返回按键键值*/returnkey;/*按键控制PID子程序/voidPIDset(void)(key=scankey();/*按键扫描*/if(A-H='H')(if(key=key_up)/手动状态通过按键tI改变比例增益kp*/Kp+=0.2;elseif(key=key_down)Kp-=0.2;elseif(key=keyjeft)/*手动状态通过按键-改变积分时间Ti*/Ti-=0.2;elseif(key=key_right)Ti+=0.2;elseif(key=key_pgup)*手动状态通过按键pgupgdn改变微分时间Td*/Td+=0.2;elseif(key=key-pgdown)Td-=0.2;elseif(key=key_U)/手动状态通过按键UD改变阶跃输出/manu=,÷'elseif(key=key-D)manu='-')if(A-H='A,)(if(key=key_U)/*自动状态按键UD改变设定值sp/sp+=l;if(key=key_D)sp-=l;if(key=key,Ekey=key,ESC)/按键ESCE控制程序退出*/closegraph();exit(l);if(key=key_A)A-H='A'/*手自动标志*/if(key=key_H)A-H=,H,;if(key=key_l)Tf=O;/*理想,实际PlD标志*/if(key=key-P)Tf=TfO;/*配置PID模块子程序*/floatPID(floatSplJIoatpvl,floatKpl,floatTil,floatTdl,floatTfl,charA_HIJIoatTl)(intdeath=0.001,A=5,k=;floatdelta-u,uO,Cl,C2,C3,C4;staticfloatelze2,ul,delta-ul;if(Kpl<OTil<OTdl<O)printf("error.");else(Cl=Tfl(Tl+Tfl);C2=Kpl*Tl*(l+TlTil+TdlTl)(Tl+Tfl);C3=-Kpl*Tl*(l+2*TdlTl)(Tl+Tfl);C4=Kpl*Tdl(Tl+Tfl);if(A.Hl=,A,)*按下按键A,PID处于自动状态*/(e=pvl-spl;if(abs(e)<death)return;/死区*/if (fabs(e)>A) k=100;*积分分离算法(偏差较大不积分)/elsek=l;delta_u=Cl*delta_ul+C2*e+C3*el+C4*e2;u=ul+delta-u;if(abs(u)>5)/*抗积分饱和*/k=l;Ti=Ti*k;e2=el;el=e;delta_ul=delta_u;ul=u;returnu;elseif(A-Hl=,H')/*按下按键H,PID处于手动状态if(manu='+')u+=l;if(manu='-')u-=l;ul=u;e=0;spl=pvl;*PID处于手动状态,设定值跟踪PV*/sp=pvl;el=0;e2=0;delta-ul=O;returnu;)/*三阶负增益对象*/floatObject(floatul,floatTl)*G(s)=-3(4.7s+l)3fK=-3,Tpl=Tp2=Tp3=4.7*/staticfloatyl,y2,y3;floaty;y=(103.82*y3-(311.469+66.27*Tl)*y2+(311.469+132.54*Tl+14.1*Tl*Tl)*yl-3*Tl*Tl*Tl*ul)/(103.82+66.27*T1+14.1*T1*T1+T1*T1*T1);y3=y2;y2=yi;yi=y;returny;)*mainprogram*/voidmain(void)disable();*关闭中断*/faddlC=getvect(OxlC);/*取得中断向量入口地址*/setvect(OxlC,INT_lC);/*设置中断向量入口*/enable();/*开启中断*/drawcycle:a×is(round);WhiIe(SPt<sjtime*(round+l)if(spt*T)<(tim-countr18.2)/*定时输出,定时采样*/(ad=AD_convert(0);/对二阶惯性环节输出进行采样*/PV=DelayACtiOn(ad);/*采样值进行延时*/PIDset();U=PlD(SP,pv,Kp,Ti,Td,Tf,A_H,T);DA_convert(u);/pv=0bject(u,T);*/*pv=DelayAction(pv);*/Drawline(round,spt,pv,sp,u,e);/*绘制过度过程曲线*/manu=0;*statusbaatthetopofthescreen*/gotoxy(l,l);*设置光标位置(1,1)*/if(Tf=O)printf("ldealPID,Mode:%cttKp=%.2fJi=%.2fJd=%.2Atn",A_H,Kp,Ti,Td);elseif(Tf>O)printf("ParallalPID,Mode:%ctKp=%.2f,Ti=%.2f,Td=%.2Atn",A_H,Kp,TiJd);elseprintf("ttTfgotawrongvalue!Pleaseexitandrestartthisprogram.n");printf(,tsp=%.2f,pv=%.2f,u=%.2f,error=%.2tr,',sp,pv,u,e);spt+;)round+;if(round<500)gotodrawcycle;closegraph();disable();Setvect(OxlCJaddlC);enable();/*A/D转换子程序*/floatAD_convert(unsignedcharchannal)(floatresult=0;unsignedcharHbyte,Lbyte,DRDY=Ox10;inti;*Initial*/outportb(BASE+ll,l);for(i=0;i<10000;i+);outportb(BASE+10,channal);for(i=0;i<10000;i+);outportb(BASE+9,0);for(i=0;i<10000;i+);*convert*/outportb(BASE+12,0);for(i=0;i<10000;i+);do(DRDY=inportb(BASE+5);)while(DRDY0×10);Hbyte=inportb(BASE+5);for(i=0;i<10000;i+);Lbyte=inportb(BASE+4);result=(Hbyte&0x0F)*256+Lbyte;result=(result-2047)*10.4095.0;returnresult;*D/A转换子程序*/voidDA_convert(floatu)(unsignedcharHbytezLbyte;intoutj;*×ianfu*/if(u>5)u=5;elseif(u<-5)u=-5;out=(int)(u*40955.0);Hbyte=out256;Lbyte=out-Hbyte*256;outportb(BASE,l);for(i=0J<100J+);outportb(BASE+4,Lbyte);for(i=0;i<10000;i+);outportb(BASE+5,Hbyte);)*延时子程序*/floatDelayAction(floaty)(floaty_out;staticfloaty-old10=0;inteye;y_out=y_old10-l;for(cyc=l;cyc<10;cyc+)y_old10-cyc=y_old10-cyc-l;y-old0=yO;returny_out;实际控制效果图Para1IalPID,Mode:AKp=1.Z0,Ti=30.00.Td=4.50NUTE:P1U-P1,oe-HH,spu-U×U,Kp-up/dn,Ti-Ieit/rght,Td-pgup/pgdn三阶负对象动态响应图IdealPID,Mde:HKp=I.4,Ti=ZZ.,Td=4.4tsp=-3.,pu=-3.,u=l.,error=.