Vous êtes sur la page 1sur 8

MTE360 Automatic Control

1
Tutorial #1: An Introduction to Matlab/Simulink

Example #1: Plotting a trajectory vs. time

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.

1. Given Equations 1 to 4, calculate the desired trajectory, s(t), velocity, s(t), and
acceleration, s(t).
2. Plot the trajectory, velocity and acceleration profiles with respect to time as in
Figure 1.
3. Save the time and trajectory profile data to a text file.


0 0.1 0.2 0.3 0.4 0.5 0.6 0.7
0
20
40
60
A Simple Trajectory
d
i
s
p
l
a
c
e
m
e
n
t

[
m
m
]
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7
0
50
100
f
e
e
d
r
a
t
e

[
m
m
/
s
e
c
]
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7
-500
0
500
a
c
c
e
l
e
r
a
t
i
o
n

[
m
m
/
s
e
c
2
]
Time [msec]



Figure 1. A simple trajectory with trapezoidal velocity.


Given:
Acceleration: A = 500 mm/sec
2
Deceleration: D = -500 mm/sec
2
Feedrate (Velocity): F = 100 mm/sec

Travel Length: L = 50 mm
Sampling Period: T
s
= 0.001 sec
F
A
D
L

1
T
1 T
2
T
3
MTE360 Automatic Control

2


1
2
1
2T
Ft
;
1 1
0 T s st
( ) = t s
2
1
2
FT
F + t ;
2 2
0 T s <t Equation (1)

2 2
3
3
3
2
3
FT
L F
T
F
+ +

t
t
;
3 3
0 T s <t

A
F
T =
1
Equation (2)

D
F
T =
3
Equation (3)

( )
2
3 1
2
T T
F
L
T
+
= Equation (4)



Solution: Matlab Code























Continues on page 3.
% file: tutorial01_example01.m

clear all % clears the workspace
close all % closes all figure windows

% 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

T1=F/A; % from equation 2
T3=-F/D; % from equation 3
T2=L/F - (T1+T3)/2; % from equation 4

if T1<0 || T2<0 || T3<0 % kinematic compatibility conditions
disp('Error: acceleration, deceleration and travel length are not
kinematically compatible.');
else
% create row vector for time, initial time : step size : final time
tau1=0:Ts:T1;
%... (do same for tau2 and tau3)


MTE360 Automatic Control

3
Continued from page 2.













































% preallocate array for speed
s1=zeros(1,length(tau1));
sd1=zeros(1,length(tau1));
sdd1=zeros(1,length(tau1));
%... (do same for s2 and s3)

% 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

t1=tau1'; % change tau1 from a row vector to a column vector tau1'
t2=tau2(2:end)' + T1; % shift bounds and drop first vector element
t3=tau3(2:end)' + T1 + T2;
t = [t1;t2;t3]; % concatenation of time vector

s1=s1';
s2=s2(2:end)';
s3=s3(2:end)';
s = [s1;s2;s3]; % concatenation of trajectory vector
%... (do same for sd and sdd)

figure(1); % opens a figure window
subplot(3,1,1); % subplot(rows, columns, position)
plot(t,s); % plots trajectory versus time
title('A Simple Trajectory'); % creates a title for the plot
ylabel('displacement [mm]'); % labels the y-axis
xlabel('Time [msec]') % labels the x-axis
subplot(3,1,2);
%... (do same for sd and sdd)

% save data to file
data = [t s]; % t and s should be column vectors
save ex1_data.txt -ASCII -DOUBLE data
%... (save also sd and sdd to the same file)

%-- End of File --%
MTE360 Automatic Control

4
Example #2: Simulation of a dynamic system

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.

2. Apply trajectory from Example 1 as the input, as in Figure 3. Save data and plot
results.



Figure 2: A simple first order system.




Figure 3: A simple first order system with trajectory input from Example 1.
MTE360 Automatic Control

5
Solution to Question 1



Figure 4: Scope of first order system simulink model.


Solution to Question 1: Matlab Code






















% file: tutorial01_example02_q1.m

K=1; % set gain in workspace
T=1; % set time constant in workspace

open('fosystem.mdl'); % opens the model file
sim('fosystem.mdl'); % runs the simulation

% extract data from Scope Data struct
t = ScopeData.time;
u = ScopeData.signals.values(:,1);
y = ScopeData.signals.values(:,2);

% plot step input and output response
figure(2);
plot(t,u,t,y);
title('Step Input Response');
ylabel('Response, y');
xlabel('Time, t');
legend('Input','Output'); % creates a legend for the plot

% save data to file
data = [t u y];
%... (save as shown in Example 1)

%-- End of File --%

MTE360 Automatic Control

6

Solution to Question 2: Matlab Code































End of Tutorial


***Please attempt Examples 3 and 4 on pages 7-8***


For full solutions, Matlab code (.m files) and Simulink models (.mdl) will be available on
the course website.



% file: tutorial01_example02_q1.m

S = load('ex1_data.txt'); % loads data from file to the workspace
t = S(:,1); % extract time vector
u = S(:,3); % extract velocity profile

Tend = t(end); % get total time of simulation

K = 1; % set gain in workspace
T = 0.01; % set time constant in workspace

simin = [t u]; % array format for 1-D input signal

open('fosystem_traj.mdl'); % opens the model file
sim('fosystem_traj.mdl'); % runs the simulation

time = simout.time; % if the save format of simout is "Structure with Time"
y = simout.signals.values; % output vector

% plot input and output in the same figure
figure(3);
plot(t,u,'b-'); hold on % "hold on" retains the current plot
plot(time,y,'r-.');
title('Velocity Response');
ylabel('Response [mm/sec]');
xlabel('Time [sec]');
legend('Input','Output');

% save data to file
%... (as shown in Example 1)

%-- End of File --%



MTE360 Automatic Control

7
Example #3: Design a Proportional Controller (p-controller)

A proportional controller is a simple feedback control design where the control signal, u,
is the system error (e=x
r
-x) multiplied by a gain, K
p
.

1. Create a simulink model with a first order system, with gain, K=1, and time
constant, T=0.1. 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, x
r
(t), input,
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.

Figure 5. A proportional controller.

5 6 7 8 9 10
-2
-1
0
1
2
Position Response
P
o
s
i
t
i
o
n

[
m
m
]


Reference
Actual
5 6 7 8 9 10
-20
-10
0
10
20
C
o
n
t
r
o
l

s
i
g
n
a
l
,

u
Time [sec]


Figure 6. Position response and control signal.
MTE360 Automatic Control

8
Example #4: Frequency Response Function

Given a transfer function: ( )
1 2
1 . 0
+
=
s
s G

Plot its frequency response (also known as a Bode diagram).

Procedure (refer to textbook, page 826):
1. Subsitute s = j: G(s) G(j)
2. Rationalize the denominator with its complex conjugate.
3. Solve for the magnitude |G(j)| and the phase angle <G(j).

( ) ( ) ( ) ( ) ( )
2 2
Im Re e e e j G j G j G + = Equation (5)

( )
( )
( )
|
|
.
|

\
|
= Z

e
e
e
j G
j G
j G
Re
Im
tan
1
Equation (6)

4. Plot for frequencies of 0 to 60 Hz. Recall: =2f. Set the step size to 0.001.
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.

10
-3
10
-2
10
-1
10
0
10
1
10
2
10
3
10
-4
10
-3
10
-2
10
-1
Frequency Response
M
a
g
n
i
t
u
d
e
10
-3
10
-2
10
-1
10
0
10
1
10
2
10
3
-100
-50
0
P
h
a
s
e

[
d
e
g
]
Frequency, w [rad/sec]


Figure 7. Frequency response of a first order system.

Vous aimerez peut-être aussi