function [SSDATA,SingularFlag] = ss(PID)
% Conversion to @ssdata

%   Author(s): Rong Chen
%   Copyright 1986-2013 The MathWorks, Inc.
%   $Revision $  
SingularFlag = false;

% convert to internal parameterization
[P, I, D, T] = utGetInternalParams(PID);
% get sample time
Ts = abs(PID.Ts);
% compute matrices
e = [];
if Ts==0
   if I==0
      % no integrator
      a = []; b = zeros(0,1); c = zeros(1,0); d = P;
   else
      % integrator --> a=0, b=1, c=1, d=0
      a = 0; b = 1; c = I; d = P;
   end
   if D~=0
      % has derivative
      if T==0
         % ideal derivative --> a=eye(2), b=[0;-1], c=[1 0], d=0, e=[0 1;0 0]
         e = blkdiag(eye(size(a)), [0 1;0 0]);
         a = blkdiag(a, eye(2)); b = [b; 0; -1]; c = [c D 0];
      else
         % filter s/(Ts+1) --> a=-1/T, b=-1/T, c=1/T, d = 1/T
         a = blkdiag(a, -1/T); b = [b; -1/T]; c = [c D/T]; d = d + D/T;
      end
   end
else
   alphas = [0 Ts Ts/2];
   if I==0
      % no integrator
      a = [];  b = zeros(0,1);  c = zeros(1,0); d = P;
   else
      % integrator
      a = 1;  b = Ts;  c = I;  d = P+I*alphas(PID.IFormula(1)=='FBT');
   end
   if D~=0
      % derivative filter
      % Note that when T+alpha=0, the derivative term becomes (z-1)/Ts
      % which requires a descriptor realization
      beta = T + alphas(PID.DFormula(1)=='FBT');
      if beta==0
         e = blkdiag(eye(size(a)), [0 1;0 0]);
         a = blkdiag(a, eye(2));
         b = [b; 0; -1];
         c = [c D/Ts 0];
         d = d - D/Ts;
      else
         aux1 = Ts/beta;
         aux2 = D/beta;
         a = blkdiag(a, 1-aux1);
         b = [b; -aux1];
         c = [c , aux2];
         d = d + aux2;
      end
   end
end
SSDATA = ltipack.ssdata(a,b,c,d,e,PID.Ts);

