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

📄 cnf_set.m

📁 hard disk drive servo control tools, it is very helpful
💻 M
📖 第 1 页 / 共 4 页
字号:
end    

if isempty(cnfdb.Entry.B)
    set(handles.edit_B,'String', matrix2str(cnfdb.System.B));
else
    set(handles.edit_B,'String', cnfdb.Entry.B);
end    

if isempty(cnfdb.Entry.C1)
    set(handles.edit_C1,'String', matrix2str(cnfdb.System.C1));
else
    set(handles.edit_C1,'String', cnfdb.Entry.C1);
end

if isempty(cnfdb.Entry.C2)
    set(handles.edit_C2,'String', matrix2str(cnfdb.System.C2));
else
    set(handles.edit_C2,'String', cnfdb.Entry.C2);
end

if isempty(cnfdb.Entry.D2)
    set(handles.edit_D2,'String', matrix2str(cnfdb.System.D2));
else
    set(handles.edit_D2,'String', cnfdb.Entry.D2);
end

if isempty(cnfdb.Entry.x0)
    set(handles.edit_x0,'String', matrix2str(cnfdb.System.x0));
else
    set(handles.edit_x0,'String', cnfdb.Entry.x0);
end

set(handles.check_DT,'Value', cnfdb.System.DT_in);
if isempty(cnfdb.Entry.E)
    set(handles.edit_E,'String', matrix2str(cnfdb.System.DT_E));
else
    set(handles.edit_E,'String', cnfdb.Entry.E);
end    
set(handles.edit_w, 'String', cnfdb.System.DT_w );

set(handles.check_CS,'Value', cnfdb.System.CS_in);
if isempty(cnfdb.Entry.Um)
    set(handles.edit_Umax,'String', matrix2str(cnfdb.System.CS_Umax));
else
    set(handles.edit_Umax,'String', cnfdb.Entry.Um);
end    

set(handles.check_RM,'Value', cnfdb.System.RM_in);
if isempty(cnfdb.Entry.RM)
    [N1 D1]=tfdata(cnfdb.System.RM, 'v');
    set(handles.edit_RM,'String', strcat('tf(', matrix2str(N1), ',', matrix2str(D1), ')' ) );
else
    set(handles.edit_RM,'String', cnfdb.Entry.RM);
end    
% static nonlinearity
set(handles.check_NL,'Value', cnfdb.System.NL_in);
set(handles.edit_NF,'String', cnfdb.System.NL_Fcn);
% parasite nonlinear dynamics
set(handles.check_QD, 'Value', cnfdb.System.QD_in);
set(handles.edit_QD_Fcn, 'String', cnfdb.System.QD_Fcn);
set(handles.edit_Q0, 'String', matrix2str(cnfdb.System.Q0) );


% Window button 

BW = Params.ButtonW; % button width
ButtonH = Params.ButtonH;
Gap = BW/3;
X0 = 1-3*BW;
Y0 = 2*Params.vBorder;

% Add window buttons
ud.Handles.OKButton = uicontrol('Parent',ThisFig, ...
    'Unit',Params.StdUnit,...
    'Position',[X0 Y0 BW ButtonH], ...
    'FontWeight','bold', ...
    'Callback',{@button_OK_System},...
    'String','Apply');
%     'BackgroundColor',[0.7 1 1], ...

X0 = X0 + BW + Gap;
ud.Handles.CancelButton = uicontrol('Parent',ThisFig, ...
    'Callback','close(gcbf)',...
    'Unit',Params.StdUnit,...
    'Position',[X0 Y0 BW ButtonH], ...
    'FontWeight','bold', ...
    'String','Cancel');
%    'BackgroundColor',[1 1 0.7], ...
%    'Fontsize',11, ...

% Make figure visible and store database
set(ud.Figure,'UserData',ud, 'Visible','on');


% ----------------------------------------------------------
function edit_NF_Callback(hObject, eventdata, handles)
% hObject    handle to edit_NF (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

str = deblank(get(hObject,'String'));
Tag = get(hObject,'Tag');

if  ~isempty(str)
    %     syms y t;
    %     val= eval(str, '[]');
    %     if  isempty(val)
    %         val= evalin('base',str, '[]');
    %     end
    %     if  isempty(val)
    %         warndlg('The function you just entered is invalid !', 'Warning ...', 'modal');
    %     else
    %         if isnumeric(val)
    %             set(hObject,'String', num2str(val) );
    %         else    
    %             set(hObject,'String', char(val) );
    %         end
    %     end
    
    % if evalin('base',['exist(','''', Tag,'''', ',''var'')'])
    
    VarList=evalin('base','who');
    
    ind=strmatch(str, VarList, 'exact');
    if ~isempty(ind)
        val = evalin('base', str, '[]');
        if  ~isempty(val)
            if isnumeric(val)
                str=num2str(val); 
            else    
                str=char(val);
            end
            set(hObject,'String', str );
        end   
    end    
    eval(['syms ', Tag]);  %  y t;
    val= eval(str, '[]');
    if  isempty(val)
        warndlg('The function you just entered is invalid !', 'Warning ...', 'modal');
    end
    
end

% ----------------------------------------------------------
function edit_RM_Callback(hObject, eventdata, handles)
% hObject    handle to edit_RM (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

rm=deblank(get(gcbo,'String'));
if ~isempty(rm)
    if  ~isa(evalin('base', rm, '[]'),'lti')
         warndlg('This does not look like a valid LTI model !', 'Warning ...', 'modal');
    end
end



% --- Executes on button press in button_Apply.
function button_OK_System(hObject, eventdata, handles)
% hObject    handle to button_Apply (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

ud=get(gcbf, 'UserData');

MainFig = ud.Parent;
cnfdb = guidata(MainFig);
if  isempty(cnfdb)
    return;
end    

handles=ud.Handles;

cnfdb.sys_ok=1;

cnfdb.System.Name=get(handles.edit_Name,'String');

% A,B,C1,C2,D2
IA = deblank(get(handles.edit_A,'String'));
IB = deblank(get(handles.edit_B,'String'));
IC1 = deblank(get(handles.edit_C1,'String'));
IC2 = deblank(get(handles.edit_C2,'String'));
ID2 = deblank(get(handles.edit_D2,'String'));

% x0
cnfdb.Entry.x0 = deblank(get(handles.edit_x0,'String'));

% for disturbance
cnfdb.System.DT_in=get(handles.check_DT,'Value');
% E
cnfdb.Entry.E= deblank(get(handles.edit_E,'String'));;
% w
cnfdb.System.DT_w = deblank(get(handles.edit_w,'String'));

% for control saturation
cnfdb.System.CS_in=get(handles.check_CS,'Value');
cnfdb.Entry.Um=deblank(get(handles.edit_Umax,'String'));


% for resonance modes
cnfdb.System.RM_in=get(handles.check_RM,'Value');

the_str = deblank(get(handles.edit_RM,'String'));
if isempty(the_str)
    the_str='zpk(1)';
end    
cnfdb.Entry.RM=the_str;


% for nonlinearity in input channel
cnfdb.System.NL_in = get(handles.check_NL,'Value');
NL_Fcn = deblank(get(handles.edit_NF,'String'));
cnfdb.System.NL_Fcn = NL_Fcn;

% preset Nonlinear compensation
if cnfdb.System.NL_in  
    if NL_Fcn(1)=='-'
        cnfdb.Controller.NC_Fcn=NL_Fcn(2:end);
    else
        cnfdb.Controller.NC_Fcn=['-',NL_Fcn];
    end
end   

% for nonlinear zero dynamics
cnfdb.System.QD_in = get(handles.check_QD,'Value');
cnfdb.System.QD_Fcn = deblank(get(handles.edit_QD_Fcn,'String'));
Q0 = eval(get(handles.edit_Q0,'String'),'[]');
cnfdb.System.Q0 = Q0;

    
cnfdb.Entry.A=IA;
cnfdb.Entry.B=IB;
cnfdb.Entry.C1=IC1;
cnfdb.Entry.C2=IC2;
cnfdb.Entry.D2=ID2;

%  check whether there are any changes to the nominal model
if isempty(IA) | isempty(IB) | isempty(IC1) | isempty(IC2) | isempty(ID2)
    cnfdb.sys_ok=0;
    uiwait(msgbox('The plant model is not fully defined.', get(gcf, 'Name'), 'help', 'modal'));
else
    % verify the model
    cnfdb = cnf_update_model(cnfdb);
    if cnfdb.sys_ok == 0  % model is invalid at this moment
        
        selection = questdlg({'Your plant model cannot pass validation at this moment,',...
                'What would you like to do ?'}, get(gcf, 'Name') , 'Revise','Accept','Revise');
        if strcmp(selection,'Revise')
            return;
        else
            cnfdb.sys_ok=1;
        end
        
    else    % model is valid at this moment
        

        % test the stability of the nonlinear zero dynamics
        % nonlinear zero dynamics
        if cnfdb.System.QD_in==1
            QD_Fcn = cnfdb.System.QD_Fcn;
            Q0 = cnfdb.System.Q0;
            
            tstep=cnfdb.StepSize;
            tstop=cnfdb.StopTime;
            tt=0;
            nn=0;
            QT=Q0; 
            x0=cnfdb.System.x0;
            h0=cnfdb.System.C2*x0;
            while tt<tstop & nn<50
                [t1,qt] = ode45(@qdotfun,[0,tstep], QT, [], QD_Fcn,x0,h0);
                pt=length(t1);
                QT=qt(pt,:)';
                tt=tt+tstep;
                nn=nn+1;
            end
            if norm(QT)> norm(Q0)
                selection = questdlg({'It looks like the nonlinear zero dynamics is not stable.',...
                        'Do you still wish to continue?'}, get(gcf, 'Name') , 'Yes','No','Yes');
                if strcmp(selection,'No')
                    return;
                end
                %        uiwait(warndlg('It looks like the nonlinear zero dynamics is not stable.' , 'Warning ...', 'modal'));
                %        return;
            end
        end        
        
        
        selection='No';
        
        if  cnfdb.ctrl_ok==0
            selection = questdlg({'Your plant model is ready, but the controller is not.',...
                    'Would you like this toolkit to preset your controller?'}, get(gcf, 'Name') , 'Yes','No','Yes');
        end
        
        if strcmp(selection,'Yes')
            
            % initialize the controller
            Umax=cnfdb.System.CS_Umax;
            if isempty(Umax) | Umax<=0
                Umax=1;
            end    
            [nx nn]=size(cnfdb.System.A);
            bb=norm(cnfdb.System.B);  % B
%             rr=cnfdb.SetPoint;
%             if rr< 1e-2 | rr> 1e2
%                 rr=1;
%             end    
            rr=1;
            ts=2*sqrt(rr/(bb*Umax)) ;  % the ball park figure for settling time
            zeta=0.3;
            omega=4.6/(ts*zeta) * 0.15;
            cnfdb.Controller.FB_poles=[1 zeta omega -0.01];
            cnfdb.Controller.FB_choice=3;   % pole placement
            cnfdb.Controller.OV_poles=[1 3*omega];  %  
            cnfdb.Controller.OV_choice=3;   % butterworth observer
            cnfdb.Controller.OV_type=2;     % reduced order observer
            cnfdb.Controller.ctrl_type=1;   % cnf control
            cnfdb.Controller.CNF_W=ones(1,nx); % matrix W
            
            StopTime=5*ts;
            StepSize=ts/50;
            
            tt=StopTime;
            NN=1;
            while tt<1
                tt=tt*10;
                NN=NN*10;
            end
            tt=ceil(tt)/NN;
            cnfdb.StopTime=tt;
            
            tt=StepSize;
            NN=1;
            while tt<1
                tt=tt*10;
                NN=NN*10;
            end
            tt=floor(tt)/NN;
            cnfdb.StepSize=tt;
            
%             cnfdb.SetPoint=rr;
            cnfdb.ErrorBound=2;
            cnfdb.ErrorInPercent=1;
            
            cnfdb.ctrl_ok=1;
        end
        
    end    
end


% save the data
guidata(MainFig, cnfdb);
close(gcbf);


%
% set the controller
% -------------------------------------------------------------------------
function set_controller(FigHandle)


Params = struct(...
   'hBorder', 0.01,...
   'vBorder', 0.006,...
   'ButtonW', 0.10,...
   'ButtonH', 0.05,...
   'EditW',   0.4,...
   'EditH',   0.033,...
   'TextH',   0.030,...
   'TextW',   0.1,...
   'TextWL',  0.46,...
   'StdUnit','normalized ',...
   'StdColor',[.7 .7 .8]);  %get(0,'DefaultUIControlBackground')


cnfdb=guidata(FigHandle);

% Widget database
ud = struct('Parent',FigHandle,...
    'Figure',[],...
    'Handles',[]);


ThisFig = figure(...
   'Units',Params.StdUnit, ...
   'Color',Params.StdColor, ...
   'Position',[0,0.02, 1,0.92],...
   'MenuBar','none', ...
   'Visible','off',...
   'IntegerHandle','off',...
   'Name',xlate('CNF Controller Setup'), ...
   'NumberTitle','off', ...
   'Resize','on',...
   'WindowStyle','modal');

ud.Figure = ThisFig;

% Center wrt the main window
% centerfig(ThisFig);

top=1;
frmBorder=0.01;

frmWid = 0.96;
frmHt  = 0.25;
frmLft = (1-frmWid)/2;
frmBtm = top-2*frmBorder-frmHt;

frmPos   = [frmLft frmBtm frmWid frmHt];
ud.DiagramHandle = axes( ...
    'Parent',ThisFig, ...
    'Position',frmPos,...
    'Color','w', ...  % [.7 .7 .75]
    'XColor',[.5 .5 .75], ...
    'YColor',[.5 .5 .75], ...
    'XLim',[-0.8 11], ...
    'XTick',[], ...
    'YLim',[0 3], ...
    'YTick',[], ...
    'Box','on', ...
    'Visible','on');
%==============================================

% draw the block diagram of the controller
% cnf_diagram(ud.DiagramHandle,'Con');


TextH = Params.TextH;
TextW = Params.TextW;
TextWL = Params.TextWL;
EditH = Params.EditH;
EditW = Params.EditW;

Vgap = EditH+Params.vBorder;
top = 0.99;
Y0 = top -frmHt -Vgap;
Y = Y0;

frmLft = Params.hBorder;
frmWid = 0.5-2*frmLft;
frmHt = Vgap*7+Params.vBorder;
frmBot = Y-frmHt;
uicontrol('Parent',ThisFig, ...
   'Unit',Params.StdUnit,...
   'BackgroundColor',Params.StdColor, ...
   'ForegroundColor','w',...
   'Position',[frmLft frmBot frmWid frmHt], ...
   'Style','frame');

X0 = frmLft + Params.hBorder;
X = X0+TextW/2;

%  choose controller structure
Y=Y-Vgap;
uicontrol('Parent',ThisFig, ...
   'Unit',Params.StdUnit,...
	'BackgroundColor',Params.StdColor, ...
	'ForegroundColor','b', ...
	'Position',[X0,Y, TextWL  TextH], ...
   'FontSize',10,...
	'String','Controller Structure', ...
    'Style','text', ...
   'HorizontalAlignment','left');
%  Nonlinearity Pre-compensation
Y=Y-Vgap;
ud.Handles.check_NL=uicontrol('Parent',ThisFig, ...
   'Unit',Params.StdUnit,...
	'BackgroundColor',Params.StdColor, ...
	'Position',[X0,Y, EditW  TextH], ...
	'String','Using Nonlinearity Pre-compensation', ...
	'Style','Checkbox', ...
	'Callback',{@callback_check}, ...
    'HorizontalAlignment','left');
% integral
Y=Y-Vgap;
ud.Handles.check_IC=uicontrol('Parent',ThisFig, ...
   'Unit',Params.StdUnit,...
	'BackgroundColor',Params.StdColor, ...
	'Position',[X0,Y, TextWL  TextH], ...
	'String','Using Integral Compensation', ...
	'Style','Checkbox', ...
	'Callback',{@callback_check}, ...
    'HorizontalAlignment','left');

% filter
 Y=Y-Vgap;
 ud.Handles.check_FT=uicontrol('Parent',ThisFig, ...
     'Unit',Params.StdUnit,...
     'BackgroundColor',Params.StdColor, ...
     'Position',[X0,Y, TextWL  TextH], ...
     'String','Using an Additional (Notch) Filter', ...
     'Style','Checkbox', ...
     'Callback',{@callback_check}, ...
     'HorizontalAlignment','left');

% state feedabck or measurement feedback (full order or reduced order)
Y=Y-Vgap;
ud.Handles.radio_OV_none =uicontrol('Parent',ThisFig, ...
   'Unit',Params.StdUnit,...
	'BackgroundColor',Params.StdColor, ...
	'Position',[X0,Y, TextWL  TextH], ...

⌨️ 快捷键说明

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