1、PID算法Matlab仿真程序和C程序精品文档增量式PID控制算法Matlab仿真程序设一被控对象G(s)=50/(0.125s2+7s),用增量式PID控制算法编写仿真程序(输入分别为单位阶跃、正弦信号,采样时间为1ms,控制器输出限幅:-5,5,仿真曲线包括系统输出及误差曲线,并加上注释、图例)。程序如下clear all;close all; ts=0.001;sys=tf(50,0.125,7, 0);dsys=c2d(sys,ts,z);num,den=tfdata(dsys,v); u_1=0.0;u_2=0.0;y_1=0.0;y_2=0.0;x=0,0,0;error_1=0;
2、error_2=0;for k=1:1:1000time(k)=k*ts; S=2;if S=1 kp=10;ki=0.1;kd=15; rin(k)=1; %Step Signalelseif S=2 kp=10;ki=0.1;kd=15; %Sine Signal rin(k)=0.5*sin(2*pi*k*ts); end du(k)=kp*x(1)+kd*x(2)+ki*x(3); %PID Controlleru(k)=u_1+du(k);%Restricting the output of controllerif u(k)=5 u(k)=5;endif u(k)=110 u(k)
3、=110;endif u(k)=10 u(k)=10;endif u(k)=-10 u(k)=-10;endu_5=u_4;u_4=u_3;u_3=u_2;u_2=u_1;u_1=u(k);y_3=y_2;y_2=y_1;y_1=yout(k);error_1=error(k);endfigure(1);plot(time,rin,b,time,yout,r);xlabel(time(s);ylabel(rin,yout);figure(2);plot(time,u,r);xlabel(time(s);ylabel(u);figure(3);plot(time,rin-yout,r);xlab
4、el(time(s);ylabel(error);figure(4);bode(Q,r);dcgain(Q);C语言PID演示程序#include #includetypedef struct PID double Command; /输入指令 double Proportion; /比例系数 double Integral; /积分系数 double Derivative; /微分系数 double preErr; /前一拍误差 double sumErr; /误差累积PID;double PIDCale(PID *p,double feedback) double dErr,Err; Er
5、r=p-Command-feedback; /当前误差 p-sumErr+=Err; /误差累加 dErr=Err-p-preErr; /误差微分 p-preErr=Err; return(p-Proportion*Err /比例项 +p-Derivative*dErr /微分项 +p-Integral*p-sumErr); /积分项void PIDInit(PID *p) memset(p,0,sizeof(PID); /初始化typedef struct motor double lastY; double preY; double lastU; double preU;motor;voi
6、d motorInit(motor *m) memset(m,0,sizeof(motor);double motorCal(motor *m,double u) double y=1.9753*m-lastY-0.9753*m-preY+0.00003284*u+0.00006568*m-lastU+0.00003284*m-preU;/二阶系统 m-preY=m-lastY; m-lastY=y; m-preU=m-lastU; m-lastU=u; return y;void main() FILE *fp=fopen(data.txt,w+); PID sPID; motor m_mo
7、tor; int k=0; double u; double y=0; PIDInit(&sPID); sPID.Proportion=2; sPID.Derivative=1; sPID.Integral=0.00001; sPID.Command=10; motorInit(&m_motor); while(k=1000) fprintf(fp,%d 设定值=%f 被控量=%f 偏差=%f 控制量=%fn,k,sPID.Command,y,sPID.Command-y,u); u=PIDCale(&sPID,y); y=motorCal(&m_motor,u); k+; printf(%f
8、n,y); fclose(fp);增量式PID控制C语言代码增量式PID控制C语言代码 / 定义PID参数结构体/typedef struct PID /结构体定义 int SetPoint /设定值 int Proportion; / Proportion 比例系数 int Integral; / Integral 积分系数 int Derivative; / Derivative 微分系数 int LastError; / Error-1 前一拍误差 int PreError; / Error-2 前两拍误差 PID;main() PID vPID; /定义结构变量名PIDInit ( &
9、vPID ); /Initialize StructurevPID.Proportion = 10; /Set PID Coefficients vPID.Integral = 10; / Set PID Integral vPID.Derivative = 10; / Set PID Derivative vPID. SetPoint = /根据实际情况设定 while(1) Verror=Measure(); /得到AD的输出值 Error =vPID. SetPoint- Verror; /与设定值比较,得到误差值tempi=PIDCal(&vPID, Error; laser.Valu
10、e+=tempi; / Value与Num2为共同体,共同体名laserLASERH=laser.Num0;LASERL=laser.Num1; /Title:PID参数初始化/Description: Proportion=0/ Integral=0/ LastError=0/Input: PID的P、I控制常数和之前的误差量(PID *pp)/Return: /void PIDInit (PID *pp) /PID参数初始化,都置0 memset ( pp,0,sizeof(PID);/memset()的函数, 它可以一字节一字节地把整个数组设置为一个指定的值。/ memset()函数在m
11、em.h头文件中声明,它把数组的起始地址作为其第一个参数,/第二个参数是设置数组每个字节的值,第三个参数是数组的长度(字节数,不是元素个数)。/其函数原型为:void *memset(void*,int,unsigned);/头文件 /Title:增量式PID算法程序/Description:给出一个误差增量/Input: PID的P、I控制常数和之前的误差量(PID *pp)& 当前误差量(ThisError)/Return: 误差增量templ/int PIDCal( PID *pp, int ThisError ) /增量式PID算法(需要控制的不是控制量的绝对值,而是控制量的增量) i
12、nt pError,dError,iError;long templ; pError = ThisError-pp-LastError; iError = ThisError; dError = ThisError-2*(pp-LastError)+pp-PreError; /增量计算 templ=pp-Proportion*pError + pp-Integral*iError+pp-Derivative*dError; /增量 /存储误差用于下次运算 pp-PreError = pp-LastError; pp-LastError = ThisError; return (int)(templ8);收集于网络,如有侵权请联系管理员删除