Code covered by the BSD License  

Highlights from
Motion Control Demo

image thumbnail

Motion Control Demo

by

 

30 Oct 2007 (Updated )

Model Based Design Demonstration Based on a Motion Control Case Study

ScriptFunction(choice)
function ScriptFunction(choice)
%---------------------------------------------
%|     The Digital Motion Control Demo       |
%---------------------------------------------
%| A practical lesson in Model-Based Design. |
%| Prepared by:                              |
%| Paul Lambrechts, May, 2007                |
%| Copyright 2007, The MathWorks, Inc.       |
%---------------------------------------------
%| Note: This demo is prepared with R2007a   |
%---------------------------------------------

% test if xPC target software is available
target_true=1;
try
    xpctargetping; 
catch
    target_true=0;
end

switch choice
    case 0 % Initialization: Pro Memori as this is already done in StartDemo 
        clear('all');
        close('all');
        bdclose('all');
        DMCdir=pwd;addpath([DMCdir,'\Utils'],[DMCdir,'\Model']);
        cd('Model');
        clc
        disp('DMC Demo Initialized')
    case 1 % Problem Formulation
        evalin('base','clean_desk;')
        cd('..\PF');
        DMC_system_demo; 
        set_param([bdroot,'/Reference and Feedforward'],'BlockChoice','Rigid Body P2P GUI');
        set_param([bdroot,'/Tunable Controller/Feedback Controller'],'BlockChoice','Preliminary');
        set_param(bdroot,'StopTime','inf');
        if target_true==1 && strcmp(xpctargetping,'success')
            set_param([bdroot,'/Plant'],'BlockChoice','Rapid Prototyping');
            set_param(bdroot,'simulationmode','external');
            base_addresses('Rapid Prototyping');
            ok=downloaddlm;
            % Next line is a trick to make the feedforward a little less
            % effective
            % set_param([bdroot,'/Reference and Feedforward/Rigid Body P2P GUI/Gain'],'Gain','0.9');
            if ok==1
                disp('Click ''Connect to Target'' to open user interface');
            end
        else
            set_param([bdroot,'/Plant'],'BlockChoice','Model');
            set_param([bdroot,'/Plant/Model/Motion System'],'BlockChoice','Simulink Model with VR');
            set_param(bdroot,'simulationmode','normal');
            % Next line is a trick to make the feedforward a little less
            % effective
            set_param([bdroot,'/Reference and Feedforward/Rigid Body P2P GUI/Gain'],'Gain','0.9');
            clc
            disp('No target found: model used instead');
        end
    case 2 % Transfer Function Measurement
        evalin('base','clean_desk;')
        cd('..\TFM');
        DMC_system_demo;
        set_param([bdroot,'/Reference and Feedforward'],'BlockChoice','Noise Generator');
        set_param(bdroot,'StopTime','20');
        if target_true==1 && strcmp(xpctargetping,'success')
            set_param([bdroot,'/Plant'],'BlockChoice','Black Box');
            set_param(bdroot,'simulationmode','external');
            base_addresses('Black Box');
            downloaddlm;
        else
            set_param([bdroot,'/Plant'],'BlockChoice','Virtual Black Box');
            set_param(bdroot,'simulationmode','normal');
            disp('No target found: Virtual Black Box activated');
        end
    case 3 % Retrieve Data
        cd('..\TFM');
        evalin('base','Retrieve_Data')
    case 4 % Plot Results
        cd('..\TFM');
        evalin('base','Calc_Hplant')
    case 5 % System Identification
        cd('..\TFM');
        if evalin('base','~exist(''Hpm'')') 
            evalin('base','load measdat;') 
            disp('Measurement data loaded from file'); 
        end; 
        ident
    case 6 % Open Simulink Model (Identification)
        cd('..\TFM');
        if evalin('base','~exist(''sys'',''var'')') 
            evalin('base','load sysdata;') 
            disp('Identified system loaded from file'); 
        end; 
        DMC_system_demo;
        set_param([bdroot,'/Plant'],'BlockChoice','Identified Model');
        set_param([bdroot,'/Reference and Feedforward'],'BlockChoice','Simple Step');
        set_param(bdroot,'StopTime','2');
        set_param(bdroot,'simulationmode','normal');
    case 7 % First Principles modeling
        evalin('base','clean_desk;') 
        cd('..\Model');
        DMC_system_demo
    case 8 % Parameter Estimation
        evalin('base','clean_desk;')
        cd('..\Model');
        DMC_system_demo
        set_param([bdroot,'/Reference and Feedforward'],'BlockChoice','Parameter Estimation');
        evalin('base','load PE_data')
        assignin('base','k',0.012);
        assignin('base','b12',1e-6);
        disp(sprintf('Parameters initialized for estimation: k=%g, b12=%g',evalin('base','k'),evalin('base','b12')));
    case 9 % Control Design
        evalin('base','clean_desk;') 
        cd('..\Model');
        DMC_system_demo
    case 10 % Rapid Prototyping
        evalin('base','clean_desk;')
        cd('..\RP');
        DMC_system_demo; 
        % Change the following line to activate your own designed controller for RP
        set_param([bdroot,'/Tunable Controller/Feedback Controller'],'BlockChoice','Tuned 10Hz');
        set_param([bdroot,'/Reference and Feedforward'],'BlockChoice','Fourth Order P2P GUI');
        set_param(bdroot,'StopTime','inf');
        if target_true==1 && strcmp(xpctargetping,'success')
            set_param([bdroot,'/Plant'],'BlockChoice','Rapid Prototyping');
            set_param(bdroot,'simulationmode','external');
            base_addresses('Rapid Prototyping');
            ok=downloaddlm;
            if ok==1
                disp('Click ''Connect to Target'' to open user interface');
            end
        else
            set_param([bdroot,'/Plant'],'BlockChoice','Model');
            set_param([bdroot,'/Plant/Model/Motion System'],'BlockChoice','Simulink Model with VR');
            set_param(bdroot,'simulationmode','normal');
            disp('No target found: model used instead');
        end
    case 11 % Show measured response
        evalin('base','Show_Response;')
    case 12 % Show generated code
        cd('..\RP');
        if exist('.\DMC_system_demo_xpc_rtw\html\DMC_system_demo_codegen_rpt.html','file')
            winopen('.\DMC_system_demo_xpc_rtw\html\DMC_system_demo_codegen_rpt.html')
        else
            disp('Code report not found')
        end
    case 13 % Open xPC Explorer
        cd('..\RP');
        xpcexplr
    case 14 % View via Web Browser
        evalin('base','clean_desk;')
        cd('..\RP');
        if target_true==1 && strcmp(xpctargetping,'success') 
            xpcwwwenable; web http://192.168.20.200:22222/ -browser; 
        else
            disp('ERROR: no xPC target connected'); 
        end
end
% End of ScriptFunction

function ok=downloaddlm
% Check if compiled target application file exists and download
if exist('.\DMC_system_demo.dlm','file')==2
    tg=xpc; 
    tg.load('DMC_system_demo'); 
    disp('Model downloaded, maybe you need to rebuild'); 
    ok=1;
else
    disp('Compiled target application file not found, build model before continuing'); 
    ok=0;
end

function base_addresses(plant)
% Change base addresses of IO cards to values specified in DMC_parameters
maskvals = get_param([bdroot,'/Plant/',plant,'/D//A'],'MaskValues');
maskvals{6} = evalin('base','IO301_base;');
set_param([bdroot,'/Plant/',plant,'/D//A'],'MaskValues',maskvals)
for IOblock={'ENC2','ENC1'}
    maskvals = get_param([bdroot,'/Plant/',plant,'/',IOblock{:}],'MaskValues');
    maskvals{5} = evalin('base','IO306_base;');
    set_param([bdroot,'/Plant/',plant,'/',IOblock{:}],'MaskValues',maskvals)
end

Contact us