Company Events Academic NI Developer Zone Support Solutions Products & Services Contact NI MyNI

Tutorial: Getting Started with Simulation (Control Design and Simulation Module)

LabVIEW 2009 Control Design and Simulation Module Help

Edition Date: June 2009

Part Number: 371894D-01

»View Product Info

This tutorial shows you how to construct a simulation diagram that represents the behavior of a dynamic system. This dynamic system consists of a spring with one end fixed and the other end attached to a mass. Because a damper inhibits the movement of the mass, this dynamic system is called a spring-mass damper system.

If you are new to LabVIEW, consider completing the exercises in the Getting Started with LabVIEW manual before proceeding with this tutorial.

You can complete this tutorial in approximately 30 minutes.
Note  You can print this tutorial by right-clicking this page and selecting Print from the shortcut menu.

Background Information

A dynamic system model is a differential or difference equation that describes the behavior of the dynamic system. In this tutorial, the following differential equation describes the dynamic system.

F(t) – cx'(t) – kx(t) = mx''(t)

where t is the simulation time, F(t) is an external force applied to the system, c is the damping constant of the spring, k is the stiffness of the spring, m is a mass, and x(t) is the position of the mass. x' is the first derivative of the position, which equals the velocity of the mass. x'' is the second derivative of the position, which equals the acceleration of the mass.

The following figure shows this dynamic system.

The goal of this tutorial is to use the LabVIEW Control Design and Simulation Module to view the position x(t) of the mass m with respect to time t. You can calculate the position by integrating the velocity of the mass. You can calculate the velocity by integrating the acceleration of the mass. If you know the force and mass, you can calculate this acceleration by using Newton's Second Law of Motion, given by the following equation:

Force = Mass × Acceleration

Therefore,

Acceleration = Force / Mass

Substituting terms from the differential equation above yields the following equation:

x''(t) = (F(t) – cx'(t) – kx(t))/m

In this tutorial you will construct a simulation diagram that iterates the following steps over a period of time.

  1. Divides a known force by a known mass to calculate the acceleration of the mass.
  2. Integrates acceleration to calculate the velocity of the mass.
  3. Integrates velocity to calculate the position of the mass.

Performing these integrations over a period of time requires an ordinary differential equation (ODE) solver. LabVIEW includes several ODE solvers you can use in a simulation. Each ODE solver has properties that make it suitable for different tasks. For example, some ODE solvers have variable step sizes. During a simulation, these ODE solvers can adjust the points in time at which the Control Design and Simulation Module evaluates the simulation diagram. Conversely, ODE solvers with fixed step sizes cannot make this change.

Complete the steps in the following sections to build a simulation diagram that simulates the spring-mass damper dynamic system model.

Creating the Simulation Diagram

You create a simulation diagram by placing a Control & Simulation Loop on the LabVIEW block diagram. The Control & Simulation Loop contains parameters that affect the simulation, such as which ODE solver to use and how long to run the simulation. Complete the following steps to create the simulation diagram.

  1. Launch LabVIEW and select File»New VI to create a new, blank VI.
  2. Select Window»Show Block Diagram to view the block diagram. You also can press the <Ctrl-E> keys to view the block diagram.
  3. If you are not already viewing the Functions palette, select View»Functions Palette to display this palette.
  4. Select Control Design & Simulation»Simulation to view the Simulation palette.
  5. Click the Control & Simulation Loop icon, shown in the following figure.

  6. Move the cursor over the block diagram. Click to place the top left corner of the loop, drag the cursor diagonally to establish the size of the loop, and click again to place the loop on the block diagram.

    The simulation diagram is the area enclosed by the Control & Simulation Loop. Notice the simulation diagram has a pale yellow background to distinguish it from the rest of the block diagram. You can resize the Control & Simulation Loop by dragging its borders.

Configuring Simulation Parameters

The Control & Simulation Loop contains the parameters that define how the simulation executes. Complete the following steps to view and configure these simulation parameters.

  1. Double-click the Input Node, attached to the left side of the Control & Simulation Loop, to display the Configure Simulation Parameters dialog box. You also can right-click the loop border and select Configure Simulation Parameters from the shortcut menu.
  2. Ensure the value of the Final Time (s) numeric control is 10, which specifies that this tutorial simulates ten seconds of time.
  3. Click the ODE Solver pull-down menu to view the list of ODE solvers the Control Design and Simulation Module includes. If the term (variable) appears next to an ODE solver, that solver has a variable step size. The other ODE solvers have a fixed step size. Ensure a checkmark is beside the default ODE solver Runge-Kutta 23 (variable).
  4. Because this ODE solver is a variable step-size solver, you can specify the Minimum Step Size (s) and Maximum Step Size (s) this ODE solver can take. Enter 0.01 in the Maximum Step Size (s) numeric control to limit the size of the time step this ODE solver can take.
  5. Click the Timing Parameters tab to access parameters that control how often the simulation executes.
  6. Ensure the Synchronize Loop to Timing Source checkbox does not contain a checkmark. This option specifies that the simulation executes without any timing restrictions.
    Note  Use this option when you want the simulation to run as fast as possible. If you are running this simulation on a real-time (RT) target, you can place a checkmark in this checkbox and configure how often the simulation executes.
  7. Click the OK button to save changes and return to the simulation diagram.

Building the Simulation

The next step is to build the simulation by placing Simulation functions on the simulation diagram and wiring these functions together. Note that you can place most Simulation functions only on the simulation diagram, that is, you cannot place Simulation functions on a LabVIEW block diagram. Complete the following steps to build the simulation of this dynamic system.

Placing Functions on the Simulation Diagram

  1. View the Simulation palette.
  2. Select the Signal Arithmetic palette and place a Multiplication function on the simulation diagram. You will use this function to divide the force by the mass to calculate the acceleration.
  3. Double-click the Multiplication function to display the Multiplication Configuration dialog box. You can double-click most Simulation functions to view and change the parameters of that function.
  4. The function currently displays two × symbols on the left side of the dialog box. This setting specifies that both incoming signals are multiplied together. Click the bottom × symbol to change it to a ÷ symbol. This Multiplication function now divides the top signal by the bottom signal.
    Tip  To view detailed information about the Multiplication function, click the Help button in this dialog box.
  5. Click the OK button to save changes and return to the simulation diagram.
  6. Right-click the Multiplication function and select Visible Items»Label from the shortcut menu. Double-click the Multiplication label and enter Calculate Acceleration as the new label.
  7. Return to the Simulation palette and select the Continuous Linear Systems palette.
  8. Place an Integrator function on the simulation diagram. You will use this function to calculate velocity by integrating acceleration.
    Note  If you move the Integrator function close enough to the Multiplication function, LabVIEW might automatically create a wire between the two functions. In this situation, place the Integrator on the simulation diagram and then delete the wire LabVIEW creates by right-clicking the wire and selecting Delete Wire Branch from the shortcut menu.
  9. Label this Integrator function Calculate Velocity.
    Tip  To view detailed information about the Integrator function, right-click this function and select Help from the shortcut menu.
  10. Press the <Ctrl> key and click and drag the Integrator function to another location on the simulation diagram. This action creates a copy of the Integrator function, which you will use to calculate position by integrating velocity. Label this new Integrator function Calculate Position.
  11. Select the Graph Utilities palette and place two SimTime Waveform functions on the simulation diagram. You will use these functions to view the results of the simulation over time.
  12. Each SimTime Waveform function has an associated Waveform Chart. Label the first waveform chart Velocity and the second waveform chart Position.
  13. Arrange the functions to look like the following simulation diagram:

    Note  If the Velocity and Position indicators do not resemble the previous figure, you can right-click each indicator and remove the checkmark from the View as Icon option on the shortcut menu.
  14. Save this VI by selecting File»Save. Save this VI to a convenient location as Spring-Mass Damper Example.vi.

Wiring the Simulation Functions Together

The next step is wiring the functions together to represent the flow of data from one function to another.

Note  Wires on the simulation diagram include arrows that show the direction of the dataflow, whereas wires on a LabVIEW block diagram do not show these arrows.

Complete the following steps to wire these functions together.

Tip  If you have trouble viewing the input and output terminals the following steps mention, display the Context Help window by pressing <Ctrl-H>. This window displays all input and output terminals when you move the cursor over a function.
  1. Right-click the Operand1 input of the Calculate Acceleration function and select Create»Control from the shortcut menu to add a numeric control to the front panel window.
  2. Label this control Force.
  3. Double-click this control on the simulation diagram. LabVIEW displays the front panel and highlights the Force control.
  4. Display the block diagram and create a control for the Operand2 input of the Calculate Acceleration function. Label this new control Mass.
  5. Wire the Result output of the Calculate Acceleration function to the input input of the Calculate Velocity function.
  6. Wire the output output of the Calculate Velocity function to the input input of the Calculate Position function.
  7. Right-click the wire you just created and select Create Wire Branch from the shortcut menu. Wire this branch to the Value input of the SimTime Waveform function that has the Velocity waveform chart.
  8. Wire the output output of the Calculate Position function to the Value input of the SimTime Waveform function that has the Position waveform chart. The simulation diagram now resembles the following figure:

    Note  If the Force and Mass controls do not resemble the previous figure, you can right-click each control and remove the checkmark from the View as Icon option on the shortcut menu.

In the previous figure, notice how the arrows on each wire indicate data flow. For example, the values you will provide for Force and Mass controls flow into the Calculate Acceleration function, the result of this function flows into the Calculate Velocity function, and so on.

Running the Simulation

You now can run this simulation to test that the data is flowing properly through the Simulation functions. Complete the following steps to run this simulation.

  1. Select Window»Show Front Panel, or press <Ctrl-E>, to view the front panel of this simulation. The front panel displays the following objects: a control labeled Force, a control labeled Mass, a waveform chart labeled Velocity, and a waveform chart labeled Position.
  2. If necessary, rearrange these controls and indicators so that all objects are visible.
  3. Enter -9.8 in the Force numeric control. This value represents the force of gravity, 9.8 meters per second squared, acting on the dynamic system.
  4. Enter 1 in the Mass numeric control. This value represents a mass of one kilogram.
  5. Click the Run button, or press the <Ctrl-R> keys, to run the VI.

The front panel now resembles the following figure:

In the previous figure, notice that the force of gravity causes the mass position and velocity to constantly decrease. However, in the real world, a mass attached to a spring oscillates up and down. This simulated spring does not oscillate because the simulation diagram does not represent damping or stiffness. You must represent these factors to have a complete simulation of the dynamic system.

Representing Damping and Stiffness

Representing damping and stiffness involves feeding back the velocity and position, each multiplied by a different constant, to the input of the Calculate Acceleration function. Recall the following differential equation this VI simulates.

F(t) – cx'(t) – kx(t) = mx''(t)

In the previous equation, notice you multiply the damping constant c by the velocity of the mass x'(t). You multiply the stiffness constant k by the mass position x(t). You then subtract these quantities from the external force applied to the mass.

Complete the following steps to represent damping and stiffness in this dynamic system model.

  1. View the simulation diagram.
  2. Select the Signal Arithmetic palette and place a Summation function on the simulation diagram. Move this function to the left of the Force and Mass controls.
  3. Double-click the Summation function to configure its operation. By default, the Summation function displays the following three input terminals: a Ø symbol, a + symbol, and a symbol. This configuration subtracts one input signal from another.
    Note  The Ø terminal configures the visual layout of the function and does not provide a terminal for an input signal.
  4. Click the Ø symbol twice to change this terminal to the symbol. This Summation function now subtracts the top and bottom input signals from the left input signal.
  5. Click the OK button to save changes and return to the simulation diagram.
  6. Select the Signal Arithmetic palette and place a Gain function on the simulation diagram. Move this function above the existing simulation diagram code but still within the Control & Simulation Loop.
  7. The input of the Gain function is on the left side of the function, and the output is on the right side. You can reverse the direction of these terminals to indicate feedback better. Right-click the Gain function and select Reverse Terminals from the shortcut menu. The Gain function now points toward the left side of the simulation diagram.
  8. Label this Gain function Damping.
  9. Press the <Ctrl> key and drag the Gain function to create a separate copy. Move this copy below the existing simulation diagram code but still within the Control & Simulation Loop. Label this function Stiffness.
  10. Right-click the wire connecting the Force control to the Calculate Acceleration function and select Delete Wire Branch from the shortcut menu. Move the Force control to the left of the Summation function, and wire this control to the Operand2 input of the Summation function.
  11. Create wires 1–5 as indicated in the following figure:



    The simulation diagram now fully represents the equation that defines the behavior of the dynamic system.
  12. Press <Ctrl-S> to save the VI.

Configuring the Stiffness of the Spring

Before you run the simulation again, you must configure the stiffness of the simulated spring. Complete the following steps to configure this Simulation function.

  1. Double-click the Stiffness function to display the Gain Configuration dialog box.
  2. Enter 100 in the gain numeric control. This value represents a stiffness of 100 Newtons per meter.
  3. Click OK to return to the simulation diagram. Notice that the Stiffness function displays 100.
  4. Display the front panel and ensure the Force control is set to -9.8 and the Mass control is set to 1.
  5. Run the simulation. The Velocity and Position charts display the behavior of the mass as the spring oscillates.

    The front panel now resembles the following figure:



    Notice the new behavior compared to the last time you ran the simulation. This time, the velocity and position do not constantly decrease. Both values oscillate, which is how a spring behaves in the real world.
  6. Change the value of the Mass control to 10 and run the simulation again. Notice the different behavior in the Velocity and Position charts. The 10 kg mass forces the spring to oscillate less frequently and within a smaller velocity/position range.

Configuring Simulation Functions Programmatically

The previous section provided information about configuring Simulation functions using the configuration dialog box. Instead of using the configuration dialog box, you can improve the interactivity of a simulation by creating front panel controls that configure a Simulation function programmatically. Complete the following steps to configure the Stiffness function programmatically.

  1. If you are not already viewing the Context Help window, press <Ctrl-H> to display this window.
  2. Display the block diagram and move the cursor over the Stiffness function. Notice this function has only one input terminal.
  3. Display the Gain Configuration dialog box of the Stiffness function.
  4. Select Terminal from the Parameter source pull-down menu. This action disables the gain numeric control.
  5. Click the OK button to save changes and return to the block diagram.
  6. Move the cursor over the Stiffness function. Notice the Context Help window displays the Gain function with the new gain input terminal.
  7. Create a control for this input, and label the control gain (k).
  8. View the front panel. Notice the new control gain (k). Enter a value of 100 for this control and run the simulation. Notice the behavior is exactly the same as when you used the configuration dialog box to configure the Stiffness function.

Modularizing Simulation Diagram Code

You can create simulation subsystems to divide simulation diagrams into components that are modular, reusable, and independently verifiable. Complete the following steps to create a simulation subsystem from this simulation diagram.

  1. View the simulation diagram.
  2. Select the Calculate Acceleration, Calculate Velocity, and Calculate Position functions by pressing the <Shift> key and clicking each function.
  3. Select Edit»Create Simulation Subsystem. LabVIEW replaces these three functions with a single function that represents the simulation subsystem, which is circled in the following figure:



    The inputs and outputs of the simulation subsystem include the inputs and outputs of all the functions you selected. Also, notice the amount of blank space on the simulation diagram. Because you combined three functions into a subsystem, you can resize the Control & Simulation Loop and reposition the functions to make the simulation diagram easier to view.
  4. Press <Ctrl-S> to save the simulation diagram. LabVIEW prompts you to save the simulation subsystem you just created. Click the Yes button and save this simulation subsystem as Newton.vi. You now have a simulation subsystem that obtains the position of a mass by using Newton's Second Law of Motion.
Note  You can resize the simulation subsystem to better display its simulation diagram. You also can double-click the simulation subsystem to display the configuration dialog box of that simulation subsystem.

Editing the Simulation Subsystem

Edit the simulation subsystem Newton.vi by right-clicking this subsystem and selecting Open Subsystem from the shortcut menu. View the simulation diagram. Notice this simulation subsystem does not contain a Control & Simulation Loop, but the entire background is pale yellow to indicate a simulation diagram. If you place this simulation subsystem in a Control & Simulation Loop, the simulation subsystem inherits all simulation parameters from the Control & Simulation Loop. If you run this subsystem as a stand-alone VI, you can configure the simulation parameters by selecting Operate»Configure Simulation Parameters. Any parameters you configure using this method do not take effect when the subsystem is within another Control & Simulation Loop. If you place this simulation subsystem on a block diagram outside a Control & Simulation Loop, you can configure the simulation parameters by double-clicking the simulation subsystem to display the configuration dialog box of that simulation subsystem.

Configuring Simulation Parameters Programmatically

Earlier in this example, you used the Configure Simulation Parameters dialog box to configure the parameters of Spring-Mass Damper Example.vi. You also can configure simulation parameters programmatically by using the Input Node of the Control & Simulation Loop. Complete the following steps to configure simulation parameters programmatically.

  1. View the simulation diagram of Spring-Mass Damper Example.vi.
  2. Move the cursor over the Input Node to display resizing handles.
  3. Drag the bottom handle down to display all available Node inputs. You use these inputs to configure the simulation parameters without displaying the Configure Simulation Parameters dialog box.
    Tip  You also can right-click the Input Node and select Show All Inputs from the shortcut menu.


    Notice the gray boxes next to each input. These boxes display values you configure in the Configure Simulation Parameters dialog box. For example, the third gray box from the top displays 10.0000, which is the value of the Final Time numeric control that you configured. The fifth gray box from the top displays RK 23. This box specifies the current ODE solver, which you configured as Runge-Kutta 23 (variable).
    Tip  Move the cursor over the left edge of each Node input to display the label of that input.
  4. Right-click the input terminal of the ODE Solver input and select Create»Constant from the shortcut menu. A block diagram constant appears outside the Control & Simulation Loop. The value of this constant is Runge-Kutta 1 (Euler), which is different than what you configured in the Configure Simulation Parameters dialog box. However, the gray box disappears from the Input Node, indicating that the value of this parameter does not come from the Configure Simulation Parameters dialog box. Values that you programmatically configure override any settings you made in the Configure Simulation Parameters dialog box.

    The Input Node now looks like the following figure:

    Tip  You also can use this method to create a front panel control instead of a block diagram constant.

Summary

This tutorial introduced you to the following concepts:

  • The simulation diagram reflects the dynamic system model you want to simulate. This dynamic system model is a differential or difference equation that represents a dynamic system.
  • The Control & Simulation Loop contains the parameters that define the behavior of the simulation. The Control & Simulation Loop also defines the visual boundary of the simulation diagram. Double-click the Input Node of the Control & Simulation Loop to access configurable parameters. You also can expand the Input Node to access these parameters.
  • The Simulation palette contains the VIs and functions you use to build a simulation. You can double-click most Simulation functions to display a dialog box that configures that function. You also can create input terminals for function inputs.
  • You can create simulation subsystems to modularize, encapsulate, validate, and re-use portions of the simulation diagram.

Where to Go from Here

Refer to the following documentation for more information about the Control Design and Simulation Module and LabVIEW.

Additional Control Design and Simulation Module Resources

The Control Design and Simulation Module ships with many example VIs that demonstrate the capabilities of the product. Select Help»Find Examples to display the NI Example Finder and browse these examples.

Additional LabVIEW Resources

  • The Getting Started with LabVIEW manual contains an in-depth introduction to LabVIEW, including several tutorials that showcase LabVIEW features.
  • The LabVIEW Fundamentals book provides information about LabVIEW programming concepts, techniques, features, VIs, and functions you can use to create many types of applications.

Refer to the National Instruments Web site for additional developer resources, training, technical support, and so on.


 

Your Feedback! poor Poor  |  Excellent excellent   Yes No
 Document Quality? 
 Answered Your Question? 
Add Comments 1 2 3 4 5 submit