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

📄 proc_model.m

📁 很优良的PID控制器设计仿真程序与模型,经过严格检验
💻 M
📖 第 1 页 / 共 2 页
字号:
   set(g_mod,'UserData',uu); chg_model_type;
   if length(k0)==0
      ii=findobj('Tag','CtrlLABError_E');
      if length(ii)>0, set(ii,'UserData',g_mod); figure(ii); end
   end   
elseif key==1, open_system(uu0{3})   
elseif key==-1
   ii=findobj('Tag','CtrlLABError_E'); close(ii);
   warn_msg=str2mat('Sorry, the SIMLINK model no longer exist now.',...
      'You should start building it again.  To prevent ',...
      'future lose of model, you should save it before ','exiting from SIMULINK.');
   warndlg(warn_msg,'Warning: Model failed!');
   set(uu{1}(g_model),'UserData',[]); new_model('off');
end

%-------------------------------------------------------
%chg_model_type allows one to change model format
%-------------------------------------------------------
function chg_model_type(n)

uu=get(gcf,'UserData'); b=uu{2}; g_model=uu{3}; u_type=get(b(9),'Value'); 
h_main=findobj('Tag','CtrlLABMain'); uu0=get(h_main,'UserData');
ii=1:4; i=find(ii~=u_type);
set(uu0{3}(15+ii(i)),'Checked','off'); set(uu0{3}(15+u_type),'Checked','on');
uu1=get(h_main,'UserData'); u_model=uu1{1}(g_model); u_data=get(u_model,'UserData');
if g_model==4
   %sets the display/model entering to the delay block
   extra_funs(4,2,'Visible',[4:7],[10:13]);
   set(b(4),'String','Delay constant'); set(b(6),'String','Order of Pade Approximation');
   if length(u_data)==0, set(b(5),'String','1'); set(b(7),'String','3');
   else, set(b(5),'String',num2str(u_data{1})); set(b(7),'String',int2str(u_data{2})); end      
else   
   switch u_type
   case 1,  %set the transfer function input boxes
      extra_funs(4,2,'Visible',[4:7],[10:13]);
      set(b(4),'String','Numerator polynomial'); set(b(6),'String','Denominator Polynomial');
      if length(u_data)==0
         %if no model pre-exist, then set defaults
         set(b(5),'String','[1]'); set(b(7),'String','[1,1]');
      else   
         if u_data{1}==1
            %if TF model exists, then fill the data
            set(b(5),'String',u_data{3}); set(b(7),'String',u_data{4});
         else
            %if other model exists, then fill the data
            [num,den]=tfdata(u_data{2},'v');
            set(b(5),'String',mat2str(num)); set(b(7),'String',mat2str(den));
         end   
      end   
   case 2, %set the state space model input boxes
      set(b([4:7,10:13]),'Visible','on'); set(b(4),'String','Coefficient Matrix A');
      set(b(6),'String','Vector B'); set(b(10),'String','Vector C');
      if length(u_data)==0
         set(b(5),'String','[-1]'); set(b(7),'String','[1]');
         set(b(11),'String','[1]'); set(b(13),'String','[0]');
      else   
         if u_data{1}==2
            set(b(5),'String',u_data{3}); set(b(7),'String',u_data{4});
            set(b(11),'String',u_data{5}); set(b(13),'String',u_data{6});
         else
            g_mod=ss(u_data{2});
            set(b(5),'String',mat2str(g_mod.a)); set(b(7),'String',mat2str(g_mod.b));
            set(b(11),'String',mat2str(g_mod.c)); set(b(13),'String',mat2str(g_mod.d));
         end   
      end   
   case 3, %set the pole-zero-gain model input boxes
      extra_funs(4,2,'Visible',[4:7,10:11],[12:13]);
      set(b(4),'String','Zeros of the System'); set(b(6),'String','Poles of the System');
      set(b(10),'String','Gain');
      if length(u_data)==0
         set(b(5),'String','[]'); set(b(7),'String','[-1]'); set(b(11),'String','[1]');
      else   
         if u_data{1}==3
            set(b(5),'String',u_data{3}); set(b(7),'String',u_data{4}); set(b(11),'String',u_data{5});
         else
            [z,p,k]=zpkdata(u_data{2},'v'); set(b(5),'String',mat2str(z)); 
            set(b(7),'String',mat2str(p)); set(b(11),'String',mat2str(k));
         end   
      end   
   case 4, %set the SIMULINK model input boxes
      extra_funs(4,2,'Visible',[4:5],[6:7,10:13]); set(b(4),'String','SIMULINK Model Name');
      %check whether a SIMULINK model is present
      key=0;
      if length(u_data)>0, key=(u_data{1}==4); end
      if key==1, strMod=u_data{3}; %if exists, use the original model name
      else,   
         %if new, create a new model name
         i=0;
         while (1)
            if exist(['my_model',int2str(i)])~=4, break; end
            i=i+1;   
         end
         strMod=['my_model',int2str(i)];  
      end   
      set(b(5),'String',strMod);
   end
   uu{1}=u_type; set(gcf,'UserData',uu);
end

%-------------------------------------------------------
%enter_model allows one to enter system model
%
%   W_sys=enter_model(num_str,den_str)
%-------------------------------------------------------
function W_sys=enter_model(num_str,den_str)

h_main=findobj('Tag','CtrlLABMain');
if nargin==0
   figure(findobj('Name','Enter System Model'));
   uu=get(gcf,'UserData'); b=uu{2}; g_model=uu{3}; 
   uu=get(h_main,'UserData'); u_model=uu{1}(g_model);
   u_data=get(u_model,'UserData'); uu{1}(5)=g_model; set(h_main,'UserData',uu);
   
   key_model=0;
   if g_model==4, %enter the delay model
      Td=eval(get(b(5),'String')); nPade=eval(get(b(7),'String'));
      set(u_model,'UserData',{Td,nPade,[],[]}); u_type=0;
   else   
      u_type=get(b(9),'Value');
      switch u_type
      case 1
         num_str=get(b(5),'String'); den_str=get(b(7),'String');
         i1=find(num_str=='s'); i2=find(den_str=='s');
         if length(i1)==0, num=eval(num_str); 
         else, num=get_poly(num_str); end   
         if length(i2)==0, den=eval(den_str);
         else, den=get_poly(den_str); end
         num=real(num); den=real(den); 
         if nargout==0 & length(num)>length(den)
            key_model=1;
            errordlg('System not realizable, order of num higher than den!','Model enter failed!');
         else   
            g_sys=tf(reshape(num,1,length(num)),reshape(den,1,length(den)));
            set(u_model,'UserData',{1,g_sys,num_str,den_str});
         end   
      case 2
         A_str=get(b(5),'String'); B_str=get(b(7),'String');
         C_str=get(b(11),'String'); D_str=get(b(13),'String');
         A=real(eval(A_str)); B=real(eval(B_str)); 
         C=real(eval(C_str)); D=real(eval(D_str));
         str=abcdchk(A,B,C,D);
         if length(str)>0, 
            key_model=1; errordlg(str,'Model enter failed!');
         else   
            g_sys=ss(A,B,C,D); set(u_model,'UserData',{2,g_sys,A_str,B_str,C_str,D_str});
         end   
      case 3
         z_str=get(b(5),'String'); p_str=get(b(7),'String'); k_str=get(b(11),'String');
         z=eval(z_str); p=eval(p_str); k=eval(k_str);
         g_sys=zpk(reshape(z,length(z),1),reshape(p,length(p),1),k);
         set(u_model,'UserData',{3,g_sys,z_str,p_str,k_str});
      case 4
         sys_str=get(b(5),'String'); build_simu(sys_str);
         set(u_model,'UserData',{4,[],sys_str});
      end
   end  
   if key_model==0
      close(gcf);
      if u_type~=4
         uu{1}(6)=1; set(h_main,'UserData',uu);
         if g_model==1, ctrllab(0,1); elseif g_model==2, ctrllab(0,2); end
         disp_model(g_model, get(u_model,'UserData'));
      end   
   end   
else
   i1=find(num_str=='s'); i2=find(den_str=='s');
   if length(i1)==0, num=eval(num_str); else, num=get_poly(num_str); end   
   if length(i2)==0, den=eval(den_str); else, den=getpoly(den_str); end
   W_sys=tf(reshape(num,1,length(num)),reshape(den,1,length(den)));
end

%-------------------------------------------------------
%get_poly converts a polynomial 's' string into a vector
%
%   [array,ikey]=get_poly(string)
%where 
%  string -- the 's' string representing a polynomial
%  array -- the vector converted
%  ikey -- if 1, then an error occur in conversion.
%-------------------------------------------------------
function [array,ikey]=get_poly(string)

string=string(find(string~=' ')); ikey=1;  nLen=length(string); array=1;
ii=find(string=='('); ij=find(string==')');
if length(ii)~=length(ij),
   warndlg('( and ) mismatching, string error!','Warning: Model failed!'); 
else, ii=[ii length(string)+1]; 
   if (ii(1)~=1), [array,ikey]=poly_evl(string(1:ii(1)-1)); end
   for i=1:length(ii)-1, 
      [array1,ikey]=poly_evl(string(ii(i)+1:ij(i)-1));
      if i~=length(ii), 
         if (ii(i+1)-1~=ij(i)), 
            k0 = str2num(string(ij(i)+1:ii(i+1)-1));
            if length(k0)==0, 
               warndlg(['Error in sub-string ',string(ij(i)+1:ii(i+1)-1)],'Warning: Model failed!'); 
               return;
            end
         else, k0=1; end
         for j=1:k0, array=conv(array,array1); end
      end
   end
end

%-------------------------------------------------------
%poly_evl is a supporting function for get_poly
%
%   [array,key]=poly_evl(string)
%where the arguments see above function.
%-------------------------------------------------------
function [array,key]=poly_evl(string)
str=string(2:length(string)); array=[]; key=0; i0=1; is0=[]; 
is=find(str=='+'|str=='-'); is=is+ones(size(1,length(is)));
for i=1:length(is), if (string(is(i)-1)~='e'), is0=[is0 is(i)]; end, end, 
is0 = [is0 length(string)+1]; 
for i=1:length(is0), 
   if (i0>1), if (string(i0-1)=='-'), i0=i0-1; end, end
   substr=string(i0:is0(i)-1); i0=is0(i)+1; is=find(substr=='s'|substr=='S');
   if length(is)==0, 
      k0=0; v1=str2num(substr);
      if isempty(v1)==1, key=1; end, if key==1, return; end
   elseif length(is)==1, 
      if (is==1), v1=1; 
      elseif (is==2&substr(1)=='-'), v1=-1; 
      else, v1 = str2num(substr(1:is-1));
         if isempty(v1)==1, key=1; end, if key==1, return; end
      end,
      if is==length(substr), k0=1;
      else, k0=str2num(substr(is+1:end));
      if isempty(k0)==1, key=1; end
         if key==1, return; end
         k0=floor(k0); 
      end
   else, 
      warndlg('Too many s''','Transfer function Failure!'); return; 
   end
   if (length(array)<k0+1),  array(k0+1)=v1;
   else, array(k0+1)=array(k0+1)+v1; 
   end, key=0; 
end
array=array(end:-1:1);

%-------------------------------------------------------------------------------
%build_simu is used to provide an template of SIMULINK model for the user to add 
%his own system model.
%-------------------------------------------------------------------------------
function build_simu(strMod)
open_system('simu_modlib'); sys=strMod;
if exist(sys)==4, open_system(sys);
else,
   new_system(sys); open_system(sys);
   set_param(sys,'Location',[197,131,618,343])
   add_block('built-in/Inport',[sys,'/','Input'])
   set_param([sys,'/','Input'],'Position',[65,80,85,100])
   add_block('built-in/Outport',[sys,'/','Output'])
   set_param([sys,'/','Output'],'Position',[355,80,375,100])
   add_line(sys,'Input/1','Output/1')
   new_system([sys,'/To CtrlLAB'])
   set_param([sys,'/To CtrlLAB'],'Mask Display','Return',...
      'OpenFcn','proc_model(9);','Position',[30,135,90,170]);
   %      'Mask Dialogue','eval(''proc_model(3);'')',...
end

%-----------------------------------------------------------------------------------
%mk_sim is used to process the SIMULINK model entered and display the final results.
%
%IMPORTANT NOTICE:-
%Due to the problem in SIMULINK, one should not click the Return block, instead, he 
%should type mk_sim under MATLAB prompt.  It is strange but we can do nothing about it.
%
%This function is not currently used.
%-----------------------------------------------------------------------------------
function mk_sim()

figure(findobj('Tag','CtrlLABMain')); uu0=get(gcf,'UserData'); 
g_model=extra_funs(5,3,'Checked',10:13);   
uu=get(uu0{1}(g_model),'UserData'); %get model name

%getting the linearized model
strMod=uu{3}; [xx,ux,yy,dx]=trim(strMod); 
[a,b,c,d]=linmod2(strMod,xx,ux); uu{2}=ss(a,b,c,d);  

%save system model to the block
set(uu0{1}(g_model),'UserData',uu);

%display the linearized model
disp_model(6,uu);

%----------------------------------------------------------
%is_delay checks whether there is delay in the whole system
%----------------------------------------------------------
function key=is_delay()
key=0; uu0=get(findobj('Tag','CtrlLABMain'),'UserData');
g4=get(uu0{1}(4),'UserData');
if g4{1}>0; key=1; end

%---------------------------------------------------------
%is_simu checks whether there is delay in the whole system
%---------------------------------------------------------
function key=is_simu()
key=0;
uu0=get(findobj('Tag','CtrlLABMain'),'UserData');
for i=1:3
   eval(['g=get(uu0{1}(' int2str(i), '),''UserData'');']);
   if length(g)>0, if g{1}==4, key=1; break; end, end
end

%---------------------------------------------------------
%clear_model clears the current model.
%---------------------------------------------------------
function clear_model()
close(gcf);
uu0=get(findobj('Tag','CtrlLABMain'),'UserData');
g_model=extra_funs(5,3,'Checked',10:13);
switch g_model
case 1
   set(uu0{1}(1),'UserData',[]);  
   set([uu0{3}([5:8,13,15]),uu0{4}(1),uu0{5}(1)],'Enable','off');
case 2, set(uu0{1}(2),'UserData',[]); set(uu0{4}(26),'Enable','off','Checked','off');
case 3, set(uu0{1}(3),'UserData',[]);  
case 4, set(uu0{1}(4),'UserData',{0,3});  
end      

⌨️ 快捷键说明

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