📄 cnf.m
字号:
else
warndlg(['The feedback gain matrix F=',cnfdb.Entry.F, ' is invalid. '], 'Warning ','modal');
return;
end
cnfdb.Controller.FB_F=F;
case 2
% H2 control
% epsilon>0 ?
epsilon = get(handles.slider_epsilon,'Value');
if epsilon<=0
warndlg('The parameter epsilon for H2 control design should be a positive scalar.','Warning ','modal');
cnf=0;
return;
end
F=eval('h2state(A,B,C2,D2,epsilon)','[]');
if isempty(F)
warndlg('The specified epsilon for H2 control design is not appropriate.','Warning ','modal');
cnf=0;
return;
end
cnfdb.Controller.FB_poles(1)=epsilon;
case 3
% pole placement
zeta=get(handles.slider_Zeta,'Value');
omega=get(handles.slider_Omega,'Value');
if zeta <=0 | omega<=0
warndlg('The specified damping ratio or natural frequency is not appropriate.','Warning ','modal');
cnf=0;
return;
end
cnfdb.Controller.FB_poles(2:3)=[zeta omega];
%
if nx==1
pv=[-omega];
else
pv=[ -zeta*omega+j*omega*sqrt(1-zeta^2) -zeta*omega-j*omega*sqrt(1-zeta^2)];
end
nd = length(pv);
% remanent poles organized in Butterworth pattern
if nx-nd >0
% [Z,P,K] = BUTTER(nx-nd, 3*omega, 's');
no = nx-nd;
% PP = 3*omega * exp(sqrt(-1)*(pi*(1:2:2*no-1)/(2*no)+pi/2));
PP = butter_poles(no, 3*omega);
pv=[pv PP];
end
if IC>0
omega2=get(handles.slider_Omega2, 'Value'); % integration pole
if omega2 >=0
warndlg('The specified integration pole is not appropriate.','Warning ','modal');
cnf=0;
return;
end
pv=[-abs(omega2) pv];
cnfdb.Controller.FB_poles(4)=omega2;
end
if nx<5
F=eval('-acker(A,B,pv)','[]');
else
F=eval('-place(A,B,pv)','[]');
end
if isempty(F)
warndlg('The specified preliminary poles locations are not feasible.','Warning ','modal');
cnf=0;
return;
end
end % switch
% CNF linear feedback gain
A0=cnfdb.System.A;
B0=cnfdb.System.B;
C20=cnfdb.System.C2;
if IC==0
FF=F;
else
FF=F(2:end);
end
if rank(A0+B0*FF) <nx | rank(D2-(C20+D2*FF)*inv(A0+B0*FF)*B0 ) < 1
warndlg('The specified preliminary poles locations are not feasible.','Warning ','modal');
uiwait;
cnf=0;
return;
end
IAf =inv(A0+B0*FF);
G = inv( D2-(C20+D2*FF)*IAf*B0 );
Ge = -IAf*B0*G;
if IC>0
Ge =[0; Ge];
end
H =F*Ge+G;
cnfdb.Controller.FB_F =F;
cnfdb.Controller.FB_H =H;
cnfdb.Controller.FB_Ge =Ge;
cnfdb.Controller.FB_G =G;
% CNF nonlinear feedback gain
cnfdb.Controller.CNF_F =F*0;
if cnfdb.Controller.ctrl_type > 0
cnfdb.Controller.CNF_Beta=get(handles.slider_Beta,'Value');
cnfdb.Controller.CNF_Alpha=get(handles.slider_Alpha,'Value');
nx=nx+IC;
W=[cnfdb.Controller.CNF_W zeros(1,nx)];
for nn=1:ud.WM_size,
W(nn) = get(ud.WM_slider(nn),'Value');
end
W=W(1:nx); % trim the tail 0s
cnfdb.Controller.CNF_W = W; % only store the vector of diagonal elements
% check for positiveness of W ?
% if isempty(W)
% warndlg('This CNF weighting matrix W is invalid.','Warning ','modal');
% cnf=0;
% end
% if isequal(size(W), [nx nx])
% for n=1:nx,
% if det(W(1:n,1:n))<0
% warndlg('This CNF weighting matrix W is not positive definite.','Warning ','modal');
% cnf=0;
% end
% end
% else
% warndlg('This CNF weighting matrix W is of wrong dimension.','Warning ','modal');
% cnf=0;
% end
P=eval('lyap((A+B*F)'',diag(W) )','[]');
if ~isempty(P)
cnfdb.Controller.CNF_F=B'*P;
cnfdb.Controller.CNF_P=P;
else
warndlg({'The lyapunov equation (A+BF)''P+P(A+BF)=-W has no solution of P','for the specified parameters F and W .'}, 'Warning ','modal');
cnf=0;
return;
end
end
% reset the observer
A=cnfdb.System.A;
B=cnfdb.System.B;
C1=cnfdb.System.C1;
OV_type = cnfdb.Controller.OV_type ;
if OV_type>0 % 0=state feedback; 1=full; 2=reduced order observer
% check for detectability
% skipped
AA=A;
CC=C1;
if cnfdb.System.DT_in
EE=cnfdb.System.DT_E;
else
EE=cnfdb.System.B;
end
DD=CC*EE*0;
if OV_type==2 % reduced order
[nc nx]=size(C1);
if nc==nx
warndlg({'The measurement output has same dimension as that of state vector.','Reduced order observer is non-existent.'},'Warning ','modal');
cnf=0;
return;
end
Ct=null(C1);
T=[C1;Ct'];
A=T*A*inv(T);
EE=T*EE;
AA=A(nc+1:nx,nc+1:nx);
CC=A(1:nc,nc+1:nx);
DD=EE(1:nc,:);
EE=EE(nc+1:nx,:);
end
[ny no]=size(CC);
OV_choice = cnfdb.Controller.OV_choice ;
switch OV_choice
case 1 % a specific K
if isempty(cnfdb.Entry.K)
K=[];
else
K=evalin('base',cnfdb.Entry.K,'[]');
end
if isequal( size(K),[no ny] )
if max(real(eig(AA-K*CC))) > 0
warndlg('The observer gain matrix K will result in some RHP poles.','Warning ','modal');
cnf=0;
return;
end
else
warndlg(['The observer gain matrix K=',cnfdb.Entry.K, ' is invalid.'], 'Warning ','modal');
cnf=0;
return;
end
cnfdb.Controller.OV_K=K;
case 2 % H2 observer
epsilon=get(handles.slider_OV_epsilon,'Value');
if epsilon<=0
warndlg('The observer parameter epsilon for H2 design should have a positive value.','Warning ','modal');
cnf=0;
return;
end
K=eval('h2state(AA'',CC'',EE'',DD'',epsilon)','[]');
if isempty(K)
warndlg('The specified value for observer parameter epsilon is not feasible.','Warning ','modal');
cnf=0;
return;
else
K=-K';
cnfdb.Controller.OV_K=K;
cnfdb.Controller.OV_poles(1)=epsilon;
end
case 3 % butterworth observer
omega=get(handles.slider_OV_omega,'Value');
if omega>0
% [Z,P,K] = BUTTER(no, omega, 's'); % Butterworth filter
% pv = omega * exp(sqrt(-1)*(pi*(1:2:2*no-1)/(2*no)+pi/2));
pv = butter_poles(no, omega);
K = eval('place(AA'',CC'',pv)','[]');
if isempty(K)
warndlg('The specified observer bandwidth is not feasible.','Warning ','modal');
cnf=0;
return;
else
K=K';
cnfdb.Controller.OV_K=K;
cnfdb.Controller.OV_poles(2)=omega;
end
else
warndlg('The specified value for observer bandwidth is not appropriate.','Warning ','modal');
cnf=0;
return;
end
end % switch OV_choice
[ny nx]=size(C1);
switch OV_type
case 1 % full order
cnfdb.Controller.OV_Ao=A-K*C1;
cnfdb.Controller.OV_Bo=[B K];
cnfdb.Controller.OV_Co=eye(nx);
cnfdb.Controller.OV_Do=zeros(nx,ny+1);
case 2 % reduced order
A22=A(ny+1:nx,ny+1:nx);
A11=A(1:ny,1:ny);
A21=A(ny+1:nx,1:ny);
A12=A(1:ny,ny+1:nx);
B=T*B;
B1=B(1:ny,:);
B2=B(ny+1:nx,:);
Ao=A22-K*A12;
cnfdb.Controller.OV_K=K;
cnfdb.Controller.OV_Ao=Ao;
cnfdb.Controller.OV_Bo=[B2-K*B1 A21-K*A11+Ao*K];
cnfdb.Controller.OV_Co=inv(T)*[zeros(ny,nx-ny); eye(nx-ny)];
cnfdb.Controller.OV_Do=inv(T)*[zeros(nx,1) [eye(ny);K]];
end
end % OV_type
% save the simulation parameters
cnfdb.ErrorBound = evalin('base', get(ud.Handles.edit_ErrorBound, 'String'), '[]');
cnfdb.ErrorInPercent = get(ud.Handles.check_Percent,'Value');
%cnfdb.SetPoint = evalin('base', get(ud.Handles.edit_SetPoint, 'String'), '[]');
cnfdb.SetPoint = get(ud.Handles.edit_SetPoint, 'String');
t=0;
rr = eval(cnfdb.SetPoint,'[]');
% if isempty(rr)
if ~isequal(size(rr),[1 1]) % invalid setpoint
warndlg('The specified target reference is not appropriate.','Warning ','modal');
cnf=0;
return;
end
cnfdb.StepSize = evalin('base', get(ud.Handles.edit_StepSize, 'String'), '[]');
cnfdb.StopTime = evalin('base', get(ud.Handles.edit_StopTime, 'String'), '[]');
guidata(ud.Figure, cnfdb);
cnf=1;
return;
%*************************************************************************
function result=change_function_argument(Src, xyt)
result=Src;
ind=findstr(Src,xyt);
for i=1:length(ind),
n=ind(i);
change=1;
if n>1
if isletter(Src(n-1))
change=0;
end
end
if n<length(Src)
if isletter(Src(n+1))
change=0;
end
end
if change==1
result(n)='u';
end
end
% 'sign' works in workspace, while 'sgn' in function block
result=strrep(result,'sign','sgn');
%*************************************************************************
function export_to_workspace(cnfdata,xtitle)
Params = struct(...
'hBorder',3,...
'vBorder',0.5,...
'ButtonH',2,...
'ButtonW',15,...
'EditH',1.5,...
'TextH',1.1,...
'EditW',40,...
'TextW',15,...
'TextWL',45,...
'StdUnit','character',...
'StdColor', [0.7 0.7 0.8]); %get(0,'DefaultUIControlBackground')
% Widget database
ud = struct('Parent',0,...
'Figure',[],...
'data',cnfdata,...
'Handles',[]);
FigW = 80;
FigH = 24;
ThisFig = figure(...
'Units',Params.StdUnit, ...
'Color',Params.StdColor, ...
'MenuBar','none', ...
'Visible','on',...
'IntegerHandle','off',...
'Name',xlate(xtitle), ...
'NumberTitle','off', ...
'Position',[3 7 FigW FigH], ...
'Resize','off',...
'WindowStyle','modal');
ud.Figure = ThisFig;
% Center wrt SISO Tool main window
centerfig(ThisFig);
TextH = Params.TextH;
TextW = Params.TextW;
TextWL = Params.TextWL;
EditH = Params.EditH;
EditW = Params.EditW;
Y0 = FigH-Params.vBorder;
X0 = Params.hBorder;
X = X0+TextW+1;
Vgap = EditH+Params.vBorder;
Y=Y0-TextH*2;
uicontrol('Parent',ThisFig, ...
'Unit',Params.StdUnit,...
'BackgroundColor',Params.StdColor, ...
'ForegroundColor','b',...
'Position',[X0,Y, TextWL TextH], ...
'String','Existing variables', ...
'Style','text',...
'FontWeight','bold',...
'HorizontalAlignment','left');
Y=Y-Vgap*5.2;
ud.Handles.list_Name = uicontrol('Parent',ThisFig, ...
'Unit',Params.StdUnit,...
'Bac
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -