% Step response example.
% 1) Controlling time delay plant with an integral controller.
% 2) Calculate the margins and the 4 parameters GM, PM, w180 and wc
% 3) Plot the unit step response.

s=tf('s');

% 1) 
hp=exp(-s);     % tau=1 example
hc=1/(2*s);     % I-controller 
 
h0=hc*hp;       % Loop transfer function
h_ry=h0/(1+h0); % Transfer function from r to y
                % Complementary transfer function, T(s)=h_ry

% 2)                
[GM, PM,w180,wc] = margin(h0) % Calculate the 4 parameters                

% 3)
figure(1)       % Plot the step response from r to y
step(h_ry), grid
title('Step response from r to y: Standard feedback system')
ylabel('y')

% May increase plant gain by GM*K before stability limit
[GM0, PM0] = margin(GM*h0) % Calculate the 4 parameters 

dt_max=PM*(pi/180)/wc;
hp_true=exp(-s*(1+dt_max));     % tau=1 example
h0_true=hc*hp_true;       % Loop transfer function
[GM0, PM0] = margin(h0_true) % Calculate the 4 parameters 