# RBF-PID

%Adaptive PID control based on RBF Identification
clear all;
close all;

xite=0.5;          %学习速率xite
alfa=0.05;          %动量因子alfa
% beta=0.01;          %
x=[0,0,0]';         %输入向量x

ci=zeros(3,6);      %第i个节点的中心矢量C
bi=10*ones(6,1);    %第i个节点的基宽向量B
w=0.10*ones(6,1);   %权值向量W

h=[0,0,0,0,0,0]';   %径向基向量h

ci_1=ci;ci_3=ci_1;ci_2=ci_1;    %c(i-1),c(i-3),c(i-2)，迭代用
bi_1=bi;bi_2=bi_1;bi_3=bi_2;    %b(i-1),b(i-2),b(i-3)
w_1=w;w_2=w_1;w_3=w_1;          %w(i-1),w(i-2),w(i-3)

u_1=0;y_1=0;                    %u(k-1),y(k-1)，迭代用
xc=[0,0,0]';                    %增量式PID的输入
error_1=0;error_2=0;            %error(k-1),error(k-1)，迭代用
kp0=0.01;ki0=0.01;kd0=0.01;
% kp0=0.003;ki0=0.34;kd0=0.01;

pidu_1=0;pidy_1=0;
pidxc=[0,0,0]';
piderror_1=0;piderror_2=0;
% pidkp=0.003;pidki=0.34;pidkd=0.01;
pidkp=0.01;pidki=0.01;pidkd=0.01;

kp_1=kp0;
kd_1=kd0;
ki_1=ki0;

xitekp=0.15;
xitekd=0.15;
xiteki=0.15;

% ts=0.001;
ts=0.01
for k=1:1:2000
time(k)=k*ts;
%     yd(k)=1;
%阶梯响应
%    if mod(floor(k/100),3)==0
%        yd(k)=1;
%    elseif mod(floor(k/100),3)==1
%        yd(k)=0;
%    else
%        yd(k)=-1;
%    end

%单位正弦响应
%    yd(k)=sin(time(k));              %预期输出yd
%复合简弦响应
yd(k)=sin(time(k))*cos(2*time(k));
% yd(k)=3*sin(time(floor(k/10)+1))-2*cos(time(floor(k/10)+1));

%    y(k)=(-0.1*y_1+u_1)/(1+y_1^2);
y(k)=(-0.1*y_1+u_1)/(1+y_1^2);  %Nonlinear plant

for j=1:1:6
h(j)=exp(-norm(x-ci(:,j))^2/(2*bi(j)*bi(j)));  %计算径向基向量h
end
ym(k)=w'*h;                      %辨识器网络输出ym

d_w=0*w;
for j=1:1:6
d_w(j)=xite*(y(k)-ym(k))*h(j);%计算deta_w供迭代计算下次w
end
w=w_1+d_w+alfa*(w_1-w_2);        %权值向量w调整

d_bi=0*bi;
for j=1:1:6
d_bi(j)=xite*(y(k)-ym(k))*w(j)*h(j)*(bi(j)^-3)*norm(x-ci(:,j))^2;%计算deta_b供迭代计算下次第i个节点的基宽向量
end
bi=bi_1+ d_bi+alfa*(bi_1-bi_2);  %基宽向量B调整
for j=1:1:6
for i=1:1:3
d_ci(i,j)=xite*(y(k)-ym(k))*w(j)*h(j)*(x(i)-ci(i,j))*(bi(j)^-2);%计算deta_ci供迭代计算下次第i个节点的中心矢量ci
end
end
ci=ci_1+d_ci+alfa*(ci_1-ci_2);   %中心矢量调整
%%%%%%%%%%%%%%%%%%%%%%Jacobian%%%%%%%%%%%%%%%%%%%%%%%
yu=0;
for j=1:1:6
yu=yu+w(j)*h(j)*(-x(1)+ci(1,j))/bi(j)^2;
end
dyu(k)=yu; %被控对象的Jacobian信息
%%%%%%%%%%%%%%%%%%%%%%Start of Control system%%%%%%%%%%%%%%%%%%
error(k)=yd(k)-y(k);
%用梯度下降法调整P,I,D
kp(k)=kp_1+xitekp*error(k)*dyu(k)*xc(1);
kd(k)=kd_1+xitekd*error(k)*dyu(k)*xc(2);
ki(k)=ki_1+xiteki*error(k)*dyu(k)*xc(3);
if kp(k)<0
kp(k)=0;
end
if kd(k)<0
kd(k)=0;
end
if ki(k)<0
ki(k)=0;
end

M=1;
switch M
case 1
case 2  %Only PID Control
kp(k)=kp0;
ki(k)=ki0;
kd(k)=kd0;
end
du(k)=kp(k)*xc(1)+kd(k)*xc(2)+ki(k)*xc(3);%PID输出的调整量deta_u
u(k)=u_1+du(k);%计算下次的输出
if k==300     % Adding disturbance(1.0v at time 0.3s)
u(k)=u(k)+0.5;
end
%    if u(k)>=10
%    u(k)=10;
%    end
%    if u(k)<=-10
%    u(k)=-10;
%    end

%Return of parameters
x(1)=du(k);
x(2)=y(k);
x(3)=y_1;%RBF网络的输入向量

%量的迭代
u_1=u(k);
y_1=y(k);

ci_3=ci_2;
ci_2=ci_1;
ci_1=ci;

bi_3=bi_2;
bi_2=bi_1;
bi_1=bi;

w_3=w_2;
w_2=w_1;
w_1=w;

xc(1)=error(k)-error_1;             %Calculating P
xc(2)=error(k)-2*error_1+error_2;   %Calculating D
xc(3)=error(k);                     %Calculating I

error_2=error_1;
error_1=error(k);

kp_1=kp(k);
kd_1=kd(k);
ki_1=ki(k);

%传统的Pid控制
pidy(k)=(-0.1*pidy_1+pidu_1)/(1+pidy_1^2);%传统PID控制的输出
piderror(k)=yd(k)-pidy(k);
piddu(k)=pidkp*pidxc(1)+pidkd*pidxc(2)+pidki*pidxc(3);%增量式PID计算
pidu(k)=pidu_1+piddu(k);%传统PID计算PID输出
if k==300     % Adding disturbance(1.0v at time 0.3s)
pidu(k)=pidu(k)+0.5;
end
%     if pidu(k)>=10
%        pidu(k)=10;
%     end
%     if pidu(k)<=-10
%        pidu(k)=-10;
%     end

pidu_1=pidu(k);
pidy_1=pidy(k);
pidxc(1)=piderror(k)-piderror_1;             %Calculating P
pidxc(2)=piderror(k)-2*piderror_1+piderror_2;   %Calculating D
pidxc(3)=piderror(k);                     %Calculating I

piderror_2=piderror_1;
piderror_1=piderror(k);
end
if M==1
figure(1);
% % plot(time,yd,'r',time,y,'k:','linewidth',2);
% xlabel('time(s)');ylabel('yd,y');
% legend('ideal position','position tracking');
% figure(2);
% plot(time,y,'r',time,ym,'b','linewidth',2);
% xlabel('time(s)');ylabel('y,ym');
% figure(3);
% plot(time,dyu,'r','linewidth',2);
% xlabel('time(s)');ylabel('Jacobian value');
% figure(4);
% subplot(311);
% plot(time,kp,'r','linewidth',2);
% xlabel('time(s)');ylabel('kp');
% subplot(312);
% plot(time,ki,'r','linewidth',2);
% xlabel('time(s)');ylabel('ki');
% subplot(313);
% plot(time,kd,'r','linewidth',2);
% xlabel('time(s)');ylabel('kd');
% elseif M==2
% figure(1);
% plot(time,yd,'r',time,y,'k:','linewidth',2);
plot(time,yd,'r',time,y,'g',time,pidy,'b');
xlabel('time(s)');ylabel('yd,y');
legend('ideal position','RBF tracking','PID tracking');
figure(2);
plot(time,y,'r',time,ym,'b');
xlabel('time(s)');ylabel('y,ym');
figure(3);
plot(time,dyu,'r');
xlabel('time(s)');ylabel('Jacobian value');
figure(4);
subplot(311);
plot(time,kp,'r');
xlabel('time(s)');ylabel('kp');
subplot(312);
plot(time,ki,'r');
xlabel('time(s)');ylabel('ki');
subplot(313);
plot(time,kd,'r');
xlabel('time(s)');ylabel('kd');
figure(5);
subplot(211);
plot(time,error,'r');
xlabel('time(s)');ylabel('error');
subplot(212);
plot(time,piderror,'r');
xlabel('time(s)');ylabel('piderror');
elseif M==2
figure(1);
plot(time,yd,'r',time,y,'k:');
xlabel('time(s)');ylabel('yd,y');
legend('ideal position','position tracking');
end

©️2020 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。