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

📄 autogui.m

📁 这是一个PID自动调节的程序
💻 M
📖 第 1 页 / 共 3 页
字号:
function [sys,x0,str,ts] = autogui(t,x,u,flag,RefBlock)
%AUTOGUI S-function for making a simple PID GUI.
%
%   When the model autotunerPID.mdl is run, this S-function create a panel
%   which closer resemble the layout of a typical real industrial autotuner
%   (at least in the control options). 
%   The upper part of the GUI is used to interact with the control system,
%   by changing the set-point value and by reading the value of the process
%   value and the control variable.  Moreover when used in MANUAL mode the
%   control variable is under the direct control of the user.  The
%   parameters area shows the parameter of the PID which can be changed 
%   online (except for the derivative weight c which is fixed to 0.
%   The last two areas allow to select the operating mode (AUTO/MANUAL) and
%   the autotuning method, split in the selection of the identification
%   method, the selection of the tuning method and the selection of the
%   regulator structure.
%
%   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(RefBlock);
      % Update 
   case 2,
      sys = mdlUpdate(t,x,u);
      % Unused flags
   case { 1, 3, 4, 9 },
      sys = [];
      % DeleteBlock
   case 'DeleteBlock',
      LocalDeleteBlock
      % DeleteFigure
   case 'DeleteFigure',
      LocalDeleteFigure
      % SliderSP
   case 'SliderSP',
      LocalSliderSP
      % EditSP
   case 'EditSP',
      LocalEditSP
      % SliderCV
   case 'SliderCV',
      LocalSliderCV
      % EditCV
   case 'EditCV',
      LocalEditCV
      % Close
   case 'Close',
      LocalClose
      % Autotune
   case 'Autotune',
      LocalAutotune
      % Manual
   case 'Man',
      LocalMan
      % Auto
   case 'Auto',
      LocalAuto
      % EditK
   case 'EditK',
      LocalEditK
      % EditTi
   case 'EditTi',
      LocalEditTi
      % EditTd
   case 'EditTd',
      LocalEditTd
      % EditN
   case 'EditN',
      LocalEditN
      % Editb
   case 'Editb',
      LocalEditb
      % IdentMethod
   case 'Identification',
      LocalIdentification
      % TuningMethpd
   case 'Tuning',
      LocalTuning
      % Structure
   case 'Structure',
      LocalStructure
      % EditParam
   case 'EditParam',
      LocalEditParam
      % Unexpected flags
   otherwise
      error(['Unhandled flag = ',num2str(flag)]);
end
% end autogui

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

% set up S-function
sizes = simsizes;

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

sys = simsizes(sizes);

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

LocalPIDInit(RefBlock);
% end mdlInitializeSizes


%=============================================================================
% mdlUpdate
% Update the PID 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');
      LocalPIDSets(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


%=============================================================================
% 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


%=============================================================================
% LocalSliderSP
% The callback function for the animation window slider SP control
%=============================================================================
function LocalSliderSP
ud = get(gcbf,'UserData');
set_param(ud.RefBlock,'Value',num2str(get(gcbo,'Value')));
% end LocalSliderSP


%=============================================================================
% LocalSliderCV
% The callback function for the animation window slider CV control
%=============================================================================
function LocalSliderCV
global CVVALUE
ud = get(gcbf,'UserData');
CVVALUE = get(gcbo,'Value');
set(ud.EditCV,'String',num2str(CVVALUE));
% end LocalSliderCV


%=============================================================================
% LocalEditCV
% The callback function for the edit field of parameter CV
%=============================================================================
function LocalEditCV
global CVVALUE
ud = get(gcbf,'UserData');
CVVALUE = str2num(get(ud.EditCV,'String'));
set(ud.SlideCV,'Value',CVVALUE);
% end LocalEditCV


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


%=============================================================================
% LocalAutotune
% The callback function for the autotune button. Start autotuning
%=============================================================================
function LocalAutotune
global AUTOTUNE
global INACTIVE
AUTOTUNE = 1;
INACTIVE = 1;
ud = get(gcbf,'UserData');
set(ud.SlideSP,'Enable','inactive');
set(ud.EditSP,'Enable','off');
set(ud.SlideCV,'Enable','inactive');
set(ud.EditCV,'Enable','off');
set(ud.Man,'Enable','off');
set(ud.Auto,'Enable','off');
set(ud.EditK,'Enable','inactive');
set(ud.EditTi,'Enable','inactive');
set(ud.EditTd,'Enable','inactive');
set(ud.Editb,'Enable','inactive');
set(ud.EditN,'Enable','inactive');
set(ud.Ident,'Enable','off');
set(ud.Tuning,'Enable','off');
set(ud.TuningParam,'Enable','off');
set(ud.Structure,'Enable','off');
% end LocalAutotune


%=============================================================================
% LocalMan
% The callback function for the manual selector button
%=============================================================================
function LocalMan
global AUTOMAN
ud = get(gcbf,'UserData');
set(ud.Auto,'Value',0);
set(ud.Man,'Value',1);
set(ud.EditCV,'Enable','on');
set(ud.SlideCV,'Enable','on');
AUTOMAN = 0;
% end LocalMan


%=============================================================================
% LocalAuto
% The callback function for the automatic selector button
%=============================================================================
function LocalAuto
global AUTOMAN
ud = get(gcbf,'UserData');
set(ud.Man,'Value',0);
set(ud.Auto,'Value',1);
set(ud.EditCV,'Enable','off');
set(ud.SlideCV,'Enable','inactive');
AUTOMAN = 1;
% end LocalAuto


%=============================================================================
% LocalEditK
% The callback function for the edit field of parameter K
%=============================================================================
function LocalEditK
global PIDPARAMETERS
ud = get(gcbf,'UserData');
PIDPARAMETERS = [str2num(get(ud.EditK,'String')) PIDPARAMETERS(2:5)];
% end LocalEditK


%=============================================================================
% LocalEditTi
% The callback function for the edit field of parameter Ti
%=============================================================================
function LocalEditTi
global PIDPARAMETERS
ud = get(gcbf,'UserData');
PIDPARAMETERS = [PIDPARAMETERS(1)...
      str2num(get(ud.EditTi,'String')) PIDPARAMETERS(3:5)];
% end LocalEditTi


%=============================================================================
% LocalEditTd
% The callback function for the edit field of parameter Td
%=============================================================================
function LocalEditTd
global PIDPARAMETERS
ud = get(gcbf,'UserData');
PIDPARAMETERS = [PIDPARAMETERS(1:2)...
      str2num(get(ud.EditTd,'String')) PIDPARAMETERS(4:5)];
% end LocalEditTd


%=============================================================================
% LocalEditN
% The callback function for the edit field of parameter N
%=============================================================================
function LocalEditN
global PIDPARAMETERS
ud = get(gcbf,'UserData');
PIDPARAMETERS = [PIDPARAMETERS(1:3)...
      str2num(get(ud.EditN,'String')) PIDPARAMETERS(5)];
% end LocalEditN


%=============================================================================
% LocalEditb
% The callback function for the edit field of parameter b
%=============================================================================
function LocalEditb
global PIDPARAMETERS
ud = get(gcbf,'UserData');
PIDPARAMETERS = [PIDPARAMETERS(1:4) str2num(get(ud.Editb,'String'))];
% end LocalEditb


%=============================================================================
% LocalEditSP
% The callback function for the edit field of parameter SP
%=============================================================================
function LocalEditSP
ud = get(gcbf,'UserData');
set_param(ud.RefBlock,'Value',get(ud.EditSP,'String'));
% end LocalEditSP


%=============================================================================
% LocalIdentification
% The callback function for the selection of the identification method
%=============================================================================
function LocalIdentification
global TUNING_PARAM
global IDENTIFICATION_METHOD
global TUNING_METHOD
ud = get(gcbf,'UserData');
str = get(ud.Ident,'String');
IDENTIFICATION_METHOD = deblank(upper(str(get(ud.Ident,'Value'),:)));
if strcmp(IDENTIFICATION_METHOD,'STEP')
   TUNING_PARAM = [];
   TUNING_METHOD = 'ZN (OL)';
   set(ud.TunParText,'String','');
   set(ud.TuningParam,'Style','edit');
   set(ud.TuningParam,'Visible','off');
   set(ud.Tuning,'Value',3);
elseif strcmp(IDENTIFICATION_METHOD,'RELAY')
   TUNING_PARAM = [];
   TUNING_METHOD = 'ZN (CL)';
   set(ud.TunParText,'String','');
   set(ud.TuningParam,'Style','edit');
   set(ud.TuningParam,'Visible','off');
   set(ud.Tuning,'Value',4);
end
% end LocalIdentification


%=============================================================================
% LocalTuning
% The callback function for the selection of the tuning method
%=============================================================================
function LocalTuning
global TUNING_PARAM
global TUNING_METHOD
global IDENTIFICATION_METHOD
ud = get(gcbf,'UserData');

⌨️ 快捷键说明

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