# 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 4 – Introduction to the DC Motor

Day 5 of ECE 486 Lab. A nontrivial Prelab 4 hinted that there will be both heavy Matlab programming and data collecting in Lab 4.

This is our first 3-hour long lab. From this point on, lab class meets every other week until final project starts.

### Maths Behind the Scenes

• The reason why we call working with exponential data “linear fitting” is due to the fact that given any generic time exponential of the form $\varphi(t) = C_1 e^{C_2 t}$, where $C_1, C_2$ are constants, we can regard it as a map sending a real $t \in \RR$ to another real $e^t \in \RR^\times = \RR \setminus \{0\}$, i.e.,

\begin{align*} \varphi : \RR &\to \RR^\times, \\ \varphi : \hspace{1.9mm} t &\mapsto e^t. \end{align*}

Since $\varphi(t_1 + t_2) = C_1 e^{C_2(t_1 + t_2)} = C_1 e^{C_2 t_1} e^{C_2 t_2}$, if we are interested only in the coefficient $C_2$ but not $C_1$, exponentiation $\varphi$ turns summation into multiplication. Conversely, inverse map $\varphi^{-1} = \log$ ($= \ln$ in calculus sense) will reduce product to sum.

By another similar observation, we have

$\tag{*} \label{eqn:ln_fit} \underbrace{\ln (C_1 e^{C_2 t})}_{:= {\rm LHS}} = \ln C_1 + C_2 t.$

It is easy to see that working with exponential data, take natural logarithm of raw data and denote the manipulated data LHS of Equation \eqref{eqn:ln_fit}, then LHS is a linear function of time $t$.

• In calculus or analysis of real variables, we did not define what logarithm of a negative real number means. So things like $\log (-1)$ simply do not make sense.

However when working with complex numbers $z = a + b i \in \CC$, where $a, b \in \RR$, we can fix this problem and follow the intuition that logarithm is the inverse of the exponential function. To define $\log w$, we need to find such a $z$ that $e^z = w$ for a given $w \in \CC$. We can use the definition

$\tag{**} \label{eqn:lab4_log} \log w = \ln |w| + i \arg w.$

Note since $\arg w = \varphi + 2k \pi$ (for some $\varphi \in [0, 2\pi]$, $k \in \ZZ$) is $2π$-periodic, $\log w$ is not a single-valued function, i.e., the equality in Equation \eqref{eqn:lab4_log} means the two sets on both sides are equal.

For example, $\log(-1) = \pi i + 2k \pi i$, $k \in \ZZ$ according to the above definition of complex logarithm. (By Euler’s identity, $e^{\pi i} = \cos \pi + i \sin \pi = -1$.)

In Matlab, the imaginary part will be thrown away when plotting complex numbers as shown in the following example.

% ...

% define a complex number
a = 1 + 2*1i % 1i is the same as j and i but 1i speeds up computation
% create an array of complex numbers
t = 1:10;
at = a*t;

% see the warning from matlab
% "Warning: Imaginary parts of complex X and/or Y arguments ignored"
figure
plot(t, at)

% ...


### Matlab Part

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

Given raw data, how do we know which region shall return linear data for fitting? Read the following example.

% linear fitting with dirty data set

%% preamble
% lab 4 - 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

%% prepare a noisy data set
% create clean step response of a first order transfer function
% G(s) = 1/(1.5s + 1) with time constant 1.5[s]
sysG = tf(1, [1.5 1]);
% save step response of 10[s] in y(t) and t time array
[y, t] = step(sysG, 10);

% work on figure 1
figure(1) % clean data set
plot(t, y, 'b--')

% padding clean data set with noise
tHead = 0:0.1:3; % add noise data of 3 seconds at the beginning of t
t = t + 3;       % shift time by 3 seconds b/c of added 3 extra seconds
tTail = t(end):0.1:t(end)+5; % add noise data at 5 seconds at the tail of t
tTail = tTail';
yTail = 5*rand(size(tTail));
% created noisy data sets: t, y arrays

% work on figure 2
figure(2)
plot(t, y, 'r') % noisy data set we are going to use
xlabel('Time [s]')
ylabel('Voltage [V]')
title('Collected Data of First Order System Response with Noise')

%% how to extract 'linear region' in the noisy data set [y, t]?
% the response was collected when a unit step input was applied to first
% order system transfer function. from fig.2, at time t = 11.2, y settles
% at 0.9958.

% although hardcoding steady state is not recommended, here I use
% hardcoding to illustrate a bigger picture, i.e., to extract linear region
% for data fitting.

vss = 0.9958; % get steady state voltage from data set
v = y;        % assign data to voltage array

% we have
%
% Log(vss - v(t)) = -1/tau*t + Log(vss) -- (eqn. *)
%
% since v(t) = vss*(1 - e^(-t/tau)), where tau is the time constant.
% read ./ece486_day5.html#maths behind scenes for more.
%

% if we get the linear region correct, it will show as a line on the plot
% for manipulated data according to Equation (*).

% work on figure 3
figure(3)
plot(t, log(vss - v))

% on fig.3, as expected, we saw a straight line between second 3ish and 13ish
% and now we apply data cursor to record a start point of the linear region
% e.g., tStrt = 3.5, and end point tEnd = 9.5. but we don't know their
% indices in the array yet, so we need to use find() command

strtIdx = find(t > 3.5, 1, 'first'); % cf. Lab 0 script
endIdx  = find(t > 9.5, 1, 'first');

% supposedly we get the linear region already. to verify, extract linear
% portion of fig.3 and plot it on fig.4

% work on figure 4
figure(4)
plot(t(strtIdx:endIdx), log(vss - v(strtIdx:endIdx)))

% now we can compute fitting result using fig.4
lnCoeff = polyfit(t(strtIdx:endIdx), log(vss - v(strtIdx:endIdx)), 1);
% lnCoeff is a 2x1 vector, the first component is slope and second
% y-intercept.

% based on Equation (*), the slope of the straight line in fig. 4 is -1/tau.

% experimentally computed tau (should be close to 1.5) is
tauExp = - 1/lnCoeff(1) % tauExp = 1.3903

% question: why do we want to choose 'widest possible' linear range, i.e., in
% the above example, I chose t = 3.5 to 9.5. in practice, can we choose a
% narrower range than that? for example, t = 5.5 to 6.0? what problem will we
% have if use a narrower range?


A trick we can employ with six (or more) different data sets for figuring out linear region is, we don’t want to repeat the above code six times; instead, we overlay six graphs within a single plot and eyeball the common linear region for all of them only once using the code.

Visualize data using plot().

### Hardware Part

• All $K$’s are defined on page 27 of lab book and in case any confusion arises, refer to that page. Units of parameters are also indicated in the same table there.
• Measurements come in as voltages. We need to understand which voltage is associated with position, velocity etc. That is why in order to get the real velocities, we need to divide velocity measurement voltage $V_{\rm tach}$ by a gain $K_{\rm tach}$.
• 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

• Federico Miretti raised the question during lab that how we can take into account non-trivial values of wire resistance. If we denote the resistance of each wire as

$R_{{\rm wire}\ 1}, R_{{\rm wire}\ 2}, \ldots, R_{{\rm wire}\ n}$

respectively and denote the sum of them

$R_{\rm wire} := \sum_{i = 1}^n R_{{\rm wire}\ i},$

then we can rewrite the KVL equation of the circuit in Figure 4.4 (Lab manual) as

$\label{eqn:eq2_lab4} \tag{#} v_i(t) = i_a(t) \left( [R_s + R_{\rm wire}] + R_a \right) + L_a \frac{d i_a(t)}{dt}.$

In Equation \eqref{eqn:eq2_lab4}, I lumped the new resistance term $R_{\rm wire}$ with $R_s$. Now the new first order transfer function $\frac{V_o}{V_i}(s)$ becomes

$\label{eqn:eq3_lab4} \tag{##} \frac{V_o}{V_i}(s) = \frac{\frac{R_s}{ [R_s + R_{\rm wire}] + R_a }}{\left( \frac{L_a}{[R_s + R_{\rm wire}] + R_a} \right) s + 1}.$

If you look at the new time constant $\tau_e'$ of the transfer function in Equation \eqref{eqn:eq3_lab4}, it says when resistance of these wires cannot be ignored, you only have to update the $R_s$ term in your linear fitting equation with $[R_s + R_{\rm wire}]$. It is indeed a quick fix.

• You can look at your data and play with the linear fitting equation from Prelab 4

$\label{eqn:eq1_lab4} \tag{###} \ln (V_{\rm ss} - v_o(t)) = \underbrace{\ln{V_{\rm ss}}}_{\text{doesn't agree with data}} + \underbrace{(-\frac{1}{\tau_e})}_{\text{slope agrees with data}} t,$

where $V_{\rm ss} = V_i \frac{R_s}{R_a + R_s}$. Lian Yu raised the question that why $\ln V_{\rm ss}$ actually does not agree with the $y$-intercept from the fitting equation by Matlab, either from Basic fitting -> Show equations or polyfit(). His partner Howy Ho figured out (Kudos to him) this is because not only do we have to apply Equation \eqref{eqn:eq1_lab4} to linear region, when it comes to $y$-intercept, we also have to take into account when the linear portion occurs. In our lab, the step response started at time $t = 0$ but linear region did not show up until some time later. A horizontally shifted line will give you many different $y$-intercepts but the slope stays the same. (Think of sawtooth wave.)

• We’ve completed our first 3-hour long lab this week. From now on, future labs are based on previous ones (incremental). You are strongly encouraged to save programming scripts and data sets collected in lab before you leave each lab session.

Only Matlab programming in “script” is recommended. You would program in script file rather than in command line, since it would be more convenient to debug or rerun your code in script file. (Remember what I said about not hard coding transfer functions, using variables for Simulink block etc.?)

• Remember to start Prelab 5 early. There is a Prelab 5 template on the course website. Please include all your scratch work to get full credit if you decide to use that template. Handwritten Prelab 5 is acceptable but there are several comments in that template (i.e., hints) for you to check out. Prelab 5 and Lab 4 Report will be due in two weeks.

### Due Date

Lab 4 Report is due at the beginning of Lab 5 (Oct 19, Group A; Oct 26, Group B). Prelab 5 is due on Monday by 5pm of the week of Lab 5 (Oct 16, Group A; Oct 23, 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.

Last updated: 2017-10-12 Thu 12:31