⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 envgui.m

📁 这是一个PID自动调节的程序
💻 M
📖 第 1 页 / 共 2 页
字号:
function [sys,x0,str,ts] = envgui(t,x,u,flag,NoiseBlock,LoadDistBlock)
%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 $

% Plots every major integration step, but has no states of its own
switch flag,
   
   % Initialization 
   case 0,
      [sys,x0,str,ts] = mdlInitializeSizes(NoiseBlock,LoadDistBlock);
      % Update 
   case 2,
      sys = mdlUpdate(t,x,u);
      % Unused flags
   case { 1, 3, 4, 9 },
      sys = [];
      % DeleteBlock
   case 'DeleteBlock',
      LocalDeleteBlock
      % DeleteFigure
   case 'DeleteFigure',
      LocalDeleteFigure
      % Close
   case 'Close',
      LocalClose
      % Noise
   case 'Noise',
      LocalNoise
      % NoiseParam
   case 'NoiseParam',
      LocalNoiseParam
      % Dist
   case 'Dist',
      LocalDist
      % DistParam
   case 'DistParam',
      LocalDistParam
      % StepPID
   case 'StepPID',
      LocalStepPID
      % BodePID
   case 'BodePID',
      LocalBodePID
      % Help
   case 'Hlp',
      LocalHlp
      % Unexpected flags
   otherwise
      error(['Unhandled flag = ',num2str(flag)]);
end
% end envgui

%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
function [sys,x0,str,ts] = mdlInitializeSizes(NoiseBlock,LoadDistBlock)
% initialize parameters (setpoint)
set_param(get_param([get_param(gcs,'Parent') '/' NoiseBlock],'Handle'),...
   'Variance','0');
set_param(get_param([get_param(gcs,'Parent') '/' LoadDistBlock],'Handle'),...
   'Value','0');

% set up S-function
sizes = simsizes;

sizes.NumContStates  = 0;
sizes.NumDiscStates  = 0;
sizes.NumOutputs     = 0;
sizes.NumInputs      = 0;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;

sys = simsizes(sizes);

x0  = [];
str = [];
ts  = [0.1 0];

LocalEnvInit(NoiseBlock,LoadDistBlock);
% end mdlInitializeSizes


%=============================================================================
% mdlUpdate
% Update the Environment Control GUI animation.
%=============================================================================
function sys = mdlUpdate(t,x,u)
fig = get_param(gcbh,'UserData');
if ishandle(fig),
   if strcmp(get(fig,'Visible'),'on'),
      ud = get(fig,'UserData');
      LocalEnvSets(t,ud,u);
   end
end
sys = [];
% end mdlUpdate


%=============================================================================
% LocalDeleteBlock
% The animation block is being deleted, delete the associated figure.
%=============================================================================
function LocalDeleteBlock
fig = get_param(gcbh,'UserData');
if ishandle(fig),
   delete(fig);
   set_param(gcbh,'UserData',-1)
end
% end LocalDeleteBlock


%=============================================================================
% LocalClose
% The callback function for the animation window close button.  Delete
% the animation figure window.
%=============================================================================
function LocalClose
delete(gcbf)
% end LocalClose


%=============================================================================
% LocalDeleteFigure
% The animation figure is being deleted, set the S-function UserData to -1.
%=============================================================================
function LocalDeleteFigure
ud = get(gcbf,'UserData');
set_param(ud.Block,'UserData',-1);
% end LocalDeleteFigure


%=============================================================================
% LocalNoise
% The callback function for the activation/deactivation of the measurement
% noise
%=============================================================================
function LocalNoise
ud = get(gcbf,'UserData');
if get(ud.Noise,'Value')
   set_param(ud.NoiseBlock,'Variance',get(ud.NoiseVar,'String'));
else
   set_param(ud.NoiseBlock,'Variance',num2str(0));
end
% end LocalNoise


%=============================================================================
% LocalNoiseParam
% The callback function for setting of noise variance
%=============================================================================
function LocalNoiseParam
ud = get(gcbf,'UserData');
if get(ud.Noise,'Value')
   set_param(ud.NoiseBlock,'Variance',get(ud.NoiseVar,'String'));
end
% end LocalNoiseParam


%=============================================================================
% LocalDist
% The callback function for the activation/deactivation of load disturbance
%=============================================================================
function LocalDist
ud = get(gcbf,'UserData');
if get(ud.LoadDist,'Value')
   set_param(ud.LoadDistBlock,'Value',get(ud.LoadAmp,'String'));
else
   set_param(ud.LoadDistBlock,'Value',num2str(0));
end
% end LocalDist


%=============================================================================
% LocalDistParam
% The callback function for setting the load diturbance amplitude
%=============================================================================
function LocalDistParam
ud = get(gcbf,'UserData');
if get(ud.LoadDist,'Value')
   set_param(ud.LoadDistBlock,'Value',get(ud.LoadAmp,'String'));
end
% end LocalDistParam


%=============================================================================
% LocalStepPID
% The callback function for comparing step response with different
% autotuning method
%=============================================================================
function LocalStepPID
global MODELRUNNING
if ~MODELRUNNING
   try
      num = str2num(get_param('autotunerPID/Plant/Transfer Fcn','Numerator'));
      den = str2num(get_param('autotunerPID/Plant/Transfer Fcn','Denominator'));
      tau = str2num(get_param('autotunerPID/Plant/Transport Delay','DelayTime'));
   catch
      msgbox('The step response can be computed only for linear models',...
         'AutotunerPID','error');
      return
   end
   stepPIDcompare(num,den,tau);
else
   msgbox('The simulation must be STOPPED to perform the Comparative Analysis',...
         'AutotunerPID','warn');
end
% end LocalStepPID


%=============================================================================
% LocalBodePID
% The callback function for comparing Bode diagram with different
% autotuning method
%=============================================================================
function LocalBodePID
global MODELRUNNING
if ~MODELRUNNING
   try
      num = str2num(get_param('autotunerPID/Plant/Transfer Fcn','Numerator'));
      den = str2num(get_param('autotunerPID/Plant/Transfer Fcn','Denominator'));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -