function Handles = loopstruct(ConfigNumber,AxisHandle,Render)
%LOOPSTRUCT  Draws feedback loop configuration.
%
%   Handles = LOOPSTRUCT(ConfigID,AxisHandle,Render) draws the loop
%   configuration with number CONFIGID in the axis with handle AXISHANDLE. 
%   Available RENDERs include
%     1) 'plain':  just the loop topology sketch
%     2) 'signal': include signal names
%
%   The axis limits will be set from 0-1 along both axes to ensure the 
%   loop structure scales appropriately. Any children on the axis are 
%   deleted before the new feedback configuration is drawn. 

%   Author(s): Karen D. Gondoly
%              P. Gahinet 3/00
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.8 $  $Date: 2000/06/14 19:59:10 $

% Defaults
if nargin<3,
    Render = 'plain';
end

% Draw it
Handles = LocalDrawStruct(AxisHandle,ConfigNumber,Render);


%---------------------Internal Functions-----------------------

%%%%%%%%%%%%%%%%%%%%%%%
%%% LocalDrawStruct %%% 
%%%%%%%%%%%%%%%%%%%%%%%
function Handles = LocalDrawStruct(AxisHandle,ConfigNumber,Render)   
% Draws the current feedback structure

SignalFlag = strcmp(Render(1),'s');

% Delete all children
kids = get(AxisHandle,'Children');
delete(kids(ishandle(kids)))

% Initialize
set(AxisHandle,'Ylim',[0 1],'Xlim',[0 .9+.1*SignalFlag],...
    'XlimMode','manual','YlimMode','manual');
Handles = struct('OpenLine',[],'ClosedLine',[],'SumPoint',[],...
    'FilterPatch',[],'FilterText',[],'CompPatch',[],'CompText',[],...
    'SensorPatch',[],'SensorText',[],'PlantPatch',[],'PlantText',[],...
    'Signals',[]);

%---Add new cases to add new feedback structures
switch ConfigNumber
case 1
    % Tracking loop, compensator in forward path
    yr = .7+.02*SignalFlag;
    Handles.OpenLine = line([0.05 .8 .76 NaN .8 .76 NaN .11 .15 .11], ...
        [.65 .65 .68 NaN .65 .62 NaN .62 .65 .68],'Color',[0 0 0],'Parent',AxisHandle);
    Handles.ClosedLine = line([.3 .3 yr yr NaN .59 .55 .59],...
        [.65 .2 .2 .65 NaN .17 .2 .23],'Color',[0 0 0],'Parent',AxisHandle);
    Handles.SumPoint = line(.3,.65,'Marker','o','MarkerEdgeColor',[0 0 0],...
        'MarkerFaceColor',[0 0 0],'MarkerSize',5,'Parent',AxisHandle);
    Handles.FilterPatch=patch([.15 .25 .25 .15 .15],[.5 .5 .8 .8 .5],'y',...
        'Parent',AxisHandle);
    Handles.FilterText = ...
        text(.18,.65,'F','Parent',AxisHandle,'FontSize',7,'Color',[0 0 0]);
    Handles.CompPatch = patch([.35 .45 .45 .35 .35],[.5 .5 .8 .8 .5],'c',...
        'Parent',AxisHandle,'Tag','CompPatch');
    Handles.CompText = ...
        text(.38,.65,'C','Parent',AxisHandle,'FontSize',7,'Color',[0 0 0]);
    Handles.PlantPatch=patch([.55 .65 .65 .55 .55],[.5 .5 .8 .8 .5],'y', ...
        'Parent',AxisHandle,'Tag','PlantPatch');
    Handles.PlantText = ...
        text(.58,.65,'G','Parent',AxisHandle,'FontSize',7,'Color',[0 0 0]);
    Handles.SensorPatch = patch([.45 .55 .55 .45 .45],[.05 .05 .35 .35 .05],'y', ...
        'Parent',AxisHandle,'Tag','SensorPatch');
    Handles.SensorText = ...
        text(.48,.2,'H','Parent',AxisHandle,'FontSize',7,'Color',[0 0 0]);
    if SignalFlag
        Handles.Signals = [...
                text(0.02,.65,'r','Parent',AxisHandle,'FontSize',7,'Color',[0 0 0]) ; ...
                text(0.83,.65,'y','Parent',AxisHandle,'FontSize',7,'Color',[0 0 0]) ; ...
                text(0.49,.55,'u','Parent',AxisHandle,'FontSize',7,'Color',[0 0 0]) ; ...
                line([.5 .5 NaN .5 .49 NaN .5 .51],[.85 .65 NaN .65 .7 NaN .65 .7],...
                'Color',[0 0 0],'Parent',AxisHandle) ; ...
                text(0.49,.92,'du','Parent',AxisHandle,'FontSize',7,'Color',[0 0 0]) ; ...
                line([.7 .7 NaN .7 .69 NaN .7 .71],[.85 .65 NaN .65 .7 NaN .65 .7],...
                'Color',[0 0 0],'Parent',AxisHandle) ; ...
                text(0.69,.92,'dy','Parent',AxisHandle,'FontSize',7,'Color',[0 0 0]) ; ...
                line([.35 .35 NaN .34 .35 NaN .35 .36],[.2 0.05 NaN .15 .2 NaN .2 .15],...
                'Color',[0 0 0],'Parent',AxisHandle) ; ...
                text(.345,0,'n','Parent',AxisHandle,'FontSize',7,'Color',[0 0 0])];
    end
    
case 2,
    % Tracking loop, compensator in return path
    Handles.OpenLine = line([0.05 .8 .76 NaN .8 .76 NaN .11 .15 .11], ...
        [.65 .65 .68 NaN .65 .62 NaN .62 .65 .68],'Color',[0 0 0],'Parent',AxisHandle);
    Handles.ClosedLine = line([.3 .3 .7 .7 NaN .69 .65 .69],...
        [.65 .2 .2 .65 NaN .17 .2 .23],...
        'Color',[0 0 0],'Parent',AxisHandle,'Tag','ClosedLine');
    Handles.SumPoint = line(.3,.65,'Marker','o','MarkerEdgeColor',[0 0 0],...
        'MarkerFaceColor',[0 0 0],'MarkerSize',5,'Parent',AxisHandle);
    Handles.FilterPatch = patch([.15 .25 .25 .15 .15],[.5 .5 .8 .8 .5],'y',...
        'Parent',AxisHandle,'Tag','FilterPatch');
    Handles.FilterText = ...
        text(.18,.65,'F','Parent',AxisHandle,'Tag','FilterText','FontSize',7,'Color',[0 0 0]);
    Handles.PlantPatch=patch([.45 .55 .55 .45 .45],[.5 .5 .8 .8 .5],'y', ...
        'Parent',AxisHandle,'Tag','PlantPatch');
    Handles.PlantText = ...
        text(.48,.65,'G','Parent',AxisHandle,'Tag','PlantText','FontSize',7,'Color',[0 0 0]);
    Handles.CompPatch=patch([.35 .45 .45 .35 .35],[.05 .05 .35 .35 .05],'c',...
        'Parent',AxisHandle,'Tag','CompPatch');
    Handles.CompText = ...
        text(.38,.2,'C','Parent',AxisHandle,'Tag','CompText','FontSize',7,'Color',[0 0 0]);
    Handles.SensorPatch = patch([.55 .65 .65 .55 .55],[.05 .05 .35 .35 .05],'y', ...
        'Parent',AxisHandle,'Tag','SensorPatch');
    Handles.SensorText = ...
        text(.58,.2,'H','Parent',AxisHandle,'Tag','SensorText','FontSize',7,'Color',[0 0 0]);
    if SignalFlag
        Handles.Signals = [...
                text(0.02,.65,'r','Parent',AxisHandle,'FontSize',7,'Color',[0 0 0]) ; ...
                text(0.83,.65,'y','Parent',AxisHandle,'FontSize',7,'Color',[0 0 0]) ; ...
                text(0.4,.56,'u','Parent',AxisHandle,'FontSize',7,'Color',[0 0 0]) ; ...
                line([.36 .36 NaN .36 .35 NaN .36 .37],[.85 .65 NaN .65 .7 NaN .65 .7],...
                'Color',[0 0 0],'Parent',AxisHandle) ; ...
                text(0.35,.92,'du','Parent',AxisHandle,'FontSize',7,'Color',[0 0 0]) ; ...
                line([.65 .65 NaN .65 .64 NaN .65 .66],[.85 .65 NaN .65 .7 NaN .65 .7],...
                'Color',[0 0 0],'Parent',AxisHandle) ; ...
                text(0.64,.92,'dy','Parent',AxisHandle,'FontSize',7,'Color',[0 0 0]) ; ...
                line([.5 .5 NaN .49 .5 NaN .5 .51],[.2 0.05 NaN .15 .2 NaN .2 .15],...
                'Color',[0 0 0],'Parent',AxisHandle) ; ...
                text(.495,0,'n','Parent',AxisHandle,'FontSize',7,'Color',[0 0 0])];
    end
    
case 3,
    Handles.OpenLine = line([.2 .8 .76 NaN .8 .76 NaN .31 .35 .31 NaN 0.1 .35 NaN .31 .35 .31], ...
        [.65 .65 .68 NaN .65 .62 NaN .62 .65 .68 NaN .75 .75 NaN .72 .75 .78],...
        'Color',[0 0 0],'Parent',AxisHandle);
    Handles.ClosedLine = line([.2 .2 .7 .7 NaN .59 .55 .59],...
        [.65 .2 .2 .65 NaN .17 .2 .23],...
        'Color',[0 0 0],'Parent',AxisHandle,'Tag','ClosedLine');
    Handles.CompPatch=patch([.35 .45 .45 .35 .35],[.5 .5 .85 .85 .5],'c',...
        'Parent',AxisHandle,'Tag','CompPatch');
    Handles.CompText=text(.375,.675,'C','Parent',AxisHandle,'Tag','CompText','FontSize',7);
    Handles.PlantPatch=patch([.55 .65 .65 .55 .55],[.5 .5 .8 .8 .5],'y', ...
        'Parent',AxisHandle,'Tag','PlantPatch');
    Handles.PlantText=text(.575,.65,'G','Parent',AxisHandle,'Tag','PlantText','FontSize',7);
    Handles.SensorPatch=patch([.45 .55 .55 .45 .45],[.05 .05 .35 .35 .05],'y', ...
        'Parent',AxisHandle,'Tag','SensorPatch');
    Handles.SensorText=text(.475,.2,'H','Parent',AxisHandle,'Tag','SensorText','FontSize',7);
    
case 4,
    Handles.OpenLine = line([.15 .75 .69 NaN .75 .69 NaN .36 .41 .36], ...
        [.75 .75 .78 NaN .75 .72 NaN .72 .75 .78],'Color',[0 0 0],'Parent',AxisHandle);
    Handles.ClosedLine = line([.4 .25 .25 .65 .65 .5 NaN .54 .5 .54],...
        [.65 .65 .2 .2 .65 .65 NaN .17 .2 .23],...
        'Color',[0 0 0],'Parent',AxisHandle,'Tag','ClosedLine');
    Handles.PlantPatch=patch([.4 .5 .5 .4 .4],[.5 .5 .85 .85 .5],'y', ...
        'Parent',AxisHandle,'Tag','PlantPatch');
    Handles.PlantText=text(.425,.675,'G','Parent',AxisHandle,'Tag','PlantText','FontSize',7);
    Handles.CompPatch=patch([.4 .5 .5 .4 .4],[.05 .05 .35 .35 .05],'c', ...
        'Parent',AxisHandle,'Tag','CompPatch');
    Handles.CompText=text(.425,.2,'C','Parent',AxisHandle,'Tag','CompText','FontSize',7);
    
end % switch ConfigNumber





%%%%%%%%%%%%%%%%%%%%%%%
%%% LocalOpenStruct %%% %---Opens the Feedback Structure Window
%%%%%%%%%%%%%%%%%%%%%%%
function a = LocalOpenStruct(ParentFig,OldStructNum,SISOflag);

StdColor = get(0,'DefaultFigureColor');
PointsToPixels = 72/get(0,'ScreenPixelsPerInch');
StdUnit = 'points';

CurrentVal = zeros(1,4);
CurrentVal(OldStructNum)=1;

if SISOflag,
   FigWidth = 170;
   NumConfig = 2;
else
   FigWidth = 300;
   NumConfig = 4;   
end

a = figure('Color',StdColor, ...
   'MenuBar','none', ...
   'WindowStyle','modal',...
   'Name',xlate('Feedback Structures'), ...
   'NumberTitle','off', ...
   'IntegerHandle','off', ...
   'Resize','off', ...
   'Unit','point', ...
   'Position',[86.25 93.75 FigWidth 180],...
   'visible','off',...
   'Tag','ConfigFig');

ConfigText = [{'Compensator in the Forward path'};
   {'Compensator in the Feedback path'};{' '};{' '}];

b = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'Position',[102 215 113.25 12], ...
   'backgroundcolor',StdColor, ...
   'FontWeight','bold', ...
   'String','Select a configuration', ...
   'Style','text');

for ct=1:NumConfig,
   
   t = uicontrol('Parent',a, ...
      'Units',StdUnit,...
      'style','text', ...
      'FontWeight','bold', ...
      'FontSize',8,...
      'string',ConfigText{ct}, ...
      'BackgroundColor',StdColor, ...
      'Position',[12+(150*floor(ct/3)) 163-(74*rem(ct+1,2)) 150 12]);
   
   b = axes('Parent',a, ...
      'Units',StdUnit, ...
      'Box','on', ...
      'CameraUpVector',[0 1 0], ...
      'Color',[1 1 1], ...
      'Position',[33.75+(144.75*floor(ct/3)) 109-(74*rem(ct+1,2)) 108 50], ...
      'Tag',['Config',num2str(ct),'Axes'], ...
      'XColor',[0 0 0], ...
      'XTickMode','manual', ...
      'YColor',[0 0 0], ...
      'YLimMode','manual', ...
      'YTickMode','manual', ...
      'ZColor',[0 0 0]);
   
   Handles = LocalDrawStruct(b,ct);
   
   ConfigButton(ct) = uicontrol('Parent',a, ...
      'Units',StdUnit, ...
      'BackgroundColor',[0.8 0.8 0.8], ...
      'Position',[12+(150*floor(ct/3)) 134-(74*rem(ct+1,2)) 15 15], ...
      'Style','radiobutton', ...
      'Callback','rlfcn(''radiocallback'');', ...
      'Tag',['Config',num2str(ct),'Button'], ...
      'Value',CurrentVal(ct));
end

if SISOflag,
   set(ConfigButton(1),'UserData',ConfigButton(2));
   set(ConfigButton(2),'UserData',ConfigButton(1));
else
   set(ConfigButton(1),'UserData',ConfigButton(2:4));
   set(ConfigButton(2),'UserData',ConfigButton([1,3:4]));
   set(ConfigButton(3),'UserData',ConfigButton([1:2,4]));
   set(ConfigButton(4),'UserData',ConfigButton(1:3));
end

b = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'Position',[5 5 50 20], ...
   'String','Help', ...
   'Callback','loopstruct(''help'',gcbf);', ...
   'Tag','HelpButton');
b = uicontrol('Parent',a, ...
   'Units','points', ...
   'Position',[FigWidth/2-25 5 50 20], ...
   'String','OK', ...
   'Callback','loopstruct(''apply'',gcbf);', ...
   'Tag','OKButton');
b = uicontrol('Parent',a, ...
   'Units',StdUnit, ...
   'Position',[FigWidth-55 5.25 50 20], ...
   'String','Cancel', ...
   'UserData',ParentFig,...
   'Callback','loopstruct(''close'',gcbf);', ...
   'Tag','CancelButton');

set(a,'visible','on',...
   'UserData',struct('Parent',ParentFig,'StructNum',OldStructNum, ...
   'ConfigButtons',ConfigButton));
