|Activities are shown in blue boxes as this one.|
Please send comments or suggestions regarding this document via e-mail to firstname.lastname@example.org.
More tutorials that may be relevant for you as a LabVIEW user are available from Finn's LabVIEW page.
There are also lots of tutorials available on the National Instruments home page, http://ni.com. I suggest that you - now - or actually better (because then you will not be distracted :-), after you have accomplished my tutorial - browse this LabVIEW Interactive Tutorial which gives you a good overview over plattforms and applications. The present tutorial (that you now read) focuses more on LabVIEW as a programming tool. (I think you quickly meet the wall if you do not master basic LabVIEW.)
LabVIEW is a development system for industrial, experimental, and educational measurement and automation applications based on graphical programming, in contrast to textual programming - however, textual programming is supported in LabVIEW. LabVIEW has a large number of functions for numerical analysis and design and visualization of data.
LabVIEW now has several toolkits and modules which brings the LabVIEW to the same level of functionality as Matlab and Simulink in analysis and design in the areas of control, signal processing, system identification, mathematics, and simulation, and more. In addition, LabVIEW has, of course, inbuilt support for the broad range of measurement and automation hardware produced by National Instruments. Communication with third party hardware is also possible thanks to the availability of a large number of drivers and the support for communication standards as OPC, Modbus, GPIB, etc.
LabVIEW is produced by National Instruments.
You can launch LabVIEW on you PC as follows:
|Start LabVIEW via Start / All Programs / National Instruments / LabVIEW 8.5 / LabVIEW.|
Depending on the way LabVIEW has been installed on your computer, you may be asked by the Activation Wizard to activate the LabVIEW license online (via Web). The activation is done only once on a given PC.
Assuming that the license activation has run without problems, starting LabVIEW opens the Getting Started dialog window shown below.
Getting Started dialog window
Here are a comments to the most commonly used options in this dialog window:
|Try the items listed above. However, it is assumed that you do not spend too much time on each of them now. (If you open new files, close them without saving.) We will return to several of these items during this tutorial.|
Note: Below are two AVI videos.Playing these videos gives the same information as is provided with the subsequent text in this Chapter.
The video level_measurement_front_panel.avi (17 minutes, 180 MB) describes the operation and contents of the Front panel of the level_measurement.vi example described in Sections 4.1, 4.2, and 4.3 below.
Here begins the text that gives the same information as the videos above.
To see how a LabVIEW program works and how it is constructed, let us study the VI (Virtual Instrument) named level_measurement.vi which I have made for the purpose of having a simple yet illuminating introductory example. In Sec. 7 of this document you will learn how to develop this VI. This VI converts an assumed (user-adjusted) level measurement signal, u, in milliamperes into a level value, y, in meters according to the following mathematical formula:
y = K*(u-u0)
where K is the gain and u0 is the zero of the measurement. K and u0 can be adjusted by the user. By default, K is 0.5m/mA and u0 is The level value is shown in a tank indicator and in a chart. If the level becomes greater than 7 meters og smaller than 1 meter, a proper alarm is displayed in each case. The program runs periodically with a cycle time of 0.1s. A lamp, or a LED (Light Emitting Diode), indicates if the programs runs or not.
|Download level_measurement.vi to any folder (directory) you wish (e.g. C:\temp), or just open it from the default download folder. Then open the VI via in the Getting Started dialog window.|
Opening level_measurement.vi opens two windows:
The front panel and the block diagram of level_measurement.vi are shown in the following figures. At the moment, disregard the comments added to the figures. We will soon return to the details.
The front panel of level_measurement.vi
The block diagram of level_measurement.vi
The VI is operated using the buttons on the toolbar, cf. the front panel figure:
|Run the level_measurement.vi. Then play with the VI by
adjusting some of the elements on the front panel. To stop the VI click the
Run the VI again. Try the Pause button and the Abort button.
Now that you have played with the level_measurement.vi, we will study the contents of the VI in so that you get a more detailed knowledge about LabVIEW. Look at level_measurement.vi. It contains two types of elements:
Controls. You can adjust the value of a control. A control is an input element.
Indicators. These elements are used to indicate values. An indicator is an output element.
|State if each of the following elements is a control or an
Below are detailed comments about the elements on the front panel.
|Run the level_measurement.vi.
Click the Show Context Help Window button in the front panel toolbar, thereby displaying the description and the tip of the front panel element where the cursor is at the moment. Then, disable the Show Context Help Window button.
|Find at least one front element for each of the data types
listed above. (To see a textual element, run the VI and set the value of u
greater than 18.)
|Run the level_measurement.vi. Try the three different ways of adjusting the measurement value, u.|
|Run the level_measurement.vi. Observe that the level is indicated in a chart and in an inherent numeric display. The tank indicator is actually one separate level indicator.|
|Run the level_measurement.vi. Observe that the tank indicator has a caption (on the Front panel) which is different from the label (the label, not the caption) is always shown on the diagram.|
The Front panel is the user interface of the VI. However, the Front panel itself contains no program code. The program code defining the functionality of the program is contained in the Block diagram of the VI.
|It is assumed here that the level_measurement.vi does not run. Open the block diagram of the VI using the menu selection . Then use the menu to show the front panel. Also try toggling between the front panel window and the block diagram window using the keyboard shortcut Ctrl E (this is a useful shortcut to remember - it will save you a lot of time during a lifetime with LabVIEW).|
The following are several comments to the block diagram.
A LabVIEW program contains graphical program code, i.e. the code contains various types of elements, blocks and signal wires. (It is however possible to include textual program code in LabVIEW, using e.g. the Formula node or the MathScript node.)
To any front panel element (e.g. a numeric control or indicator) there is a corresponding terminal in the block diagram. The terminals play the same role as variables in other programming languages, as C, Delphi, Visual Basic etc., and sometimes it is natural to say variable in stead of terminal. Note that terminals look a little different depending on wether it corresponds to a control or an indicator, cf. the examples indicated in the Block diagram. One example is the u terminal in the block diagram. It corresponds to the slider on the Front panel. The value of the terminal and of the front panel element (control or indicator) is always the same. You can find the corresponding front panel element to a given block diagram terminal by double-clicking on the terminal, or by right-clicking the terminal and selecting - or - from the menu that is opened. The reverse operation works, too: You can can find the corresponding block diagram terminal to a given front panel element by double-clicking on the element, or by right-clicking the element and selecting from the menu that is opened.
|Find the Front panel element corresponding to the u terminal. Is the
Front panel element a control or an indicator?
Also find the Front panel element corresponding to the y_chart terminal. Is the Front panel element a control or an indicator?
Different data types are indicated with different colors. The following data types exist in level_measurement.vi:
|Which two elements are contained in the cluster named
(Answer: K and u0.)
Constants are defined values that exist only in the block diagram (so there are no corresponding front panel element). Constants can have various data types, cf. the description of data types above. One example is the constant of value 7 entering the Greater? function.
|Locate the following elements on the block diagram:
A property node represents one or more properties of a front panel element. These properties can be either readable or writable. (The latter is typical.) Using property nodes various properties can be set (or retrieved) programatically. Most properties can also be set via the front panel, by right-clicking on the element there.
|Locate the two property nodes to the right in the block diagram of
To which elements or terminals do these property nodes belong? Which
property is set (written a value to) in both theses property nodes? What is
the purpose of the property nodes in this case?
(Answers: The property nodes belong to the two text indicators named High Alarm and Low Alarm, respectively. The Visible property is set. The purpose of the property nodes is to show them, i.e. to make them visible, on the front panel when the level passes the respective alarm limits.)
A LabVIEW program is executed according to the data flow principle: A block or some other program part is executed only if all inputs to the block has valid values. LabVIEW distributes the execution resources equally to the various parts of the program so that no part is left out.
As seen in the block diagram figure, the progam code is structured using a Flat Sequence structure. The (Flat) Sequence structure is in the form of a film strip with frames. Code can be put into the different frames. The code in the first frame will be executed first, then the code in the second frame, and so on. In the present VI, the code in the first frame writes an empty array to the History property of the chart, thereby emptying the chart just before the cyclic program execution starts. In the second frame is a While loop. The program code inside the While loop frame is executed over and over again cyclically, until the stop condition of the While loop is satisfied. It is the value that is wired into the Loop condition terminal (down left in the While loop) that determines if the loop will stop or continue to run. In this VI the Stop terminal is wired to the loop condition terminal, and hence, this switch is used to stop the while loop. And when the loop stops, any program code outside the While loop waiting for data from some tunnels on the While loop frame will be executed. In this VI the Running? local variable will get value False just after the the While loop has stopped, and after the While loop has stopped the program will stop.
The Metronome function, which is actually denoted the Wait Until Next ms Multiple function, ensures that the cycle time of the While loop is 0.1 seconds. The Metronome actually waits the number of milliseconds wired to the input of the function. In our VI the 0.1 constant is multiplied by 1000 constant to transfer from seconds to milliseconds. Alternatively, we could have wired just a constant of value 100 (ms) into the Metronome.
Note that the Metronome does not implement a time delay of 100ms (in our VI). Suppose, just to take an illustrative example, that LabVIEW uses 2ms to execute the program code inside the While loop (the actual execution time is smaller for our simple program). Then, if the Metronome implemented a time delay of 100ms, the actual cycle time would be 2ms + 100ms = 102ms which is not as specified. In stead, furtunately, the Metronome implements a time delay of 98ms, so that the cycle time becomes 2ms + 98ms = 100ms, as specified at the Metronome input.
LabVIEW shows the process of program execution if you click the Highlight Execution button in the toolbar of the block diagram. Furthermore, you can run the program step-by-step by clicking the Start Single Stepping button. These functions are particularly useful for debugging the program since the program execution is shown clearly and in slow speed.
|Run level_measurement.vi. Open
the block diagram of the VI.
Click the Highlight Execution button in the toolbar, and observe how the program execution is highlighted. Then stop the highlighting execution.
Click the Start Single Stepping button in the toolbar to step through the program step-by-step. Stop the single stepping by clicking the Step Out button in the toolbar.
A local variable is a copy of a terminal (or variable), and the value of a local variable is the same as the value of the terminal. A local variable can be either writable, or readable. One terminal can have several local variables, being readable and/or writeable. Local variables can be used anywhere within the same VI, but not in another VI - in such cases, you can use shared variables.
| level_measurement.vi has one
local variable, namely the local variable corresponding to the terminal
named Running?. Locate this local variable. What is its data type? Try to
explain how the local variable is used in this program.
(Answer: Just before the while loop starts, the Running? terminal gets the value of True, thereby lighting the lamp on the front panel. When the user has clicked the Stop button on the front panel, the while loop stops. Just before the while loop stops, the boolean constant of value False is written to the Running? local variable, causing the terminal to get value False, too, thereby turning off the lamp.)
Various help (technical documents, application notes etc.) can be found on http://ni.com.
|It is assumed that the block diagram of level_measurement.vi is opened. Try the various
You can customize LabVIEW via themenu. This opens the dialog window shown below. Note that some of the options do not take effect until the next time you start LabVIEW (these options are marked with an asterix, cf. the figure below).
The menu Tools / Options opens the Options dialog window
Below are suggested changes of the default settings:
The difference between placing front panel elements as icons (i.e. large icon) and not (i.e. small icon)
As seen from the figure, space is saved in the block diagram by not activating Place front panel elements as icons. (I prefer saving space compared to having the more illustrative icons on the block diagram.)
|Customize LabVIEW according to the above suggestions or your own preferences.|
I have provided two alternative options for learning LabVIEW programming:
The front panel of level_measurement_simple.vi
The block diagram of level_measurement_simple.vi
To start the programming you must open a blank VI:
|Open a new (blank) VI via the menu, thereby opening both a blank Front panel and a blank Block diagram, see the figure below.|
The menu selection File / New VI opens both a blank Front panel and a blank Block diagram
The toolbars of both the Front panel and the Block diagram contain buttons which can be used during the programming, cf. the figure above.
During the programming you will be using the following palettes:
These palettes are described below:
The Tools Palette can be opened by the menu(if it has not already been opened). The figure below shows the Tools Palette.
The Tools Palette, which can be opened by the menu View / Tools Palette
|Open the Tools Palette (menu).|
The Tools Palette contains buttons which you can use to set the cursor in various modes:
The Controls Palette contains sub-palettes which contains the elements that you can put on the front panel. (The Controls Palette actually contains both control elements and indicator elements. Hence, a more accurate, but longer, name would be Controls and Indicators Palette.) The Controls Palette can be opened only when the front panel is in front of the computer desktop. You can open the palette in two ways:
|Open the Controls Palette (menu). To get a glimpse of the contents, browse the palettes in the two upper rows (these palettes are probably the most frequently used).|
The figure below shows the Controls Palette. Note that new palettes may be added to the Controls Palette due to installation of additional modules or toolkits. Therefore, the Control Palette on your computer may look a somewhat different from the Controls Palette shown in the figure below.
The Controls Palette, which can be opened by the menu View / Controls Palette
The Functions Palette contains sub-palettes with functions (blocks) that you can put on the block diagram. The Functions Palette can be opened only when the block diagram is in front of the computer desktop. You can open the palette in two ways:
|First, put the Block diagram of your VI in front (e.g. by shortcut Ctrl+E). Then open the Functions Palette. To get a glimpse of the contents, browse the palettes in the three upper rows (these palettes are probably the most frequently used).|
The figure below shows the Functions Palette. Note that new palettes may be added to the Functions Palette due to installation of additional modules or toolkits. Therefore, the Functions Palette on your computer may look somewhat different from the Functions Palette shown in the figure below.
The Functions Palette, which can be opened by the menu View / Functions Palette
The programming of a LabVIEW program (a VI) may follow this procedure:
Remember to save your work frequently. It may be wise to save the VI under slightly different names as the program evolves, thereby making it easier to retrieve earlier version of the program if the development has taken an adverse direction.
I have seen many complicated VIs that does not work correctly when they are run the first time, and due to the complexity, it may be very difficult and time consuming to find the errors. In some cases, as if the VI is to control some physical systems, it may be dangerous to run a VI that has not been tested appropriately.
A VI that you develop should not be regarded as complete (finished) until it has been tested! You should test the VI continuously, i.e. at the different stages of the development. The final test should, if possible, be accomplished by some other person than you. (When I work on projects, I always let the user run the VI to give me feedback before I regard the VI finished.)
How do you actually test a VI? It depends on the nature of the VI:
These two cases are deescribed below.
To test the VI, adjust the front panel elements over all of their ranges of values, including extremal values, so that alarm setting etc. may be triggered. From the observed responses, conclude if the VI, or the part of the VI under concern, works correctly. This testing principle is illustrated in the figure below.
Testing a LabVIEW program can be done by adjusting the inputs over all theirs ranges of values
Examples are VIs involving I/O (input/output) with physical processes in control applications where the VI calculates control signals as functions of process measurements. When testing the VI you substitute the physical process by a simulated process. The controller function controls the simulated process, and the simulated process feeds measurements back to the controller. This testing principle is illustrated in the figure below. The simulated process can be in the form of a transfer function model or a state-space model representing the differential equations describing the physical process. To learn more about creating simulators and using simulations for testing control systems, see Introduction to LabVIEW Simulation Module.
Testing a LabVIEW program for control using simulation
The editing tools that you probably are familiar with from text editing etc. also apply in LabVIEW. Here is a list over these tools:
Now, let us create level_measurement.vi!
It is assumed that the front panel of a blank VI is in front of the desktop of your PC.
|Save the blank VI with name my_level_meas.vi in any folder you prefer.|
We will now insert elements on the Front panel and configure them according to level_measurement.vi.
Starting with the free text "Level Measurement":
Save the VI.
Color palette opened via the Set Color button on the Tools palette
Note: You can set the color of every part of an element on the front panel the same way as you set the color of the text field above, i.e..
You must also add the free text y = K*(u-u0) to the front panel:
Enter the free text "y = K*(u-u0)" on the front panel (with no
particular configuration of the text).
Save the VI.
Next, we insert and configure the Vertical pointer slide of the simulated measurement signal, u, cf. level_measurement.vi:
Property window of the Vertical Pointer Slide
The default value of an element is the value that the element has just after the VI is loaded into the RAM (random access memory) of the computer, that is, just after you have opened the VI from the permement memory, e.g. the hard disk. Thus, the default value is a start value when the VI is run the first time after being opened. But is the default value also the starting value of en element if you stop the VI and then start it again without closing it? No! In that case the starting value is the value that the element had at the end of the previous run. (So, LabVIEW remembers the latest value.) LabVIEW uses zero as the default default value! Default values can be set in couple of ways:
Save the VI.
You can at any time reinitialize the value of an element to its default value:
Save the VI.
Next, we insert and configure the Gain K element, cf. level_measurement.vi:
Next, we insert and configure the Zero u0 element, cf. level_measurement.vi:
Sometimes you want to align a specific edge of front panel elements to e.g. a common bottom line or to distribute elements with equal space between them or to resize elements so that they get equal size. This can be done using the Align Objects button, the Distribute Objects button, or the Resize Objects button, respectively, on the front panel toolbar.
Align the left edges of the Gain K numeric control and the Zero u0 numeric
control: Select both these controls (using the mouse). Then click the Align
Object button on the toolbar and select Left Edges in the palette that is
Save the VI.
Then, we insert and configure the tank indicator, cf. level_measurement.vi:
Save the VI.
Looking at the Front panel of level_measurement.vi, there is still one element on the front panel that we have not put on the Front panel of my_level_meas_simple.vi, namely the Stop button. Although we could very well have placed it on the Front panel as with the other elements, we will in stead place it there via the Block diagram. It is generally useful to be aware of this possibility.
Now, it's time to make the VI work properly! The functionality of the VI is implemented in the Block diagram.
The Block diagram of your my_level_measurement.vi should be similar to the Block diagram shown in the figure below. So far, the Block diagram just contains the terminals of four Front panel elements. Of course, the Block diagram is completely undeveloped.
The (completely) undeveloped Block diagram of my_level_measurement.vi after (almost complete) Front panel development
Our VI shall run continuously with a cycle time of 0.1 s. Therefore, we need to put the code inside a While loop.
Insert the While loop: Right-click somewhere up
to the left in the Block diagram (causing the Functions palette to appear) / Select
Structures / Select While Loop, and click to drop it on the Front panel
and with the mouse button still down expand the While loop so that it
embraces the four terminals.
The result should be as in the figure below.
The Block diagram after the While loop has been inserted
Save the VI.
Note: It is essential that code that shall be inside a While loop, programmatically, actually appears (is seen) inside the loop.
Next, we insert two mathematical functions - the Subtract and Multiply functions into the Block diagram.
The result should be as shown in the figure below.
The Block diagram after the Subtract and the Multply functions have been inserted
Save the VI.
Next, we draw wires between the blocks on the Block diagram.
Draw the wires in the Block diagram: Move the cursor to a connection point
of either a terminal or a function block, causing the cursor icon to be a
spool. Then (left-)click the mouse, and draw a wire between the elements
that you want to connect.
The result should be as shown in the figure below.
The Block diagram after wires have been drawn using the Connect wire tool
Save the VI.
Here are a number of useful tips about drawing wires:
Let us try out these tips!
Delete the part of the wire between the u terminal and the Subtract function that enters the latter function. The resulting Block diagram is shown in the figure below.
Remove the broken wire going out from the u terminal using the shortcut Ctrl + b. The resulting block diagram is shown in the figure below.
Draw a "crazy" wire from the u terminal to the Subtract function, see the figure below.
Make LabVIEW clean up the long wire between the u terminal and the Subtract function by right-clicking the wire and selecting in the menu that is opened. The resulting block diagram is shown in the figure below.
Save the VI.
To make your VI even more self-documenting labels of functions and program stucture elements can be made visible.
Make the labels of the While loop frame, the Subtract and the
functions visible: Right-click on the
element / Visible Items / Label, see the figure below.
The Block diagram after wires have been drawn using the Connect wire tool
Save the VI (my_level_measurement.vi).
Next, we add the Wait Until Next ms Multiple function, which we for simplicity denote the Metronome function, to implement a cycle time of 0.1s of the While loop. (The Metronome function was explained here.) Since the Metronome must have the cycle time in units of millisensonds at its input, 0.1 is multiplied by 1000, and the product is wired to the Metronome.
The result should be as shown in the figure below.
The Block diagram including the Metronome function (Wait Until Next ms Multiple function)
Save the VI.
Now we will create a Stop button on the Front panel. As mentioned earlier, we could have put a Stop button on the front panel by selecting from the Controls palette / Boolean subpalette, and then wire its terminal to the Loop condition terminal in the Block diagram. In stead we will create the button from the Block diagram. It is my experience that this is very useful in many cases, since LabVIEW then automatically selects the correct data type (it may be more cumbersome to create manually e.g. a cluster of arrays of numeric element at the Front panel than to let LabVIEW create a correct Front panel element).
Create a boolean Front panel element whose terminal will be wired to the
Loop condition terminal:
Right-click on the Loop condition terminal / Create Control.
The resulting Block diagram is shown in the figure below.
The Block diagram including the boolean stop terminal wired to the Loop condition terminal
What was actually created on the Front panel?
|Open the Front panel of the VI (Ctrl+E on the keyboard).|
The figure below shows the Front panel.
The Front panel containing the Stop button automatically created by LabVIEW
So, the Stop button was placed in the upper left corner of the Front panel. (This is the default position of Front panel elements that are automatically created.) Let us do a few cosmetic changes on the button. Furthermore, we have to make the button operate correctly - a button is not just a button! There are a number of ways a button can operate: It may change its state immediately after having been pressed, or when the press is released. And it may remain in the down-position, or it may pop up as if it is forced by a spring.
Move the Stop button to the bottom of the Front panel.
Do the following changes to the appearance of the button via its Property window which you open by right-click on the Stop button / Selecting Properties in the menu. The Property window is shown below (with the Operation tab selected, however it is the Appearance tab that is selected by default).
The Operation tab of the Property window of the Stop button
By now your my_level_measurement.vi should similar to level_measurement_2.vi. The figures below show the Front panel and the Block diagram of level_measurement_2.vi. (If you want, you may proceed with level_measurement_2.vi as the starting point, starting by saving it as my_level_measurement.vi in a proper folder.)
Front panel of level_measurement_2.vi
Block diagram of level_measurement_2.vi
Hopefully, the VI - my_level_meas.vi - works as assumed. But the only way to confirm it, is by testing it, and of course correcting the errors, which is denoted debugging in computer science.
Before you can test the VI it must be free of syntax errors, that is, it must have no technical errors. Actually, a VI will not run if it has syntax errors. LabVIEW helps a lot in finding syntax errors. Let us try!
Introduce a syntax error in the VI by deleting a part of the wire between
the u terminal and the Subtract function, see the figure below. Observe that
the Run button is grey and broken!
The Run button is grey and broken due to a syntax error
Click the Run button to try to run the VI. Since the VI has an error the Error List window is opened, see the figure below.
Error List window
Now, double-clicking the first of the two errors in the error list (see figure above), causing LabVIEW to zoom into the error in the block diagram. Fix the error (by completing the wire)! Observe that the Run button now again is normal (i.e. white and not broken).
Even though the VI has no syntax errors it may, of course, still not work correctly due to functional errors. It is necessary to test the VI you should operate it through all possible operating ranges and under the various operation conditions while observing the responses and judging if the VI behaves correctly.
With our simple VI we can perform the test as follows:
Start (run) and stop the VI a couple of times to check that these operations
Run the VI. Adjust the values of u, K and u0 to some integer values (so that it is easy to check the calculations by hand), e.g. u = 10, K = 0.5, and u0 = 4. The VI then produces y = 3. Check this by manual calculations according to the implemented formula y = K*(u-u0).
With complicated VIs you should test the VI at several stages of its development. (It is like checking that the lower part of the building is safe and strong before you start building the next part upon it.)
While performing functional tests probes, execution highlighting, and stepwise execution may be useful tools to detect errors. These tools are described (and practiced) earlier in this document.
Now that you have got some experience in debugging and testing a VI, we will continue developing the complete level_measurement.vi. The following elements will be added to the VI:
We will add a Description and a Tip strip for the u element (the simulated measurement signal):
Open the Property window of the vertical pointer slide of u (right-click on
the element, and select Properties). Open the Documentation tab, and enter
the Description and Tip strip text shown in the figure below.
Description and Tip strip
Save the VI.
Run the VI and check if the Description and Tip strip are effective:
Tip strip for the vertical pointer slide
To display the Loop iteration number of the While loop, we can connect a numeric indicator to the Loop iteration terminal, see the lower left corner of the block diagram shown in this figure.
Open the block diagram of my_level_meas.vi.
thereby creating a numeric display indicator at the upper left part of the
Open the front panel of my_level_meas.vi, and move the indicator to a new position, as shown in this figure. Give the element Label "i" (invisible), and Caption "Iteration no." (visible).
Run the VI. Does it seem from the continuously increasing value of the Iteration i element that the While loop runs ten times per second?
Stop the VI. Save it.
Often we want to have the elapsed time displayed at the front panel. This can be implemented by adding the an Elapsed Time function on the block diagram and displaying the Elapsed Time output of the function in a numeric display on the front panel.
Open the block diagram of my_level_meas.vi. Make the While loop somewhat
larger, to make space for the Elapsed Time function to be added, cf.
Locate the Elapsed Time function on the Timing subpalette on the Functions palette, and add it to the block diagram, cf. this figure. As you drop the block a large blue icon of the function is displayed, and a Configuration window is automatically opened. In this window disable Automatically reset after time target, see the figure below. Then click OK.
Configuration window of the Elapsed Time function
Create a numerical indicator to the Elapsed Time output of the Elapsed Time function:.
Open the front panel of my_level_meas.vi, and move the Elapsed Time indicator to a new position, as shown in this figure. Give the element Label "t" (invisible), and Caption "t [sec]" (visible).
Open the block diagram. The icon of the Elapsed Time function block is large. To use a small icon in stead: this figure.. The resulting icon is as shown in
Open the front panel. Run the VI. Does the t element display the elapsed time?
Stop the VI. Save it.
The Elapsed Time function block has a light blue color, indicating it is an Express VI. Express VIs, or Express functions, are actually made of elementary LabVIEW functions. You can configure Express VIs by double-clicking on them. There are many Express VIs on the various subpalettes of the Functions palette.
Open the block diagram of a new VI (you do not have to save it, as we will not use it later). Open the following subpalettes, and locate the Express VIs there. You can, to the extent you have time and interesest, drop the Express VIs onto the block diagram to see the configuration window of the Express VI (the configuration window is automatically opened).
Clusters are elements that contain one or more elements of possibly different data types. We will create a cluster containing the Gain K element and the Zero u0 element. What is the benefits of using clusters? On the Front panel clusters visually groups elements that are somehow related, e.g. controller parameters. On the Block diagram clusters may simplify the code since one cluster wire represents several single wires.
Open the front panel of my_level_meas.vi. Add an empty cluster from the
Array, Matrix & Cluster palette to the front panel. The empty cluster may
intermediately be placed anywhere on the Block diagram since we will move it
soon. The figure below shows the Front panel with the empty cluster.
Front panel with empty cluster
Now, first drag the Gain K element and then the Zero u0 element into the cluster (you may fine-position the elements using the keyboard arrows). Right-click on the cluster boarder and select.
Set the label and caption of the cluster via the Property window of the cluster (to be opened via right-click on the cluster boarder) as follows: Label "Cluster_param" (invisible). Caption: "Parameters" (visible).
Move the cluster to the original position of the Gain K and the Zero u0 elements, see the figure below.
The Front panel with the cluster containing the Gain K and the Zero u0 elements
Now, open the Block diagram, see the figure below. The Cluster_param terminal has been placed at an more or less arbitrary position.
The Block diagram with the Cluster_param terminal situated (arbitrarily) below the Metronome
Next, do as follows. The final result is shown in the figure below.
Block diagram with the cluster terminal and the Unbundle By Name function.
Save the VI.
We will now add a boolean indicator in the form of a LED (Light Emitting Diode) which will be lightening while the VI runs. Local variables will be used in the implementation.
Open the block diagram of my_level_meas.vi. Add a True constant and a False
constant from the Boolean subpalette on the Functions palette, see
the figure below. (Comment: By clicking a
boolean constant the value changes from True to False or from False to
Open the front panel of my_level_measurement.vi. Add a LED from the Boolean subpalette on the Controls palette at the position shown here (the LED labeled Running?). Set the properties of the LED as follows (in its Property window):
Note: It may happen that the Caption has a black background color. By double-clicking the Caption field the background color becomes white.
Open the block diagram of the VI. Locate the boolean Running? terminal and move it to the position shown in the figure below. Then wire the True constant to the Running? terminal.
Create a Local variable belonging to the Running? variable (terminal):. Then, place the Local variable just outside the While loop, and connect it to the False constant, see the figure below.
Note: By default a local variable is readable. You can set it to be writable by right-clicking on it and then selectingin the menu that is opened.
Block diagram with a True constant connected to the Running? terminal and a False constant connected to the Local variable belonging to the Running? terminal (or variable)
Open the Front panel, and run the VI. Observe that the LED is turned on. Stop the VI, and the LED should turn off.
Save the VI.
Now we will implement the alarms. A High Alarm text will be shown on the front panel if the level is greater then 7 meters, and a Low Alarm text will be shown if the level is less than 1 meter.
We start by adding String indicators which will be used to display the
Now we will add the necessary functionality to the block diagram. While following the instructions below, refer to the figure just below showing the resulting block diagram.
Block diagram with Comparison functions, text indicators and property nodes
Open the front panel, and run the VI. Adjust the value of u to the maximum value and then to the minimum value. Are the alarms displayed correctly?
Save the VI.
We will now add a chart to our VI. A chart is a continuously updated diagram for plotting variables with time along the x-axis. In our VI the chart will plot three signals:
Open the front panel of my_level_meas.vi. Add a chart at the position shown in the figure below. The chart is found at .
A (waveform) chart is added to the Front panel
The (three) signals to be plotted in the chart are collected using a Bundle function in the Block diagram. The Bundle function produces a cluster of signals, and this cluster is then wired to the chart. The relevant resulting part of the Block diagram is shown in the figure below.
Using a Bundle function to collect the signals to be plottet in the chart
Now, let us do some changes to the chart on the Front panel:
There is a large number of options when configuring a chart. We will now add a chart with typical basic settings. We will mostly make the settings in the Property window of the chart. Later you will see how to configure the chart programmatically, i.e. by wiring values (constants) to a Property node of the chart in the Block diagram.
To configure the chart, open its Property window (by right-clicking somewhere on the chart). Then do the following setting in the different tabs:
To set the Plot legend (above the diagram, to the right) properly:
Open the front panel. Run the VI, and play with the u value. The y-axis has automatic scaling, which is the default setting. Change to manual scaling while the VI runs by right-clicking on the chart, and selecting AutoScale Y.
The chart has an inbuilt data buffer which stores previous (historical) data so that the previous signal values are shown in the chart. The default length of this buffer is 1024. Thus, at each instant of time the most 1024 recent samples of the signal are stored in this buffer. In our VI, with a sampling time of 0.1s and a range of 50s along the x-axis, the buffer must have a length of 50s/0.1s = 500 samples. Therefore the default buffer length is ok in our case. But in other cases it may be necessary to increase the buffer length.
To see/set the buffer length: Right-click on the chart / Select Chart History Length, see the figure below. However, as argued above, it is not necessary for us to change the buffer length, so you can just close the dialog window.
The Chart History Length can be seen/set via Right-click on the chart / Chart History Length
As an example of programmatic configuration of the chart, we will set the History property of the Property node of the chart in the Block diagram so that the chart is emptied as the VI starts.
Open the block diagram of my_level_meas.vi. Do as follows:
Next, we have to ensure that the added code is executed before the While loop starts. This can be done using a Sequence structure which is in the form of a film strip with frames. Code can be put into the different frames. The code in the first frame will be executed first, then the code in the second frame, and so on.
Add a Flat Sequence from the Structures palette (on the Functions palette), and ensure it embraces the While loop, see the figure below.
A Flat Sequence structure embracing the While loop
Then, do as follows:
The resulting block diagram is shown in the figure below.
Block diagram with a Flat Sequence structure
Open the Front panel, and Run the VI. Adjust the input signal u. Stop the VI, and run it again. Is the chart emptied at the moment the VI is started, as expected?
A Case structure consists of a number of different cases or frames containing their individual LabVIEW code. Only one of the cases is active at any instant of time. The Case selector selects or controls which one among the cases is active. So, a Case structure implements a way of selecting among alternative portions of program code. The Case structure in LabVIEW si similar to the case or switch structure in other programming languages
case.vi is a simple example of a Case structure. The Case structure is used to display alternative texts in the String indicator, depending on wheter the value of u adjusted with the vertical slider is greater than (or equal to zero) or less than zero. The figures below show the Front panel and the Block diagram of this VI.
The Front panel of case.vi
The Block diagram of case.vi. The two alternative cases (frames) are shown. The True case is shown in the upper part of the figure, and the False case is shown in the lower part.
Comments to case.vi:
You may want to try adding a Case structure yourself:
Finally, some general comments to the Case structure:
The For loop is a program structure which can be used to run a certian amount of program code over and over again a predefined number of times. The For loop is quite similar to the While loop, however, as said, with the For loop the number of loop iterations is fixed (predefined) while with the While loop that number is not predefined.
Let us look at an example of a For loop. The program reads successively new values from an array according to the following pseudo code:
Arr1 = [10,20,30,40,50,60,70,80,90,100];
N = 5;
for i = 0:N-1
x = Arr(i);
The VI for_loop.vi implements this code in LabVIEW. The Front panel and the Block diagram of this VI are shown in the figures below.
The Front panel of for_loop.vi
The Block diagram of for_loop.vi
Comments to for_loop.vi:
You may want to try adding a For loop to a VI yourself:
In LabVIEW a Shift register is a memory element storing values from previous executions of loops, e.g. While loops and For loops. A Shift register can store values of any type, e.g. integers, decimal number (floating point numbers), arrays, clusters. A Feedback node is (almost) equivalent to a Shift register. It has less features than a Shift register, but appears somewhat different in the Block diagram. Both Shift register and Feedback node will be described in the following.
Let us look at a simple example where a Shift register is used to implement the recursive formula
y(i)=y(i-1)+u(i), with initial value y(-1)=2
where i is the time index (which counts the discrete time steps), which starts on zero. The figures below show the Front panel and the Block diagram of shiftregister.vi.
Front panel of shiftregister.vi
Block diagram of shiftregister.vi
The user can adjust the control u (an integer) while the VI is running. A Shift register is used to store y(i) so that it becomes available as y(i-1) in the subsequent execution (or cycle) of the While loop, and y(i-1) is used in the calculation of y(i), cf. the formula given above.
You may want to try create a Shift register yourself (by first removing the existing Shift register and then immediately creating a new one):
The Shift register in shiftregister.vi stores a value created in the previous cycle of the While loop. What if you want to store older values than just the previous value? No problem - just expand the left Shift register terminal by dragging the bottom line of the terminal downwards. And you can assign initial values to each of the elements of the Shift register, see the figure below.
The Shift register may contain values from older cycles of the While loop
The figure below shows a Feedback node. It can store the value of an element from one previous loop cycle (whereas a Shift register may store values from even older cycles). A Feedback node appears somewhat simpler in the block diagram.
A Feedback node, which is (almost) equivalent to a Shift register
You can create a Feedback node in two ways:
Let us try both ways.
In all programming languages, e.g. Visual Basic, C++, Delphi, MATLAB, you can create your own functions which can be used in - or be called by - the main program. Similarly, in LabVIEW you can create your own SubVIs. A SubVI is a special kind of VI as it has input and/or output connectors. SubVIs are used in the same way as function blocks in the Block diagram and they communicate with the rest of the Block diagram via the connectors on the SubVI block.
The figure below shows an example of a Block diagram containing the SubVI named sub_vi.vi. The Block diagram belongs to a VI similar to level_meas.vi. The SubVI implements the mathematical formula
y = K*(u-u0)
Block diagram containing the SubVI named sub_vi.vi
A SubVI has a Front panel and a Block diagram. By double-clicking on the SubVI block, the Front panel is opened, and then the Block diagram can be opened in the usual way. As you see in the figure below, the Block diagram implements the formula y = K*(u-u0).
Front panel of the SubVI sub_vi.vi
Block diagram of the SubVI sub_vi.vi
The Front panel controls (and their corresponding Block diagram terminals) represent input connectors on the SubVI. The Front panel indicators (and their corresponding Block diagram terminals) represent output connectors on the SubVI.
Where are the connectors of the SubVI? You can see them by right-clicking the SubVI icon which is the icon at the upper right corner of the Front panel window, and selectingin the context menu, see the figure below. By clicking one of the connectors in the Connector pane, the corresponding element on the Front panel is highlighted. In the figure below the connector corresponding to the K element is selekcted and the K control element is thereby highlighted.
The Connector pane of the SubVI sub_vi.vi
Why would you use a SubVI? You can use a copy of the SubVI in several places in one or more Block diagrams, thereby reusing code effectively. For example, you may implement a mathematical formula in a SubVI. Furthermore, you can also make your Block diagram look simpler by putting code into a SubVI.
As an example, let us add an existing SubVI to the Block diagram of an existing VI. (The application is based on level_meas.vi.)
In the above section we added an existing SubVI into a Block diagram. But how do you create a SubVI? It can be done in two ways:
In the following only the automatic method will be described in detail.
Let us try to create a SubVI using the automatic method:
Block diagram containing the SubVI named sub_vi.vi
Here are a few additional comments to SubVIs:
The Icon Editor
Setting a SubVI to become reentrant: If several copies of the same SubVI will be used in one, or in several Block diagrams (of other VIs) it is important that each of these copies are independent of each other. This is implemented by making the original SubVI reentrant. To define a SubVI as reentrant: Right-click on the SubVI icon in the upper right corner of the SubVI Front panel / Select VI Properties / Select Category: Execution / Select Reentrant Execution.
Editing the SubVI: You can edit a SubVI as you would edit any VI, e.g. you can add code to the Block diagram. You can also add or remove Front panel controls and indicators corresponding to new SubVI inputs or outputs, respectively. To edit the connectors (representing the SubVI inputs and outputs): Right-click on the SubVI icon in the upper right corner of the SubVI Front panel / Select Show Connector, therby opening a context menu containing several options for editing the Connector pane, cf. this figure. For example, you can add and remove inputs/outputs, can change the pattern of the connectors, disconnect a connector from a Front panel element, etc.
LabVIEW has several functions for writing data to files - both continuous writing (online, while the VI runs) and batch (offline) writing. LabVIEW has also functions for reading data in such logfiles. These functions are available on the File I/O palette.
The file formats that is supported by the above two File I/O functions are:
As a general rule I suggest that you use the binary file format (TDMS) unless you really need the text format (LVM).
Both TDMS (binary) and LVM (text) files writing and reading are described in the subsequent sections.
The figures below show the Front panel and the Block diagram of write_to_tdms_file.vi.
Front panel of write_to_tdms_file.vi
Block diagram of write_to_tdms_file.vi
The figures below show the Front panel and the Block diagram of read_from_tdms_file.vi.
Front panel of read_from_tdms_file.vi
Block diagram of read_from_tdms_file.vi
If you run the read_from_tdms_file.vi the TDMS Viewer is also opened, in addition to the Front panel, see the figure below. The TDMS Viewer is a graph tool for quickly plotting the channels in the logfile. Note that you have to click the Quit button in this window to quit the TDMS Viewer, and to stop the VI.
Here are comments to read_from_tdms_file.vi:
Dialog window of the Convert from Dynamic Data function
The figures below show the Front panel and the Block diagram of write_to_lvm_file.vi.
Front panel of write_to_lvm_file.vi
Block diagram of write_to_lvm_file.vi
By double-clicking the Write To Measurement File function in the block diagram a dialog window opens, see the figure below.
Dialog window of the Write To Measurement File function in the block diagram of write_to_lvm_file.vi
Comments to the dialog window shown in the figure above:
In our example, the result of running write_to_lvm_file.vi is a text file named logfile1.lvm. The figure below shows the file opened in Notepad from one experiment. (The cycle time of the While loop was set to 0.1 s, cf.the block diagram shown above. As you see, the actual cycle time varies a little, but it is very close to the specified cycle time of 0.1 s.)
logfile1.lvm file opened in Notepad
The figures below show the Front panel and the Block diagram of read_from_lvm_file.vi.
Front panel of read_from_lvm_file.vi
Block diagram of read_from_lvm_file.vi
Comments to read_from_lvm_file.vi:
DIAdem is a powerful tool for displaying and analyzing data offline. (It is a National Instruments tool, but it is used independent of LabVIEW.) Both TDMS and LVM data files can be imported into DIAdem. The figure below shoes the DIAdem dialog window. DIAdem is quite intuitive to use. It is beyond the scope of this LabVIEW tutorial to describe DIAdem in further details here.
The DIAdem tool for displaying ans analyzing data
You have already learnt to plot signals in Charts. A Chart is continuously updated by the most recent signal values, and the x axis is typically the time axis. A graph is another type of plots. Graphs plots any array versus any other array in an XY (and the increment in the X array does not have to be constant). There are several Graphs on the Graph palette on the Functions palette, the most general being the XY Graph.
Here is an example based on a somewhat simplified verson of level_meas. The three arrays consisting of the historical values of the y, y_max, and y_min are plotted against one time array containing the discrete times covering the elapsed time as the VI was run. The Chart is updated once the While loop has stopped. (The VI also contains a Chart, which is continuously updated as the VI runs.) Below are the Front panel and the Block diagram of graph.vi.
Front panel of graph.vi
Block diagram of graph.vi
Comments to this VI:
Often your application implements a number of different tasks to be executed simultaneously, e.g.
One conventient way to organize such parallel tasks is to put the tasks into individual While loops that run in parallel (simulateneously). These While loops may run with different cycle times. LabVIEW will allocate resources to each While loop (parallel tasks) so that each get a cycle time equal to or close to the cycle time set by the Metronome function (Wait Until Next ms Multiple).
Here is an example: Below are the Front panel and the Block diagram of parallel.vi.
The Front panel of parallel.vi
The Block diagram of parallel.vi
Comments to parallel.vi:
The VI adds 10 to a user adjusted value of u (this is denoted Signal processing in the Block diagram). The resulting value is then plotted in a Chart (denoted Plotting).
The VI contains two parallel While loops inside a Sequence structure. The two While loops implements signal processing and plotting, respectively.
The While loops runs with different cycle times, 10 and 500 ms, respectively.
Data is transferred from the Signal processing loop to the Plotting lopp using a local variable of x.
The x indicator is hidden on the Front panel (Right-click on the element / Hide indicator).
One Stop button is used to stop both loops. A Local variable of the button is used to stop the Plotting loop. Note: It is necessary that the button remains in the down position until both loops have stopped. Therefore the mechanical operation mode of the button is set to Switch when pressed. This is set in the Properties window of the Stop button, see the figure below.
The button behaviour is set to Switch when pressed in the Property window of the button
When both loops have stopped, the Stop button must of cousre pup out again. This is implemented with the Sequence structure embracing both While loops, and with the False constant being written to a Local variable of the Stop button outside the Sequence structure.
The Formula node is a window in the block diagram where you can write textual program code following the C-syntax. Using a Formula node for mathematical expressions is often more convenient than building the expression using elementary blocks in the ordinary graphical way in LabVIEW since it is easier to write and maintain textual mathematical expressions than drawing equivalent block diagram code.
Here is the interesting part of the block diagram of the VI formula_node.vi where the Subtract and Multiply functions have been replaced by a Formula node containing the textual code
z = u-u0;
y = K*z;
The VI is otherwise the same as level_meas.vi.
Block diagram of the VI formula_node.vi where the Subtract and Multiply functions has been replaced by a Formula node containing the textual code
Add a Formula node to the block diagram as follows:
You may wonder why you entered the code
z = u-u0;
y = K*z;
in stead of the somewhat simpler
y = K*(u-u0);
Actually, the latter would work perfect. However, often you will want to split an expression into smaller parts each consisting of expressions (which must be ended with a semicolon), and then having one final expression based on the intermediate results from these expressions. In such cases, remember to define one output for each of the left-hand variables used in the expressions. What actually happens is that each variable is defined. It is not necessary to wire the outputs to some terminal of functions outside the Formula node. As an alternative to defining variables using such "dummy" outputs, you can use ordinary variable declaring expressions as in the C language. In our case we could have used the following code:
z = u-u0;
y = K*z;
MathScript is a LabVIEW tool for executing textual mathematical commands or expressions equal to Matlab. Please see the Introduction to MathScript.
You can create a documentation of your VI with various outputs:
The figure below shows one such HTML file opened in a Web browser (Opera).
A documentation HTML file opened in a Web browser (Opera)
Creating the documentation is done via themenu. This opens a number of successive dialog windows which are not described in detail here. However, one tip for generating an HTML file is to select True color in the dialog window shown below, otherwise some colors on the Front panel may look strange in the resulting document.
The dialog window where you select Color depth. There you should select True color.
Let us try:
LabVIEW projects defines a logical organization of various files used in an application. These files may be VIs, documentation files, support files, external code, data files and hardware configuration settings. LabVIEW projects makes it easier to manage and organize these files. The organization of the files in a LabVIEW project is independent of the physical location of the files on the computer.
It is actually not necessary to organize files into LabVIEW projects, although it makes the file management easier, but in one case it is required to use LabVIEW project, namely if you are going to create a so-called Build, that is, if you are to create an executable, an installer, a DLL, a source distribution, or a zip file. (I use LabVIEW projects while developing SimView.)
To create a LabVIEW project, select File / New Project in any LabVIEW window. To include a file (e.g. a VI) or a folder in a project, select My Computer in the Project tree, and select the menu Project / Add File (or Add Folder). The figure below shows one example of a LabVIEW project.
LabVIEW project of name project1
To open a file that is in a project, just double-click the file in the project tree.