ENGR 058 (Control Theory) Laboratory 1

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.

No single part of this lab should take a long time.  If you are having difficulties, or you are unfamiliar with the equipment in the lab (e.g., signal generators and oscilloscopes), please come to me for help.

Apparatus

You will need a PC with data acquisition card and software, a signal generator, an oscilloscope, and assorted cables and connectors.  These are all in Hicks 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:

  1. Start MATLAB.
  2. Create an empty directory on a disk where you have read and write privileges.
  3. Change the current MATLAB directory (left pane in MATLAB window when "Home" tab is selected) so that it shows the folder you created.
  4. Download the file rtwShell.slx into your new directory.  Rename the file (but keep the ".slx" extension).
  5. Start Simulink (type "simulink") at the MATLAB prompt; this may take a minute or two.
  6. From Simulink, open the model file that you just renamed (or just double-click on it).  It should look similar to the one shown below.
    Note: You may get a warning that the "Analog Input" and "Analog Output" blocks reference a board that is not installed. If this happens just double click on the "Analog Input" block in the Simulink diagram and follow the prompts to add the board.
  7. 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 dialog box should look as shown below. 

    Note that the analog channels in this window are labeled from 1 to 8; the "Analog Inputs" on the Terminal board are labeled from 0 to 7 (i.e., ADIN 0 through ADIN 7).  Make sure you pay attention to this discrepancy in numbering when you are connecting signals to the box.  Also - there are RCA to BNC adapters available in Hicks 310, or from Ed Jaoudi (the connectors on the board are RCA (Radio Corporation of America) connectors, the oscilloscope and function generators have BNC (Bayonet Needle Connector) connectors).
    Terminal Board
  8. 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 output channels in this window are labeled from 1 and 2; the "Analog Outputs" on the Terminal Board are labeled 0 and 1 (i.e., DAC 0 and DAC 1).  Make sure you pay attention to this when you are connecting signals to the board.
  9. Now connect a signal that is approximately a 1V peak to peak, 2Hz sine wave that is centered around ground from a  signal generator in the lab to Analog Input channel 0 (ADIN 0) on the Terminal Board (this is input channel 1 according the Analog Input block dialog box) and to an input on the oscilloscope. 
  10. Connect the channel 1 output (DAC 0) to another oscilloscope input oscilloscope in the lab (this is output channel 1 according the Analog Output block dialog box).
  11. Set the simulation to run for 10 seconds (go to Simulation→Model Configuration Parameters and set the end time to 10 seconds). Also set the solver type to "Fixed Step" and the solver to "discrete".
  12. Open the Simulink scope (by double clicking on it).
  13. Make sure the appropriate C compiler is chosen by entering "mex -setup" at the MATLAB prompt. Select the "Microsoft Software Development Kit (SDK) 7.1". You should only have to do this the first time you run the software.
  14. Compile and run the the model by going to Simulation→Run.  You can observe the progress in the MATLAB window.  This may take some time to finish (the Simulink model is transformed into a C language program and compiled).
  15. 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.   You can rerun the code by repeating this step - the code only needs to be rebuilt if you change the Simulink model.
  16. Check to make sure that the output on the Simulink scope matches that on the scope in the lab. The Simulink scope should look something like that shown below.  This is after right clicking on the graphs and choosing "Autoscale".    Make sure you understand what is being displayed on the diagram below and how it relates to the Simulink model; you will need to know this for the report.
    Simulink Scope Output


    Oscilloscope Output
    Yellow is the sine wave, and green is the output of the D/A.  Note differences between this graph and previous; t=0 for oscilloscope is arbitrary with respect to the Simulink simulation.  Any output data from t<1 or t>9 is set to zero (the output data below only exists for 8 seconds, as opposed to 10 seconds in the graph above).
  17. 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 (input has white background, MATLAB output has gray background).  Your results may vary slightly.
>> whos     % Lists all variables. "simout" is a structure.
Name     Size   Bytes    Class
simout   1x1    13594    struct array
Grand total 1628 elements, 13594 bytes
 
>> simout    % "simout" has three fields: time, signals, blockname
simout = 
time: [800x1 double]
signals: [1x1 struct]
blockName: 'rtwShell/To Workspace'
 
>> simout.signals    % simout.signals is a structure with three fields:  values, dimensions, label 
ans = 
   values: [800x1 double]
   dimensions: 1
   label: ''
 
>> t=simout.time;           % time variable
>> y=simout.signals.values; % data
>> plot(t,y)                % plot (see below)
>> xlabel('Time (S)');
>> ylabel('Data (V)');
>> title('Real Time Data')
>> axis([0 10 -2 2]);
>> grid on

Important: The simulation sometimes fails and Simulink won't let you run another simulation,  If this happens try entering the following commands at the MATLAB prompt:
>> rtclear
>> rtunload

Lab Procedure:

Task 0:

  1. Delete the block labeled "Add Signals" from the Simulink Diagram.  We are now going to create our own block that does the same thing.
  2. Open the Simulink Library browser (go to View→Library Browser from the Simulink menu).
  3. From the Library Browser add a function from "Simulink→User-Defined Functions→MATLAB Function".
  4. Double click on the function and rename it mySum.  Add text as shown below.  Make sure you leave in the comment line "%#codegen" this is necessary for proper compilation.
  5. Save the block.  There should now be a block in your Simulink diagram with two inputs and an output that is labeled "mySum."
  6. Connect the new block in the place of the "Add Signals" block that you deleted above.
  7. Compile and run the model to make sure it behaves as you expect it to.
  8. Save your Simulink model.

Task 1:

  1. Save your Simulink model under a new name.
  2. Edit it so it looks like the one shown below.
  3. Add to the model so that it:
  4. 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. 
  5. Go to the MATLAB workspace and use MATLAB to plot both the input and the output.
  6. Also get an image from the oscilloscope.  To get an image from the scope hit the "Utility" button on the front of the scope. The bottom of the scope screen will have labels, hit the button labeled "I/O" and get the IP address of the oscilloscope. Type the address into the title bar of a browser.  A web page opens from which you can download data and images.
  7. Make graphs showing the input and output results when the signal is at 20, 30, 50 and 90 Hz. Make sure you don't change the sampling rate.  Also save data in files.
  8. Save this Simulink model.

Saving multiple variables to simout.

To save several variables to the MATLAB workspace use either the "bus creator" or "Mux" block from the "Simulink→Commonly Used Blocks" library in the Simulink browser. 

   

Connect all the variables you would like to save to the left side of the block (you can change the number of inputs by doubling clicking on the block) and connect the output to the "Workspace" block, simout.   You can then extract the signals with the following code.

>> simout    % "simout" has the same three fields as before
simout = 
         time: [1001x1 double]
      signals: [1x1 struct]
    blockName: 'E58_Lab1EC/To Workspace'

>> simout.signals  % This time, the "values" field has two columns (one for each variable)
ans = 
        values: [1001x2 double]
    dimensions: 2
         label: ''

>> var1=simout.signals.values(:,1);   % 1st variable is 1st column of "values"
>> var2=simout.signals.values(:,2);   % 2nd variable is 2nd column

Task 2:

  1. 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 (in "Simulink→Discrete"  in the Library Browser).  This generates a vector of the last "n" input values; if you use this as an input to a "MATLAB Function" block, you can treat it as an n-element vector.  Use a "MATLAB Function" block to do the averaging (instead of just using Simulink blocks) - you will be using such code blocks in later labs, and I want to make sure you understand how to use them.
  2. Repeat the measurements from Task 1 (i.e., input and output at 10, 20, 30, 50 and 90 Hz).
  3. Save this Simulink model.

Task3:

  1. Change your model (and give it a new name) so it generates an output that is the difference of the last 2 input points. 
  2. Repeat the measurements from Task 1 (i.e., input and output at 10, 20, 30, 50 and 90 Hz).
  3. Save this Simulink model.

Report

Strive to be organized.  There is no strict format requirement for this lab and there is no need for a lot of prose. 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.  There is no need for separate sections for "Introduction," "Theory," or "Procedure...."

Include the following as distinct sections in your report:

Section 1 of report:

Explain (in one or two sentences each) the function of each of the highlighted blocks (labeld A-G)  in the diagram below. (10 pts)

Sections 2, 3, 4  of report:

For Tasks 1 through 3,  include the following subsections in  your report:

  1. A printout of your Simulink diagram  along with any well-documented MATLAB code.  You can get a screen shot of a window by selecting it an then hitting "Ctrl-Alt-PrtScn" on the keyboard. (5 points per task)
  2. There are several ways to present the data; consider several and pick the one that you feel displays the information most clearly.    There are lots of graphs in this experiment, pay attention to organization and presentation.  Neatness and clarity count. (15 points per task)
    G
    raphs:
  3. Comment on your results.  Explain as much as you can.  Be clear and concise. (10 pts per task)

To Turn In:

Turn in a pdf of your report on the course moodle page. 

The report should have 4 sections, and sections 2, 3, and 4 should have three parts (a, b, c).     You don't need a lot of text, but think about how you present your results; neatness and clarity count.  In particular make sure your graphs conform to format described above.