Sim U Link Example
Sim U Link Example
F input force
f orce v elocity
position v velocity
dynamic system
1 position
positive velocity
Out1 if { }
if (u1 > 0)
zero velocity
Out1 else { }
u1
else
If
Initialization Function in Model Properties Callbacks clear all, close all, clc
data = load('data.txt'); t = data(:,1); % time [s] dt = t(2) - t(1); % sample period [s] tfinal = max(t); % final time [s] F = [t data(:,2)]; % input force [N] xe = data(:,3); % experimental position [m]
% simulation parameters % t = linspace(0,2,20001)'; % time [s] % F = [t 4*sign(sin(2*pi*t))]; % square wave input voltage [V] % F = [t 4*sin(2*pi*t)]; % sine wave input voltage [V] % F = [t 4*(2/pi)*asin(sin(2*pi*t))]; % triangle wave input voltage [V] dt = t(2) - t(1); % sample period [s] tfinal = max(t); % final time [s]
% define model parameters M = 4; % mass [kg] B = 2; % damping coefficient [N/(m/s)] Fp = 0.1; % Coulomb friction magnitude in positive direction [N] Fn = 0.15; % Coulomb friction magnitude in negative direction [N] Stop Function in Model Properties Callbacks figure, subplot(2,1,1), plot(t,x,'k-',t,xe,'k:')
set(gca,'FontName','Arial','Fontsize',12,'FontWeight','Bold') legend('simulation','experimental','Location','NorthWest','Orientation','Horizontal') legend('Boxoff'), ylabel('position (m)') subplot(2,1,2); plot(t,F(:,2),'k-') set(gca,'FontName','Arial','Fontsize',12,'FontWeight','Bold') xlabel('time (s)'), ylabel('force (N)')
figure, subplot(2,1,1), plot(t,x,'k-') set(gca,'FontName','Arial','Fontsize',12,'FontWeight','Bold') ylabel('position (m)') subplot(2,1,2); plot(t,F(:,2),'k-') set(gca,'FontName','Arial','Fontsize',12,'FontWeight','Bold') xlabel('time (s)'), ylabel('force (N)')
To use Simulink, make sure the Current Directory in the Command Window is set to the directory containing your Simulink model. In the Model Explorer Window, under Model Properties, code can be inserted into the InitFcn and the StopFcn. The simulation parameters can be set by pressing Ctrl+E. Under Simulation Time, set the Start Time to 0.0 and Stop Time to tfinal, where the variable tfinal is set in the Callback InitFcn. Under Solver Options, set Type to Fixedstep, set Solver to ode4 (RungeKutta), and Fixedstep size to dt, where the variable dt is set in the Callback InitFcn. Some common blocks are given below.
Coulomb and Viscous Friction block Located in Discontinuities The output is a linear and nonlinear function of a velocity.
Coulomb friction
From Workspace block Located in Sources The output drives the simulation. The variable is a vector. The first column is time and the subsequent columns are the inputs corresponding to time. Gain block Located in Math Operations The output is the input multiplied by a number.
input input
K gain
Integrator block Located in Continuous Output is integral of input. Initial condition of output is contained in block. Mux block Located in Signal Routing The output is a vector composed of the input scalars. Subsystem block Located in Ports & Subsystems The output(s) are a complex function of the input(s). The complex function is described by a Simulink diagram. Sum block Located in Math Operations The output is a summation of the inputs. To Workspace block Located in Sinks The signal input to this block creates a vector of this signal at each sample period. Set the Save Format to Array.
1 s integrator
in
out
subsystem
output output
if
In1 if { } Out1
if (u1 > 0)
1 in
Merge
1 out
elseif
else
If
In else { } Out
else