📄 ctrllab.m
字号:
n=length(p1); c=(p2(1)-p1(1))/2; d=(p2(2)-p1(2))/2; f=(p2(1)+p1(1))/2;
g=(p2(2)+p1(2))/2; x=[-1 1 0.5 1 0.5]; y=[0 0 0.5*w 0 -0.5*w];
xx=c*x-d*y+f; yy=d*x+c*y+g; h=line(xx,yy);
if nargin==4, set(h,'Color',col); end
%---------------------------------------------------------------------------------
%ctrllab_init perform initialization in CtrlLAB
%
% ctrllab_init(key)
%where key=0 for absolute initialization and 1 for only enabling of relevant menus
%---------------------------------------------------------------------------------
function ctrllab_init(key,arg1)
figure(findobj('Tag','CtrlLABMain')); uu0=get(gcf,'UserData'); kk='on';
if key==2, set(uu0{4}(26),'Enable','on'); %set properties with Gc(s)
else
if key==0,
kk='off'; set(uu0{1}(1:3),'UserData',[]); set(uu0{1}(4),'UserData',{0,3,[],[]});
uu0{1}(6)=0; set(gcf,'UserData',uu0);
end
set([uu0{3}([5:8,13,15]),uu0{4}(1),uu0{5}(1)],'Enable',kk);
g4=get(uu0{1}(4),'UserData'); g4{3}=[]; set(uu0{1}(4),'UserData',g4)
end
%----------------------------------------------------------------
%shw_tutorial activate the toturial program in CtrlLAB
%
% shw_tutorial(key)
%
%where key=1,2,3,4 for the four cases in the help menu of CtrlLAB
%----------------------------------------------------------------
function shw_tutorial(nTask)
uu0=get(gcf,'UserData');
set(uu0{1}(1),'UserData',{1,tf(1,[1,3,3,1]),'1','(s+1)3'});
set(uu0{1}([2,3]),'UserData',[]); set(uu0{1}(4),'UserData',{0,3,[],[]});
ctrllab_init(1);
switch nTask
case 1, proc_model(1);
case 2, sys_analysis(1);
case 3, sys_analysis(3);
case 4, pid_design(1); sys_analysis(6);
end
clab_help(2),
%-----------------------------------------------------
%plant_realiz is used to display the realization of the
%plant model
%
% plant_realiz(key)
%where key=1,2,3,4,5 controllable, observable, Jordan,
%minimal realization and balanced realization,
%respectively. This function is part of CtrlLAB
%-----------------------------------------------------
function plant_realiz(nTask)
uu0=get(gcf,'UserData'); uu0=uu0{1}; g_0=get(uu0(1),'UserData');
g_sys=g_0{2}; G1=ss(g_sys); D=G1.d;
switch nTask
case 1, [A,B,C,T,K]=ctrbf(G1.a,G1.b,G1.c);
case 2, [A,B,C,T,K]=obsvf(G1.a,G1.b,G1.c);
case 3, [A,B,C,D]=canon(G1.a,G1.b,G1.c,G1.d,'modal');
n=length(C);
for i=1:n, for j=1:n
if i~=j,
if abs(A(i,j))<1e-11, A(i,j)=0; end
end,end,end
case 4, [A,B,C,D]=minreal(G1.a,G1.b,G1.c,G1.d);
case 5
if any(real(eig(G1.a))>=0)
warndlg('System not is not positive definite!','Warning: Realization failed');
return;
else, [A,B,C]=balreal(G1.a,G1.b,G1.c); end
end
%display the converted realization model
display_str; proc_model(1,A,B,C,D,nTask);
%-------------------------------------------------------------------------------
%add_sys_blocks adds the extra system properties to the plant model, such as the
%ZOH, Actuator saturation, and measurement noise.
%
% add_sys_blocks(nTask)
%where nTask=1 for adding ZOH, nTask=2,3,4 for saturation actuator, time delay
%and measurement noise, respectively.
%-------------------------------------------------------------------------------
function add_sys_blocks(nTask)
figure(findobj('Tag','CtrlLABMain')); uu=get(gcf,'UserData');
if nTask<=4, set(uu{3}(29+nTask),'Checked','on'); end
switch nTask
case 1
case {5,6},
hh=uu{4}(21+nTask); extra_funs(3,hh,'Checked');
ii=extra_funs(5,4,'Checked',26:27);
if length(ii)==0,
%to ensure at least one item is checked
set(uu{4}(27),'Checked','on');
end
end
%------------------------------------------------------------------------
%file_proc is the function to administrate the file management in CrtlLAB.
%
% file_proc(nTask)
%where
% nTask=1 for file load, 2 for save, 3 for save as
% 4 for confirm quit, 5 for absolute quit.
%------------------------------------------------------------------------
function yesno=file_proc(nTask)
g_main=findobj('Tag','CtrlLABMain'); uu=get(g_main,'UserData');
switch nTask
case 1 %Open
[filename,filepath]=uigetfile('*.ctr','Please Specify the File Name');
if ~isa(filename,'double')
uu{8}=[filepath,filename]; eval(['load(uu{8}, ''-mat'');'])
set(uu{1}(1),'UserData',Mod_Dat{1}); set(uu{1}(2),'UserData',Mod_Dat{2});
set(uu{1}(3),'UserData',Mod_Dat{3});
if length(Mod_Dat{1})>0, ctrllab(0,1); end
uu{1}(6)=0; set(g_main,'UserData',uu);
end
case 2 %Save
if length(uu)==8
Md1=get(uu{1}(1),'UserData'); Md2=get(uu{1}(2),'UserData');
Md3=get(uu{1}(3),'UserData'); Mod_Dat={Md1,Md2,Md3};
eval(['save(uu{8},''Mod_Dat'')']);
uu{1}(6)=0; set(g_main,'UserData',uu);
if nargout==1, yesno=1; end
else
if nargout==1, yesno=file_proc(3);
else, file_proc(3); end
end
case 3 %Save As
if nargout==1, yesno=1; end
[filename,filepath]=uiputfile('*.ctr','Please Specify the File Name');
if ~isa(filename,'double')
uu{8}=[filepath,filename]; set(gcf,'UserData',uu); file_proc(2);
else,
if nargout==1, yesno=0; end
end
case 4, %display a confirm quit box
if uu{1}(6)==1
ButtonName=questdlg('The models have not been save. Save them?', ...
'Quit CtrlLAB Confirm','Yes','No','Cancel','Cancel');
switch ButtonName,
case 'Yes', file_proc(6);
case 'No', file_proc(5);
end
else, file_proc(5); end
case 5, %closing all associated windows
delete(g_main);
close(findobj('Tag','CtrlLABInfo'));
close(findobj('Tag','CtrlLABHelp'));
close(findobj('Tag','CtrlLABFigs'));
close(findobj('Tag','CtrlLABLegends'));
close(findobj('Tag','CtrlLABExtras'));
close(findobj('Tag','CtrlLABPltPref'));
close(findobj('Tag','MatxProcMain'));
close(findobj('Tag','MatxProcNew'));
close(findobj('Tag','MatxProcExtras'));
case 6, %save the system and then quit
k=file_proc(2); pause(0.0001);
if k, file_proc(5); end
end
%-------------------------------------------------------------------------------
%select_sigs is the function which allows the user to select output signals from
%the block diagram of the feedback system
%
% select_sigs(nTask)
%where
% nTask=1 for e(t), 2 for u(t) and 3 for y(t)
%-------------------------------------------------------------------------------
function select_sigs(nTask);
figure(findobj('Tag','CtrlLABMain')); uu=get(gcf,'UserData'); h_Sig=uu{1}(8:10);
switch nTask
case 1 %signal e(t)/Sensitivity TF selected
extra_funs(4,4,'Checked',[28,32],[29:31,33])
case 2 %signal u(t) selected
extra_funs(4,4,'Checked',[29,31],[28,30,32:33])
case 3 %signal y(t)/IO TF selected
extra_funs(4,4,'Checked',[30,31],[28:29,32:33]);
case 4 %complementary sensitivity function selected
extra_funs(4,4,'Checked',[30,33],[28:29,31:32]);
case 5 %open-loop selected
extra_funs(4,4,'Checked',[24,30,31],[25,28:29,32:33]);
case 6 %closed-loop selected
extra_funs(4,4,'Checked',25,24);
end
if nTask<=3 %for signals selected, set color of signal label
uu=get(gcf,'UserData'); h_Sig=uu{1}(8:10); ii=find(1:3~=nTask);
set(h_Sig(nTask),'Color',[1,0,0]); set(h_Sig(ii),'Color',[0.6,0.6,0]);
end
%if there was analysis performed, then perform it again for new setting
if uu{1}(7)~=0, sys_analysis(uu{1}(7)); end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -