%% Tune Control System to Both Hard and Soft Requirements
% Tune a cascaded control system to meet requirements of reference tracking and disturbance rejection. These requirements are subject to a hard constraint on the stability margins of the inner and outer loops.
% 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 you can open loops or inject signals 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 at r with a response time of 5 seconds and a steady-state error of 1%.
% A disturbance injected at x_{2} is suppressed at the output, y_{1}, by a factor of 10.
% Impose these tuning requirements subject to hard constraints on the stability margins of both loops.
% 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',5,0.01);
Rreject = TuningGoal.Gain('X2','y',0.1);

% The TuningGoal.Tracking requirement specifies that the signal at 'y' tracks the signal at 'r' with a response time of 5 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 the output, 'y'. (See AnalysisPoint.) Limiting this gain to a value less than 1 ensures that a disturbance injected at X2 is suppressed at the output.
%
% Specify tuning requirements for the gain and phase margins.

RmargOut = TuningGoal.Margins('X1',18,60);
RmargIn = TuningGoal.Margins('X2',18,60);
RmargIn.Openings = 'X1';

% RmargOut imposes a minimum gain margin of 18 dB and a minimum phase margin of 60 degrees. Specifying X1 imposes that requirement on the outer loop. Similarly, RmargIn imposes the same requirements on the inner loop, identified by X2. To ensure that the inner-loop margins are evaluated with the outer loop open, include the outer-loop analysis-point location, X1, in RmargIn.Openings.
%
% Tune the control system to meet the soft requirements of tracking and disturbance rejection, subject to the hard constraints of the stability margins.

SoftReqs = [Rtrack,Rreject];
HardReqs = [RmargIn,RmargOut];
[CL,fSoft,gHard] = systune(CL0,SoftReqs,HardReqs);

% systune converts each tuning requirement into a normalized scalar value, f for the soft constraints and g for the hard constraints. The command adjusts the tunable parameters of CL0 to minimize the f values, subject to the constraint that each g < 1.
% The displayed value Hard is the largest of the minimized g values in gHard. This value is less than 1, indicating that both the hard constraints are satisfied.
%
% Validate the tuned control system against the stability margin requirements.

figure;
viewGoal(HardReqs,CL)

% The viewGoal plot confirms that the stability margin requirements for both loops are satisfied by the tuned control system at all frequencies. The blue lines show the margin used in the optimization calculation, which is an upper bound on the actual margin of the tuned control system.
%
% Examine whether the tuned control system meets the tracking requirement by examining the step response from 'r' to 'y'.

figure;
stepplot(CL,20)

%
% The step plot shows that in the tuned control system, CL, the output tracks the input but the response is somewhat slower than desired and the tracking error may be larger than desired. For further information, examine the tracking requirement directly with viewGoal.

figure;
viewGoal(Rtrack,CL)

% The actual tracking error crosses into the shaded area between 1 and 10 rad/s, indicating that the requirement is not met in this regime. Thus, the tuned control system cannot meet the soft tracking requirement, time subject to the hard constraints of the stability margins. To achieve the desired performance, you may need to relax one of your requirements or convert one or more hard constraints to soft constraints.
% Copyright 2012 The MathWorks, Inc.