DAQ, Simulation and Control in MATLAB and Simulink
DAQ, Simulation and Control in MATLAB and Simulink
Faculty of Technology, Postboks 203, Kjlnes ring 56, N-3901 Porsgrunn, Norway. Tel: +47 35 57 50 00 Fax: +47 35 57 54 01
Preface
In this Lab we will simulate different systems using Simulink. Simulink is a powerful graphical tool for
simulation and model-based design. The base for the simulations will be block diagrams, State Space
models and Transfer functions. We will also learn how we can get MATLAB and Simulink to work
together.
Next we will use the Data Acquisition Toolbox in MATLAB to control either the Air Heater or the
Level Tank.
Go to the following website for more background information about the Lab Work:
http://home.hit.no/~hansha/?lab=matlab_simulink
Note! You need to have the Data Acquisition Toolbox for MATLAB installed. In addition you need to
have the NI-DAQmx driver installed.
ii
Table of Contents
Preface......................................................................................................................................................ii
Table of Contents ....................................................................................................................................iii
1
Introduction .................................................................................................................................... 4
1.1
MATLAB ................................................................................................................................... 4
1.2
Simulink ................................................................................................................................... 4
Simulink .......................................................................................................................................... 6
2.1
2.2
Simulation................................................................................................................................ 6
Task 1:
Task 2:
Task 3:
Task 4:
Introduction ........................................................................................................................... 13
Task 5:
Task 6:
Task 7:
iii
1 Introduction
1.1 MATLAB
MATLAB is a tool for technical computing, computation and visualization in an integrated
environment, e.g.,
MATLAB is developed by The MathWorks. MATLAB is a short-term for MATrix LABoratory. MATLAB is
in use world-wide by researchers and universities.
For more information, see www.mathworks.com
For more information about MATLAB, see the following Tutorial: Introduction to MATLAB
MATLAB Quickie!.
1.2 Simulink
Simulink, developed by The MathWorks, is a commercial tool for modeling, simulating and analyzing
dynamic systems. Its primary interface is a graphical block diagramming tool and a customizable set
of block libraries. It offers tight integration with the rest of the MATLAB environment and can either
drive MATLAB or be scripted from it. Simulink is widely used in control theory and digital signal
processing for simulation and design.
Introduction
Recommended self-study material before you start with the assignment itself:
Read the Introduction to Simulink document. You will need to know these topics when
doing the rest of the tasks in this Lab Work.
The Tutorial is available from: http://home.hit.no/~hansha/.
Simulink Quickie!. Try to keep up and create the Simulink Block diagram while watching
the video.
Lab Work: Data Acquisition, Simulation and Control in MATLAB and Simulink
2 Simulink
Simulink, developed by The MathWorks, is a commercial tool for modeling, simulating and analyzing
dynamic systems. Its primary interface is a graphical block diagramming tool and a customizable set
of block libraries. It offers tight integration with the rest of the MATLAB environment and can either
drive MATLAB or be scripted from it. Simulink is widely used in control theory and digital signal
processing for simulation and design.
In this part you will learn:
Read the paper Differential equations represented as block diagrams by Finn Haugen,
available from my Blog. What are the most used blocks (the elementary blocks) in block diagrams?
Get to know Simulink Library Browser and find out where you can find the different blocks from
the task above.
2.2 Simulation
In this task you will use Simulink for simulation.
Simulink
Create the block diagram in Simulink for the system in the previous task and run the simulation from
Simulink using:
Use the different Sources blocks in Simulink in order to define the control value u, e.g., Step or Ramp.
Show and plot the results in, e.g., a Scope.
Run the simulation from MATLAB running the m-file from the command window.
[End of Task]
Simulink
Modify your block diagram (save with a new name) and use this function in your block-diagram to
calculate the output y instead.
X1
y=f(x1,x2)
X2
Tip! Use the Embedded MATLAB Function block in Simulink. For more details, see Appendix A
Embedded Algorithms.
Run the simulation. Is the result the same?
[End of Task]
State-Space models:
Define a state-space model of your system on the form:
Create a new block diagram in Simulink using the state-space model instead.
Use the State-space block in Simulink:
Define
in the m-file.
Lab Work: Data Acquisition, Simulation and Control in MATLAB and Simulink
Simulink
Transfer Functions:
Create a new block diagram in Simulink where you use the Transfer Function block:
Define the numerator and denominator from the transfer function in the m-file.
Use the m-file from the task above (save with new name) and use the MATLAB function ss2tf to
find the numerator and denominator in the transfer function from the state-space model found
above.
x1
__3__
1+0,5s
x2
2
s
Create block diagram of the system using pen and paper. Then create the block diagram in
Simulink. Run the simulation. Plot
. Is the result the same as in the task above?
6
-
1
s
x1
2
1
s
Lab Work: Data Acquisition, Simulation and Control in MATLAB and Simulink
x2
10
Simulink
Use Block diagram reduction in order to change the block diagram above to the form:
H1
x1
H2
x2
G__
1+GH
Create the block diagram in Simulink and run the simulation using Transfer function blocks in
Simulink. Define the transfer function in an m-file. Plot
in the same plot (Scope). Is the result
the same as in the task above? Why/Why Not?
[End of Task]
Lab Work: Data Acquisition, Simulation and Control in MATLAB and Simulink
11
Simulink
or
Where:
Please refer to the LM-900 Level Tank Lab Equipment document available from:
http://home.hit.no/~hansha/ for more information about how to use and operate the LM-900 Level
Tank system.
You may, e.g., use the following values in the simulations:
Air Heater:
Lab Work: Data Acquisition, Simulation and Control in MATLAB and Simulink
12
Simulink
]}
where,
Please refer to the Air Heater Lab Equipment document available from:
http://home.hit.no/~hansha/ for more information about how to use and operate the Air Heater
system.
You may, e.g., use the following values in the simulations:
LabVIEW:
In LabVIEW you can convert Simulink models into native LabVIEW code. Import your model of the Air
Heater and make it work in LabVIEW.
[End of Task]
Lab Work: Data Acquisition, Simulation and Control in MATLAB and Simulink
3 Data Acquisition in
MATLAB and Simulink
3.1 Introduction
Note! The method explained here will only work for a 32-bit MATLAB version. If you have a 64-bit
MATLAB, you need to use something called Session-based Interface.
The 64-bit version of Data Acquisition Toolbox supports National Instruments devices that can be
used with the session-based interface. For other supported NI data acquisition devices, you must use
the 32-bit version of Data Acquisition Toolbox and MATLAB. The 32-bit versions of Data Acquisition
Toolbox and MATLAB can be installed on a 64-bit Windows OS.
For more details, please read the Tutorial: DAQ in MATLAB.
We will use the Data Acquisition Toolbox in MATLAB to create a simple Data Acquisition application.
A Simple DAQ application should follow these steps:
1. Initialization
2. Read/Write
3. Clean Up
We will explain the different steps below:
Initialization:
In Initialization you need to specify what kind of device you are using. We can use the analoginput()
and analogoutput() functions in the Data Acquisition Toolbox.
Example:
ai = analoginput('nidaq', 'Dev1');
and:
ao = analogoutput('nidaq', 'Dev1');
Note! Dev1 is your device name, and it might be different on your computer. In the Measurement
and Automation Explorer can you see your device name or even change it if you want to.
13
14
Next we need to specify which channel(s) we want to use. We can use the addchannel() function.
Example:
ai0 = addchannel(ai, 0);
Read/Write:
If we want to write a single value to the DAQ device, we can use the putsample() function.
Example:
ao_value = 3.5;
putsample(ao, ao_value)
If we want to read a single value from the DAQ device, we can use the getsample() function.
Example:
ai_value = getsample(ai)
Clean Up:
When we are finished with the Data Acquisition we need to close or delete the connection. We can
use the delete() function.
Example:
delete(ai)
to the DAQ device on an AO channel, we will then read the same value on the
Extend your program by using a loop and read new values in each interval.
Lab Work: Data Acquisition, Simulation and Control in MATLAB and Simulink
15
[End of Task]
Lab Work: Data Acquisition, Simulation and Control in MATLAB and Simulink
4 PID Control
In this task we will implement a control system in Simulink and use a PI controller. We will both use a
simulator and the real process.
Additional help for this task is found in the following Tutorials:
Introduction to Simulink
DAQ in MATLAB
( )
( )
( )
( )
This transfer function can easily be implemented in the standard transfer function block in Simulink.
Finally, Test your application on the real process. Make sure to use a Low-pass filter.
Find proper PI parameters. Make sure the PI controller works well on both the real process and the
simulator. Make a change in the reference and see if the controller can do the job.
Show the results with and without a low-pass filter.
[End of Task]
16
Appendix A Embedded
Algorithms
Follow these steps in order to create an Embedded Algorithm in MATLAB:
1. Tutorial: Introduction to Simulink Chapter 7 EMBEDDED ALGORITHMS Read it!!
2. Getting Started with Simulink video by The MathWorks (see Figure below)
Note! The Tutorial: Introduction to Simulink Chapter 7 EMBEDDED ALGORITHMS explains the
basic principle for how you should solve this Task.
The procedure is as follows:
Step 1: Create your function calculate_output in the m-file Editor in MATLAB
Note! Make sure your MATLAB function is compiled as an embedded MATLAB function using the
#eml directive!!!
Example (You have to adjust it to your code):
17
18
Step 3: Double-click on Embedded MATLAB Function block and use this to call your function
(calculate_output).
Lab Work: Data Acquisition, Simulation and Control in MATLAB and Simulink
19
If you still cant get it right and you get an error like this:
20
Lab Work: Data Acquisition, Simulation and Control in MATLAB and Simulink
E-mail: hans.p.halvorsen@hit.no
Blog: http://home.hit.no/~hansha/