clf;axis([1,2,3,4]);axis;
echo on
%---------------------------------------------------------
%
%          Dept. of Electrical & Computer Eng.
%       University of California, Santa Barbara.
%
%               ECE 147 B:  Digital Control Systems
%
%               module 6:    Sampled Data Systems
%
%       -------------------------------------------------

% This module introduces the techniques required
% to study sampled data systems.  This material is
% introduced in Chapter 3 of the text.
%
% In sampled data systems we have both continuous
% and discrete time signals and would like to be able
% to do simulations to show both kinds of signal.

% Please adjust the Graph and Command
% windows for maximum size viewing.

pause   % press any key to continue

% We will study a dead beat control design for a double
% integrator plant (for example a satellite).  The plant
% is simply,
%
%	P(s) = 1/s^2.

P = tf(1, [1,0,0]);

% Our control system will have a 1 second sample period.

T = 1.0;

% The input to the plant is preceeded by a zero-order
% hold and the output is sampled.  We can calculate
% the digital equivalent of this plant using sys2dsys.

Pd = c2d(P,T,'zoh');

pause	% press any key to continue.

% Let's specify a desired closed loop transfer function,
% M(z).  Initially take M(z) to be a unit delay.

% It is a bit tricky to use Matlab to calculate the
% deadbeat controller.  The formula is,
%
%	            M(z)
%       C(z) = ------------
%              (1-M(z))*P(z)
%
% The deadbead controller, C, required to give
% a tracking response of a unit delay is:
%
%		2(z-1)
%	C  =  ----------
%              T^2(z+1)

C = tf([2,-2],[T^2,T^2],T);

pause	% press any key to continue.

% Now we want to close the loop around  P(z)C(z),
% in the usual way.  Note that this gives a
% digital result which can be calculated in the
% usual way.

m6disc
[a,b,c,d] = dlinmod('m6disc',T);
dclp = ss(a,b,c,d,T);   

% Let's look at the poles of this.

clf
zgrid
axis('square')
axis([-1.25,1.25,-1.25,1.25])
pzmap(dclp)

% Something funny is going on here - there are poles
% on the unit circle.

pole(dclp)

pause	% press any key to continue.

% Now examine the step response, from input to output.

dtime = [0:T:20*T]';
r = ones(length(dtime),1);
yd = lsim(dclp,r,dtime);

clf
plot(dtime,yd,'*',dtime,r,'o')
axis([0,20*T,-5,5])
title('discrete step response')
legend('output: yd','reference: r')
xlabel('time: seconds')

% This looks just fine.  The error goes to zero in one
% sample period.

pause	% press any key to continue.

% Now we would like to see what the effect is on the
% plant itself.  To do this we have to be able to
% calculate the output of the controller (which is
% of course the input to the plant).

% It's easy to see the required relationship by looking at
% a block diagram,
%
%	u(z) = C(z) * (r(z) - y(z)).
%
% Ok lets do this as a simulation to get u.

esig = r - yd;
u = lsim(C,esig,dtime);

% And include the controller output on the plot.

clf
plot(dtime,yd,'*',dtime,r,'o',dtime,u,'x')
axis([0,20*T,-5,5])
title('discrete step response')
legend('output: yd','reference: r','controller output: u')
xlabel('time: seconds')

% The controller output is a bit wild.  Here we see what
% the poles on the unit circle are doing - they create
% an oscillation at the controller output which doesn't
% die out.

pause	% press any key to continue.

% Now do the cts time response by putting the controller
% output into a ZOH equivalent of the plant.  We can
% use the continuous time system for the simulation and
% recreate the effect of the zero-order hold on the 
% input signal

tfine = [0:T/100:20*T]';      % make a fine time grid.
ufine = zeros(length(tfine),1);
echo off;
for i = 1:length(tfine),
  uptr = max(find(dtime <= tfine(i)));
  ufine(i) = u(uptr);
end; echo on

ycts = lsim(P,ufine,tfine);
plot(dtime,yd,'*',tfine,ycts,'-')
axis([0,20*T,0,2]);
title('System outputs')
legend('discrete output','continuous output')
xlabel('time: seconds')

% Here we see what happens to the continuous plant as a
% result of the input thrashing around.  Note that at each
% sample instant (after the one at zero) there is a no
% error.  However between samples the plant oscillates
% wildly.  Maybe we asked for too much here.  The problems
% will look at trying different closed loop specifications.

pause	% press any key to continue.

% The critcal feature in this analysis was the fact that
% the loop was closed with a sampled signal.  So a discrete
% closed loop analysis could determine all of the signals
% at the sample times.  To get the intersample behavior,
% we just put the appropriate signals into the appropriate
% open-loop components.

%------------------------------------------------------------

echo off



