- 论坛徽章:
- 0
|
clear all;
close all;
ts=0.001;
J=1/133;
q=25/133;
sys=tf(1,[J,q,0]);
dsys=c2d(sys,ts,'z');
[num,den]=tfdata(dsys,'v');
r_1=0;r_2=0;
u_1=0;u_2=0;
y_1=0;y_2=0;
error_1=0;
ei=0;
for k=1:1:1000
time(k)=k*ts;
s=2;
if s==1 %Step Signal
rin(k)=1.0;
elseif s==2 %Sine Signal
rin(k)=1.0*sin(1*2*pi*k*ts);
end
yout(k)=-den(2)*y_1-den(3)*y_2+num(2)*u_1+num(3)*u_2;
error(k)=rin(k)-yout(k);
derror(k)=(error(k)-error_1)/ts;
ap=22;bp=8.0;cp=0.8;
kp(k)=ap+bp*(1-sech(cp*error(k)));
ad=0.5;bd=2.5;cd=6.5;dd=0.30;
kd(k)=ad+bd/(1+cd*exp(dd*error(k)));
ai=1;ci=1;
ki(k)=ai*sech(ci*error(k));
ei=ei+error(k)*ts;
u(k)=kp(k)*error(k)+kd(k)*derror(k)+ki(k)*ei;
%Update Parameters
r_2=r_1;r_1=rin(k);
u_2=u_1;u_1=u(k);
y_2=y_1;y_1=yout(k);
error_1=error(k);
end
figure(1);
plot(time,rin,'b',time,yout,'r');
xlabel('time(s)');ylabel('error');
figure(2);
plot(time,derror,'r');
xlabel('time(s)');ylabel('derror');
M=1;
if M==1
figure(3);
plot(error,kp,'r');xlabel('error');ylabel('kp');
figure(4);
plot(error,kp,'r');xlabel('error');ylabel('kd');
ad+bd/(1+cd)
figure(5);
plot(error,ki,'r');xlabel('error');ylabel('ki');
elseif M==2
figure(4);
plot(time,kp,'r');xlabel('time(s)');ylabel('kp');
figure(5);
plot(time,kd,'r');xlabel('time(s)');ylabel('kd');
figure(6);
plot(time,ki,'r');xlabel('time(s)');ylabel('ki');
end |
|