lab work: data acquisition, simulation and control in ...home.hit.no/~hansha/documents/lab/lab...

21
Telemark University College Department of Electrical Engineering, Information Technology and Cybernetics Faculty of Technology, Postboks 203, Kjølnes ring 56, N-3901 Porsgrunn, Norway. Tel: +47 35 57 50 00 Fax: +47 35 57 54 01 DAQ, Simulation and Control in MATLAB and Simulink HANS-PETTER HALVORSEN, 2012.08.20

Upload: ngominh

Post on 25-Apr-2018

225 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Lab Work: Data Acquisition, Simulation and Control in ...home.hit.no/~hansha/documents/lab/Lab Work/matlab_simulink/DAQ... · Lab Work: Data Acquisition, Simulation and Control in

Telemark University College

Department of Electrical Engineering, Information Technology and Cybernetics

Faculty of Technology, Postboks 203, Kjølnes ring 56, N-3901 Porsgrunn, Norway. Tel: +47 35 57 50 00 Fax: +47 35 57 54 01

DAQ, Simulation and Control in

MATLAB and Simulink

HANS-PETTER HALVORSEN, 2012.08.20

Page 2: Lab Work: Data Acquisition, Simulation and Control in ...home.hit.no/~hansha/documents/lab/Lab Work/matlab_simulink/DAQ... · Lab Work: Data Acquisition, Simulation and Control in

ii

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.

Page 3: Lab Work: Data Acquisition, Simulation and Control in ...home.hit.no/~hansha/documents/lab/Lab Work/matlab_simulink/DAQ... · Lab Work: Data Acquisition, Simulation and Control in

iii

Table of Contents

Preface ......................................................................................................................................................ii

Table of Contents .................................................................................................................................... iii

1 Introduction .................................................................................................................................... 4

1.1 MATLAB ................................................................................................................................... 4

1.2 Simulink ................................................................................................................................... 4

2 Simulink .......................................................................................................................................... 6

2.1 Block Diagrams ........................................................................................................................ 6

2.2 Simulation ................................................................................................................................ 6

Task 1: Basic Simulation in Simulink .................................................................................................... 6

Task 2: Using MATLAB Functions inside Simulink ............................................................................... 7

Task 3: State-space models and Transfer Functions ........................................................................... 8

Task 4: Simulation of Laboratory Process .......................................................................................... 10

3 Data Acquisition in MATLAB and Simulink ................................................................................... 13

3.1 Introduction ........................................................................................................................... 13

Task 5: My First DAQ Example in MATLAB ........................................................................................ 14

Task 6: DAQ in Simulink ..................................................................................................................... 15

4 PID Control .................................................................................................................................... 16

Task 7: PID Control ............................................................................................................................ 16

Appendix A – Embedded Algorithms..................................................................................................... 17

Page 4: Lab Work: Data Acquisition, Simulation and Control in ...home.hit.no/~hansha/documents/lab/Lab Work/matlab_simulink/DAQ... · Lab Work: Data Acquisition, Simulation and Control in

4

1 Introduction

1.1 MATLAB

MATLAB is a tool for technical computing, computation and visualization in an integrated

environment, e.g.,

Math and computation

Algorithm development

Data acquisition

Modeling, simulation, and prototyping

Data analysis, exploration, and visualization

Scientific and engineering graphics

Application development, including graphical user interface building

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

I also recommend the following Video:

“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.

Page 5: Lab Work: Data Acquisition, Simulation and Control in ...home.hit.no/~hansha/documents/lab/Lab Work/matlab_simulink/DAQ... · Lab Work: Data Acquisition, Simulation and Control in

5 Introduction

Lab Work: Data Acquisition, Simulation and Control in MATLAB and Simulink

Recommended self-study material before you start with the assignment itself:

Watch the video Getting Started with Simulink by The MathWorks.

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/.

I also recommend the following Video:

“Simulink Quickie!”. Try to keep up and create the Simulink Block diagram while watching

the video.

Page 6: Lab Work: Data Acquisition, Simulation and Control in ...home.hit.no/~hansha/documents/lab/Lab Work/matlab_simulink/DAQ... · Lab Work: Data Acquisition, Simulation and Control in

6

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:

The basic concepts in Simulink

The basic concepts in MATLAB

How to use a modern Simulation Tool with a graphical User Interface

Learn to create block diagrams

2.1 Block Diagrams

The main task is to be familiar with block diagrams and the different blocks used in Simulink.

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.

Task 1: Basic Simulation in Simulink

The following system is given:

Page 7: Lab Work: Data Acquisition, Simulation and Control in ...home.hit.no/~hansha/documents/lab/Lab Work/matlab_simulink/DAQ... · Lab Work: Data Acquisition, Simulation and Control in

7 Simulink

Lab Work: Data Acquisition, Simulation and Control in MATLAB and 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.

Method 1: Run Simulation from Simulink

Start the simulation from Simulink itself, by clicking the “Start Simulation” arrow in Simulink.

Method 2: Start Simulation in Simulink from MATLAB Command Window

Define the variables ( ) in MATLAB using the Command window and then run the

simulation from Simulink.

Method 3: Start Simulation in Simulink from a MATLAB M-file

Create a m-file in MATLAB where you define your variables

Run the simulation from MATLAB running the m-file from the command window.

[End of Task]

Task 2: Using MATLAB Functions inside Simulink

Create the following MATLAB function:

function y = calculate_output(x1, x2)

where

Page 8: Lab Work: Data Acquisition, Simulation and Control in ...home.hit.no/~hansha/documents/lab/Lab Work/matlab_simulink/DAQ... · Lab Work: Data Acquisition, Simulation and Control in

8 Simulink

Lab Work: Data Acquisition, Simulation and Control in MATLAB and Simulink

Modify your block diagram (save with a new name) and use this function in your block-diagram to

calculate the output y instead.

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]

Task 3: State-space models and Transfer Functions

The following system is given:

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.

Run the simulation. Is the result the same?

y=f(x1,x2) X1

X2

y

Page 9: Lab Work: Data Acquisition, Simulation and Control in ...home.hit.no/~hansha/documents/lab/Lab Work/matlab_simulink/DAQ... · Lab Work: Data Acquisition, Simulation and Control in

9 Simulink

Lab Work: Data Acquisition, Simulation and Control in MATLAB and 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.

Given the following system:

__3__

1+0,5s

x2u 2

s

x1

→ Create the system in Simulink and run a simulation. Plot

→ Convert the system above to a state-space model on the form:

Tip! Use pen and paper and Inverse Laplace.

→ 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?

Given the following block diagram:

1

s2

1

s

u

-

x2x1

2

6

Page 10: Lab Work: Data Acquisition, Simulation and Control in ...home.hit.no/~hansha/documents/lab/Lab Work/matlab_simulink/DAQ... · Lab Work: Data Acquisition, Simulation and Control in

10 Simulink

Lab Work: Data Acquisition, Simulation and Control in MATLAB and Simulink

→ Use Block diagram reduction in order to change the block diagram above to the form:

H1x2u

H2x1

Find the transfer functions:

Tip! Use the following Block diagram reduction rule:

G

H

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]

Task 4: Simulation of Laboratory Process

Create a model of the Level Tank and the Air Heater in Simulink and perform simulations. The models

should be implemented using Subsystems to make it easy to use the models in other block diagrams.

Simulate the models using a step and check the response using, e.g. a Scope.

LM-900 Level Tank:

Page 11: Lab Work: Data Acquisition, Simulation and Control in ...home.hit.no/~hansha/documents/lab/Lab Work/matlab_simulink/DAQ... · Lab Work: Data Acquisition, Simulation and Control in

11 Simulink

Lab Work: Data Acquisition, Simulation and Control in MATLAB and Simulink

A very simple (linear) model of the water tank is as follows:

or

[ ]

Where:

[cm] is the level in the water tank

[V] is the pump control signal to the pump

[cm2] is the cross-sectional area in the tank

[(cm3/s)/V] is the pump gain

[cm3/s] is the outflow through the valve (this outflow can be modeled more accurately

taking into account the valve characteristic expressing the relation between pressure drop

across the valve and the flow through the valve).

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:

Page 12: Lab Work: Data Acquisition, Simulation and Control in ...home.hit.no/~hansha/documents/lab/Lab Work/matlab_simulink/DAQ... · Lab Work: Data Acquisition, Simulation and Control in

12 Simulink

Lab Work: Data Acquisition, Simulation and Control in MATLAB and Simulink

A simple mathematical model of the Air Heater system could be:

{ [ ( ) ]}

where,

[ ] is the control signal to the heater.

[ ] is the time-constant.

[ ] is the heater gain.

[ ] is the time-delay representing air transportation and sluggishness in the heater.

is the environmental (room) temperature. It is the temperature in the outlet air of the

air tube when the control signal to the heater has been set to zero for relatively long time

(some minutes).

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]

Page 13: Lab Work: Data Acquisition, Simulation and Control in ...home.hit.no/~hansha/documents/lab/Lab Work/matlab_simulink/DAQ... · Lab Work: Data Acquisition, Simulation and Control in

13

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.

Page 14: Lab Work: Data Acquisition, Simulation and Control in ...home.hit.no/~hansha/documents/lab/Lab Work/matlab_simulink/DAQ... · Lab Work: Data Acquisition, Simulation and Control in

14 Data Acquisition in MATLAB and Simulink

Lab Work: Data Acquisition, Simulation and Control in MATLAB and Simulink

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)

For more details, please read the Tutorial: DAQ in MATLAB.

Task 5: My First DAQ Example in MATLAB

In this simple example we will create a m-file that write one single value to the DAQ device and then

read one single value from the DAQ device.

We start by connecting the Analog In and Analog Out wires together on the DAQ device (a so called

Loopback connection).

If we write, e.g., to the DAQ device on an AO channel, we will then read the same value on the

AI channel.

Extend your program by using a loop and read new values in each interval.

Page 15: Lab Work: Data Acquisition, Simulation and Control in ...home.hit.no/~hansha/documents/lab/Lab Work/matlab_simulink/DAQ... · Lab Work: Data Acquisition, Simulation and Control in

15 Data Acquisition in MATLAB and Simulink

Lab Work: Data Acquisition, Simulation and Control in MATLAB and Simulink

[End of Task]

Task 6: DAQ in Simulink

Create a simple Simulink application where you write and read values from the USB-6008 DAQ

device. Simulink has built-in blocks for Data Acquisition, but depending on the version of

MATLAB/Simulink you are using they might not work properly with the USB-6008 DAQ device. In that

case you can call MATLAB functions from Simulink.

[End of Task]

Page 16: Lab Work: Data Acquisition, Simulation and Control in ...home.hit.no/~hansha/documents/lab/Lab Work/matlab_simulink/DAQ... · Lab Work: Data Acquisition, Simulation and Control in

16

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

Task 7: PID Control

In this task we will create our own control system and implement it in Simulink. The process will be

either the Level Tank or the Air Heater (your choice).

Start with developing a control system for a model of the system (which you have already developed

in a previous task). You should develop a PI controller in Simulink

You can e.g., use the standard transfer function block. The transfer function for å PI controller is as

follows:

( ) ( )

( ) ( )

Where is the controller output and is the control error:

( ) ( ) ( )

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]

Page 17: Lab Work: Data Acquisition, Simulation and Control in ...home.hit.no/~hansha/documents/lab/Lab Work/matlab_simulink/DAQ... · Lab Work: Data Acquisition, Simulation and Control in

17

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):

Page 18: Lab Work: Data Acquisition, Simulation and Control in ...home.hit.no/~hansha/documents/lab/Lab Work/matlab_simulink/DAQ... · Lab Work: Data Acquisition, Simulation and Control in

18 Appendix A – Embedded Algorithms

Lab Work: Data Acquisition, Simulation and Control in MATLAB and Simulink

Step 2: Drag in the “Embedded MATLAB Function” into your Diagram.

Step 3: Double-click on “Embedded MATLAB Function” block and use this to call your function

(calculate_output).

Page 19: Lab Work: Data Acquisition, Simulation and Control in ...home.hit.no/~hansha/documents/lab/Lab Work/matlab_simulink/DAQ... · Lab Work: Data Acquisition, Simulation and Control in

19 Appendix A – Embedded Algorithms

Lab Work: Data Acquisition, Simulation and Control in MATLAB and Simulink

Example (You have to adjust it to your code):

If you still can’t get it right and you get an error like this:

Then the solution is:

Open the Help window in Simulink

Type “%#eml” in the Search for

The following window should appear:

Page 20: Lab Work: Data Acquisition, Simulation and Control in ...home.hit.no/~hansha/documents/lab/Lab Work/matlab_simulink/DAQ... · Lab Work: Data Acquisition, Simulation and Control in

20 Appendix A – Embedded Algorithms

Lab Work: Data Acquisition, Simulation and Control in MATLAB and Simulink

Scroll down and you find the solution:

Note! This is just an example. You have to adjust it to your code.

Page 21: Lab Work: Data Acquisition, Simulation and Control in ...home.hit.no/~hansha/documents/lab/Lab Work/matlab_simulink/DAQ... · Lab Work: Data Acquisition, Simulation and Control in

Telemark University College

Faculty of Technology

Kjølnes Ring 56

N-3918 Porsgrunn, Norway

www.hit.no

Hans-Petter Halvorsen, M.Sc.

Telemark University College

Faculty of Technology

Department of Electrical Engineering, Information Technology and Cybernetics

E-mail: [email protected]

Blog: http://home.hit.no/~hansha/