Back
ENGR 058 (Control Theory) Laboratory
Introduction to the data
acquisition system
In this lab you will learn how to use MatLab and Simulink to develop and run a simple program that interacts
with the real world. The exercise is somewhat contrived, but will use many
of the features of the programs that you will use in lab throughout the semester. Although the program shell is given
to you, you should make an effort to understand how it works because you will be
using it as the basis for programs throughout the semester (you don't have to
use it, but it will probably make your life a bit easier).
If you are interested in the capabilities of the
PCI 6221 data acquisition
card, you can find them on the National
Instruments web site.
Apparatus
You will need a PC with data acquisition card and software, a signal generator, and an oscilloscope,
these are all in 310. Please put away any equipment you use when you are
done. There are only two sets of apparatus, so you may need to schedule
with other groups to avoid conflicts.
Creating a running a real-time model with MatLab/Simulink:
- Start MatLab.
- Start Simulink (type "Simulink") at the MatLab prompt.
- Create an empty directory on a disk where you have read
and write privileges.
- Change the current MatLab directory (upper left pane in
MatLab window) so that it shows the folder you created.
- Copy the file rtwShell.mdl into your new directory. Rename the file (but keep the
".mdl" extension).
- From Simulink, open the model file that you just
renamed. It should look like the one shown below.

- Open the Analog Input block and set the sample time to
0.01 seconds, and the input channel to 0. Either select the PCI 6221
board from the drop-down list, or install it as a new board. The diagram
should look as shown below.

Note that the analog channels in this window are labeled from 1 to 8; the
"Analog Inputs" on the DAQ box are labeled from 0 to 7. Make sure you
pay attention to this discrepancy in numbering when you are connecting signals to the box.
- Open the Analog Output block and set the output channel
to 1 and the sampling time to -1 (so that it uses the same sampling time as
the Analog Input block).

Note that the analog channels in this window are labeled from 1 and 2; the
"Analog Outputs" on the DAQ box are labeled 0 and 1. Make sure you pay
attention to this when you are connecting signals to the box.
- Now connect a signal that is approximately a 1V, 2Hz
sine wave from a (real) signal generator in the lab to Analog Input channel 0
on the DAQ box (this is input channel 1 according the Analog Input block dialog
box). Connect the channel 0 output to a (real) oscilloscope in the lab
(this is output channel 1 according the Analog Output block dialog box).
- Set the simulation to run for 10 seconds (go to
Simulation->Configuration Parameters and
set the end time to 10 seconds).

- Open the Simulink scope (by double clicking on it).
- Compile the model by going to
Tools->Real Time Workshop->Build Model.
You can observe the progress in the MatLab window. This takes several
seconds to finish.
- Make sure that the Simulation is set to
External (Simulation->External).
Connect to the model you just built, Simulation->Connect to Target, and then run it,
Simulation->Start Real-Time Code.
- Check to make sure that the output on the Simulink
scope matches that on the (real) scope in the lab. It should look
something like that shown below. This is after hitting the binocular
icon to zoom in on the data. Make sure you understand what is being
displayed on the diagram below and how it relates to the Simulink model.
Simulink Scope Output

Agilent Scope Output (Note
differences betweem this graph and previous for t<1 and t>9)

- Because we have the "To Workspace" block (labeled "simout"
on the Simulink diagram), the data has also been sent to the MatLab
environment. To access it, go to the MatLab window and type the
following commands (in green).
| Command |
Output |
Explanation |
|
>>
whos |
Name Size Bytes Class
simout 1x1 13594 struct array
Grand total 1628 elements, 13594 bytes |
Lists all variables.
Simout is a structure. |
|
>>
simout |
simout =
time: [800x1 double]
signals: [1x1 struct]
blockName: 'rtwShell/To Workspace' |
Ssmout has three parts:
time
signals
blockname |
|
>>
simout.signals |
ans =
values: [800x1 double]
dimensions: 1
label: '' |
simout.signals is a
structure
with three parts:
values
dimensions
label |
|
>>
t=simout.time; |
|
time variable |
|
>>
y=simout.signals.values; |
|
data |
>>
whos
|
Name
Size Bytes Class
simout 1x1 13594 struct array
t 1000x1 6400 double array
y 1000x1 6400 double array
Grand total 4032 elements,
bytes |
List all variables
again. |
>>
plot(t,y)
>> xlabel('Time');
>> ylabel('Data');
>> title('Real Time Data')
>> axis([0 10 -2 2]);
>> grid on |
|
plot (see below) |
Procedure:
Task 0:
- Delete the block labeled "Add Signals" from the Simulink
Diagram. We are now going to create our own block that does the same thing.
- Open the Simulink Library browser (go to
View->Library Browser
from the Simulink menu).
- From the Library Browser add an "Embedded MatLab
Function" from the "User-Defined Functions" submenu (under "Simulink") to your
Simulink model.
- Double click on the function and rename it MySum.
Add text as shown below.

- Save the block. There should now be a block in
your Simulink diagram with two inputs and an output that is labeled "MySum."
- Connect the new block in the place of the "Add Signals"
block that you deleted above.
- Compile and run the model to make sure it behaves as
you expect it to.
- Save your Simulink model.
Task 1:
- Save your Simulink model under a new name.
- Edit it so it looks like the one shown below.

- Add to the model so that it:
- samples data every 0.01 seconds for 5 seconds,
- outputs a quantity equal to twice the analog input for 0≤t≤4 seconds,
- shows output and input on the scope,
- saves both output and input to simout,
- outputs 0 for t>4 seconds.
- Connect a 2 volt, peak-to-peak, sine wave at 10 Hz from a signal generator
to the analog input of the Data Acquisition System and to an oscilloscope.
Also connect the output of the D/A to the oscilloscope. Run the program
and verify that it works as expected. If it takes more
than a few minutes to get everything working, please let me know and I can
give you a hand.
- Go to the MatLab workspace and use MatLab to plot both
the input and the output.
- Make graphs showing the input and output results when the signal is at
20 and 90 Hz. Make
sure you don't change the sampling rate. Also save data in files.
- Save this Simulink model.
Task 2:
- Change your model (and give it a new name) so it generates an output that is the
average of the last 5 input points. Do this by using the "Tapped Delay"
block (under Simulink and Discrete in Simulink Library Browser). This
generates a vector of the last "n" input values. Use an
embedded Matlab function to do the averaging (instead of just using Simulink
blocks) - you will be using code blocks in later labs, and I want to make sure
you understand how to use them.
- Repeat the measurements from Task 1 (i.e., input and
output at 10, 20 and 90 Hz).
- Save this Simulink model.
Task3:
- Change your model (and give it a new name) so it generates an output that is the
difference of the last 2 input points.
- Repeat the measurements from Task 1 (i.e., input and
output at 10, 20 and 90 Hz).
- Save this Simulink model.
Analysis and Report
Strive to be organized, though there is no strict format requirement
for this lab. Every graph should be numbered (i.e., graph 1, graph 2...)
and labeled (with a title, an x-axis label with units, and a y-axis label with
units) and should be explicitly referenced in the report; any tables should
likewise be numbered and labeled. You can simply reference these
laboratory instructions within your report (rather than merely repeating what I
have already written), unless you deviated significantly from
these instructions.
Explain (in one or two sentences each) the
function of each of the highlighted blocks (labeld A-G) in the diagram
below.

Include (at least) the following for Tasks 1 to 3:
- A printout of your Simulink diagram along with any
well-documented MatLab code.
- Clearly labeled and documented graphs. Make sure
input and output are clearly distinguished. There are several ways to present the data; consider several and pick the one that you feel displays the information most clearly.
Make the scale on all graphs the same as much as possible, for easy comparison
of results. There are lots of graphs in this experiment, pay attention to
organization and presentation (e.g., you can put all graphs from one task on one
page...).
- Comment on your results. Explain as much as you can. Be clear and concise.
- If you submit your report as a web page, make it so
that all of the pertinent information is on a single page (so I can easily
print it out to grade it).
Back