%% Tune Control System to Soft Requirements
% Tune a cascaded control system to meet requirements of reference tracking and disturbance rejection.
% The cascaded control system of the following illustration includes two tunable controllers, the PI controller for the inner loop, C_{2}, and the PID controller for the outer loop, C_{1}.
%
% The blocks x_{1} and x_{2} mark analysis-point locations. These are locations at which loops can be opened or signals injected for the purpose of specifying requirements for tuning the system.
% Tune the free parameters of this control system to meet the following requirements:
% The output signal, y_{1}, tracks the reference signal, r, with a response time of 10 seconds and a steady-state error of 1%.
% A disturbance injected at x_{2} is suppressed at y_{1} by a factor of 10.
% Create tunable Control Design Blocks to represent the controllers, and numeric LTI models to represent the plants. Also, create AnalysisPoint blocks to mark the points of interest in each feedback loop.

G2 = zpk([],-2,3);
G1 = zpk([],[-1 -1 -1],10);

C20 = tunablePID('C2','pi');
C10 = tunablePID('C1','pid');

X1 = AnalysisPoint('X1');
X2 = AnalysisPoint('X2');

%
% Connect these components to build a model of the entire closed-loop control system.

InnerLoop = feedback(X2*G2*C20,1);
CL0 = feedback(G1*InnerLoop*C10,X1);
CL0.InputName = 'r';
CL0.OutputName = 'y';

% CL0 is a tunable genss model. Specifying names for the input and output channels allows you to identify them when you specify tuning requirements for the system.
%
% Specify tuning requirements for reference tracking and disturbance rejection.

Rtrack = TuningGoal.Tracking('r','y',10,0.01);
Rreject = TuningGoal.Gain('X2','y',0.1);

% The TuningGoal.Tracking requirement specifies that the signal at 'y' track the signal at 'r' with a response time of 10 seconds and a tracking error of 1%.
% The TuningGoal.Gain requirement limits the gain from the implicit input associated with the AnalysisPoint block, X2, to 'y'. (See AnalysisPoint.) Limiting this gain to a value less than 1 ensures that a disturbance injected at X2 is suppressed at the output.
%
% Tune the control system.

[CL,fSoft] = systune(CL0,[Rtrack,Rreject]);

% systune converts each tuning requirement into a normalized scalar value, f. The command adjusts the tunable parameters of CL0 to minimize the f values. For each requirement, the requirement is satisfied if f < 1 and violated if f >1. fSoft is the vector of minimized f values. The largest of the minimized f values is displayed as Soft.
% The output model CL is the tuned version of CL0. CL contains the same Control Design Blocks as CL0, with current values equal to the tuned parameter values.
%
% Validate that the tuned control system meets the tracking requirement by examining the step response from 'r' to 'y'.

stepplot(CL)

% The step plot shows that in the tuned control system, CL, the output tracks the input with approximately the desired response time.
%
% Validate the tuned system against the disturbance rejection requirement by examining the closed-loop response to a signal injected at X2.

CLdist = getIOTransfer(CL,'X2','y');
stepplot(CLdist);

% getIOTransfer extracts the closed-loop response from the specified inputs to outputs. In general, getIOTransfer and getLoopTransfer are useful for validating a control system tuned with systune.
%
% You can also use viewGoal to compare the responses of the tuned control system directly against the tuning requirements, Rtrack and Rreject.

viewGoal([Rtrack,Rreject],CL)

%
% Copyright 2012 The MathWorks, Inc.