AutotunerPID Toolkit Previous page   Next Page

envgui

PURPOSE ^

Matlab S-function for making a simple PID GUI.

SYNOPSIS ^

function [sys,x0,str,ts] = envgui(t,x,u,flag,NoiseBlock,LoadDistBlock)

DESCRIPTION ^

ENVGUI S-function for making a simple PID GUI

   When the model autotunerPID.mdl is run, this S-function create a panel
   designed as a console from which controlling the simulation
   environment.  For example a measurement noise or a step on the load
   disturbance can be included. 
   The most interesting part of this interface is the one termed
   ``Comparative Analysis'' which include analysis tools for both time and
   frequency domain.  
   The time domain analysis perform a comparison of the responses to a
   step on the set-point and on the load disturbance for a PID tuned with
   all the available methods.
   The frequency domain analysis reports the open loop Bode diagram of the
   control system together with the Bode diagram of the complementary
   sensitivity and the sensitivity function. 

   Everything started looking at PENDAN, an S-function for making pendulum
   animation

   Author:    William Spinelli (wspinell@elet.polimi.it)
   Copyright  2004 W.Spinelli
   $Revision: 1.0 $  $Date: 2004/02/27 12:00:00 $

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [sys,x0,str,ts] = envgui(t,x,u,flag,NoiseBlock,LoadDistBlock)
0002 %ENVGUI S-function for making a simple PID GUI
0003 %
0004 %   When the model autotunerPID.mdl is run, this S-function create a panel
0005 %   designed as a console from which controlling the simulation
0006 %   environment.  For example a measurement noise or a step on the load
0007 %   disturbance can be included.
0008 %   The most interesting part of this interface is the one termed
0009 %   ``Comparative Analysis'' which include analysis tools for both time and
0010 %   frequency domain.
0011 %   The time domain analysis perform a comparison of the responses to a
0012 %   step on the set-point and on the load disturbance for a PID tuned with
0013 %   all the available methods.
0014 %   The frequency domain analysis reports the open loop Bode diagram of the
0015 %   control system together with the Bode diagram of the complementary
0016 %   sensitivity and the sensitivity function.
0017 %
0018 %   Everything started looking at PENDAN, an S-function for making pendulum
0019 %   animation
0020 %
0021 %   Author:    William Spinelli (wspinell@elet.polimi.it)
0022 %   Copyright  2004 W.Spinelli
0023 %   $Revision: 1.0 $  $Date: 2004/02/27 12:00:00 $
0024 
0025 % Plots every major integration step, but has no states of its own
0026 switch flag,
0027    
0028    % Initialization
0029    case 0,
0030       [sys,x0,str,ts] = mdlInitializeSizes(NoiseBlock,LoadDistBlock);
0031       % Update
0032    case 2,
0033       sys = mdlUpdate(t,x,u);
0034       % Unused flags
0035    case { 1, 3, 4, 9 },
0036       sys = [];
0037       % DeleteBlock
0038    case 'DeleteBlock',
0039       LocalDeleteBlock
0040       % DeleteFigure
0041    case 'DeleteFigure',
0042       LocalDeleteFigure
0043       % Close
0044    case 'Close',
0045       LocalClose
0046       % Noise
0047    case 'Noise',
0048       LocalNoise
0049       % NoiseParam
0050    case 'NoiseParam',
0051       LocalNoiseParam
0052       % Dist
0053    case 'Dist',
0054       LocalDist
0055       % DistParam
0056    case 'DistParam',
0057       LocalDistParam
0058       % StepPID
0059    case 'StepPID',
0060       LocalStepPID
0061       % BodePID
0062    case 'BodePID',
0063       LocalBodePID
0064       % Help
0065    case 'Hlp',
0066       LocalHlp
0067       % Unexpected flags
0068    otherwise
0069       error(['Unhandled flag = ',num2str(flag)]);
0070 end
0071 % end envgui
0072 
0073 %=============================================================================
0074 % mdlInitializeSizes
0075 % Return the sizes, initial conditions, and sample times for the S-function.
0076 %=============================================================================
0077 function [sys,x0,str,ts] = mdlInitializeSizes(NoiseBlock,LoadDistBlock)
0078 % initialize parameters (setpoint)
0079 set_param(get_param([get_param(gcs,'Parent') '/' NoiseBlock],'Handle'),...
0080    'Variance','0');
0081 set_param(get_param([get_param(gcs,'Parent') '/' LoadDistBlock],'Handle'),...
0082    'Value','0');
0083 
0084 % set up S-function
0085 sizes = simsizes;
0086 
0087 sizes.NumContStates  = 0;
0088 sizes.NumDiscStates  = 0;
0089 sizes.NumOutputs     = 0;
0090 sizes.NumInputs      = 0;
0091 sizes.DirFeedthrough = 0;
0092 sizes.NumSampleTimes = 1;
0093 
0094 sys = simsizes(sizes);
0095 
0096 x0  = [];
0097 str = [];
0098 ts  = [0.1 0];
0099 
0100 LocalEnvInit(NoiseBlock,LoadDistBlock);
0101 % end mdlInitializeSizes
0102 
0103 
0104 %=============================================================================
0105 % mdlUpdate
0106 % Update the Environment Control GUI animation.
0107 %=============================================================================
0108 function sys = mdlUpdate(t,x,u)
0109 fig = get_param(gcbh,'UserData');
0110 if ishandle(fig),
0111    if strcmp(get(fig,'Visible'),'on'),
0112       ud = get(fig,'UserData');
0113       LocalEnvSets(t,ud,u);
0114    end
0115 end
0116 sys = [];
0117 % end mdlUpdate
0118 
0119 
0120 %=============================================================================
0121 % LocalDeleteBlock
0122 % The animation block is being deleted, delete the associated figure.
0123 %=============================================================================
0124 function LocalDeleteBlock
0125 fig = get_param(gcbh,'UserData');
0126 if ishandle(fig),
0127    delete(fig);
0128    set_param(gcbh,'UserData',-1)
0129 end
0130 % end LocalDeleteBlock
0131 
0132 
0133 %=============================================================================
0134 % LocalClose
0135 % The callback function for the animation window close button.  Delete
0136 % the animation figure window.
0137 %=============================================================================
0138 function LocalClose
0139 delete(gcbf)
0140 % end LocalClose
0141 
0142 
0143 %=============================================================================
0144 % LocalDeleteFigure
0145 % The animation figure is being deleted, set the S-function UserData to -1.
0146 %=============================================================================
0147 function LocalDeleteFigure
0148 ud = get(gcbf,'UserData');
0149 set_param(ud.Block,'UserData',-1);
0150 % end LocalDeleteFigure
0151 
0152 
0153 %=============================================================================
0154 % LocalNoise
0155 % The callback function for the activation/deactivation of the measurement
0156 % noise
0157 %=============================================================================
0158 function LocalNoise
0159 ud = get(gcbf,'UserData');
0160 if get(ud.Noise,'Value')
0161    set_param(ud.NoiseBlock,'Variance',get(ud.NoiseVar,'String'));
0162 else
0163    set_param(ud.NoiseBlock,'Variance',num2str(0));
0164 end
0165 % end LocalNoise
0166 
0167 
0168 %=============================================================================
0169 % LocalNoiseParam
0170 % The callback function for setting of noise variance
0171 %=============================================================================
0172 function LocalNoiseParam
0173 ud = get(gcbf,'UserData');
0174 if get(ud.Noise,'Value')
0175    set_param(ud.NoiseBlock,'Variance',get(ud.NoiseVar,'String'));
0176 end
0177 % end LocalNoiseParam
0178 
0179 
0180 %=============================================================================
0181 % LocalDist
0182 % The callback function for the activation/deactivation of load disturbance
0183 %=============================================================================
0184 function LocalDist
0185 ud = get(gcbf,'UserData');
0186 if get(ud.LoadDist,'Value')
0187    set_param(ud.LoadDistBlock,'Value',get(ud.LoadAmp,'String'));
0188 else
0189    set_param(ud.LoadDistBlock,'Value',num2str(0));
0190 end
0191 % end LocalDist
0192 
0193 
0194 %=============================================================================
0195 % LocalDistParam
0196 % The callback function for setting the load diturbance amplitude
0197 %=============================================================================
0198 function LocalDistParam
0199 ud = get(gcbf,'UserData');
0200 if get(ud.LoadDist,'Value')
0201    set_param(ud.LoadDistBlock,'Value',get(ud.LoadAmp,'String'));
0202 end
0203 % end LocalDistParam
0204 
0205 
0206 %=============================================================================
0207 % LocalStepPID
0208 % The callback function for comparing step response with different
0209 % autotuning method
0210 %=============================================================================
0211 function LocalStepPID
0212 global MODELRUNNING
0213 if ~MODELRUNNING
0214    try
0215       num = str2num(get_param('AutotunerPID/Plant/Transfer Fcn',...
0216          'Numerator'));
0217       den = str2num(get_param('AutotunerPID/Plant/Transfer Fcn',...
0218          'Denominator'));
0219       tau = str2num(get_param('AutotunerPID/Plant/Transport Delay',...
0220          'Delay'));
0221    catch
0222       msgbox('The step response can be computed only for linear models',...
0223          'AutotunerPID','error');
0224       return
0225    end
0226    stepPIDcompare(num,den,tau);
0227 else
0228    msgbox('The simulation must be STOPPED to perform the Comparative Analysis',...
0229          'AutotunerPID','warn');
0230 end
0231 % end LocalStepPID
0232 
0233 
0234 %=============================================================================
0235 % LocalBodePID
0236 % The callback function for comparing Bode diagram with different
0237 % autotuning method
0238 %=============================================================================
0239 function LocalBodePID
0240 global MODELRUNNING
0241 if ~MODELRUNNING
0242    try
0243       num = str2num(get_param('AutotunerPID/Plant/Transfer Fcn','Numerator'));
0244       den = str2num(get_param('AutotunerPID/Plant/Transfer Fcn','Denominator'));
0245       tau = str2num(get_param('AutotunerPID/Plant/Transport Delay','Delay'));
0246    catch
0247       msgbox('The Bode diagrams can be computed only for linear models',...
0248          'AutotunerPID','error');
0249       return
0250    end
0251    bodePIDcompare(num,den,tau);
0252 else
0253    msgbox('The simulation must be STOPPED to perform the Comparative Analysis',...
0254          'AutotunerPID','warn');
0255 end
0256 
0257 % end LocalBodePID
0258 
0259 
0260 %=============================================================================
0261 % LocalHlp
0262 % The callback function for Help
0263 %=============================================================================
0264 function LocalHlp
0265 web([strrep(which('envgui'),'envgui.m','') 'help/index.html'],'-browser')
0266 % end LocalHlp
0267 
0268 
0269 %=============================================================================
0270 % LocalEnvSets
0271 % Local function to set the position of the graphics objects in the Env GUI
0272 % animation window.
0273 %=============================================================================
0274 function LocalEnvSets(time,ud,u)
0275 global AUTOTUNE
0276 global ENVINACTIVE
0277 
0278 if AUTOTUNE
0279    set(ud.Noise,'Enable','off');
0280    set(ud.NoiseVar,'Enable','off');
0281    set(ud.LoadDist,'Enable','off');
0282    set(ud.LoadAmp,'Enable','off');
0283    ENVINACTIVE = 1;
0284 elseif ~AUTOTUNE & ENVINACTIVE
0285    % Autotuning process completed (update parameters and set the GUI
0286    % active)
0287    set(ud.Noise,'Enable','on');
0288    set(ud.NoiseVar,'Enable','on');
0289    set(ud.LoadDist,'Enable','on');
0290    set(ud.LoadAmp,'Enable','on');
0291    ENVINACTIVE = 0;
0292 end
0293 
0294 % Force plot to be drawn
0295 pause(0), drawnow
0296 % end LocalEnvSets
0297 
0298 
0299 %=============================================================================
0300 % LocalEnvInit
0301 % Local function to initialize the Env GUI animation.  If the animation
0302 % window already exists, it is brought to the front.  Otherwise, a new
0303 % figure window is created.
0304 %=============================================================================
0305 function LocalEnvInit(NoiseBlock,LoadDistBlock)
0306 % The name of the reference is derived from the name of the
0307 % subsystem block that owns the GUI S-function block.
0308 % This subsystem is the current system and is assumed to be the same
0309 % layer at which the reference block resides.
0310 sys = get_param(gcs,'Parent');
0311 
0312 global AUTOTUNE
0313 global AUTOMAN
0314 global INACTIVE
0315 
0316 
0317 % The animation figure handle is stored in the GUI block's UserData.
0318 % If it exists, initialize all the fields
0319 Fig = get_param(gcbh,'UserData');
0320 if ishandle(Fig),
0321    FigUD = get(Fig,'UserData');
0322    
0323    set(FigUD.Noise,...
0324       'Value',0,...
0325       'Enable','on');
0326    set(FigUD.NoiseVar,...
0327       'String','0.0001',...
0328       'Enable','on');
0329    set(FigUD.LoadDist,...
0330       'Value',0,...
0331       'Enable','on');
0332    set(FigUD.LoadAmp,...
0333       'String','1',...
0334       'Enable','on');
0335    
0336    % bring it to the front
0337    figure(Fig);
0338    return
0339 end
0340 
0341 % the animation figure doesn't exist, create a new one and store its
0342 % handle in the animation block's UserData
0343 FigureName = 'Environment Panel';
0344 
0345 % Figure
0346 FigH = 150;                % figure height
0347 FigW = 272;                % figure width
0348 Fig = figure(...
0349    'Units',              'pixel',...
0350    'Position',           [100 300-FigH FigW FigH],...
0351    'Name',               FigureName,...
0352    'NumberTitle',        'off',...
0353    'IntegerHandle',      'off',...
0354    'HandleVisibility',   'callback',...
0355    'Resize',             'off',...
0356    'MenuBar',            'none',...
0357    'DoubleBuffer',       'on',...
0358    'DeleteFcn',          'envgui([],[],[],''DeleteFigure'')',...
0359    'CloseRequestFcn',    'envgui([],[],[],''Close'');');
0360 
0361 % operating condition
0362 uicontrol(...
0363    'Parent',             Fig,...
0364    'Style',              'text',...
0365    'Units',              'pixel',...
0366    'Position',           [12 FigH-19 140 14], ...
0367    'HorizontalAlignment','left',...
0368    'Fontweight',         'bold',...
0369    'Backgroundcolor',    [0.8 0.8 0.8],...
0370    'String',             'Operating Conditions');
0371 uicontrol(...
0372    'Parent',             Fig,...
0373    'Style',              'frame',...
0374    'Units',              'pixel',...
0375    'Position',           [12 FigH-64 248 44]);
0376 Noise = uicontrol(...
0377    'Parent',             Fig,...
0378    'Style',              'checkbox',...
0379    'Position',           [16 FigH-42 140 18],...
0380    'String',             'Measurement noise',...
0381    'Fontweight',         'bold',...
0382    'Value',              0,...
0383    'Callback',           'envgui([],[],[],''Noise'');');
0384 uicontrol(...
0385    'Parent',             Fig,...
0386    'Style',              'text',...
0387    'Units',              'pixel',...
0388    'Position',           [156 FigH-40 40 14], ...
0389    'HorizontalAlignment','right',...
0390    'Fontweight',         'bold',...
0391    'String',             'var. ');
0392 NoiseVar = uicontrol(...
0393    'Parent',             Fig,...
0394    'Style',              'edit',...
0395    'Units',              'pixel',...
0396    'Position',           [196 FigH-42 60 18], ...
0397    'HorizontalAlignment','center',...
0398    'String',             '0.0001',...
0399    'Backgroundcolor',    [1 1 1],...
0400    'Callback',           'envgui([],[],[],''NoiseParam'');');
0401 LoadDist = uicontrol(...
0402    'Parent',             Fig,...
0403    'Style',              'checkbox',...
0404    'Position',           [16 FigH-62 150 18],...
0405    'String',             'Load disturbance',...
0406    'Fontweight',         'bold',...
0407    'Value',              0,...
0408    'Callback',           'envgui([],[],[],''Dist'');');
0409 uicontrol(...
0410    'Parent',             Fig,...
0411    'Style',              'text',...
0412    'Units',              'pixel',...
0413    'Position',           [166 FigH-60 30 14], ...
0414    'HorizontalAlignment','right',...
0415    'Fontweight',         'bold',...
0416    'String',             'amp. ');
0417 LoadAmp = uicontrol(...
0418    'Parent',             Fig,...
0419    'Style',              'edit',...
0420    'Units',              'pixel',...
0421    'Position',           [196 FigH-60 60 18], ...
0422    'HorizontalAlignment','center',...
0423    'String',             '1',...
0424    'Backgroundcolor',    [1 1 1],...
0425    'Callback',           'envgui([],[],[],''DistParam'');');
0426 
0427 % compact analysis
0428 uicontrol(...
0429    'Parent',             Fig,...
0430    'Style',              'text',...
0431    'Units',              'pixel',...
0432    'Position',           [12 FigH-88 240 14], ...
0433    'HorizontalAlignment','left',...
0434    'Fontweight',         'bold',...
0435    'FontSize',           8,...
0436    'Backgroundcolor',    [0.8 0.8 0.8],...
0437    'String',             'Comparative Analysis');
0438 uicontrol(...
0439    'Parent',             Fig,...
0440    'Style',              'frame',...
0441    'Units',              'pixel',...
0442    'Position',           [12 FigH-118 248 28]);
0443 uicontrol(...
0444    'Parent',             Fig,...
0445    'Style',              'pushbutton',...
0446    'Position',           [22 FigH-114 72 20],...
0447    'String',             'Time', ...
0448    'Fontweight',         'bold',...
0449    'Callback',           'envgui([],[],[],''StepPID'');');
0450 uicontrol(...
0451    'Parent',             Fig,...
0452    'Style',              'pushbutton',...
0453    'Position',           [176 FigH-114 72 20],...
0454    'String',             'Frequency', ...
0455    'Fontweight',         'bold',...
0456    'Callback',           'envgui([],[],[],''BodePID'');');
0457 % help button
0458 uicontrol(...
0459    'Parent',             Fig,...
0460    'Style',              'pushbutton',...
0461    'Position',           [100 FigH-145 72 20],...
0462    'String',             'Help', ...
0463    'Fontweight',         'bold',...
0464    'Callback',           'envgui([],[],[],''Hlp'');');
0465 
0466 
0467 % all the HG objects are created, store them into the Figure's UserData
0468 
0469 % operating conditions
0470 FigUD.Noise        = Noise;
0471 FigUD.NoiseVar     = NoiseVar;
0472 FigUD.LoadDist     = LoadDist;
0473 FigUD.LoadAmp      = LoadAmp;
0474 % Simulink Block Interaction
0475 FigUD.Block        = get_param(gcbh,'Handle');
0476 FigUD.NoiseBlock   = get_param([sys '/' NoiseBlock],'Handle');
0477 FigUD.LoadDistBlock= get_param([sys '/' LoadDistBlock],'Handle');
0478 
0479 set(Fig,'UserData',FigUD);
0480 
0481 drawnow
0482 
0483 % store the figure handle in the animation block's UserData
0484 set_param(gcbh,'UserData',Fig);
0485 % end LocalEnvInit

Previous page  butterdesign idareas Next page

Generated on Wed 17-Mar-2004 09:29:44 by m2html © 2003