Matlab Simulink Tutorial
Matlab Simulink Tutorial
Erkorkmaz
In motion control systems, a reference trajectory describes the desired motion from
position A to position B. Taking derivatives of the trajectory corresponds to getting the
velocity, acceleration, jerk, snap and so on.
A Simple Trajectory
60
displacement
L
40
[mm]
20
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7
100
F
[mm/sec]
feedrate
50
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7
500 A
acceleration
[mm/sec 2]
D
-500
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7
1 2Time [msec] 3
T1 T2 T3
Given:
Acceleration: A = 500 mm/sec2
Deceleration: D = -500 mm/sec2
Feedrate (Velocity): F = 100 mm/sec
Travel Length: L = 50 mm
1
MTE360 Automatic Control Prepared by: M.Heng, Revised by: K. Erkorkmaz
F 2
2T 1 , 0 1 T1
1
FT1
s (t ) = + F2 , 0 < 2 T2 (1)
2
L FT3 + F3 F 32 , 0 < 3 T3
2 2T3
F
T1 = (2)
A
F
T3 = (3)
D
L T +T
T2 = 1 3 (4)
F 2
% Given:
A=500; % acceleration mm/sec^2
D=-500; % deceleration mm/sec^2
F=100; % feedrate (velocity) mm/sec
L=50; % travel length mm
Ts=0.001; % sampling period sec
Continues on page 3.
2
MTE360 Automatic Control Prepared by: M.Heng, Revised by: K. Erkorkmaz
% from equation 1
for index=1:length(tau1)
s1(index)=F*tau1(index)*tau1(index)/2/T1;
sd1(index)=F*tau1(index)/T1; % first derivative: velocity
sdd1(index)=A; % second derivative: acceleration
end
for index=1:length(tau2)
s2(index)=F*tau2(index) + F*T1/2;
sd2(index)=F;
sdd2(index)=0;
end
for index=1:length(tau3)
s3(index)=-F*tau3(index)*tau3(index)/2/T3 + F*tau3(index) +
L - F*T3/2;
sd3(index)=-F*tau3(index)/T3 + F;
sdd3(index)=D;
end
end
s1=s1';
s2=s2(2:end)';
s3=s3(2:end)';
s = [s1;s2;s3]; % concatenation of displacement vector
%... (do same for sd and sdd)
3
MTE360 Automatic Control Prepared by: M.Heng, Revised by: K. Erkorkmaz
Given a defined set for time, t , a dynamic system can be described with a function that
receive inputs, u (t ) , and produces outputs, y (t ) .
1. Create a Simulink model with a first order system, with gain, K , and time
constant, T . Simulate a unit step input and view both the input, u (t ) , and output,
y (t ) , through a scope, as in Figure 2. Experiment with K , T , the step input and
observe how the system response, y (t ) , behaves. Save data and plot results.
Hint: In order to save your data in the Workspace as Structure with time, you need to
double click on the Scope, go to Parameters > History, and modify the data format.
2. Apply trajectory from Example 1 as the input, as in Figure 3. Save data and plot
results.
4
MTE360 Automatic Control Prepared by: M.Heng, Revised by: K. Erkorkmaz
Figure 3: A simple first order system with trajectory input from Example 1.
5
MTE360 Automatic Control Prepared by: M.Heng, Revised by: K. Erkorkmaz
Solution to Part 1
6
MTE360 Automatic Control Prepared by: M.Heng, Revised by: K. Erkorkmaz
7
MTE360 Automatic Control Prepared by: M.Heng, Revised by: K. Erkorkmaz
A proportional controller is a simple feedback control design where the control signal, u ,
is the system error ( e = xr x ) multiplied by a gain, K p .
1. Create a Simulink model of a first order system cascaded with an integrator. The
gain should be K = 1 and time constant T =0.1 s. Simulate a square wave input
with unit amplitude and frequency of 0.3 Hz. The sample time is 0.001 sec. View
the reference position, xr (t ) , control signal, u (t ) , and actual position, x(t ) ,
through a scope, as in Figure 5. Experiment with different values of K p and
observe how the system response changes. Plot the results as in Figure 6.
Position Response
2
Position [mm]
0
Reference
-1
Actual
-2
5 6 7 8 9 10
20
Control signal, u
10
-10
-20
5 6 7 8 9 10
Time [sec]
Figure 6. Position response and control signal.
8
MTE360 Automatic Control Prepared by: M.Heng, Revised by: K. Erkorkmaz
2n
Given a transfer function: G (s) = , with n =5 rad/s, =0.1
s 2 + 2n s + 2n
Im G ( j )
G ( j ) = tan 1 Equation (6)
Re G ( j )
4. Use a logarithmic frequency scale from 10-1 to 102 rad/s, with 1000 points.
Magnitude and frequency should be log scales (help loglog) while Phase and
Frequency are linear and log scales (help semilogx), respectively, as shown in
Figure 7.
9
MTE360 Automatic Control Prepared by: M.Heng, Revised by: K. Erkorkmaz
clear;
figure(4);
nyquist(G);
10
MTE360 Automatic Control Prepared by: M.Heng, Revised by: K. Erkorkmaz
G(j)
|G(j)|
11
MTE360 Automatic Control Prepared by: M.Heng, Revised by: K. Erkorkmaz
For full solutions, Matlab code (.m files) and Simulink models (.mdl) will be available on
the course website.
12