📄 selftuning_rls_ga.m
字号:
%Selftuning_RLS_GA.m
%Written by Ian Griffin July 2003
%Last Modified 20-08-03
%____________________________________________________________________
clc
clear
global sys_controlled
global time
global Ball_Hoop
global u
global time
global Ball_Hoop_rls
global z%remove
global ROW
global observe_pop
ROW=1;
close all
last_stable=1;
rand('state',0);%keeps using the same initial values
%____________________________________________________________________
%Initalising the PID parameters to ones found in root locus as a
backup
%incase the GA doesn't compile before the step signal initiates.
Discrete_PID_num=[12.97 -23.96 11.07];
Discrete_PID_den=[1 0 -1];
%____________________________________________________________________
%Simulating the Adaptive_GA_RLS_rls simulink file
sim('Adaptive_GA_RLS_rls');
%____________________________________________________________________
%Creating a while loop so the Genetic Algorithm will compute PID
gains indefinately
z=1;
while z<5
sys_unstable=0;
%if z==2
% co=100;
%elseif z==4
%co=50;
%end
%____________________________________________________________________
%Displaying the Iteration number on screen
disp('ITERATION')
disp(z)
%____________________________________________________________________
%Getting the most recent estimates from the RLS estimator
B0=b0(max(size(b0)));
B1=b1(max(size(b1)));
B2=b2(max(size(b2)));
A1=a1(max(size(a1)));
A2=a2(max(size(a2)));
A3=a3(max(size(a3)));
%____________________________________________________________________
%Discrete Ball and Hoop system from RLS estimates
rls_discrete_num=[B0 B1 B2];
rls_discrete_den=[1 -A1 -A2 -A3]; %Multiplying by a minus to make
signs match
disp('RLS Estimation Of Plant')
Ball_Hoop_rls=tf(rls_discrete_num,rls_discrete_den,.01)
disp('')
%rls_continuous_tf=d2c(rls_discrete_tf)
%____________________________________________________________________
%Initialising the genetic algorithm
populationSize=30;
variableBounds=[-5 10;-5 10;100 200];
evalFN='Selftuning_RLS_GA_OBJFUN';%Change this to relevant object
function
evalOps=[];
options=[1e-6 1];
initPop=initializega(populationSize,variableBounds,evalFN,evalOps,opt
ions);
%____________________________________________________________________
%Setting the parameters for the genetic algorithm
bounds=[-5 10;-5 10;100 200];
evalFN='Selftuning_RLS_GA_OBJFUN';%change this to relevant object
function
evalOps=[];
startPop=initPop;
opts=[1e-6 1 0];
termFN='maxGenTerm';
termOps=80;
selectFN='normGeomSelect';
selectOps=0.08;
xOverFNs='arithXover';
xOverOps=4;
mutFNs='unifMutation';
mutOps=2;%one thousanth of the (population size x maxGenTerm)
%____________________________________________________________________
%Iterating the genetic algorithm
[x,endPop,bPop,traceInfo]=ga(bounds,evalFN,evalOps,startPop,opts,...
termFN,termOps,selectFN,selectOps,xOverFNs,xOverOps,mutFNs,mutOps);
%____________________________________________________________________
%Printing to screen the PID values
%x
disp('_________________________________')
disp('| PID GAIN VALUES |')
disp('|________________________________|')
disp('| Kd Kp Ki |');
disp([x(1) x(2) x(3)]);
disp('|________________________________|')
%____________________________________________________________________
%Plotting best population progress
figure(z)
subplot(3,1,1),plot(bPop(:,1),bPop(:,3)),...
title('Kp Value'),, ylabel('Gain');
subplot(3,1,2),plot(bPop(:,1),bPop(:,4)),...
title('Ki Value'),, ylabel('Gain');
subplot(3,1,3),plot(bPop(:,1),bPop(:,2)),...
title('Kd Value'),xlabel('Generations'), ylabel('Gain');
%____________________________________________________________________
%Generating the Continuous PID Controller
den_pid=[1 0];
num_pid=[x(1) x(2) x(3)]; %Kd Kp Ki
sys_pid=tf(num_pid,den_pid);
%Discretizing the Continuous PID Controller
disp('PID controller')
sysd_pid=c2d(sys_pid,.01,'tustin')
%Getting the Numerator from the Discrete PID Controller
[Discrete_PID_num,Discrete_PID_den]=tfdata(sysd_pid,'v');
%PID Controlled System using genetic algorithm results
controlled_sys=feedback(series(sysd_pid,Ball_Hoop_rls),1);
%lsim(sys_controlled,u,time);%Plot of new PID controller
%____________________________________________________________________
%Informing user if the system is unstable
poles=pole(sys_controlled);
if abs(real(poles(1)))>1
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('%THIS SYSTEM IS UNSTABLE DUE TO POLE 1%')
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('REAL POLE VALUE')
disp(poles(1));
sys_unstable=1;
%break
elseif abs(imag(poles(1)))>1
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('%THIS SYSTEM IS UNSTABLE DUE TO POLE 1%')
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('IMAGINARY POLE VALUE')
disp(poles(1))
sys_unstable=1;
%break
elseif abs(real(poles(2)))>1
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('%THIS SYSTEM IS UNSTABLE DUE TO POLE 2%')
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('REAL POLE VALUE')
disp(poles(2))
sys_unstable=1;
%break
elseif abs(imag(poles(2)))>1
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('%THIS SYSTEM IS UNSTABLE DUE TO POLE 2%')
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('IMAGINARY POLE VALUE')
disp(poles(2))
sys_unstable=1;
%break
elseif abs(real(poles(3)))>1
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('%THIS SYSTEM IS UNSTABLE DUE TO POLE 3%')
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('REAL POLE VALUE')
disp(poles(3))
sys_unstable=1;
%break
elseif abs(imag(poles(3)))>1
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('%THIS SYSTEM IS UNSTABLE DUE TO POLE 3%')
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('IMAGINARY POLE VALUE')
disp(poles(3))
sys_unstable=1;
%break
elseif abs(real(poles(4)))>1
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('%THIS SYSTEM IS UNSTABLE DUE TO POLE 4%')
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('REAL POLE VALUE')
88
disp(poles(4))
sys_unstable=1;
%break
elseif abs(imag(poles(4)))>1
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('%THIS SYSTEM IS UNSTABLE DUE TO POLE 4%')
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('IMAGINARY POLE VALUE')
disp(poles(4))
sys_unstable=1;
%break
elseif abs(real(poles(5)))>1
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('%THIS SYSTEM IS UNSTABLE DUE TO POLE 5%')
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('REAL POLE VALUE')
disp(poles(5))
sys_unstable=1;
%break
elseif abs(imag(poles(5)))>1
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('%THIS SYSTEM IS UNSTABLE DUE TO POLE 5%')
disp('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%')
disp('IMAGINARY POLE VALUE')
disp(poles(5))
sys_unstable=1;
%break
end
%____________________________________________________________________
%creating a matrix to store the PID values
pid_trace(z,:)=x;
%____________________________________________________________________
%if the GA designed system is Unstable use the Last stable controller
if sys_unstable==1
x=pid_trace(last_stable,:);
if z==1
x=Discrete_PID_num;
end
else
last_stable=z;
end
stable_pid_trace(z,:)=x;
%____________________________________________________________________
%Generating the Continuous PID Controller
den_pid=[1 0];
num_pid=[x(1) x(2) x(3)]; %Kd Kp Ki
sys_pid=tf(num_pid,den_pid);
%Discretizing the Continuous PID Controller
disp('PID controller')
sysd_pid=c2d(sys_pid,.01,'tustin')
%Getting the Numerator from the Discrete PID Controller
[Discrete_PID_num,Discrete_PID_den]=tfdata(sysd_pid,'v');
%PID Controlled System using genetic algorithm results
controlled_sys=feedback(series(sysd_pid,Ball_Hoop_rls),1);
%lsim(sys_controlled,u,time);%Plot of new PID controller
%____________________________________________________________________
z=z+1;
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -