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
Course Manual
LabVIEW Course Simulation in LabVIEW
Hans-‐Petter Halvorsen, 2013.10.08
ii
Table of Contents Table of Contents .................................................................................................................................... ii
1 Introduction ...................................................................................................................................... 4
2 Differential Equations and Block Diagrams ...................................................................................... 6
2.1 1. Order systems ....................................................................................................................... 6
Task 1: 1.order system ............................................................................................................ 6
2.2 2. Order systems ....................................................................................................................... 7
Task 2: 2.order system ............................................................................................................ 7
3 Simulation in LabVIEW ..................................................................................................................... 8
Task 3: Bacteria Population .................................................................................................. 12
Task 4: Simulation of a “spring-‐mass damper” system in LabVIEW ..................................... 13
Task 5: Simulation Loop ........................................................................................................ 14
3.1 Simulation Subsystem ............................................................................................................. 15
Task 6: Simulation Subsystems ............................................................................................. 16
4 Discrete Systems ............................................................................................................................. 17
4.1 Introduction ............................................................................................................................ 17
4.2 Discretization in MathScript .................................................................................................... 18
4.3 Discretization in LabVIEW ....................................................................................................... 20
4.4 Exercises .................................................................................................................................. 21
Task 7: Discrete system ........................................................................................................ 22
Task 8: Discrete Controller .................................................................................................... 22
Task 9: Discrete State-‐space model ...................................................................................... 22
Task 10: Discretization .......................................................................................................... 23
Task 11: Bacteria Population – Discrete Simulation ............................................................. 24
iii Table of Contents
LabVIEW Course: Simulation in LabVIEW
Task 12: Discrete Low-‐pass Filter .......................................................................................... 25
Task 13: Discrete PI controller .............................................................................................. 26
Task 14: Simulation ............................................................................................................... 27
4
1 Introduction LabVIEW has lots of built-‐in features for control and simulation. Below we see some of the palettes and functions available inside LabVIEW.
If we have the differential equation(s) or the transfer function(s) for the system we can easily implement them in LabVIEW in different manners. One of the most used method is to create a block diagram based on the differential equation(s) or the transfer function(s). The block diagram we create in LabVIEW will then be almost identical to the block diagram created with pen and paper.
Below we see an example where we control and simulate a dynamic system based on a differential equation.
LabVIEW offers a spesific loop used for simulation purposes, called “Control and Simulation Loop”, but an ordinary While can also be used.
5 Introduction
LabVIEW Course: Simulation in LabVIEW
In order to create such as system in LabVIEW, we need to use the blocks available in the Simulation palette, shown below.
Some of the most used block are in the “Continuous Linear Systems” subpalette.
6
2 Differential Equations and Block Diagrams
Here you will learn how create block diagrams from your differential equations. In LabVIEW we can simulate dynamic systems using block diagrams.
Example:
Given the following dynamic system (differential equation):
𝑥 = −𝑎𝑥 + 𝑏𝑢
The block diagram for this equation is as follows:
2.1 1. Order systems
Task 1: 1.order system
Given the following system:
𝑥 = 𝑎𝑥 + 𝑏𝑢
Draw a block diagram for the system using pen and paper.
Given the following system:
𝑥 = 𝑎𝑥 + 𝑏𝑢 + 𝑐𝑣
Draw a block diagram for the system using pen and paper.
7 Differential Equations and Block Diagrams
LabVIEW Course: Simulation in LabVIEW
[End of Task]
2.2 2. Order systems
Task 2: 2.order system
Given the following system:
𝑚𝑥 = 𝐹 − 𝑑𝑥 − 𝑘𝑥
Draw a block diagram for the system using pen and paper.
𝑥 is the position
𝑥 is the speed
𝑥 is the acceleration
F is the Force (control signal, u)
d and k are constants
[End of Task]
8
3 Simulation in LabVIEW Here you will learn how to use the Simulation palette in LabVIEW. You need the “LabVIEW Control Design and Simulation Module”.
Below we see the Control Design & Simulation palette in LabVIEW:
Below we see the Simulation palette in LabVIEW:
The foundation for the simulations is the “Control & Simulation Loop”, which is located in the upper left corner in the “Simulation” palette. The Control & Simulation Loop is very similar to an ordinary “While loop”, but it has built-‐in features and is optimized for simulation purposes.
Example:
Given the following dynamic system (differential equation):
9 Simulation in LabVIEW
LabVIEW Course: Simulation in LabVIEW
𝑥 = −𝑎𝑥 + 𝑏𝑢
The block diagram for this equation is as follows:
LabVIEW Implementation:
We can easily implement this block diagram and plot the results in LabVIEW:
Let’s set 𝑎 = 0.25 and 𝑏 = 2. When we run the simulation, we get the following results:
10 Simulation in LabVIEW
LabVIEW Course: Simulation in LabVIEW
Simulation Parameters:
In the example the following simulation parameters are used (right-‐click on the Simulation Loop border and select “Configure Simulation parameters…”):
Transfer Function:
We can also find the transfer function for the system using Laplace:
11 Simulation in LabVIEW
LabVIEW Course: Simulation in LabVIEW
𝑠𝑥 𝑠 = −𝑎𝑥 𝑠 + 𝑏𝑢(𝑠)
This gives the following transfer function:
𝐻 𝑠 =𝑥(𝑠)𝑢(𝑠)
=𝑏
𝑠 + 𝑎=
𝑏/𝑎1𝑎 𝑠 + 1
=𝐾
𝑇𝑠 + 1
This means 𝐾 = 𝑏/𝑎 and 𝑇 = 1/𝑎
With 𝑎 = 0.25 and 𝑏 = 2 we get:
𝐻 𝑠 =𝑥(𝑠)𝑢(𝑠)
=8
4𝑠 + 1
From the plot above we see that this is correct (𝐾 = 8 and 𝑇 = 𝑇!" = 4𝑠).
In the “Continuous Linear Systems” palette in LabVIEW we have a block for defining a transfer function:
Then we can easily use the transfer function block instead:
This gives the same results:
12 Simulation in LabVIEW
LabVIEW Course: Simulation in LabVIEW
Note! The order of the coefficients are different in LabVIEW and MathScript.
We define a transfer function in LabVIEW like this:
Try with different values for 𝑎 and 𝑏 and see what’s happen – are the results according to the theory for 1.order transfer functions?
Task 3: Bacteria Population
In this task we will use LabVIEW and the LabVIEW Control Design and Simulation Module to simulate a simple model of a bacteria population in a jar.
The mathematical model for the bacteria population is as follows:
13 Simulation in LabVIEW
LabVIEW Course: Simulation in LabVIEW
birth rate=bx
death rate = px2
Then the total rate of change of bacteria population is:
𝒙 = 𝒃𝒙 − 𝒑𝒙𝟐
Set b=1/hour and p=0.5 bacteria-‐hour
We will simulate the number of bacteria in the jar after 1 hour, assuming that initially there are 100 bacteria present.
You should follow these steps:
1. Draw Block Diagram using “pen and paper”. 2. Start LabVIEW and use the Control and Simulation Loop from Control Design and
Simulation Palette in LabVIEW 3. Drag in the necessary Blocks from the palette (including the plot). 4. Use the “Connection Wire” from the Tools palette and draw the necessary wires. 5. Configure Simulation Parameters (right-‐click on the Control and Simulation Loop border) 6. Start the Simulation. The Simulation result should be present in a plot.
[End of Task]
Task 4: Simulation of a “spring-‐mass damper” system in LabVIEW
Use “LabVIEW Control Design and Simulation Module” and the “Control and Simulation Loop” in order to create a simulation of a spring-‐mass damper system.
The differential equation for the system is as follows:
14 Simulation in LabVIEW
LabVIEW Course: Simulation in LabVIEW
𝑥 =1𝑚(𝐹 − 𝑐𝑥 − 𝑘𝑥)
Where.
𝐹 is an external force applied to the system
𝑐 is the damping constant of the spring
𝑘 is the stiffness of the spring
𝑚 is a mass
𝑥 is the position of the mass
𝑥 is the first derivative of the position, which equals the velocity of the mass
𝑥 is the second derivative of the position, which equals the acceleration of the mass
Simulate and plot the position (𝒙), the velocity (𝒙), and the acceleration (𝒙) in 3 different plots.
Note! Draw a block diagram of the system using pen and paper before you implement the system in LabVIEW.
In the simulations you may set 𝐹 = −9.8,𝑚 = 1, 𝑐 = 1 𝑎𝑛𝑑 𝑘 = 100
Then try to set 𝑚 = 10 and see the difference. Try also different values for 𝑐 and 𝑘 and see what happens. Try to explain the results.
[End of Task]
Task 5: Simulation Loop
Given the following (nonlinear) system of a liquid tank:
𝑥 = −𝐾! 𝑥 + 𝐾!𝑢
where 𝑢 is the input (control value), 𝑥 is the output (level), 𝐾! is the valve outflow parameter, and 𝐾! is the pump inflow parameter.
Set 𝐾! = 1, 𝐾! = 1 and 𝑥! = 0. Apply a Step signal for 𝑢.
Create an application where you use “LabVIEW Control Design and Simulation Module” and the “Control and Simulation Loop” in order to simulate the system. Here you will use the simulation blocks from the Simulation palette in LabVIEW to create a continuous model.
Plot the results.
[End of Task]
15 Simulation in LabVIEW
LabVIEW Course: Simulation in LabVIEW
3.1 Simulation Subsystem You may create a Simulation Subsystem (File → New…):
The Simulation Subsystem is very useful when dealing with larger simulation systems in order to create a more structured code. I recommend that you (always) use this feature.
The Simulation Subsystem is almost equal to a normal LabVIEW Block Diagram but notice the background color is slightly darker.
Note! In order to open the Simulation Subsystem, right-‐click and select “Open Subsystem”.
Below we se an example of a Simulation Subsystem:
16 Simulation in LabVIEW
LabVIEW Course: Simulation in LabVIEW
As you can see when we use a Simulation Subsystem, we don’t need to use the Simulation Loop.
Below we see an example of a Simulation Subsystem used inside an ordinary While loop.
The advantage with this solution is that we can easly reuse the same subsystem in many applications. Another advantage is that we can use an ordinary While loop instead of a Simulation Loop.
This is an approach I would recommend in most situations.
Task 6: Simulation Subsystems
Create a Simulation Subsystem for one or more of the previous tasks, e.g. the “Bacteria Population” and/or the “Mass-‐spring-‐damper” system.
[End of Task]
17
4 Discrete Systems
4.1 Introduction We will use different discretization methods using pen and paper exercises and in practical implementation in MathScript/LabVIEW along with built-‐in discretization methods.
Euler:
Two commonly used methods for discretization are as follows:
Euler forward method:
𝑥 ≈𝑥 𝑘 + 1 − 𝑥(𝑘)
𝑇!
Euler backward method:
𝑥 ≈𝑥 𝑘 − 𝑥(𝑘 − 1)
𝑇!
Other methods are Zero Order Hold (ZOH), Tustin’s method, etc.
Where 𝑇! is the sampling time, and 𝑥 𝑘 + 1 , 𝑥(𝑘) and 𝑥(𝑘 − 1) are discrete values.
Note! Different notations may be used: 𝑥 𝑘 = 𝑥!= 𝑥 𝑡! , 𝑥 𝑘 + 1 = 𝑥!!! = 𝑥 𝑡!!! , 𝑥 𝑘 −1 = 𝑥!!! = 𝑥 𝑡!!! , etc.
Example:
Given the following system:
𝑥 = −𝑎𝑥 + 𝑏𝑢
We will use the Euler forward method to create a discrete version of this system.
We get as follows:
𝑥 𝑘 + 1 − 𝑥(𝑘)𝑇!
= −𝑎𝑥(𝑘) + 𝑏𝑢(𝑘)
Further:
𝑥 𝑘 + 1 = 𝑥 𝑘 + 𝑇![−𝑎𝑥(𝑘) + 𝑏𝑢(𝑘)]
18 Discrete Systems
LabVIEW Course: Simulation in LabVIEW
This gives:
𝑥 𝑘 + 1 = 𝑥 𝑘 − 𝑇!𝑎𝑥(𝑘) + 𝑇!𝑏𝑢(𝑘)]
Finally:
𝑥 𝑘 + 1 = (1 − 𝑇!𝑎)𝑥 𝑘 + 𝑇!𝑏𝑢(𝑘)
If we use = 0.25 , 𝑏 = 2 and 𝑇! = 0.1𝑠, we get:
𝑥(𝑘 + 1) = −0.975𝑥(𝑘) + 0.2𝑢(𝑘)
[End of Example]
4.2 Discretization in MathScript In MathScript we can simulate a discrete system using a For Loop or a While Loop.
We can also use some of the built-‐in functions to convert a continuous state-‐space model to a discrete state-‐space model. In MathScript we can use the function c2d() to convert from a continuous system to a discrete system.
Example:
We will simulate the following discrete system in MathScript using a For Loop:
𝑥 𝑘 + 1 = (1 − 𝑇!𝑎)𝑥 𝑘 + 𝑇!𝑏𝑢(𝑘)
We will set 𝑎 = 0.25 and 𝑏 = 2
MathScript code:
% Simulation of discrete model clear clc % Model Parameters a = 0.25; b = 2; % Simulation Parameters Ts = 0.1; %s Tstop = 20; %s uk = 1; x(1) = 0; % Simulation for k=1:(Tstop/Ts) x(k+1) = (1-a*Ts).*x(k) + Ts*b*uk; end
19 Discrete Systems
LabVIEW Course: Simulation in LabVIEW
% Plot the Simulation Results k=0:Ts:Tstop; plot (k, x) grid on
We use 𝑇! = 0.1𝑠 as the sampling time.
This gives the following results:
As you may notice, we get the same result as we get in the previous Exercise (10b) when we used LabVIEW.
[End of Example]
Example:
Given the following system:
𝑥 = −𝑎𝑥 + 𝑏𝑢
We will set 𝑎 = 0.25 and 𝑏 = 2
We will use the built-‐in function c2d() in order to find the discrete system:
% Find Discrete model clear clc % Model Parameters a = 0.25; b = 2; Ts = 0.1; %s
20 Discrete Systems
LabVIEW Course: Simulation in LabVIEW
% State-space model A = [-a]; B = [b]; C = [1]; D = [0]; model = ss(A,B,C,D) model_discrete = c2d(model, Ts, 'forward')
This gives the following results:
a
0.975 b
0.2 c
1 d
0
or:
𝑥(𝑘 + 1) = −0.975𝑥(𝑘) + 0.2𝑢(𝑘)
𝑦(𝑘) = 𝑥(𝑘)
As you see this is the same result as in the example above.
[End of Example]
4.3 Discretization in LabVIEW There are several ways we can make a discrete model in LabVIEW as well. In this exercise we will use the “Formula Node”. A Formula Node in LabVIEW evaluates mathematical formulas and expressions similar to C on the block diagram. In this way you may use existing C code directly inside your LabVIEW code. It is also useful when you have “complex” mathematical expressions.
Example:
We use the same discrete system as in previous examples:
𝑥 𝑘 + 1 = (1 − 𝑇!𝑎)𝑥 𝑘 + 𝑇!𝑏𝑢(𝑘)
21 Discrete Systems
LabVIEW Course: Simulation in LabVIEW
With 𝑎 = 0.25 and 𝑏 = 2
We will use the Formula Node and a For Loop in LabVIEW in order to simulate this discrete model.
The LabVIEW code is as follows:
The simulation results are as follows:
As you see we get the same results here as in the other examples.
[End of Example]
4.4 Exercises
22 Discrete Systems
LabVIEW Course: Simulation in LabVIEW
Task 7: Discrete system
Given the following system:
𝑥! = 𝐾!𝑢 − 𝑥!
𝑥! = 0
𝑦 = 𝑥!
Find the discrete system and set it on state-‐space form (using “pen and paper).
Use Euler forward:
𝑥 ≈𝑥 𝑘 + 1 − 𝑥(𝑘)
𝑇!
Define the continuous state-‐space model (“pen and paper”) and then implement the continuous state-‐space model in MathScript. Then use MathScript to find the discrete state-‐space model. Compare the result from the previous task.
Set 𝐾! = 1 and 𝑇! = 0.1
[End of Task]
Task 8: Discrete Controller
A controller is given by the following transfer function:
ℎ! 𝑠 =𝑢(𝑠)𝑒(𝑠)
=2(𝑠 + 0.5)𝑠 + 1
Find the continuous differential equation.
Find the discrete difference equation. Use the Euler backward method.
[End of Task]
Task 9: Discrete State-‐space model
Given the following system:
𝑥! = −𝑎!𝑥! − 𝑎!𝑥! + 𝑏𝑢
𝑥! = −𝑥! + 𝑢
𝑦 = 𝑥! + 𝑐𝑥!
23 Discrete Systems
LabVIEW Course: Simulation in LabVIEW
Find the discrete state-‐space model
Define the continuous state-‐space model (“pen and paper”) and then implement the continuous state-‐space model in MathScript. Then use MathScript to find the discrete state-‐space model. Compare the result from the previous subtask.
Use values 𝑎! = 5, 𝑎! = 2, 𝑏 = 1, 𝑐 = 1 and 𝑇! = 0.1.
[End of Task]
Task 10: Discretization
Given the following (nonlinear) system of a liquid tank:
𝑥 = −𝐾! 𝑥 + 𝐾!𝑢
Create a new application in LabVIEW where you Simulate the model using a Formula Node to implement the discrete model.
Use one of Eulers Discretization methods in order to create a discrete model of the system. Which of Eulers methods did you use? Why?
Use a While Loop or a For Loop and select a proper time-‐step 𝑇! for the simulation. Use the same settings and values as in the previous case.
Compare the results from the 2 different methods used above. Do you get the same results?
Example:
Implementing the model as a SubVI using the Formula Node:
Use the model in a For Loop:
24 Discrete Systems
LabVIEW Course: Simulation in LabVIEW
[End of Task]
Task 11: Bacteria Population – Discrete Simulation
In this task we will use LabVIEW and the LabVIEW Control Design and Simulation Module to simulate a simple model of a bacteria population in a jar.
The mathematical model for the bacteria population is as follows:
birth rate=bx
death rate = px2
Then the total rate of change of bacteria population is:
𝒙 = 𝒃𝒙 − 𝒑𝒙𝟐
Set b=1/hour and p=0.5 bacteria-‐hour
25 Discrete Systems
LabVIEW Course: Simulation in LabVIEW
We will simulate the number of bacteria in the jar after 1 hour, assuming that initially there are 100 bacteria present.
Find the discrete system using Euler, then simulate the system in LabVIEW. Use, e.g., the Formula Node, etc.
Task 12: Discrete Low-‐pass Filter
Transfer function for a first-‐order low-‐pass filter may be written:
𝐻 𝑠 =1
𝑇!𝑠 + 1
Where 𝑇! is the time-‐constant of the filter.
Create the discrete low-‐pass filter algorithm using “pen and paper”.
Use the Euler Backward method.
𝑥 =𝑥! − 𝑥!!!
𝑇!
Create a discrete low-‐pass filter in LabVIEW using the Formula Node in LabVIEW.
Create a SubVI of the code. You will use this subVI in your project later. The user needs to be able to set the time constant of the filter 𝑇! from the outside, i.e., it should be an input to the SubVI. The simulation Time-‐step 𝑇! needs also to be set from the outside.
Test and make sure your filter works!
Note! A golden rule is that:
𝑇! ≤𝑇!5
You may, e.g., use the “Uniform White Noise PtByPt.vi”. Example:
26 Discrete Systems
LabVIEW Course: Simulation in LabVIEW
[End of Task]
Task 13: Discrete PI controller
A continuous-‐time PI controller may be written:
𝑢 𝑡 = 𝑢! + 𝐾!𝑒 𝑡 +𝐾!𝑇!
𝑒𝑑𝜏!
!
Where u is the controller output and 𝑒 is the control error:
𝑒 𝑡 = 𝑟 𝑡 − 𝑦(𝑡)
Below we see a block diagram of a simple control system:
27 Discrete Systems
LabVIEW Course: Simulation in LabVIEW
Create the discrete PI Controller algorithm using “pen and paper”. Use the Euler Backward method.
𝑥 =𝑥! − 𝑥!!!
𝑇!
Create a discrete PI controller in LabVIEW using the Formula Node. Create a SubVI of the code. You will use this subVI in your project later.
[End of Task]
Task 14: Simulation
Implement your PI controller and Low-‐pass filter in a simulation. You may, e.g., use the example “General PID Simulator.vi” as a base for your simulation. Use the “NI Example Finder” (Help → Find Examples…) in order to find the VI in LabVIEW.
Note! You will need the “LabVIEW PID and Fuzzy Logic Toolkit” in order to fulfill this Task.
Run the example and see how it is implemented and how it works.
Note! Save the VI with a new name and replace the controller used in the example with the controller you created in the previous task.
28 Discrete Systems
LabVIEW Course: Simulation in LabVIEW
[End of Task]
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/