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

Show_Response.m
%---------------------------------------------
%|     The Digital Motion Control Demo       |
%---------------------------------------------
%| A practical lesson in Model-Based Design. |
%| Prepared by:                              |
%| Paul Lambrechts, March, 2007              |
%| Copyright 2007, The MathWorks, Inc.       |
%---------------------------------------------
%| Note: This demo is prepared with R2006b   |
%---------------------------------------------

% Show measured response using data from xpctarget

% Ping target
if strcmp(xpctargetping,'failed')  % Contingency plan: use simulated data
    disp('Warning: Target PC not connected, using simulated data')
    if exist('simout','var') && exist('simout1','var')
        time = tout;
        n=simout.signals.values(:,1);
        u1=simout.signals.values(:,2);
        u2=simout.signals.values(:,3);
        Tin=n;
        Rin=simout.signals.values(:,4);
        err=simout.signals.values(:,5);
        enc1=simout1.signals.values(:,1);
        enc2=simout1.signals.values(:,2);
    else
        disp('Error: data not found')
        return
    end
    
else
    
    % Read from DATA.DAT
    fsys=xpctarget.fs;
    hfile=fsys.fopen('data.dat');
    if hfile==-1
        disp('Error: file DATA.DAT not found on target PC')
        return
    end
    rawdata =fsys.fread(hfile);
    fsys.fclose(hfile);

    % Read from DATA1.DAT
    fsys=xpctarget.fs;
    hfile=fsys.fopen('data1.dat');
    if hfile==-1
        disp('Error: file DATA1.DAT not found on target PC')
        return
    end
    rawdata1=fsys.fread(hfile);
    fsys.fclose(hfile);
    
    % Create the datastructures
    datastruct =readxpcfile(rawdata );
    datastruct1=readxpcfile(rawdata1);

    % Get the data and separate the timevector
    data =datastruct.data ;
    data1=datastruct1.data;
    time =data (:,end);
    time1=data1(:,end); 
    if time ~= time1
        disp('Error: datafiles not synchronous in time')
        return
    end
    
    % Assign the signals
    n=data(:,1);
    u1=data(:,2);
    u2=data(:,3);
    Tin=n;
    Rin=data(:,4);
    err=data(:,5);
    
    enc1=data1(:,3)*2*pi/2000;
    enc2=data1(:,4)*-2*pi/2000;
    
    disp('Measured response data retrieved from xPC target')
    
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Plot the measured signals
close all
ax(1)=subplot(3,1,1);
plot(time,[Rin enc1 enc2])
title('Measured Response')
xlabel('time [s]')
ylabel('response [rad]')
legend('reference','encoder 1','encoder 2')
grid

ax(2)=subplot(3,1,2);
plot(time,[n u2 u1])
title('Measured control torques')
xlabel('time [s]')
ylabel('response [Nm]')
legend('Tfeedforward','Tfeedback','Ttotal')
grid

ax(3)=subplot(3,1,3);
plot(time,[err enc2-enc1])
title('Measured Errors')
xlabel('time [s]')
ylabel('response [rad]')
legend('control error','encoder difference')
grid

linkaxes(ax,'x')

Contact us