%% Passive Control with Communication Delays
% This example shows how to mitigate communication delays in a passive
% control system.

% Copyright 2015 The MathWorks, Inc.

%% Passivity-Based Control
% By the Passivity Theorem, the negative-feedback  interconnection of
% two strictly passive systems $H_1$ and $H_2$ is always stable.
%
% <<../passivitythm.png>>
%
% When the physical plant is passive, it is therefore advantageous to
% use a passive controller for robustness and safety reasons. In networked
% control systems, however, communication delays can undo the benefits of 
% passivity-based control and lead to instability.
% To illustrate this point, we use the plant and 2nd-order passive 
% controller from the "Vibration Control in Flexible Beam" example.
% See this example for background on the underlying control problem.
% Load the plant model $G$ and passive controller $C$ (note that $C$
% corresponds to $-C$ in the other example).

load BeamControl G C

bode(G,C,{1e-2,1e4})
legend('G','C')

%%
% The control configuration is shown below as well as the impulse response
% from $d$ to $y$.
%
% <<../negativefb.png>>
% 

impulse(feedback(G,C))

%% Destabilizing Effect of Communication Delays
% Now suppose there are substantial communication delays between the sensor
% and the controller, and between the controller and the actuator. This
% situation is modeled in Simulink as follows.

open_system('DelayedFeedback')

%%
% The communication delays are set to 

T1 = 1;
T2 = 2;

%%
% Simulating this model shows that the communication delays destabilize 
% the feedback loop.
%
% <<../noscatter.png>>
% 

%% Scattering Transformation
% To mitigate the delay effects, you can use a simple linear transformation of
% the signals exchanged between the plant and controller over the network.
% 
% <<../networkedcontrol.png>>
% 
% *Figure 1: Networked Control System*
% 
% This is called the "scattering transformation" and given by the formulas
%
% $$ \left(\begin{array}{c}u_l\\v_l\end{array}\right) = 
%    \left(\begin{array}{cc}1 & b \\ 1 & -b \end{array}\right)
%    \left(\begin{array}{c}u_G\\y_G\end{array}\right) , \quad
%    \left(\begin{array}{c}u_r\\v_r\end{array}\right) = 
%    \left(\begin{array}{cc}1 & b \\ 1 & -b \end{array}\right)
%    \left(\begin{array}{c}y_C\\u_C\end{array}\right) , $$
%
% or equivalently
%
% $$ \left(\begin{array}{c}u_l\\u_G\end{array}\right) = S
%    \left(\begin{array}{c}v_l\\y_G\end{array}\right) , \quad
%    \left(\begin{array}{c}v_r\\u_C\end{array}\right) = S^{-1}
%    \left(\begin{array}{c}u_r\\y_C\end{array}\right) , \quad
%    S = \left(\begin{array}{cc}1 & 2b \\ 1 & b \end{array}\right)  $$
%
% with $b>0$. Note that in the absence of delays, the two scattering
% transformations cancel each other and the block diagram in Figure 1 is
% equivalent to the negative feedback interconnection of $G$ and $C$.
%
% When delays are present, however, $(u_l,v_l)$ is no longer equal to
% $(u_r,v_r)$ and this scattering transformation alters the properties of
% the closed-loop system. In fact, observing that
%
% $$ u_l = (1-bC(s))/(1+bC(s)) v_l , \quad 
%    v_r = (G(s)/b-1)/(G(s)/b+1) u_r $$
% 
% and that $bC$ and $G/b$ strictly passive ensures that
%
% $$ \| (1-bC)/(1+bC) \|_\infty < 1 , \quad \| (G/b-1)/(G/b+1) \|_\infty <
% 1 , $$
%
% the Small Gain Theorem guarantees
% that the feedback interconnection of Figure 1 is always stable no matter
% how large the delays. Confirm this by building a Simulink model of 
% the block diagram in Figure 1 for the value $b=1$.

b = 1;

open_system('ScatteringTransformation')

%%
% Simulate the impulse response of the closed-loop system as done before.
% The response is now stable and similar to the delay-free response in
% spite of the large delays. 
% 
% <<../scatter.png>>
% 
% For more details on the scattering transformation, see T. Matiakis, 
% S. Hirche, and M. Buss, "Independent-of-Delay  Stability of Nonlinear 
% Networked Control Systems by Scattering Transformation," Proceedings of 
% the 2006 American Control Conference, 2006, pp. 2801-2806.



