echo on;
%DEMO_KASKADE2a - application of Kaskade.m to three cascaded control loops.
%Calculation of 4 controller variants regarding undelayed input signals:
%                             BOD/BOD/BOD; FTS/FTS; FTS/BOD/BOD; FTS/FTS/BOD
% BOD - Discontinuous Amplitude Optimum; FTS - Finite Time Settling
% >>>DIFFERENCE to DEMO_KASKADE1: partially mean value measuring<<<
% >>>DIFFERENCE to DEMO_KASKADE2: one example features two control loops only<<<
%No consideration of controller output limitations: for practical use see method:
%                                               "limitation backward calculation"
T=1;    %sampling time
SZ=100; %simulation time = number of sampling periods T
SSZ=0;  %no disturbance variable change --> step_point==0!

% A: >>>definitions for inner control loop (e.g: current control)<<<
%coupling and measuring: zero order sample and hold / mean value
IWS1=2;
%plant: gain / two first-order time delay elements
SP1=tf(50,conv([1.5 1],[5 1]),'Variable','p'),
%controller: BOD PI-type / undelayed input signals / pole compensation
s='bod'; SV=[0 1 0 1 0 0]; TV=[T,0,IWS1];
SM1=struct('Optimization',s,'ControlVector',SV,'ControlLoop',TV);
pause %press any key please

% B: >>>definitions for middle control loop (e.g: speed control)<<<
%coupling and measuring: zero order sample and hold / mean value
IWS2=2;
%plant: gain / one first-order time delay element (friction!)
SP2=tf(2/0.01,[20/0.01 1],'Variable','p'),
%controller: BOD PI-type / undelayed input signals / no pole compensation
s='bod'; SV=[0 0 0 0 1 0]; TV=[T,0,IWS2];
SM2=struct('Optimization',s,'ControlVector',SV,'ControlLoop',TV);
pause %press any key please

% C: >>>definitions for outer control loop (e.g: positioning)<<<
%coupling and measuring: zero order sample and hold / instantaneous value
IWS3=1;
%plant: one integrator
SP3=tf(1,[100 0],'Variable','p'),
%controller: BOD P-type / undelayed input signals
s='bod'; SV=[0 0 1 0 0 0]; TV=[T,0,IWS3];
SM3=struct('Optimization',s,'ControlVector',SV,'ControlLoop',TV);
pause %press any key please

%calculation of prefilters and controllers:
[FI11,RE11,FI21,RE21,FI31,RE31]=kaskade(SM1,SP1,SM2,SP2,SM3,SP3);
FI1=FI11;RE1=RE11;FI2=FI21;RE2=RE21;FI3=FI31;RE3=RE31;
SWA=1; %setpoint step response
SGA=0; %no disturbance variable change
DEMO_KASKADEs; sim('DEMO_KASKADEs',T*SZ);
pause %press any key please

%2. example
%A - controller: FTS / undelayed input signals / minimum form
s='eez'; SV=[0 -1 -1 0 0 0]; TV=[T,0,IWS1];
SM1=struct('Optimization',s,'ControlVector',SV,'ControlLoop',TV);
%B - controller: FTS / undelayed input signals / minimum form
s='eez'; SV=[0 1 0 0 0 0]; TV=[T,0,IWS2];
SM2=struct('Optimization',s,'ControlVector',SV,'ControlLoop',TV);
%usually there are maximum two FTS control loops useful
[FI12,RE12,FI22,RE22]=kaskade(SM1,SP1,SM2,SP2);
FI1=FI12;RE1=RE12;FI2=FI22;RE2=RE22;
SWA=1; %setpoint step response
SGA=0; %no disturbance variable change
SZ=20;
DEMO_KASKADE2s; sim('DEMO_KASKADE2s',T*SZ);
pause %press any key please

%3. example
%A - controller: FTS / undelayed input signals / minimum form
s='eez'; SV=[0 -1 -1 0 0 0]; TV=[T,0,IWS1];
SM1=struct('Optimization',s,'ControlVector',SV,'ControlLoop',TV);
%B - controller: BOD PI-type / undelayed input signals / no pole compensation
s='bod'; SV=[0 0 0 0 1 0]; TV=[T,0,IWS2];
SM2=struct('Optimization',s,'ControlVector',SV,'ControlLoop',TV);
%C - controller: BOD-P / undelayed input signals
s='bod'; SV=[0 0 1 0 0 0]; TV=[T,0,IWS3];
SM3=struct('Optimization',s,'ControlVector',SV,'ControlLoop',TV);
%calculation of prefilters and controllers:
[FI13,RE13,FI23,RE23,FI33,RE33]=kaskade(SM1,SP1,SM2,SP2,SM3,SP3);
FI1=FI13;RE1=RE13;FI2=FI23;RE2=RE23;FI3=FI33;RE3=RE33;
SWA=1; %setpoint step response
SGA=0; %no disturbance variable change
SZ=50;
DEMO_KASKADEs; sim('DEMO_KASKADEs',T*SZ);
pause %press any key please

%4. example
%A - controller: FTS / undelayed input signals / minimum form
s='eez'; SV=[0 -1 -1 0 0 0]; TV=[T,0,IWS1];
SM1=struct('Optimization',s,'ControlVector',SV,'ControlLoop',TV);
%B - controller: FTS / undelayed input signals / minimum form
s='eez'; SV=[0 1 -1 0 0 0]; TV=[T,0,IWS2];
SM2=struct('Optimization',s,'ControlVector',SV,'ControlLoop',TV);
%C - controller: BOD P-type / undelayed input signals
s='bod'; SV=[0 0 1 0 0 0]; TV=[T,0,IWS3];
SM3=struct('Optimization',s,'ControlVector',SV,'ControlLoop',TV);
%calculation of prefilters and controllers:
[FI14,RE14,FI24,RE24,FI34,RE34]=kaskade(SM1,SP1,SM2,SP2,SM3,SP3);
FI1=FI14;RE1=RE14;FI2=FI24;RE2=RE24;FI3=FI34;RE3=RE34;
SWA=1; %setpoint step
SGA=0; %disturbance variable unchanged
SZ=50;
DEMO_KASKADEs; sim('DEMO_KASKADEs',T*SZ);
echo off;
clear FI1   FI13  FI21  FI24  FI33  IWS2  RE11  RE14  RE22  RE3   RE34  SM2   SP2
clear FI11  FI14  FI22  FI3   FI34  IWS3  RE12  RE2   RE23  RE31  SGA   SM3   SP3
clear FI12  FI2   FI23  FI31  IWS1  RE1   RE13  RE21  RE24  RE33  SM1   SP1   SSZ
clear SV    T     tout  SWA   TV    SZ    s
