%
%  Matlab driver for the basic 1-dof state variable model
%  This version is for the PID controller
%
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
  saturation_level = 1000/2196; % set the saturation level
%
%  enter the discrete-time model
%
  Ts = 0.1;
  Gp = tf([0 0 9.348 8.879],[1 1.495 0.8849 0],Ts)
  [num_Gp, den_Gp] = tfdata(Gp,'v');
%
% conver to a state space model
%
 [G,H,C,D] = tf2ss(num_Gp,den_Gp);
%
%  set the parameters
%
  Gpf = 1.0
%
% initial proportional controller
%
  Kp = 0.0116
  Ki = 0.0;
  Kd = 0.0;
%
%  PI Controller
%
%   K = 0.0;
%   a = 0.0;
% 
%   Kp = -K*a;
%   Ki = K-Kp;
%   Kd = 0.0;
%
%  PID Controller
%
%  K = 0.0;
%  a = 0.0;
%  b = 0.0;
%
%  Kd = K*b;
%  Kp = -K*a-2*Kd;
%  Ki = K-Kp-Kd;
%  
%  set the step amplitude and final time
%
  Amp = 1.0; % the input amplitude

  Tf = 3.0;  % seconds
%
%  now simulate the system
%
  sim('DT_PID');
%
%  plot the results
%
  figure
  orient landscape
  subplot(2,1,1); stairs(m_time,m_y); grid; xlabel('Time (sec)'); ylabel('Position (cm)');
  subplot(2,1,2); stairs(m_time,m_u); grid; xlabel('Time (sec)'); ylabel('Control Effort');
  axis([0 Tf -saturation_level saturation_level]);
