%% Convert Distillation Column Problem for Tuning With systune
% Set up the following control system for tuning with looptune. Then convert the setup to a systune problem and examine the results. These results reflect the structure of the control system model that looptune tunes. The results also reflect the tuning requirements implicitly enforced when tuning with looptune.
%
% For this example, the 2-by-2 plant G is represented by:
% G\left( s \right) = \frac{1}{{75s + 1}}\left[ {\begin{array}{*{20}{c}}
% {87.8}&{ - 86.4}\\
% {108.2}&{ - 109.6}
% \end{array}} \right].
% The fixed-structure controller, C, includes three components: the 2-by-2 decoupling matrix D and two PI controllers PI_L and PI_V. The signals r, y, and e are vector-valued signals of dimension 2.
% Build a numeric model that represents the plant and a tunable model that represents the controller. Name all inputs and outputs as in the diagram, so that looptune and looptuneSetup know how to interconnect the plant and controller via the control and measurement signals.

s = tf('s');
G = 1/(75*s+1)*[87.8 -86.4; 108.2 -109.6];
G.InputName = {'qL','qV'};
G.OutputName = {'y'};

D = tunableGain('Decoupler',eye(2));
D.InputName = 'e';
D.OutputName = {'pL','pV'};
PI_L = tunablePID('PI_L','pi');
PI_L.InputName = 'pL';
PI_L.OutputName = 'qL';
PI_V = tunablePID('PI_V','pi'); 
PI_V.InputName = 'pV';
PI_V.OutputName = 'qV'; 
sum1 = sumblk('e = r - y',2);
C0 = connect(PI_L,PI_V,D,sum1,{'r','y'},{'qL','qV'});

%
% This system is now ready for tuning with looptune, using tuning goals that you specify. For example, specify a target bandwidth range. Create a tuning requirement that imposes reference tracking in both channels of the system with a response time of 15 s, and a disturbance rejection requirement.

wc = [0.1,0.5];
TR = TuningGoal.Tracking('r','y',15,0.001,1);
DR = TuningGoal.Rejection({'qL','qV'},1/s);
DR.Focus = [0 0.1];

[G,C,gam,info] = looptune(G,C0,wc,TR,DR);

% looptune successfully tunes the system to these requirements. However, you might want to switch to systune to take advantage of additional flexibility in configuring your problem. For example, instead of tuning both channels to a loop bandwidth inside wc, you might want to specify different crossover frequencies for each loop. Or, you might want to enforce the tuning requirements TR and DR as hard constraints, and add other requirements as soft requirements.
%
% Convert the looptune input arguments to a set of input arguments for systune.

[T0,SoftReqs,HardReqs,sysopt] = looptuneSetup(G,C0,wc,TR,DR);

%
% This command returns a set of arguments you can provide to systune for equivalent results to tuning with looptune. In other words, the following command is equivalent to the previous looptune command.

[T,fsoft,ghard,info] = systune(T0,SoftReqs,HardReqs,sysopt);

%
% Examine the arguments returned by looptuneSetup.

T0

% The software constructs the closed-loop control system for systune by connecting the plant and controller at their control and measurement signals, and inserting a two-channel AnalysisPoint block at each of the connection locations, as illustrated in the following diagram.
%
%
% When tuning the control system of this example with looptune, all requirements are treated as soft requirements. Therefore, HardReqs is empty. SoftReqs is an array of TuningGoal requirements. These requirements together enforce the bandwidth and margins of the looptune command, plus the additional requirements that you specified.

SoftReqs

%
% Examine the first entry in SoftReqs.

SoftReqs(1)

%
% looptuneSetup expresses the target crossover frequency range wc as a TuningGoal.LoopShape requirement. This requirement constrains the open-loop gain profile to the loop shape stored in the LoopGain property, with a crossover frequency and crossover tolerance (CrossTol) determined by wc. Examine this loop shape.

viewGoal(SoftReqs(1))

% The target crossover is expressed as an integrator gain profile with a crossover between 0.1 and 0.5 rad/s, as specified by wc. If you want to specify a different loop shape, you can alter this TuningGoal.LoopShape requirement before providing it to systune.
%
% looptune also tunes to default stability margins that you can change using looptuneOptions. For systune, stability margins are specified using TuningGoal.Margins requirements. Here, looptuneSetup has expressed the default stability margins of looptune as soft TuningGoal.Margins requirements. For example, examine the fourth entry in SoftReqs.

SoftReqs(4)

% The last entry in SoftReqs is a similar TuningGoal.Margins requirement constraining the margins at the plant outputs. looptune enforces these margins as soft requirements. If you want to convert them to hard constraints, pass them to systune in the input vector HardReqs instead of the input vector SoftReqs.
% Copyright 2012 The MathWorks, Inc.