1/21/13

Finn Haugen, TechTeach: Introduction to LabVIEW Simulation Module

Introduction to simulation with Control Design and Simulation Module in LabVIEW 8.6
by Finn Haugen 16. March 2009

Contents:
1 Preface 2 Introduction 3 The contents of the Simulation functions palette (Video) 4 An example: Simulator of a liquid tank 4.1 Developing the mathematical model of the system to be simulated 4.2 The Front panel and the Block diagram of the simulator (Videos) 4.3 Configuring the simulation (Videos) 5 Various topics 5.1 Representing state space models using Formula node and integrators 5.2 Creating subsystems 5.3 Getting a linearized model of a subsystem 5.4 Simulating control systems 5.5 Converting models between Simulation Module and Control Design Toolkit 5.6 Putting code into a While loop running in parallell with a Simulation loop 5.7 Translating SIMULINK models into LabVIEW Simulation models

1 Preface
This document gives an introduction to the simulation tools of the LabVIEW Control and Simulation Module for LabVIEW 8.6. It is assumed that you have basic skills in LabVIEW programming. There are tutorials for LabVIEW programming available from the National Instruments' webside http://ni.com, and I have made one myself (to serve the needs in my own teaching more specifically), see Finn's LabVIEW Page. This tutorial contains a number of activities that you are supposed to perform. These activities are shown in blue boxes, as here: Activities are shown in blue boxes as this one. Most of the activities are video-based. In the former tutorial, based on LabVIEW 8.5, there were no videos, but in stead detailed explanations. I have removed the explanation in the present tutorial where the videos give the same information. If - for some reason - the videos does not display correctly in the current player, try some other. Windows Media Player is probably the default video player on your PC. Alternative players are RealPlayer and QuickTime (both can be downloaded from the Internet for free). More tutorials that may be relevant for you as a LabVIEW user are available from Finn's LabVIEW Page.
[T able of contents]

2 Introduction
The LabVIEW Control and Simulation Module contains a block diagram based environment for simulation of linear and nonlinear continuous-time and discrete-time dynamic systems. Many simulation algorithms (i.e. numerical methods for solving the underlying differential equations) are available, e.g. various Runge-Kutta methods. The mathematical model to be simulated must be represented in a simulation loop, which in many ways is similar to the ordinary while loop in LabVIEW. You can make the simulation run as fast as the computer allows, or you can make it run with a real or scaled time axis, thus simulating real-time behaviour, with the possibility of the user to interact with the simulated process. The simulation loop can run in parallel with while loops within the same VI.
[T able of contents]

techteach.no/labview/lv86/sim_module/index.htm

1/11

We assume the following (the parameters used in the expressions below are defined in the figure above): The liquid density is the same in the inlet. 4.1/21/13 Finn Haugen. The user can adjust the inlet by adjusting the pump control signal. TechTeach: Introduction to LabVIEW Simulation Module 3 The contents of the Simulation functions palette Once the Control and Simulation Module is installed. u. The simulation will run in real time. we start by developing a mathematical model of the tank. Liquid tank Any simulator is based on a mathematical model of the system to be simulated. It is assumed that you have the block diagram of any VI opened. The tank has straight. the Simulation palette is available from the Functions palette.htm 2/11 .1 Developing the mathematical model of the system to be simulated The system to be simulated is a liquid tank. I think this is an effective way to learn using the tools. Dynamic Systems . analysis and simulation or in any other book about dynamic systems theory. The liquid mass and level are related through m(t) = ρAh(t) The inlet volumetric flow through the pump is proportional to the pump control signal: qin (t) = Ku u(t) The outlet volumetric flow through the valve is proportional to the square root of the pressure drop over the valve. see the figure below. The video below gives overview over the contents of the Simulation Palette. as described in e. Thus.no/labview/lv86/sim_module/index. but not develop it from scratch. assuming that you have basic skills in LabVIEW programming. and in the tank.. the video will be opened in a new window) to get an overview over the contents of the Simulation Palette.g. You are supposed to have basic knowledge about modeling of dynamic systems. thereby giving the feeling of a "real" system. [T able of contents] 4 An example: Simulator of a liquid tank In this section we will study and partly develop a simulator of a liquid tank. in the outlet. with the possibility of scaled real time. vertical walls. Play the video sim_palette_overview (17 minutes.e. rate of change of the mass is equal to the inflow minus the outflow) yields the following differential equation: techteach. we will play with a premade example. The simulator will calculate and display the level h at any instant of time. Actually. This pressure drop is assumed to be equal to the hydrostatic pressure at the bottom of the tank (sqrt means square root): qout(t) = Kv sqrt[ρgh(t)] Mass balance (i.modelling.

vi. h(t) is calculated (by the simulator) by integrating dh(t)/dt with respect to time. we may start by adding an integrator to the empty block diagram.ρKv sqrt[ρgh(t)] (2) We will now draw a mathematical block diagram of the model.1/21/13 Finn Haugen. with initial value h(0). Then we add mathematical function blocks to construct the expression for dh/dt. which is the right side of the differential equation (3). which we here denote hinit. Mathematical block diagram of Differential Equation (3) The numerical values of the parameters are shown in the front panel picture below. This block diagram will then be implemented in the block diagram of the simulator VI.1m (Alarm Low) The block diagram developed above will be implemented in a Simulation Loop in the Block diagram of our simulation VI.2 The Front panel and the Block diagram of the simulator The subsequent figures show the front panel and the block diagram of the complete VI. 4.htm 3/11 . The limits are AH_h = 0. using the above relations. The resulting block diagram for the model (3) can be as shown in the figure below. The resulting differential equation becomes d[h(t)]/dt = (1/A)*{Ku u(t) . tanksim.ρqout(t)] or. that is. then dividing both sides by ρA.9m (Alarm High) AL_h = 0. techteach. This can be done by pulling ρ and A outside the differentiation. TechTeach: Introduction to LabVIEW Simulation Module dm(t)/dt = ρqin (t) .no/labview/lv86/sim_module/index. We will assume that there are level alarm limits to be displayed in the simulator. It tells how the time derivative dh(t)/dt can be calculated. from time 0 to time t.Kv sqrt[ρgh(t)]} (3) This is a differential equation for h(t). and the output is h(t). as a differential equation having the first order time derivative alone on the left side. The input to this integrator is dh/dt. As a proper starting point of drawing the mathematical block diagram. we write the differential equation as a state-space model. (1) d[ρAh(t)]/dt = ρKu u(t) . To draw a block diagram of the model (3).

htm 4/11 .1/21/13 Finn Haugen. TechTeach: Introduction to LabVIEW Simulation Module Front panel of tanksim.no/labview/lv86/sim_module/index.vi. techteach.

. but it does not (but you see it in the video). Play the video: tanksim_blockdiagram (42 minutes. TechTeach: Introduction to LabVIEW Simulation Module Block diagram of tanksim. The inputs to the integrators are the time derivatives.) 5. To be a little more specific.vi. The output of the integrators are the state variables.3 Configuration of the simulation Play the video tanksim_config_sim (28 minutes. Play the video tanksim_frontpanel (20 minutes.htm dx2/dt = -x1 + u 5/11 . The Front panel Open the Front panel of tanksim.1/21/13 Finn Haugen. [T able of contents] 5 Various topics (There are no blue activity boxes in Chapter.no/labview/lv86/sim_module/index. Given the following state space model: dx1/dt = x2 techteach.) dx2/dt = f2(x1. and the output function is g(•). [T able of contents] 4. In the video I say that this figure exists in the tutorial.1 Representing state space models using Formula node and integrators A state-space model is a set of first order differential equations constituting the model of the system. input variables.. Gain. Above. Writing the textual functions of f1(•) and f2(•) in a Formula Node. which are on the Simulation Palette of the Functions Palette. One example is the Block diagram of the model of the liquid tank shown here. the video is opened in a new window). using block functions as Sum.. However. but I have compiled these four videos into one video. To implement the functions you have the following two options (which also may be combined): Constructing the functions. Note: In the video a figure illustrating the time step (or step size) is shown.) where f1(•) and f2(•) are functions containing the right-hand part of the first order differential equations. They are the time-derivatives of the states. Here is a simple example of using the Formula node. The Formula node is on the Mathematics / Scripts & Formulas Palette (and on the Structures Palette).) y = g(x1.. Note: In the beginning of the video I refer to four videos. the output variable is y. Sometimes one or more output variables are defined. These functions may be linear or nonlinear. The Formula Node is explained here (in my Introduction to LabVIEW). here is a general second order state-space model (the dots represents the arguments of the functions): dx1/dt = f1(x1. and parameters. (there are numerous such nonlinear blocks in the Nonlinear Palette on the Simulation Palette).. To implement the block diagram of a state-space model. respectively. the video is opened in a new window). With the Formula Node the functions are easier to modify (it is done by justing editing text in the Formula Node). you may start by adding one Integrator block for each of the state variables on the block diagram. and the Block Diagram may appear simpler.vi.x2. State-space models is a standardized model form.. It is common that mathematical models of dynamic systems are written as state-space models.. Multiplication etc.x2. x1 and x2.x2. f1(•) and f2(•) above. backlash etc. the video is opened in a new window) to The Block diagram Open the Block diagram of tanksim... The arguments may be state variables.vi. and the f1(•) and f2(•) functions in the representative model shown are these time derivatives. it may be difficult to implement nonlinear functions as hysteresis..

Front panel and block diagram of ssformulanode. [T able of contents] 5.no/labview/lv86/sim_module/index. techteach.e. it may be more difficult to implement these in the Formula Node than with function blocks. u is the input variable. ssformulanode. the right-hand sides of the differential equations) contains nonlinear functions. The integration of the time derivatives are performed by Integrator blocks from the Continuous palette.vi. TechTeach: Introduction to LabVIEW Simulation Module dx2/dt = -x1 + u y = x1 (which is a state space model of an oscillator). and y is the output variable.vi shown below implements a simulator for this system.vi.htm 6/11 . if the expressions for the time derivatives (i.1/21/13 Finn Haugen. Using Formula Node in stead of block functions to calculate the time derivatives may give a simpler block diagram. The first step is to select or mark the part of interest.2 Creating subsystems You can create a subsystem of a part of a simulation diagram. which shows the block diagram of tanksim. see the figure below. A Formula node is used to represent the right side of the differential equations. However.

see the figure below.1/21/13 Finn Haugen. see the figure below.htm 7/11 . The front panel of the subsystem You can open the block diagram of the subsystem via the Window / Show Block Diagram menu. The resulting simulation diagram. including the subsystem Note that you can change the size of the subsystem icon using the cursor. The resulting diagram is shown in the figure below. TechTeach: Introduction to LabVIEW Simulation Module The first step in creating a subsystem in the simulation diagram is to select the part of interest Then the subsystem is created using the menu Edit / Create Simulation Subsystem.no/labview/lv86/sim_module/index. If you want you can open the front panel of the subsystem by double-clicking the subsystem icon. techteach.

5 Converting models between Simulation Module and Control Design Toolkit You can convert models between the Simulation Module and the Control Design Toolkit using the conversion functions on the Model Conversion palette on the Control Design Tookit. techteach. TechTeach: Introduction to LabVIEW Simulation Module The block diagram of the subsystem [T able of contents] 5.htm 8/11 . (Creating subsystems is described in the previous section. You can include virtually every control function in a model block diagram inside the Simulation Loop.no/labview/lv86/sim_module/index. [T able of contents] 5. The two conversion functions are shown in the figure below. Perhaps the most flexible choice is VI.1/21/13 Finn Haugen.3 Getting a linearized model of a subsystem LabVIEW can create a linear state space model from a linear or nonlinear subsystem.4 Simulating control systems Simulating control systems is done in the same way as simulating dynamic systems.) The procedure is to select or mark the subsystem of interest. You are given the option of saving the linear model as a model (to be used by functions in the Control Design Toolkit) or as a VI containing the state space model in the form of a cluster of coefficient arrays. In Guidelines to PID Control with LabVIEW there is an example of a simulator of a PID control system. [T able of contents] 5. and then create the linear model by using the following menu: Tools / Simulation Tools / Linearize Subsystem.

and the Period (which is the actual. you should not put more code inside the Simulation loop than is strictly necessary for representing the model to be simulated..no/labview/lv86/sim_module/index. Click on the figures to see them in full sizes.6 Putting code into a While loop running in parallell with a Simulation loop It is possible to put almost any LabVIEW code for e. may be put into one or more ordinary While loops running in parallel with the Simulation loop.) techteach. (You can run this simulator if you have the LabVIEW Simulation Module installed. This function is relatively computational demanding. and it is therefore put into a While loop which runs in parallell with the Simulation loop with a cycle time of 0. optimal control design functions (as the LQR function) or Kaman filter (state estimator) design functions (as the Kalman Gain function).3 sec (this is however computer-dependent) to execute Kalman Gain function. Note how local variables are used to exchange values across the loops. Other parts of the total code. Here is one example: kalmanfilter_tank.025 sec.1 sec. These While loops may be programmed to run slower than the Simulation loop. Below are the front panel and the block diagram of kalmanfilter_tank. e.g.htm 9/11 .) If we had put the Kalman Gain function inside the Simulation loop. Note also how the Stop button and its local variable is used to stop both loops. Data can be exchanged between the loops using local variables (local variables are described in Introduction to LabVIEW). it it will not be possible to create local variable for it. and the simulation execution may be delayed.vi.g. The Kalman gains are made available inside the Simulation loop using local variables. the specified Period of 0..025 sec could not have been obtained because it takes about 0. If it is set to one of the Latch. The Mechanical Action property of the Stop button must be set to Switch until Released.1/21/13 Finn Haugen. This is favourable here since the process itself is a slow system.5 sec. but doing so may give an unnecessary large or complicated simulation code. properties. and we do not have time to sit waiting for responses to come. The simulation time step is 0. real time that LabVIEW used to proceed one simulation time step) is 0.vi is a simulator of a Kalman Filter which estimates the outflow of a simulated liquid tank. The Kalman gains are calculated using the Kalman Gain function of the Control Design Toolkit.) The simulator is implemented with a Simulation Loop which contains a model of the tank and the expressions constituting the Kalman Filter algorithm. (Having the Period smaller than the simulation time step makes the simulator run faster than real time. TechTeach: Introduction to LabVIEW Simulation Module The conversion functions on the Control Design Toolkit / Model Conversion palette [T able of contents] 5. Therefore. analysis and design of into a simulation diagram inside a Simulation loop.

htm 10/11 .1/21/13 Finn Haugen. TechTeach: Introduction to LabVIEW Simulation Module Front panel of kalmanfilter_tank.vi (click on the figure to see it in full size) techteach.no/labview/lv86/sim_module/index.

7 Translating SIMULINK models into LabVIEW Simulation Module models You can translate SIMULINK (MathWorks) models into LabVIEW simulation models using the menu Tools / Simulation Tools / SIMULINK Translator in LabVIEW. [T able of contents] [Finn's LabVIEW Page] [TechTeach] techteach.no/labview/lv86/sim_module/index. TechTeach: Introduction to LabVIEW Simulation Module Block diagram of kalmanfilter_tank.vi (click on the figure to see it in full size) [T able of contents] 5.htm 11/11 .1/21/13 Finn Haugen.

Sign up to vote on this title
UsefulNot useful