# ECE 486 Control Systems Lab (Fall 2017)

 Day 1 Day 2 Day 3 Day 4 Short labs, weekly Day 5 Day 6 Day 8 Long labs, biweekly Day 7 Day 9 Day 10 Day 11 Final project, weekly

## Lab 3 – Digital Simulation of a Closed Loop System

Day 4 of ECE 486 Lab. After a “brutal” Prelab 3, we are into the much easier Lab 3 itself.

### Maths Behind the Scenes

Based off the numerical evidence in prelab 3, try to explain what are the advantages and disadvantages of a proportional controller? What’s the trade-off between choosing high $K_p$ and low $K_p$ values? (by tuning a single parameter in our case, i.e., the proportional gain $K_p$, also the same as the $K$ block in Figure 3.1 in lab book.)

There are three different controllers in this lab:

• Controller 1: Proportional control only, high $K_p$ and zero $K_d$.
• Controller 2: Proportional control only, low $K_p$ and zero $K_d$.
• Controller 3: PD control, high $K_p$ and a carefully designed $K_d$. (Why we need high $K_p$? Why we want a $K_d$ component in our control?)

### Matlab Part

Keep the header/preamble of your script from Lab 0. (Include those lines as part of your matlab script template.)

Based on the description above, we can unify the diagrams in all three cases: in the first two cases, we set $K_d = 0$ for Controllers 1 and 2; and the last one, initialize it with properly calculated numerical value as in prelab 3 for Controller 3.

Since you are not allowed to use differentiation/derivative block or transfer function block, you have to interpret the meaning of the block on the feedback loop. What does $K_d s + 1$ do to the incoming signal $\Omega(s) = \mathcal{L}\{\omega(t)\}$? You need to implement this feedback block using existing signals based on the algebra $(K_d s + 1)\Omega(s)$ tells you.

Hint: $(K_d s + 1)\Omega(s) = K_d \big(s\Omega(s)\big) + \Omega(s)$. Take inverse Laplace transform then read it again in natural language.

Our goal is to generate two figures: the first one—three overlaid step responses due to reference; the second one—overlaid step responses due to disturbance.

% ...

%% preamble
% lab 3 - digital simulation of a closed loop system
% by me and partner
% today's date

%%
clear     % clear values of (all) variables in workspace
clc       % clear messages in the command window
clf       % clear existing figures
close all % close all existing windows; w/o 'all', only close the latest

% ...

%% generate reference responses

% enable reference input; disable disturbance input
omega_r = 1;
tau_d   = 0;

% set up of controller 1: K_p = ...; K_r = ...; K_d = 0;
K   = ...;
K_r = ...;
K_d = 0;

% run simulation using controller 1

sim('lab3') % see the same line on page day 3 if you forgot what this means

% save data

% ...

% repeat what you did for controllers 2 and 3

%% generate disturbance responses

% enable disturbance input; disable reference input
tau_d   = 1;
omega_r = 0;

% set up of controller 1: K_p = ...; K_r = ...; K_d = 0;
K   = ...;
K_r = ...;
K_d = 0;

% run simulation using controller 1

sim('lab3')

% save data

% ...

% repeat what you did for controllers 2 and 3

%% plot data

% figure for step responses due to reference
figure(1)
plot(t_ref1, omega_ref1, 'r-')
hold on
plot(t_ref2, omega_ref2, 'b--')
% ...

% figure for step responses due to disturbance
figure(2)
plot(t_dist1, omega_dist1,'g:')
hold on
plot(t_dist2, omega_dist2,'k-.')
% ...


Visualize data using plot().

Always clean up before exiting. Specifically,

• Clean up bench table, restore pot, motor lock etc, reinstall screws;
• Sort out wires color by color, type by type and put them back to racks;
• Turn off oscilloscope, meters etc;
• Restore chairs.

## Follow-up

• Similar to a question in Lab 1 report, you need to sketch trajectory of closed loop poles here in Lab 3 report with varying $K_d$. (Hint: It is equivalent to a plot with varying $\zeta$. Why?) You can use pzmap() or pzplot() after you define a1 closed loop transfer function

$G(s) = \frac{ \omega_n^2 }{ s^2 + 2 \zeta \omega_n s + \omega_n^2}.$

Store varying $\zeta$’s in an array and loop over this array to get overlaid trajectory of pole locations. Note the differences between two scenarios illustrated in the following code,

% init closed loop transfer function parameters
wn = ...;

% ...

% store zeta in a data array
zeta = 0.1 : 0.1 : 10;

% define transfer function G(s)
sysG = tf(wn^2, [1 2*zeta*wn wn^2]);

% method 1: plot the trajectory; THIS IS WRONG. WHY?
pzplot(sysG)

% method 2: sweep zeta array
for i = ...
% ...

% define transfer function G(s) using only one zeta(i)
sysG = tf(wn^2, [1 2*zeta(i)*wn wn^2]);

% create a figure window to work on
% ...

% plot poles of sysG with current zeta(i) and hold this figure on
pzplot(sysG)

% ...
end % end of for loop

% ...

• Michael Zhang raised an interesting question about alignment in legend by code, I came up with a solution that is not perfect. You can check it out below. If you are aware of something better than this (of which I am sure), please update me on this—you will earn extra credit for a better solution.

% how to multiline strings in legend, title etc
% Michael Zhang raised this question

% 2017-09-28
% Y\"un Han
% ECE 486 Lab 3

%% preamble
clear     % clear values of variables in workspace
clc       % clear messages in the command window
clf       % clear existing figures
close all % close all existing windows; w/o 'all', only close the latest

%% multiline title and alignment in legend on a dummy figure

% random functions; does not really matter
t  = 0:.1:10;
y1 = sin(t);
y2 = 2*y1;
y3 = 2*y2;

% overlay plot of above three
figure(1)
plot(t, y1, 'r')
hold on
plot(t, y2, 'g--')
plot(t, y3, 'b:', 'linewidth', 2)
% how to multiline title, use cell {} data type
title({'This is an overlaid plot of', ...
'graph y_1', ...
'graph y_2', ...
'graph y_3'})
xlabel('t')
ylabel('y(t)')

% how to align in legend
% '%-20s' means left justified string of length 20 char
lgnd_ln1 = [sprintf('%-20s','y_1(t) blah '), 'aligned here'];
lgnd_ln2 = [sprintf('%-23s','y_2(t) '),      'aligned here'];
legend(sprintf('%-50s', lgnd_ln1), ...
sprintf('%-50s', lgnd_ln2), ...
['y_3(t) ','not aligned' ], ...
'location', 'best')
% note: this is what I can come up with so far, if you know a better
% solution, please let me know. my problem is with using a different width
% in the second sprintf, 23 is different from 20, which does not make sense
% to me.

• Next week Group A students meet only. Group B students have two weeks working on prelab 42 and lab 3 report. (This will be balanced for Group A when we have lab 5.)

Again, prelab 4 is the first prelab for 3-hour long labs, so you are encouraged to work on it now.

• For lab work, you always stick to formulae on page 13 of your lab book for approximation of time domain specs of a second order system (prototype).

### Due Date

Lab 3 Report is due at the beginning of Lab 4 (Oct 5, Group A; Oct 12, Group B). Prelab 4 is due on Monday by 5pm of the week of Lab 4 (Oct 2, Group A; Oct 9, Group B). Handwritten prelab is acceptable; typesetting is recommended.

### Questions

You are always very welcome to stop by office hours on Mondays. Emailing questions is another way. You can always include [ECE486]blah in the title of your question emails.

Spot any typos? Email me at once. You will earn up to +5 points for each typo/technical error reported.

## Footnotes:

1
There are two closed loop transfer functions, i.e., $\frac{\Omega(s)}{\Omega_r(s)}$ and $\frac{\Omega(s)}{T_d(s)}$, which one should you use? Or neither?
2
Prelab 4 requires no less work than Prelab 3. Warning for Prelab 4 stays the same as for Prelab 3. Please plan ahead and get started as early as possible.

Last updated: 2017-09-28 Thu 20:28