%% Passive Control of Water Tank Level
% In this example, you learn how to use *Control System Tuner* app to
% design a controller for a nonlinear plant modeled in Simulink(R). You
% accomplish the following tasks:
% 
% * Configure the model and app for compensator tuning
% * Tune a first-order compensator using passivity-based design
% * Simulate the closed-loop nonlinear response.

% Copyright 2015 The MathWorks, Inc.

%% Simulink Model of the Control System
% The watertank_comp_design model, shown in the figure below, models
% a feedback loop for regulating the water level in a water tank.
% The Controller block contains the first-order compensator to be tuned.

mdl = 'cst_watertank_comp_design';
open_system(mdl)

%% 
% The Water Tank subsystem models the water-tank dynamics.
% Water enters the tank from the top at a rate proportional to the voltage,
% V, applied to the pump. The water leaves through an opening in the tank
% base at a rate that is proportional to the square root of the water
% height, H, in the tank. The presence of the square root in the water flow
% rate makes the plant nonlinear.
%
% <<../op_watertank_schematic.gif>>
%
% The nonlinear model for the water flow is
%
% $$A\dot{x}~=~bu - a\sqrt{x}$$
%
%
% $$y~=~x$$
%
% where 
%
% * $x=H$ denotes the height of water in the tank
% * $u$ denotes the voltage applied to the pump 
% * $A$ denotes the cross-sectional area of the tank
% * $a$ and $b$ are constants related to the flow rate into and out of the tank
%
% This system is passive with storage function $V(x) = \frac{A}{2b}x^2$ since
%
% $$\dot{V}(x) - uy ~=~ -\frac{a}{b}x\sqrt{x}~\leq~ 0$$

%% Passivity-Based Control
% By the Passivity Theorem, the negative-feedback  interconnection of
% two strictly passive systems $H_1$ and $H_2$ is always stable.
%
% <<../vsp.png>>
%
% Since the water tank system is passive, it makes sense to require that
% the controller be strictly passive to guarantee closed-loop stability
% even when the plant model is inaccurate.

%% Compensator Tuning Using Control System Tuner
% You can use the *Control System Tuner* app to tune the Controller block.
%
% Step 1: Open the *Control System Tuner* app. In the Simulink model window,
% on the *Apps* tab, in the *Apps* gallery, click *Control System Tuner*.
%
%%
% Step 2: Launch the tuned block selector from the *Select
% Blocks* button in the *Tuning* tab
%
% <<../passivity_tank_gui2.png>>
%% 
% Step 3: Select the Controller block and Click OK. This block now
% appears in the Tuned Blocks list. 
%
% <<../passivity_tank_gui3.png>>
%
%% 
% Step 4: Specify the tuning goals. Here, there are two main goals:
%
% # Track step changes in water level
% # Make the controller passive
%
% Click the *New Goal* drop-down list, and first add a *Passivity* goal.
% 
% <<../passivity_tank_gui4.png>>
%
% Configure this goal to apply to the Controller block only. This is done
% by setting the input signal to be the "Desired Water Level", the output
% signal to be the output of the Controller block, and the loop opening to
% be at the Controller block output. Also specify minimum passivity indices
% of 0.01 at the inputs and outputs to enforce strict passivity.
%
% <<../passivity_tank_gui5.png>>
%
% Next, add a *Reference Tracking* goal from the *New Goal* drop-down
% list. Configure this goal for a 1 second response time.
%
% <<../passivity_tank_gui6.png>>
%
% Finally, click on the *Manage Goals* button off the *Tuning* tab
% and mark the Passivity goal as a hard tuning constraint.
% 
% <<../passivity_tank_gui7.png>>

%% 
% Step 5: You are ready to tune the Controller block. Click the Tune
% button. You can view the tuning results side-by-side by selecting
% *Left/Right* in the *View* tab.
% 
% <<../passivity_tank_gui8.png>>

%% 
% You can further analyze these results by generating a MATLAB script that 
% reproduces this tuning process.
% 
% <<../passivity_tank_gui9.png>>

%% Closed-Loop Simulation
% You can view the Bode plot of the tuned controller. Click on the *New
% Plot* button off the *Control System* tab. Select *New Bode* from the
% drop-down list.
% 
% <<../passivity_tank_gui11.png>>
%
% The controller response can be specified as follows. 
% 
% <<../passivity_tank_gui12.png>>
%
% Click on the *Plot* button. The bode plot is shown in the following
% figure.
% 
% <<../passivity_tank_gui13.png>>
%
% You can also simulate the closed-loop nonlinear response with the tuned
% controller. First, update the Controller block by clicking *Update
% Blocks* in the *Control System* tab.
% 
% <<../passivity_tank_gui10.png>>
%
% In the Simulink model, double click the Scope block to open the Scope
% window, then simulate the model. 
% 
% <<../nonlinearsimulation.png>>
%
% The nonlinear response of the tuned control system appears in the Scope
% window. This simulation shows that the tracking performance is
% satisfactory.