function pend0(RefBlock)
%PEND0  Initializes pendulum animation.
%       PEND0(SYS,REFBLOCK) initializes the pendulum animation.  If
%       a pendulum animation figure is already on the screen, it
%       is brought to the front.  Otherwise a new figure is created.
%       Important animation handles are placed in the figure's UserData.
%
%       See also: PENDAN (.M, and .C), PENDDEMO, and PENDSETS.

%	Copyright (c) 1990-94 by The MathWorks, Inc.

CurrentBlock = get_param;
sys = get_param(CurrentBlock,'Parent');

% Hack until gecko 53170 is fixed
if isempty(sys),
   return
end

TimeClock = 0;
RefSignal = str2num(get_param([sys '/' RefBlock],'Value'));
XCart = 0;
Theta = 0;

XDelta = 2;
PDelta = 0.2;
FigureName = 'Pendulum Visualization';
XPendTop = XCart + 10*sin(Theta); % Will be zero
YPendTop = 10*cos(Theta);         % Will be 10
PDcosT = PDelta*cos(Theta);       % Will be 0.2
PDsinT = -PDelta*sin(Theta);      % Will be zero

Figures = get(0,'Chil');
for INDEX=1:length(Figures),
   if strcmp(get(Figures(INDEX),'Type'),'figure'),
      if strcmp(get(Figures(INDEX),'Name'),FigureName),
         Fig = Figures(INDEX);
         set(0,'CurrentF',Fig);
         FigUD = get(Fig,'UserData');
         Cart = FigUD(1);
         Pend = FigUD(2);
         TimeField = FigUD(3);
         RefMark = FigUD(5);
         set(RefMark,'XData',RefSignal+[-XDelta 0 XDelta]);
         set(TimeField,'String',num2str(TimeClock));
         set(Cart,'XData',ones(2,1)*[XCart-XDelta XCart+XDelta]);
         set(Pend,'XData', ...
          [XPendTop-PDcosT XPendTop+PDcosT; XCart-PDcosT XCart+PDcosT], ...
          'YData',[YPendTop-PDsinT YPendTop+PDsinT; -PDsinT PDsinT]);
         % Time to exit the routine
         return
      end % if right name
   end % if ifigure
end % for INDEX

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Due to *return* above, rest of code only executed if creating animation
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Fig = figure('Unit','pixel','Pos',[100 100 500 300],'Name',FigureName);
axes('Unit','pixel','Pos',[50 50 400 200],'CLim',[1 64], ...
     'Xlim',[-12 12],'Ylim',[-2 10],'Vis','off');
Cart = surface('XData',ones(2,1)*[XCart-XDelta XCart+XDelta], ...
   'YData',[0 0; -2 -2],'ZData',zeros(2),'CData',ones(2),'Erase','xor');
Pend = surface('XData',[XPendTop-PDcosT XPendTop+PDcosT; ...
                        XCart-PDcosT XCart+PDcosT], ...
   'YData',[YPendTop-PDsinT YPendTop+PDsinT; -PDsinT PDsinT], ...
   'ZData',zeros(2),'CData',11*ones(2),'Erase','xor');
RefMark = patch(RefSignal+[-XDelta 0 XDelta],[-2 0 -2],22);
uicontrol(Fig,'Style','text','Unit','pixel','Pos',[0 0 500 50]);
uicontrol(Fig,'Style','text','Unit','pixel','Pos',[150 0 100 25], ...
   'Horiz','right','String','Time: ');
TimeField = uicontrol(Fig,'Style','text','Unit','pixel', ...
   'Pos',[250 0 100 25],'Horiz','left','String',num2str(TimeClock));
SlideControl = uicontrol(Fig,'Style','slide','Unit','pixel', ...
   'Pos',[100 25 300 22],'Min',-9,'Max',9,'Value',RefSignal,'Call', ...
	['set_param(''' sys '/' RefBlock ''',''Value'',' ...
    'num2str(get(get(gcf,''CurrentO''),''Value'')));']);
uicontrol(Fig,'Style','push','Pos',[415 15 70 20],'String','Close', ...
   'Call','delete(gcf);');
uicontrol(Fig,'Style','push','Pos',[15 15 70 20],'String','Playback', ...
   'Call', ...
    ['ud = get(gcf,''UserData'');' ...
     'if exist(''t'')==1,' ...
     '   for i=1:length(t),' ...
     '      pendsets(t(i),ud,y(i,:));' ...
     '   end,' ...
     'else,' ...
     '   disp(''Must run simulation first.''),' ...
     'end']);
set(RefMark,'Erase','xor');
set(Fig,'UserData',[Cart Pend TimeField SlideControl RefMark], ...
   'NextPlot','new');

drawnow

% end pend0
