Top Banner

of 28

introducci_n_al_m_dulo_de_simulaci_n.pdf

Apr 14, 2018

Download

Documents

Sergio Andrade
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 7/30/2019 introducci_n_al_m_dulo_de_simulaci_n.pdf

    1/28

    Introduction toLabVIEW Simulation Module 2.0

    by

    Finn Haugen

    February 19, 2007

    Introduction to LabVIEW Simulation Module 1.0

    Contents:

    1 Preface2 Introduction3 The contents of the Simulation functions palette4 An example: Simulator of a liquid tank

    4.1 Developing the mathematical model of the system to be simulated4.2 The Front panel and the Block diagram of the simulator4.3Configuring the simulation5 Various topics5.1 Representing state space models using integrators and Formula node5.2 Exchanging data between Simulation loops and While loops5.3 Converting models between Simulation Module and Control Design Toolkit5.4 Creating subsystems5.5 Getting a linearized model of a subsystem5.6 Using program flow structures in a Simulation Loop 5.7 Translating SIMULINK models into LabVIEW Simulation models

    5.8 Testing control systems using simulations

    1 Preface

    This document gives an introduction to the LabVIEW Simulation Module for LabVIEW 8.2. It isassumed 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 shownin blue boxes, as here:

    Pgina 1 de 28ntroduction to LabVIEW Simulation Module. By Finn Haugen, TechTeach

    05/03/2007http://techteach.no/publications/labview/sim_module/2_0/index.htm

  • 7/30/2019 introducci_n_al_m_dulo_de_simulaci_n.pdf

    2/28

    Please send comments or suggestions to improve this tutorial via e-mail to [email protected].

    More tutorials that may be relevant for you as a LabVIEW user are available from Finn's LabVIEW page.

    [Table of contents]

    2 Introduction

    The LabVIEW Simulation Module is a block diagram based environment for simulation of linear andnonlinear 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, whichin many ways is similar to the ordinary while loop in LabVIEW. You can make the simulation run as fastas 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 canrun in parallel with while loops within the same VI.

    [Table of contents]

    3 The contents of the Simulation funct ions palette

    Once the Simulation Module is installed, the Simulation palette is available from the Functions palette.The Simulation palette is shown in the figure below.

    Activities are shown in blue boxes as this one.

    Open the Block diagram of a blank VI.

    Open the Simulation palette as follows: Open the Functions palette (by right-clickingsomewhere in the Block diagram) / Select Control Design & Simulation / Simulation. Thefigure below shows this palette.

    Browse the palettes on the Simulation palette. Read the information about each of the palettesprovidedbelow.

    Close the VI without saving it.

    Pgina 2 de 28ntroduction to LabVIEW Simulation Module. By Finn Haugen, TechTeach

    05/03/2007http://techteach.no/publications/labview/sim_module/2_0/index.htm

  • 7/30/2019 introducci_n_al_m_dulo_de_simulaci_n.pdf

    3/28

    The Simulation Palette on the Functions Palette

    Below is an overview over the functions and possible subpalettes on the Simulation Palette.

    z Simulation Loop, which defines the borders of the simulation diagram on which the simulationfunctions are placed. Note that (most of) the functions found on the Simulation palette can be

    placed only inside a simulation loop.z

    Continuous palettecontaining the following functions and/or subpalettes:{ Zero-Pole-Gain Function Space Delay{ Derivative{ Time Delay{ State Space{ Transfer Function{ Zero-Pole-Gain

    z Nonlinear palettecontaining the following functions:{ Backlash{ Friction{ Quantizer{ Dead Zone{ Rate Limiter{ Relay{ Saturation{ Switch{ Zero Cross Detection

    z Discrete palettecontaining the following functions:{ Discrete Integrator{ Discrete Unit Delay{ Discrete Zero-Order Hold{ Discrete First-Order-Hold{ Discrete State Space{ Discrete Transfer Function{ Discrete Zero-Pole-Gain{ Discrete Filter

    Pgina 3 de 28ntroduction to LabVIEW Simulation Module. By Finn Haugen, TechTeach

    05/03/2007http://techteach.no/publications/labview/sim_module/2_0/index.htm

  • 7/30/2019 introducci_n_al_m_dulo_de_simulaci_n.pdf

    4/28

    z Signal Generation palettecontaining the following functions:{ Chirp Signal{ Ramp{ Pulse{ Signal Generator{ Sine Wave{ Step{ Indexer, which is used to index an array or waveform (like the ordinary array indexing

    function in LabVIEW) by the current simulation time.z Signal Arithmetic palettecontaining the following functions:

    { Gain{ Summation, with any number of inputs. Inputs may also be subtracted.{ Multiplication, with any number of inputs. Inputs can slo be used as divisors.

    z Lookup Tables palettecontaining the following functions, which may be used to look up valuesfrom arrays of tabular (e.g. experimental) data:

    { Lookup Table 1D (dimension one){ Lookup Table 2D (dimension two){ Lookup Table 3D (dimension three)

    z Utilities palettecontaining the following functions:{ Collector, which collects a signal at each time step of the simulation and outputs a history of

    the signal value and the time at which this function recorded each value in the history. TheCollector builds an array on its output from the input it recieves at each simulation step.

    { Memory, which stores the value of the Input signal from the previous iteration of thesimulation. You can use this function to transfer values from one iteration of the SimulationLoop to the next.

    { Indexer, which is used to index an array or waveform (like the ordinary array indexingfunction in LabVIEW) by the current simulation time.

    { Simulation Parameters, which can be used to read or get (not set) simulation parameters.{ Simulation Time, whose output is the simulation time. It works like a simulation clock.{ Report Sim Error{ Halt Simulation, which stops the simulation when its input becomes False.

    z Graph Utilities palettecontaining the following functions:{ SimTime Waveform, which is used to plot simulated variables continuously in a LabVIEW

    Chart. When this function is put into the simulation loop, a Chart is automatically attached tothe function.

    { Buffer XY Graph, which is used to collect arrays of signals to be plotted in a LabVIEWGraph. When this function is put into the simulation loop, a Graph is automatically attachedto the function.

    z Trim & Linearize palettecontaining several functions for calculating an operating point and to

    have LabVIEW develop a linear model in that operating point.z Optimal Design palettecontaining functions to calculate optimal PID settings for a process model

    from an objective function (optimization criterion).z CD Implementation palettecontaining functions to calculate state estimations using a Kalman

    Filter. (CD = Control Design. The same functions are found on the CD palette, too.)

    [Table 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. Actually, we will play with apremade example, but not develop it from scratch. I think this is an effective way to learn using the

    Pgina 4 de 28ntroduction to LabVIEW Simulation Module. By Finn Haugen, TechTeach

    05/03/2007http://techteach.no/publications/labview/sim_module/2_0/index.htm

  • 7/30/2019 introducci_n_al_m_dulo_de_simulaci_n.pdf

    5/28

    Simulation module, assuming that you have basic skills in LabVIEW programming. You are supposed tohave basic knowledge about modeling of dynamic systems, as described in e.g. Dynamic Systems -modelling, analysis and simulation or in any other book about dynamic systems theory.

    4.1 Developing the mathematical model of the system to be simulated

    The system to be simulated is a liquid tank, see the figure below. The simulator will calculate and displaythe level h at any instant of time. The simulation will run in real time, with the possibility of scaled realtime, thereby giving the feeling of a "real" system. The user can adjust the inlet by adjusting the pumpcontrol signal, u.

    Liquid tank

    Any simulator is based on a mathematical model of the system to be simulated. Thus, we start bydeveloping a mathematical model of the tank.

    We assume the following (the parameters used in the expressions below are defined in the figure above):

    z The liquid density is the same in the inlet, in the outlet, and in the tank.z The tank has straight, vertical walls.z The liquid mass and level are related through

    m(t) = Ah(t)

    z The inlet volumetric flow through the pump is proportional to the pump control signal:

    qin

    (t) = Kuu(t)

    z

    The outlet volumetric flow through the valve is proportional to the square root of the pressure dropover the valve. This pressure drop is assumed to be equal to the hydrostatic pressure at the bottomof the tank (sqrt means square root):

    Pgina 5 de 28ntroduction to LabVIEW Simulation Module. By Finn Haugen, TechTeach

    05/03/2007http://techteach.no/publications/labview/sim_module/2_0/index.htm

  • 7/30/2019 introducci_n_al_m_dulo_de_simulaci_n.pdf

    6/28

    qout(t) = Kvsqrt[gh(t)]

    Mass balance (i.e., rate of change of the mass is equal to the inflow minus the outflow) yields thefollowing differential equation:

    dm(t)/dt = qin(t) - qout(t)] (1)

    or, using the above relations,

    d[Ah(t)]/dt = Kuu(t) - K

    vsqrt[gh(t)] (2)

    We will now draw a mathematical block diagram of the model. This block diagram will then beimplemented in the block diagram of the simulator VI. As a proper starting point of drawing themathematical block diagram, we write the differential equation as a state-space model, that is, as adifferential equation having the first order time derivative alone on the left side. This can be done by

    pulling and A outside the differentiation, then dividing both sides by A. The resulting differentialequation becomes

    d[h(t)]/dt = (1/A)*{Kuu(t) - Kvsqrt[gh(t)]} (3)

    This is a differential equation for h(t). It tells how the time derivative dh(t)/dt can be calculated. h(t) iscalculated (by the simulator) by integrating dh(t)/dt with respect to time, from time 0 to time t, with initialvalue h(0), which we here denote hinit. To draw a block diagram of the model (3), we may start by adding

    an integrator to the empty block diagram. The input to this integrator is dh/dt, and the output is h(t). Thenwe add mathematical function blocks to construct the expression for dh/dt, which is the right side of thedifferential equation (3). The resulting block diagram for the model (3) can be as shown in the figure

    below.

    Mathematical block diagram of Differential Equation (3)

    The numerical values of the parameters are shown in the front panel picture below.

    We will assume that there are level alarm limits to be displayed in the simulator. The limits are

    AH_h = 0.9m (Alarm High)

    Pgina 6 de 28ntroduction to LabVIEW Simulation Module. By Finn Haugen, TechTeach

    05/03/2007http://techteach.no/publications/labview/sim_module/2_0/index.htm

  • 7/30/2019 introducci_n_al_m_dulo_de_simulaci_n.pdf

    7/28

    AL_h = 0.1m (Alarm Low)

    The block diagram developed above will be implemented in a Simulation Loop in the Block diagram of

    our simulation VI.

    4.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, tanksim.vi.

    Front panel oftanksim.vi .

    Pgina 7 de 28ntroduction to LabVIEW Simulation Module. By Finn Haugen, TechTeach

    05/03/2007http://techteach.no/publications/labview/sim_module/2_0/index.htm

  • 7/30/2019 introducci_n_al_m_dulo_de_simulaci_n.pdf

    8/28

    Block diagram oftanksim.vi .

    In the following, you are asked to remove, and then re-insert elements to the Front panel and the Blockdiagram of my_tanksim.vi. The purpose of this is to learn how to develop a simulator of a simulator ofrealistic complexity in Simulation Module, without starting completely from scratch. I guess this is aneffective way of learning. It assumes that you have basic LabVIEW programming skills, as pointed out inthe Preface.

    The Front panel

    z The Tank indicator is copied from the Numeric palette (on the Controls palette).

    Open tanksim.vi, and save it with the name my_tanksim.vi in any folder you prefer.

    Run the VI, and play with controls on the Front panel as you wish. Try adjusting theTime_scaling_factor (the VI must be stopped and started again to make any change of thiselement become effective). Finally, stop the VI.

    Read the comments to the Front panel given below.

    It is assumed that my_tanksim.vi is open. Note the Label and the Caption of the Tank indicator.Remove the Tank indicator from the Front panel. Then insert a new one at the same place, withsame Label and Caption.

    Pgina 8 de 28ntroduction to LabVIEW Simulation Module. By Finn Haugen, TechTeach

    05/03/2007http://techteach.no/publications/labview/sim_module/2_0/index.htm

  • 7/30/2019 introducci_n_al_m_dulo_de_simulaci_n.pdf

    9/28

    z The numeric controls, e.g. the vertical pointer slide, are copied from the Numeric palette.

    z The pump and the valve pictures are graphical object which I have originally created in the Visiotool for technical drawings. Here are EMF-files (Enhanced Meta Files) of these objects:pump.emfand valve.emf.

    If a picture overlaps one or more control elements on the front panel, it may not be possible toadjust these controls. To avoid this situation it is necessary to put the picture to the background

    layer of the Front panel. This is done via the Reorder button in the toolbar.

    z The pipelines are copied from the Decorations palette. The color is set to blue using the Set Colorbutton on the Tools palette, which is opened via the View / Tools palettemenu.

    z The chart is not copied directly onto the front panel from the Graph palette. In stead it is created onthe front panel via the block diagram, see the comments to the Block diagram below.

    The Block diagram

    Here are comments and activities for various part of the Block diagram:

    z The Simulation loop contains the blocks or functions representing the mathematical model of thetank. A Simulation loop looks like a While loop, but is not the same. The Simulation loop runs asimulation according to the simulation configurations. A While loop just executes the code placedinside the loop.

    z The Integrator function performs time-integration of the input to the function.

    Open the Block diagram. Connect the Tank indicator terminal to the Integrator output, whichrepresents the level.

    Check that the VI is without errors. Then save it. Run it, and finally stop it.

    Open the Front panel ofmy_tanksim.vi. Remove the pump picture.

    Downloadpump.emfto any folder. Insert the pump picture as follows: Select the following menuin LabVIEW: Edit / Import Picture from File, and select the downloaded pump.emf file. Then,

    paste the file into the Front panel at a proper place with Edit / Paste. Resize the picture properly.

    Select the pump picture (by clicking it). Then Click the Reorder button in the toolbar / SelectMove to Back.

    Save the VI.

    Remove the pipeline entering the Tank indicator. Then add a new pipeline, and give it a bluecolor.

    Save the VI.

    Open the Block diagram of my_tanksim.vi. Try to identify the corresponding elements in themathematical block diagram and the Block diagram of my_tanksim.vi (select one block at a timein the mathematical block diagram and find the corresponding element in the VI Block diagram).

    Add a Simulation loop at an empty place outside the existing Simulation loop. The Simulationloop is on the Simulation palette. (Do not remove the existing loop since many elements are wiredto its Input Node to the left.) Then remove this new Simulation loop.

    Open the Configuration window of the Integrator block by double-clicking the block. The figure

    Pgina 9 de 28ntroduction to LabVIEW Simulation Module. By Finn Haugen, TechTeach

    05/03/2007http://techteach.no/publications/labview/sim_module/2_0/index.htm

  • 7/30/2019 introducci_n_al_m_dulo_de_simulaci_n.pdf

    10/28

    Configuration window of the Integrator block. The window is opened by double-clicking the block.

    Here are comments to the Integrator Configuration window: The Parametersfield contains anumber of parameters that can be configured. Once you have selected a parameter in the list, youhave two options in the Parameter sourcefield:

    { By selecting Configuration Dialog Box, which is the default option, you can set the value ofthe selected parameter directly in the dialog box. This is an internal setting of the parameter.

    { By selectingTerminal an input terminal is created on the left part of the block in theSimulation Loop, and you can wire a value of the correct data type to that input. This is anexternal setting of the parameter. In our VI the following parameters have been setexternally: Initial condition; Upper Limit; Lower Limit. The terminals wired to these three

    block inputs are h_max, h_min, and h_init, as you can see from the block diagram picture.

    z

    The Summation function can be configured to perform both summation and subtraction.

    below shows the Integrator Configuration window.

    Remove the existing Integrator function in the Block diagram. Then, insert a new Integratorfunction from the Continuous palette at the same place, with the same configuration as for theoriginal Integrator function, cf. the description above.

    Ensure the VI is without errors, then save the VI.

    Open the Configuration window of the Summationblock by double-clicking the block. Thefigure below shows the Summation Configuration window.

    Pgina 10 de 28ntroduction to LabVIEW Simulation Module. By Finn Haugen, TechTeach

    05/03/2007http://techteach.no/publications/labview/sim_module/2_0/index.htm

  • 7/30/2019 introducci_n_al_m_dulo_de_simulaci_n.pdf

    11/28

  • 7/30/2019 introducci_n_al_m_dulo_de_simulaci_n.pdf

    12/28

    z The Build Arrayfunction is copied from the ordinary Arraypalette. It is used to collect the three

    signals to be plotted in the Chart to be described below), namely the level (being the output fromthe Integrator, and the two constants 0.9 and 0.1). Now, may be you think: Why using a BuildArray function? In a While loop we use a Bundlefunction to collect the signals. To this question

    National Instrument answers that the implementation of the Simulation Loop requires a BuildArray function to be used.

    z Although it is not used in our VI, you may flipa block by right-clicking the block and selectingReverse terminals in the menu that is opened.

    z The SimTime Waveformfunction is copied from the Graph Utilitiespalette. When you insert aSimTime Waveformblock into a simulation diagram, a Waveform Chart is automaticallycreated on the front panel. Due to this block the time axis on the Waveform Chart automaticallyshows the simulation time, so you do not have to define the Multiplier paramter in the Scale tab inthe Property window of the Chart (as you would have to with a Chart in an ordinary While loop).However, the default time format is Absolute Time which is not particularly convenient since itdisplays 1.1.1904 as a reference time. I suggest using the Floating point format in stead (this is setin the Format & Precision tab of the Property window of the Waveform Chart).

    The Chart is automatically emptied before the VI starts running, so you do not have to create anyProperty node for this Chart for this purpose (using Property nodes to configure Charts is describedhere).

    At the left part ofthe block diagram, outside the Simulation Loop, is code used to configurethesimulation. How to configure the simulation is explained in the following section.

    [Table of contents]

    4.3 Configuring the simulation

    Simulation configuration is about selecting parameters as initial simulation time, final simulation time,numerical method, time step etc. The configuration can be done in two ways:

    z Programmatically: By wiring values to the Input Nodeof the Simulation Loop, see the left partofthe block diagram.

    z By a dialog window: In the Configure Simulation Parameters dialog window which is opened by

    Remove the Square Root function from the Block diagram. Then add a Square Root function atthe same place, and wire its input and output.

    Ensure the VI is without errors, then save the VI.

    Remove the existing Build Array function in the Block diagram. Then, insert a new Build Arrayfunction at the same place, expand the number of inputs to three (by dragging the bottom line ofthe block downwards or by right-clicking the block and selecting Add Input in the menu that is

    opened).

    Ensure the VI is without errors, then save the VI.

    Remove the existing SimTime Waveform function and the Chart terminal in the Block diagram.Then, insert a new SimTime Waveform function together with a Chart Build at the same place,and connect the SimTime Waveform to the Build Array function output. Configure the Chart

    properly via its Property window (opened via right-clicking the Chart on the Front panel).

    Ensure the VI is without errors, then save the VI.

    Pgina 12 de 28ntroduction to LabVIEW Simulation Module. By Finn Haugen, TechTeach

    05/03/2007http://techteach.no/publications/labview/sim_module/2_0/index.htm

  • 7/30/2019 introducci_n_al_m_dulo_de_simulaci_n.pdf

    13/28

    right-clicking the simulation loop border and selecting the menu Configure Simulation Parameters

    The setting defined programmatically overrides the settings made in the dialog window. Since theprogrammatic method gives more flexibility and the settings are clearly shown in the block diagram itself,

    I prefer the programmatic method.

    Both configuration methods are described in the following sections.

    Programmatic setting of s imulation parameters

    By expanding the Input Node outside the left part of the Simulation Node (by pulling the bottom of thenode downwards), all parameters become available. The parameters are shown in the block diagram

    picture. To display the details somewhat better an expanded extract of that block diagram is shown in thefigure below.

    The Input Node of the Simulation Loop

    Here are comments to the parameters that are wired:

    z The Intial Timedefines the initial simulation time, typically zero.z The Final Timedefines the simulation time when the simulation will stop. In our VI the final time

    is set to Inf (infinity). Will the simulation never stop, then? It will stop, due to the Halt Simulationfunction in the block diagram. When the user clicks the Stop button on the front panel the HaltSimulation function will cause the simulator to stop.

    z The Time Step is the resolution of the simulation time scale, see the figure below.

    Pgina 13 de 28ntroduction to LabVIEW Simulation Module. By Finn Haugen, TechTeach

    05/03/2007http://techteach.no/publications/labview/sim_module/2_0/index.htm

  • 7/30/2019 introducci_n_al_m_dulo_de_simulaci_n.pdf

    14/28

    The time step is the resolution of the simulation time scale

    In general, the smaller time step the better accuracy, but the larger the total number of calculationsneeded to calulate the simulated response for a given simulation time range. The main rule ofselecting the time step is: Select the largest time step that does not influence the accuracy of thesimulated response. You may iterate to find this largest time step. Start with some initial guess ofthe time step, and try increased values until you observe that the response is influenced by the time

    step. As an initial guess you may select the time step as

    Ts = 0.1*Tmin

    where Tmin is the smallest time constant of the model. If I do not know the smallest time constant,I set h = 0.05s which gived a fairly smooth update of the plotted simulation response on the screen.

    z The ODE Solver is the numerical method used to calculate - or solve for - the values of the statevariables of the model. (ODE is abbreviation for Ordinary Differential Equation.) In general, thesimpler solver, the less burden on the LabVIEW and the computer to perform the simulation, butalso, unfortunately, the less accuracy of the simulated response. This selecting the solver method is

    a trade-off. It is my experience that using the Runge-Kutta second order method (with a fixed timestep) works fine in most situations. (This method is quite similar to numerical integration using thetrapezoid rule.)

    There are fixed-step solvers and variable-step solvers. In variable-step solvers LabVIEWautomatically calculates the time step. However, it is my experience that the simulation runssmoother and that is it easier to post-process the simulation data if you use a fixed-step solver.Therefore I suggest using a fixed-step solver.

    z Software Timingset to True makes LabVIEW run the simulation as fast as it can, i.e. thesimulation time is not equal to the real time. Software Timingset to False gives you the possibility

    to set how fast the simulation will run. This is done with the Period parameter described below.z The Discrete Time Step is the time step used for simulating dicrete-time functions (blocks). In the

    block diagram of our example the discrete time step is the tz

    parameter. Examples of discrete-time

    Pgina 14 de 28ntroduction to LabVIEW Simulation Module. By Finn Haugen, TechTeach

    05/03/2007http://techteach.no/publications/labview/sim_module/2_0/index.htm

  • 7/30/2019 introducci_n_al_m_dulo_de_simulaci_n.pdf

    15/28

    functions are discrete-time signal filters. Typically the discrete time step is set equal to thesimulation time step, as in our example.

    z The Period is the amount ofreal timebetween two subsequent simulated time points. By settingthe Period equal to the simulation time step, Ts, the simulation runs in real time. By giving thePeriod some other value, the simulation time scale is proportional to real time. For example, if thesimulation time step is 0.05s, setting Period equal to 0.01s causes the simulation to run 5 timesfaster than real time (thereby speeding up the simulation for a slow system), while setting Periodequal to 0.25s causes the simulation to run 5 times slower than real time (thereby slowing down thesimulation for a fast system).

    Setting Period to 0 causes LabVIEW to run the simulation as fast as possible (on the givencomputer).

    Note that on a PC Period is the number of milliseconds, since the PC clock runs with a frequency of1kHz. For example, Period = 10 corresponds to Period equal to 0.01s. (It is however possible to usesome other (faster) timing source than the PC clock.)

    In our example the Period is calculated from the Time Step by dividing the Time Step by theterminal labeled Time_scaling_factor. The user can adjust the value of Time_scaling_factor on thefront panel. The Round to Nearest (integer) function is used to make the result of the division aninteger.

    Setting simulation parameters in a dialog window

    By right-clicking on the border of the Simulation loop and selecting Configure Simulation Parameters inthe menu that is opened, the Configure Simulation Parametersdialog window with the SimulationParameterstab is opened, see the figure below.

    Disconnect each of the wires now connected to the Input Node of the Simulation loop, andremove brolen wires (keyboard shortcut Ctrl + B). Then, reconnect each of them, cf. this figure.

    Ensure the VI is without errors, then save the VI.

    Pgina 15 de 28ntroduction to LabVIEW Simulation Module. By Finn Haugen, TechTeach

    05/03/2007http://techteach.no/publications/labview/sim_module/2_0/index.htm

  • 7/30/2019 introducci_n_al_m_dulo_de_simulaci_n.pdf

    16/28

    The Simulation Parameters tab in the Configure Simulation Parameters dialog window

    Most of the setting parameters in the Simulation Parameters tab are as for the programmatic parametersettings describe above, and the descriptions are not repeated here. Here are some additional comments:

    z IfAuto Discrete Time is selected, the Discrete Time Step is automatically set equal to thesimulation time step if you use a fixed-step solver, but it is automatically set to the initial time stepfor variable-step solvers. By default Auto Discrete Timeis selected. I suggest that you uncheck theAuto Discrete Timecheckbox and set the Discrete Time Stepexplicitly. (This corresponds to thewiring in theprogrammatic configuration.)

    The figure below shows theTiming Parameters tab.

    Open the Configure Simulation Parametersdialog window, and locate the parameters describedbelow, but do not make any changes to the existing settings. Then, click the Cancel button.

    Pgina 16 de 28ntroduction to LabVIEW Simulation Module. By Finn Haugen, TechTeach

    05/03/2007http://techteach.no/publications/labview/sim_module/2_0/index.htm

  • 7/30/2019 introducci_n_al_m_dulo_de_simulaci_n.pdf

    17/28

    The Timing Parameters tab in the Configure Simulation Parameters dialog window

    Comments to theTiming Parameters tab:

    z The Period is the same as for the programmatic parameter settings describe above.z The Software Timing is the same as for the programmatic settings described above. By default

    Software Timing is selected.z

    IfAuto Period is selected LabVIEW automatically sets the period of the simulation equal to thesimulation time step. By default Auto Period is not selected.

    [Table of contents]

    5 Various topics

    (There are no blue activity boxes in Chapter.)

    5.1 Representing state space models using integrators and Formula node

    A state-space model is a set of first order differential equations constituting the model of the system.

    Run the VI. Hopefully it works.

    Pgina 17 de 28ntroduction to LabVIEW Simulation Module. By Finn Haugen, TechTeach

    05/03/2007http://techteach.no/publications/labview/sim_module/2_0/index.htm

  • 7/30/2019 introducci_n_al_m_dulo_de_simulaci_n.pdf

    18/28

    State-space models is a standardized model form. It is common that mathematical models of dynamicsystems are written as state-space models. To be a little more specific, here is a general second orderstate-space model (the dots represents the arguments of the functions):

    dx1/dt = f

    1(x

    1,x

    2,...)

    dx2/dt = f2(x1,x2,...)

    y = g(x1,x

    2,...)

    where f1() and f

    2() are functions containing the right-hand part of the first order differential equations.

    The arguments may be state variables, input variables, and parameters. These functions may be linear ornonlinear. They are the time-derivatives of the states, x

    1

    and x2

    , respectively. Sometimes one or more

    output variables are defined. Above, the output variable is y, and the output function is g().

    To implement the block diagram of a state-space model, you may start by adding one Integrator block foreach of the state variables on the block diagram. The output of the integrators are the state variables. Theinputs to the integrators are the time derivatives, and the f

    1() and f

    2() functions in the representative

    model shown are these time derivatives. To implement the functions you have the following two options(which also may be combined):

    z Constructing the functions, f1() and f

    2() above, using block functionsas Sum, Gain,

    Multiplication etc., 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.z Writing the textual functionsof f1() and f2() in a Formula Node. The Formula node is on the

    Mathematics / Scripts & Formulas Palette (and on the Structures Palette). The Formula Node isexplained here (in my Introduction to LabVIEW 8.0). With the Formula Node the functions areeasier to modify (it is done by justing editing text in the Formula Node), and the Block Diagrammay appear simpler. However, it may be difficult to implement nonlinear functions as hysteresis,

    backlash etc. (there are numerous such nonlinear blocks in the Nonlinear Palette on the SimulationPalette).

    Here is a simple example of using the Formula node. Given the following state space model:

    dx1/dt = x2

    dx2/dt = -x

    1+ u

    y = x1

    (which is a state space model of an oscillator). u is the input variable, and y is the output variable.ssformulanode.vi shown below implements a simulator for this system. A Formula node is used torepresent the right side of the differential equations. The integration of the time derivatives are performed

    by Integrator blocks from the Continuous palette.

    Pgina 18 de 28ntroduction to LabVIEW Simulation Module. By Finn Haugen, TechTeach

    05/03/2007http://techteach.no/publications/labview/sim_module/2_0/index.htm

  • 7/30/2019 introducci_n_al_m_dulo_de_simulaci_n.pdf

    19/28

    Front panel and block diagram ofssformulanode.vi .

    [Table of contents]

    5.2 Exchanging data between Simulation loops and While loops

    It is possible to put (almost) any LabVIEW code for e.g. analysis and design of into a simulation

    diagram, but doing so may cause the simulated model become unnecessary large, which may slow downthe simulation time. Therefore, you should not put more code inside the Simulation loop than is strictlynecessary for representing the model to be simulated. But where to put the code? It can be put into one ormore ordinary While loops running in parallel with the Simulation loop. Data can be exchanged betweenthe loops using local variables. Running loops in parallel is described here.

    [Table of contents]

    5.3 Converting models between Simulation Module and Control Design Toolki t

    You can convert models between the Simulation Module and the Control Design Toolkit using theconversion functions on the Model Conversionpalette on the Control Design Tookit. The twoconversion functions are shown in the figure below.

    Pgina 19 de 28ntroduction to LabVIEW Simulation Module. By Finn Haugen, TechTeach

    05/03/2007http://techteach.no/publications/labview/sim_module/2_0/index.htm

  • 7/30/2019 introducci_n_al_m_dulo_de_simulaci_n.pdf

    20/28

    The conversion func tions on the Contro l Design Toolk it / Model Conversion palette

    [Table of contents]

    5.4 Creating subsystems

    You can create a subsystem of a part of a simulation diagram. The first step is to select or mark the part ofinterest, see the figure below, which shows the block diagram oftanksim.vi.

    The first step in creating a subsys tem in the simulation diagram is to select the part of interest

    Then the subsystem is created using the menu Edit / Create Simulation Subsystem. The resultingdiagram is shown in the figure below.

    Pgina 20 de 28ntroduction to LabVIEW Simulation Module. By Finn Haugen, TechTeach

    05/03/2007http://techteach.no/publications/labview/sim_module/2_0/index.htm

  • 7/30/2019 introducci_n_al_m_dulo_de_simulaci_n.pdf

    21/28

    The resulting simulation diagram, including the subsystem

    Note that you can change the size of the subsystem icon using the cursor.

    If you want you can open the front panel of the subsystem by double-clicking the subsystem icon, see thefigure below.

    Pgina 21 de 28ntroduction to LabVIEW Simulation Module. By Finn Haugen, TechTeach

    05/03/2007http://techteach.no/publications/labview/sim_module/2_0/index.htm

  • 7/30/2019 introducci_n_al_m_dulo_de_simulaci_n.pdf

    22/28

    The front panel of the subsystem

    You can open the block diagram of the subsystem via the Window / Show Block Diagrammenu, see thefigure below.

    Pgina 22 de 28ntroduction to LabVIEW Simulation Module. By Finn Haugen, TechTeach

    05/03/2007http://techteach.no/publications/labview/sim_module/2_0/index.htm

  • 7/30/2019 introducci_n_al_m_dulo_de_simulaci_n.pdf

    23/28

    The block d iagram of the subsystem

    [Table of contents]

    5.5 Getting a linearized model of a subsystem

    LabVIEW can create a linear state space model from a linear or nonlinear subsystem. (Creatingsubsystems is described in the previous section.) The procedure is to select or mark the subsystem ofinterest, and then create the linear model by using the following menu:Tools / Simulation Tools /Linearize Subsystem. You are given the option of saving the linear model as a model (to be used byfunctions in the Control Design Toolkit) or as a VI containing the state space model in the form of acluster of coefficient arrays. Perhaps the most flexible choice is VI.

    [Table of contents]

    5.6 Using program flow structures within a Simulation loop

    You can use a majority of LabVIEW VIs and functions within a Simulation loop. However, you cannotplace certain structures, such as the Case structure, While Loop, For Loop, Event structure, or theSequence structures, directly on the simulation diagram. Instead, you can place these structures in a subVIand then place the subVI on a simulation diagram.

    [Table of contents]

    5.7 Translating SIMULINK models into LabVIEW Simulation Module models

    You can translate SIMULINK (MathWorks) models into LabVIEW simulation models using the menuTools / Simulation Tools / SIMULINK Translator in LabVIEW.

    Pgina 23 de 28ntroduction to LabVIEW Simulation Module. By Finn Haugen, TechTeach

    05/03/2007http://techteach.no/publications/labview/sim_module/2_0/index.htm

  • 7/30/2019 introducci_n_al_m_dulo_de_simulaci_n.pdf

    24/28

    [Table of contents]

    5.8 Testing control systems using simulations

    The figure below illustrates the principle of testing a control system by replacing the physical system (orprocess) to be controlled by a simulated system. The controller is assumed to be a PID controller, but thefigure applies to any controller function.

    Testing a PID control sys tem using a process simulator

    Let us look at a concrete example. The PID controller is supposed to control a physical process based onfeedback from a measurement signal from the process using a FieldPoint system for the physical I/O

    (which is one of the I/O systems produced by National Instruments). To test the control system prior toapplying it to the imagined real, physical process, we let the PID controller control a process simulator,which is just a first order transfer function with gain 1 and time constant 1.

    Below is the front panel ofcontrol_sim_real.vi which implements the example. (However, I haveremoved the FieldPoint I/O functions from the VI to avoid problems for you with loading the VI if you donot have FieldPoint installed. The FieldPoint functions are however shown in the Block diagram figureshown below.)

    Pgina 24 de 28ntroduction to LabVIEW Simulation Module. By Finn Haugen, TechTeach

    05/03/2007http://techteach.no/publications/labview/sim_module/2_0/index.htm

  • 7/30/2019 introducci_n_al_m_dulo_de_simulaci_n.pdf

    25/28

    Front panel ofcontrol_sim_real.vi

    Comments to the front panel:

    z The VI is ready to run, but you will get an error, of course, if you set the Select_real_sim_processswitch in the Real process position (since you do not have the FieldPoint system that is supposed to

    be attached to my computer).

    z The PID settings are according to the Ziegler-Nichols' closed loop method (the ultimate gainmethod).

    Below are two figures showing the block diagram ofcontrol_sim_real.vi.Two figures are shown todisplay the two alternate cases of the Case structure, which contains the simulator (the True case) and theFieldPoint I/O functions (the False case), respectively.

    Pgina 25 de 28ntroduction to LabVIEW Simulation Module. By Finn Haugen, TechTeach

    05/03/2007http://techteach.no/publications/labview/sim_module/2_0/index.htm

  • 7/30/2019 introducci_n_al_m_dulo_de_simulaci_n.pdf

    26/28

    Block diagram ofcontrol_sim_real.vi showing the True case of the Case structu re containing thesimulator.

    Pgina 26 de 28ntroduction to LabVIEW Simulation Module. By Finn Haugen, TechTeach

    05/03/2007http://techteach.no/publications/labview/sim_module/2_0/index.htm

  • 7/30/2019 introducci_n_al_m_dulo_de_simulaci_n.pdf

    27/28

    Block diagram ofcontrol_sim_real.vi showing the False case of the Case struc ture containing theFieldPoint I/O functions. Note: Although the FieldPoint I/O funct ions are shown in t he Block diagram

    above, I have removed the O funct ions from the VI to avoid problems for you with loading the VI if you donot have FieldPoint installed.)

    Comments to the block diagram ofcontrol_sim_real.vi:

    z The PID Advanced.vi implements the PID controller function. It is copied from Addons /Control / PID palette on the Functions palette.

    z The terminal labeled Switch_real_sim_processis connected to the Case selector input of the Casestructure. Hence the boolean state of the switch on the front panel determines wether the PIDcontrollers controls the real process or the simulated process.

    z In a real control system you will probably want to attenuate the always existing high frequentmeasurement noise using a lowpass filter. This filter should be used also with the simulated processso that the controller "feels" as much of the real system's dynamics as possible. In this example the

    measurement filter is a second order Buttherworth lowpass filter which is found on the SignalProcessing / Point By Point / Filters PtByPt paletteof the Functions palette.

    z The terminal labeledTime_scaling_factor scales the simulation time, in case you want to run the

    Pgina 27 de 28ntroduction to LabVIEW Simulation Module. By Finn Haugen, TechTeach

    05/03/2007http://techteach.no/publications/labview/sim_module/2_0/index.htm

  • 7/30/2019 introducci_n_al_m_dulo_de_simulaci_n.pdf

    28/28

    simulator slower than real time (setting theTime_scaling_factor control on the front panel to avalue between one and zero) or faster than real time (value larger than zero). Of course, you shouldnot use time scaling when the PID controller is connected to the real process (i.e. the

    Time_scaling_factor control is set to one).z In other applications your VI may implement several PID control systems. In such applications, I

    suggest that you structure the VI using one While loop for each PID control system, including a

    simulator for testing and I/O functions. These While loops are run in parallel. VIs with such parallelWhile loops are described here.

    [Table of contents]

    [Finn's LabVIEW Page] [TechTeach]

    Pgina 28 de 28ntroduction to LabVIEW Simulation Module. By Finn Haugen, TechTeach