Commit 24b014f5 authored by Kristian Soltesz's avatar Kristian Soltesz
Browse files

Example working with new code structure. Need to add visualization

parent 09e6f04e
function opts = defaults(P)
function opts = defaults(P,optType,varargin)
% Returns default configuration options for the design scripts of PIDopt.
%
% P - Process dynamics. Used to determine angular frequency grid.
%
% Downloaded from git@gitlab.control.lth.se:kristian/PIDopt.git
% P - Process dynamics. Used to determine angular frequency grid.
% optType - Either of 'pidIE', 'pidIAE', 'pidfIAE' or 'optIAE'.
% K0 - Initial controller used by optIAE can be passed as varargin.
%
%Downloaded from git@gitlab.control.lth.se:kristian/PIDopt.git
% See the git repo for further documentation.
% angular frequency grid
% Retur correct type of default options
switch optType
case 'pidIE'
opts.w = getW(P); % Angular frequency grid.
opts.p = zeros(2,1); % Initial parameter vector.
opts.maxIter = 100; % Number of convex-concave iterations
opts.stopTol = 1e-3; % Relative objective decrease stop criterium
case 'pidIAE'
opts.w = getW(P); % Angular frequency grid.
opts.p = zeros(3,1); % Initial parameter vector.
opts.fminOpts = optimset('algorithm','active-set','GradConstr',...
'on','GradObj','on','Display','off','maxIter',10); % fminCon
case 'pidfIAE'
opts.w = getW(P); % Angular frequency grid.
otps.p = zeros(4,1); % Initial parameter vector.
opts.fminOpts = optimset('algorithm','active-set','GradConstr',...
'on','GradObj','on','Display','off','maxIter',50); % fmincon
case 'optIAE'
% Initial controller
try
K0 = varargin{1};
catch
K0 = zpk(0);
end
% Sampling period
h = 0.1/bandwidth(feedback(K0*P,1));
% Avoid excessive number of states for delay-dominated plants
maxDelaySamples = 15;
if totaldelay(P)/h > maxDelaySamples
h = totaldelay(P)/maxDelaySamples;
end
% Determine time horizon for optimization
G = loopsens(P,K0);
[~,t] = step(G.PSi); % Load step response.
[y,t] = step(G.PSi,2*t(end)); % Longer load step response.
T = t(find(abs(y)>0.005*max(abs(y)),1,'first')); % Find 0.5% settling time.
% Save to struct
opts.Nq = 50;
opts.h = h;
opts.T = T;
opts.K0 = K0;
end
end
function w = getW(P)
% returns angular frequency grid
Nw = 1e3; % number of grid points
[~,~,w] = bode(P); % generate relevant range
w = logspace(log10(w(1))-2,log10(w(end))+2,Nw)'; % extend range
% pidIE
pidIE.w = w; % Angular frequency grid for cont straint evaluation.
pidIE.p = [0;0;0]; % Vector of controller initial controller parameters
% [kp;ki;kd]. Length must be 2 (PI) or 3 (PID).
% Controller must stabilize plant.
pidIE.maxIter = 100; % Number of convex-concave iterations
pidIE.stopTol = 1e-3; % Relative objective decrease stop criterium
% pidIAE
pidIAE.w = w; % Angular frequency grid for constraint evaluation.
pidIAE.p = [0;0;0]; % Vector of initial controller parameters [kp;ki;kd].
% Length must be 2 (PI) or 3 (PID).
% Controller must stabilize plant.
pidIAE.fminOpts = optimset('algorithm','active-set','GradConstr','on',...
'GradObj','on','Display','off','maxIter',10); % fminCon options
% pidfIAE
pidfIAE.w = w; % Angular frequency grid for constraint evaluation.
pidfIAE.pC = [0;0;0]; % Vector of initial controller parameters [kp;ki;kd].
% Controller must stabilize plant.
pidfIAE.pF = 0; % Initial filter time constant.
pidfIAE.fminOpts = optimset('algorithm','active-set','GradConstr','on',...
'GradObj','on','Display','off','maxIter',50); % fmincon options
% optIAE
optIAE = []; % FIXME: implement
opts.pidIE = pidIE;
opts.pidIAE = pidIAE;
opts.pidfIAE = pidfIAE;
opts.optIAE = optIAE;
\ No newline at end of file
end
\ No newline at end of file
......@@ -23,32 +23,35 @@ Mks = 10; % Maximum allowed magnitude of transfer function from process
% output to control signal, sometimes referred to as noise
% sensitivity.
opts = defaults(P); % Default options
%% IE-minimizing PI design
opts.pidIE.p = [0;0]; % design PI controller
[K1,p1,opts1] = pidIE(P,Ms,Mt,Mks,opts.pidIE);
opts = defaults(P,'pidIE');
opts.p = [0;0]; % Design PI controller
[K1,p1,opts1] = pidIE(P,Ms,Mt,Mks,opts);
%% IAE-minimizing filtered PID design
opts.pidfIAE.p = [p1;0;0]; % Hot start with PI controller.
[K2,p2] = pidfIAE(P,Ms,Mt,Mks,opts.pidfIAE); % IAE-optimal PID with filter
opts = defaults(P,'pidfIAE');
opts.p = [p1;0;0]; % Hot start with PI controller
[K2,p2] = pidfIAE(P,Ms,Mt,Mks,opts);
%% IAE-miniming Q design
K0 = K2;
h = 0.1/bandwidth(feedback(K0*P,1));
if totaldelay(P)/h > 15 % avoid excessive number of states for delay-dominated plants
h = totaldelay(P)/15;
end
h
% Determine time horizon for optimization
loops = loopsens(P,K0); % GoF
[y,t] = step(series(P,loops.So)); % load step response
T0 = t(end);
[y,t] = step(series(P,loops.So),2*T0); % longer load step response
iy = find(abs(y)>0.005*max(abs(y))); % Find 0.5% settling time
T = t(iy(end))
% Optimize
'Computing IAE optimal LTI controller'
K3 = optIAE(P,Ms,Mt,Mks,h,T,K0);
opts = defaults(P,'optIAE',K2); % Hot start with filtered PID controller
[K3,Pd,Q] = optIAE(P,Ms,Mt,Mks,opts)
% opts.optIAE.K0 = K2; % Hot start with filtered PID controller.
% %K0 = K2;
% h = 0.1/bandwidth(feedback(K0*P,1));
% if totaldelay(P)/h > 15 % avoid excessive number of states for delay-dominated plants
% h = totaldelay(P)/15;
% end
%
% % Determine time horizon for optimization
% loops = loopsens(P,K0); % GoF
% [y,t] = step(series(P,loops.So)); % load step response
% T0 = t(end);
% [y,t] = step(series(P,loops.So),2*T0); % longer load step response
% iy = find(abs(y)>0.005*max(abs(y))); % Find 0.5% settling time
% T = t(iy(end))
% % Optimize
% K3 = optIAE(P,Ms,Mt,Mks,h,T,K0);
%% Validate and visualize designs
\ No newline at end of file
......@@ -17,11 +17,10 @@ function [K,p,opts] = pidIAE(P,Ms,Mt,Mks,opts)
% Generate default options if not provided as argument
if isempty(opts)
opts = defaults(P);
opts = opts.pidIAE;
opts = defaults(P,'pidIAE');
end
% Extract optsions
% Extract options
w = opts.w;
p = opts.p;
fminOpts = opts.fminOpts;
......
......@@ -19,11 +19,10 @@ function [K,p,opts] = pidIE(P,Ms,Mt,Mks,opts)
% Generate default options if not provided as argument
if isempty(opts)
opts = defaults(P);
opts = opts.pidIE;
opts = defaults(P,'pidIE');
end
% Extract optsions
% Extract options
w = opts.w;
p = opts.p;
maxIter = opts.maxIter; % 100
......
......@@ -15,6 +15,17 @@ function [K,p,opts] = pidfIAE(P,Ms,Mt,Mks,opts)
% Downloaded from git@gitlab.control.lth.se:kristian/PIDopt.git
% See the git repo for further documentation.
% Generate default options if not provided as argument
if isempty(opts)
opts = defaults(P,'pidfIAE');
end
% Extract options
w = opts.w;
pC = opts.p(1:3);
pF = opts.p(4);
fminOpts = opts.fminOpts;
% Omit designing filter if Mks=Inf
if Mks == Inf
opts.p = opts.p(1:end-1);
......@@ -24,17 +35,6 @@ if Mks == Inf
return
end
% Generate default options if not provided as argument
if isempty(opts)
opts = defaults(P);
opts = opts.pidIAE;
end
% Extract optsions
w = opts.w;
pC = opts.pC;
pF = opts.pF;
fminOpts = opts.fminOpts;
% regularize
if pC(1) ~= 0
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment