function sb2slpid(blkname,in,fl,cod,timer)
%SB2SLPID converts PID controller blocks to SIMULINK.
%	SB2SLPID(BLKNAME,IN,FL,COD,TIMER) converts a SystemBuild block
%       to a SIMULINK block.  BLKNAME is a string containing the name of
%       the block. The parameters are set up for this block based on the
%       SystemBuild file coding integer IN and real number FL.
%
%       COD is a three dimensional vector. The first element indicates
%       whether it is a continuous (0) or discrete (1) time system.
%       The second element indicates whether it does not need (0) or
%       does need (1) a mask. The third element indicates the
%       orientation of the block.
%
%       TIMER is designed for discrete time only. The first element of
%       TIMER is the sampling time. The second element is the initial time.

% Wes Wang 11/17/92
% Copyright (c) 1990-93 by The MathWorks, Inc.
% $Revision: 1.8 $  $Date: 1993/06/09 22:09:40 $

%read in the information.
temp = 1;
% initial condition
if in(in(17)-2) == 0
  ini = zeros(1,2);
else
  ini = fl(1:2)';
  temp = 3;
end;
% Pg
Pg = fl(temp);
% Ig
Ig = fl(temp+1);
%Dg
Dg = fl(temp+2);
%Dt
Dt = fl(temp+3);
%T
if cod(1) ~= 0
  T = timer(1);
end;
%begin convert
set_param(blkname,'Location',[0,102,477,416])

if cod(1) == 0
  add_block('built-in/Integrator',[blkname '/Integrator'])
  set_param([blkname '/Integrator'],...
	'Initial','Ii',...
	'position',[200,55,220,75])
  set_param([blkname '/input1'],...
	'position',[90,100,110,120])
  add_block('built-in/Sum',[blkname '/Sum1'])
  set_param([blkname '/Sum1'],...
	'position',[180,144,195,171])
  add_block('built-in/Integrator',[blkname '/Integrator1'])
  set_param([blkname '/Integrator1'],...
	'orientation',2,...
	'Initial','Di',...
	'position',[245,195,265,215])
  set_param([blkname '/output1'],...
	'position',[460,100,480,120])
  add_block('built-in/Sum',[blkname '/Sum'])
  set_param([blkname '/Sum'],...
	'inputs','+++',...
	'position',[410,76,430,144])
  add_block('built-in/Gain',[blkname '/Gain'])
  set_param([blkname '/Gain'],...
	'Gain','Pg',...
	'position',[325,100,345,120])
  add_block('built-in/Gain',[blkname '/Gain1'])
  set_param([blkname '/Gain1'],...
	'Gain','Ig',...
	'position',[325,55,345,75])
  add_block('built-in/Gain',[blkname '/Gain2'])
  set_param([blkname '/Gain2'],...
	'Gain','Dg/Dt',...
	'position',[325,150,345,170])
  add_block('built-in/Gain',[blkname '/Gain3'])
  set_param([blkname '/Gain3'],...
	'orientation',2,...
	'Gain','1/Dt',...
	'position',[190,195,210,215])
  autoline(blkname,'input1/1','Integrator/1');
  autoline(blkname,'input1/1','Sum1/1');
  autoline(blkname,'input1/1','Gain/1');
  autoline(blkname,'Integrator/1','Gain1/1');
  autoline(blkname,'Gain1/1','Sum/1');
  autoline(blkname,'Gain/1','Sum/2');
  autoline(blkname,'Gain2/1','Sum/3');
  autoline(blkname,'Sum1/1','Gain2/1');
  autoline(blkname,'Sum/1','output1/1');
  autoline(blkname,'Sum1/1','Integrator1/1',[],[0,2]);
  autoline(blkname,'Integrator1/1','Gain3/1',[],[2,2]);
  autoline(blkname,'Gain3/1','Sum1/2',[],[2,0]);
  set_param(blkname,'Mask Display','Continuous\nPID',...
	'Mask Type','Continuous PID controller')
  set_param(blkname,...
	'Mask Dialogue','Continuous-time PID controller.|Propotional gain:|Integral gain:|Integral initial condition:|Derivative gain:|Derivative initial condition:|Derivative time constant:')
  set_param(blkname,'Mask Translate',...
	'Pg=@1; Ig=@2; Ii=@3; Dg=@4;Di=@5;Dt=@6;',...
	'Mask Help','Continuous-time propotional, integrator, and derivative (PID) controller.\nDerivative is approximated by s/(Ts+1), where T is derivative time constant.')
  set_param(blkname,'Mask Entries',...
        [mat2str(Pg) '\/' mat2str(Ig) '\/' mat2str(ini(1)) '\/' mat2str(Dg) '\/' mat2str(ini(2)) '\/' mat2str(Dt) '\/'])
else
  set_param([blkname '/input1'],...
	'position',[90,100,110,120])
  add_block('built-in/Gain',[blkname '/Gain'])
  set_param([blkname '/Gain'],...
	'Gain','Pg',...
	'position',[355,100,375,120])
  add_block('built-in/Sum',[blkname '/Sum'])
  set_param([blkname '/Sum'],...
	'inputs','+++',...
	'position',[440,76,460,144])
  set_param([blkname '/output1'],...
	'position',[490,100,510,120])
  add_block('built-in/Unit Delay',[blkname '/Unit Delay1'])
  set_param([blkname '/Unit Delay1'],...
        'Sample time','T',...
        'x0','Di',...
	'position',[185,165,235,185])
  add_block('built-in/Sum',[blkname '/Sum2'])
  set_param([blkname '/Sum2'],...
	'position',[285,145,305,165])
  add_block('built-in/Gain',[blkname '/Gain2'])
  set_param([blkname '/Gain2'],...
	'Gain','Dg/Dt',...
	'position',[355,145,375,165])
  add_block('built-in/Sum',[blkname '/Sum1'])
  set_param([blkname '/Sum1'],...
	'position',[205,40,225,60])
  add_block('built-in/Unit Delay',[blkname '/Unit Delay'])
  set_param([blkname '/Unit Delay'],...
        'Sample time','T',...
        'x0','Ii',...
	'orientation',2,...
	'position',[240,65,290,85])
  add_block('built-in/Gain',[blkname '/Gain1'])
  set_param([blkname '/Gain1'],...
	'Gain','Ig*Dt',...
	'position',[355,38,375,62])
  autoline(blkname,'input1/1','Unit Delay1/1');
  autoline(blkname,'input1/1','Sum1/1');
  autoline(blkname,'input1/1','Gain/1');
  autoline(blkname,'input1/1','Sum2/1');
  autoline(blkname,'Sum1/1','Gain1/1');
  autoline(blkname,'Sum/1','output1/1');
  autoline(blkname,'Sum1/1','Unit Delay/1',[],[0,2]);
  autoline(blkname,'Unit Delay/1','Sum1/2',[],[2,0]);
  autoline(blkname,'Unit Delay1/1','Sum2/2');
  autoline(blkname,'Sum2/1','Gain2/1');
  autoline(blkname,'Gain1/1','Sum/1');
  autoline(blkname,'Gain/1','Sum/2');
  autoline(blkname,'Gain2/1','Sum/3');
  set_param(blkname,...
	'Mask Display','Discrete\nPID',...
	'Mask Type','Discrete PID controller')
  set_param(blkname,'Mask Dialogue',...
	'Discrete-time PID controller.|Propotional gain:|Integral gain:|Integral initial condition:|Derivative gain:|Derivative initial condition:|Derivative Time Constant:|Sampling time:')
  set_param(blkname,'Mask Translate','Pg=@1;Ig=@2;Ii=@3;Dg=@4;Di=@5;Dt=@6;T=@7',...
	'Mask Help','Discrete-time proportional, integral and derivative (PID) controller.\nintegrator: Tz/(z-1); derivative: (z-1)/T/z.')
  set_param(blkname,'Mask Entries',...
     [mat2str(Pg) '\/' mat2str(Ig) '\/' mat2str(ini(1)) '\/' mat2str(Dg) '\/' mat2str(ini(2)) '\/' mat2str(Dt) '\/' mat2str(T) '\/'])
end;



