function dummy = update(Editor)
%UPDATE  Updates Root Locus Editor and regenerates plot.

%   Author(s): Karen D. Gondoly
%              P. Gahinet (UDD)
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.27 $  $Date: 2000/08/17 22:16:19 $

% REVISIT
dummy = [];

if strcmp(Editor.EditMode,'off') | strcmp(Editor.Visible,'off')
    % Editor is inactive
    return
end

% Model data
LoopData = Editor.LoopData;
CompData = LoopData.Compensator;
Ts = getTs(LoopData);  % sample time


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Update Frequency Response Data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
WarnState = warning('off');

% Get normalized open-loop (for Compensator.Gain.Magnitude=1)
NormOpenLoop = Editor.LoopData.OpenLoop;
GainMag = abs(getgain(Editor.LoopData)); 

% Compute adequate freq. grid and range (in rad/sec)
Editor.optlims(NormOpenLoop);

% Generate the Bode plot data
[Mag,Phase] = bode(NormOpenLoop,Editor.Frequency);
Editor.Magnitude = Mag(:);  % stored in absolute units
Editor.Phase = Phase(:);    % stored in degrees

warning(WarnState)

% Conversion factors
FreqConvert = unitconv(1,'rad/sec',Editor.FrequencyUnits);
MagData = unitconv(GainMag * Editor.Magnitude,'abs',Editor.MagnitudeUnits);
PhaseData = unitconv(Editor.Phase,'deg',Editor.PhaseUnits);
if Ts
    NyqFreq = FreqConvert * pi/Ts;
end

%%%%%%%%%%%%%
% Render Data
%%%%%%%%%%%%%
HG = Editor.HG;
PlotAxes = HG.PlotAxes;
Style = Editor.LineStyle;
UIC = get(PlotAxes(1),'uicontextmenu'); % axis ctx menu

% Clear exiting plot
DeleteH = get(PlotAxes,{'children'});
DeleteH = cat(1,DeleteH{:});
delete(DeleteH(ishandle(DeleteH)))

% Plot the fixed poles and zeros
% REVISIT: merge next
out = fixedpz(LoopData); [FixedZeros,FixedPoles] = deal(out{:});
HG.System = struct('Zero',cell(2,1),'Pole',cell(2,1));
if strcmp(Editor.ShowSystemPZ,'on')
    MagPZVis = Editor.MagVisible;
    PhasePZVis = Editor.PhaseVisible;
else
    MagPZVis = 'off';
    PhasePZVis = 'off';
end

% System zeros 
FixedZeros = [FixedZeros(~imag(FixedZeros),:) ; FixedZeros(imag(FixedZeros)>0,:)];
FreqZ = FreqConvert * damp(FixedZeros,Ts);
if Ts,
	% Discard roots whose CT frequency exceeds pi/Ts
	idx = find(FreqZ<=NyqFreq);
	FixedZeros = FixedZeros(idx);
	FreqZ = FreqZ(idx);
end
for ct=length(FixedZeros):-1:1
   HG.System(1).Zero(ct,1) = line(FreqZ(ct),NaN,...
      'Parent',PlotAxes(1),'UserData',FixedZeros(ct),'Visible',MagPZVis);
   HG.System(2).Zero(ct,1) = line(FreqZ(ct),NaN,...
      'Parent',PlotAxes(2),'UserData',FixedZeros(ct),'Visible',PhasePZVis);
end
set(cat(1,HG.System.Zero),...
    'LineStyle','none','Marker','o','MarkerSize',5,...
    'Color',Style.Color.System,...
    'UIContextMenu',UIC,...
    'HelpTopicKey','sisosystempolezero',...
    'ButtonDownFcn',{@LocalShowData Editor}); 
imfz = find(imag(FixedZeros)>0);
set([HG.System(1).Zero(imfz,:);HG.System(2).Zero(imfz,:)],'LineWidth',2)

% System poles
FixedPoles = [FixedPoles(~imag(FixedPoles),:) ; FixedPoles(imag(FixedPoles)>0,:)];
FreqP = FreqConvert * damp(FixedPoles,Ts);
if Ts,
	% Discard roots whose CT frequency exceeds pi/Ts
	idx = find(FreqP<=NyqFreq);
	FixedPoles = FixedPoles(idx);
	FreqP = FreqP(idx);
end
for ct=length(FixedPoles):-1:1
    HG.System(1).Pole(ct,1) = line(FreqP(ct),NaN,...
        'Parent',PlotAxes(1),'UserData',FixedPoles(ct),'Visible',MagPZVis);
    HG.System(2).Pole(ct,1) = line(FreqP(ct),NaN,...
        'Parent',PlotAxes(2),'UserData',FixedPoles(ct),'Visible',PhasePZVis);
end
set(cat(1,HG.System.Pole),...
    'LineStyle','none','Marker','x','MarkerSize',6,...
    'Color',Style.Color.System,...
    'UIContextMenu',UIC,...
    'HelpTopicKey','sisosystempolezero',...
    'ButtonDownFcn',{@LocalShowData Editor})
imfp = find(imag(FixedPoles)>0);
set([HG.System(1).Pole(imfp,:);HG.System(2).Pole(imfp,:)],'LineWidth',2)


% Plot the Bode diagrams
FreqData = FreqConvert * Editor.Frequency;
HG.BodePlot(1) = line(FreqData,MagData,...
   'Parent',PlotAxes(1),'Visible',Editor.MagVisible, ...
   'Color',Style.Color.Response,...
   'UIContextMenu',UIC,...
   'HelpTopicKey','sisomagnitudeplot',...
   'ButtonDownFcn',{@LocalMoveGain Editor});  
HG.BodePlot(2) = line(FreqData,PhaseData,...
   'Parent',PlotAxes(2),'Visible',Editor.PhaseVisible, ...
   'Color',Style.Color.Response,...
   'HelpTopicKey','sisophaseplot',...
   'UIContextMenu',UIC); 
% If discrete, add Nyquist lines
if Ts
    NyqLineM = line([NyqFreq;NyqFreq],[0 1],'Parent',PlotAxes(1),...
        'Color','k','Linestyle','-','Visible',Editor.MagVisible,'HitTest','off',...
        'HelpTopicKey','sisonyquistline');
    NyqLineP = line([NyqFreq;NyqFreq],[0 1],'Parent',PlotAxes(2),...
        'Color','k','Linestyle','-','Visible',Editor.PhaseVisible,'HitTest','off',...
        'HelpTopicKey','sisonyquistline');
    HG.NyquistLine = [NyqLineM ; NyqLineP];
else
    HG.NyquistLine = zeros(2,0);
end

% Update HG database
Editor.HG = HG;

% Plot the compensator poles and zeros
plotcomp(Editor);

% Set Y coordinates of poles/zeros to attach them to Bode curves
interpy(Editor,MagData,PhaseData);

% Update axis limits
% RE: Includes line handle restacking for proper layering, margin display,...
updatelims(Editor);


% REVISIT????
%---See if asymptotes need to be updated
%if strcmp(get(udAxM.Handles.UIcontextMenu.ShowAsymptotes,'Checked'),'on'),
%   udAxM.Asymptotes = bodeasym(OLsys,...
%      [],Ud.Handles.LTIdisplayAxes(2));
%end % if show asymptotes


%-------------------------Callback Functions-------------------------

%%%%%%%%%%%%%%%%%%%%%
%%% LocalShowData %%%
%%%%%%%%%%%%%%%%%%%%%
function LocalShowData(hSrc,event,Editor)
% Show system data
send(Editor.up,'ShowData');  % notify parent of open request


%%%%%%%%%%%%%%%%%%%%%
%%% LocalMoveGain %%%
%%%%%%%%%%%%%%%%%%%%%
function LocalMoveGain(hSrc,event,Editor)
% Callback for button down on mag plot

SISOfig = gcbf;
if strcmp(Editor.EditMode,'idle') & ...
        strcmp(get(SISOfig,'SelectionType'),'normal')
    % Left click: initialize dragging of closed-loop pole
    % Switch edit mode to drag
    Editor.ModeData = 'gain';
    Editor.EditMode = 'drag';
    % Change pointer
    setptr(SISOfig,'closedhand')
    % Initialize tracking algorithm and notify peers
    Status = trackgain(Editor,'init');
	hgset_status(Editor,Status);
end
