Introduction to
LabVIEW 8.6
by
Finn Haugen
30. August 2009
Sections 1 - 7 are now (per 30.
August 2009) "old". I have made
a streaming video
which replaces these sections. Among the new things in the video is how to use
Tab control to improve the organization of the Front panel.
Contents:
1 Preface
2 Introduction
3 Launching LabVIEW
3.1
License Activation
3.2
Starting using LabVIEW
4 Looking at an
example VI: level_measurement.vi
(video-based)
5 Help
6 Customizing
LabVIEW
7 LabVIEW
programming step-by-step
(video-based)
8
Additional topics
8.1 Case
structure
8.2 For
loop
8.3
Shift register. Feedback node
8.4 SubVIs
8.5
Log file writing and reading
8.6 Displaying
and analyzing data in DIAdem
8.7
Plotting in graphs
8.8
Structuring VIs using parallel While loops
8.9
Text-based (C-) programming using Formula node
8.10
Text-based (Matlab-like) mathematics using MathScript
8.11
Generating documentation of your VI
8.12
LabVIEW projects
8.13 How
to make parts of block diagram disabled?
1 Preface
The aim of this document is to give you an introduction to
LabVIEW version 8.6. It is assumed that you have LabVIEW 8.6
installed on your computer. The introduction is to "core" LabVIEW.
Thus, no particular LabVIEW toolkit or module is covered. (Actually,
this tutorial is based on LabVIEW 8.5, but there are just small
differences between version 8.5 and 8.6.)
Only a basic introduction is given, enough to make you able to develop your
own LabVEIW programs. If you need additional information, use Help i LabVIEW or
search for relevant examples in LabVIEW.
This tutorial is self-instructive, having a number of activities that you are
supposed to perform. These activities are shown in blue boxes, as here:
Activities are shown in blue boxes as this one. |
Videos are used to explain the topics. It assumed that you
follow the videos actively, by working with the examples used in the videos
(these examples are available via the present document).
Please send comments or suggestions regarding this document via e-mail to
finn@techteach.no.
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, on
http://www.ni.com/gettingstarted/. (The reason for making the present
tutorial is that I have found that these NI tutorials did not fully cover the
needs I have in my courses where I introduce LabVIEW.)
[Table of contents]
2 Introduction
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.
[Table of contents]
3 Launching LabVIEW
You can launch LabVIEW on you PC as follows:
Start LabVIEW via Start / All Programs / National
Instruments / LabVIEW 8.6 / LabVIEW. |
[Table of contents]
3.1 License Activation
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.
[Table of contents]
3.2 Starting using LabVIEW
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:
- Files / New / Blank VI opens a new, blank
VI. VI is short for Virtual Instrument, which represents a program
developed in LabVIEW. This is the most commonly used option.
- Files / New / Empty Project opens a new
project which is a collection of various LabVIEW files and other files making
up a project. (A project is a new construct in LabVIEW 8.)
- Files / Open shows the recently opened
files. It is kind of a history list.
- Resources / New to LabVIEW? / Getting Started with
LabVIEW links to the Getting Started with LabVIEW PDF document of
approximately 80 pages. You may regard it is an alternative to or an addition
to the present tutorial. The present tutorial is more directly aimed to
developing applications running continuously with a fixed cycle time, as in a
control, data acquisition, or a simulation application.
- Resources / LabVIEW Fundamentals links to
the LabVIEW Help, where you can find information using the Contents, Keywords,
and Search options in the usual way.
- Resources / Find Examples links to the
library of examples included in LabVIEW. This library represents a large
knowledge base. If you have a specific problem, you will probably find an
appropriate example here. The examples are also available via the Help menu in
any LabVIEW 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. |
[Table of contents]
4 Looking at an example VI: level_measurement.vi
A LabVIEW program constist of two parts - the Front panel and the
Block diagram. These two parts are described in the videos below. If you
prefer a textual description in stead of the videos, you can read
Introduction to LabVIEW
8.5.
If - for some reason - the video does not display correctly in the current
player, try some other. Windows Media Player is probably the default video
player on your PC. Alternative players are RealPlayer and QuickTime (both can be
downloaded from the Internet for free).
The following two videos are older than the one video mentioned in the
beginning of this document.
Study Front panel:
Study Block diagram:
[Table of contents]
5 Help
- Help about a specific function in the block
diagram: .
This opens a Help window for that function.
- Context help: By clicking the Context Help
button (with symbol ?) at the right side of the toolbar a context help window
is opened. By moving the cursor over an element on the block diagram or on the
front panel information about that element is shown in the context help
window.
- Help about a topic: Menu:
.
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
Help options:
- Get help about the Subtract function in the block diagram (by
right-clicking on the function block).
- Activate Context help for the block diagram. Get help about the
Subtract
function and a number of other blocks as you wish by placing the cursor
over the block.
- Search LabVIEW Help for help about the Subtract function.
|
[Table of contents]
6 Customizing LabVIEW
You can customize LabVIEW via the menu. 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:
- Front Panel:
- Disable the Use localized decimal point option. Disabling makes
LabVIEW use point as the decimal separator. (If, in stead, this option
is enabled, LabVIEW uses the decimal separator as defined in the settings of
the Windows operating system.)
- Block diagram:
- Enable Show subVI names when dropped. This causes names of subVIs
to be shown in a label on top of the subVI icon (block). A subVI is a LabVIEW
subprogram which can be used as a function in the parent program to perform a
specific task.
- Disable the Enable auto wiring option. This prevents LabVIEW from
automatically connecting adjacent blocks. Although it seems useful to have
auto wiring enables, it is my experience that the auto wiring is a little
annoying since it tends to draw wires between blocks when you do not want any
wire.
- Disable Place front panel elements as icons. This causes LabVIEW
to use small terminal icons on the block diagram. If you, in stead, activate
this option, the terminal icons are larger, with a mimic of the element as it
appears at the front panel. The figure below shows the difference.
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.)
- Alignment Grid: I suggest using alignment
grid of 6 pixels on front panel, and no alignment grid on the block diagram.
- Controls/Functions Palettes:
- The Format pull-down list: Category (Icons and Text)
- The Navigation Buttons pull-down list: Label All Icons
- Environment: Disable the Just-In-Time
Advice.
Customize LabVIEW according to the above suggestions or your own preferences. |
[Table of contents]
7 LabVIEW programming step-by-step
Open the LabVIEW program
level_measurement_simple.vi
(which
is somewhat simpler than level_measurement.vi
which was presented as an example of a LabVIEW program in Section 4).
Follow the instructions given in the video
level_measurement_simple
(50 minutes, the video will be opened in new window) which describes how to program the Front panel
and the Block diagram of level_measurement_simple.vi.
(If you prefer textual instructions in stead of watching the video,
please go to Section 7 of
Introduction to
LabVIEW 8.5.) |
[Table of contents]
8 Additional topics
8.1 Case structure
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:
- The comparison function named Greater Or Equal
To 0? is used to generate a proper Boolean value - True or False -
which is wired to the Case selector.
- The two Cases are identified by the labels True or False, respectively.
- String constants contains the alternative strings to be displayed in the
String indicator on the Front panel.
- Values out from (or into) Case frames are transferred via tunnels.
Tunnels are automatically created if as you draw a wire across a Case frame.
All output tunnels, i.e. tunnels for signals leaving frames, must be
wired to some signal. This is the case in the Block diagram shown above. A
String constant is wired to the output tunnel of each of the two frames.
(Honestly, you may drop wiring a value to an output tunnel, but if so, you
have to tell LabVIEW to use the default value of the indicator to which the
tunnel is wired in stead. This is done via right-click on the tunnel.)
You may want to try adding a Case structure yourself:
- Save case.vi as
my_case.vi (in any folder you prefer).
- Open the block diagram of my_case.vi.
- Remove the existing Case structure:
Right-click anywhere on the Case structure frame / Select Remove Case
Structure. Then LabVIEW asks you if you want to remove the contents
of all other cases than the one shown presently, and you can answer Yes to
this question.
- Remove broken wires using the keyboard shortcut Ctrl + B.
- Add a Case structure to the Block diagram: Right-click
where you want the upper left corner of the Case structure to appear / Open the
Structures palette on the Functions palette / Select Case Structure on the
palette. Then drag the Case structure so that it gets a proper size, cf.
the figure above showing the Block diagram.
Note: Ensure that all elements of interest actually appears, i.e. are
visible, inside the Case structure. If an element is not visible inside
the Case frame, the element is not functionally inside the frame!
- Ensure that there is a String constant with proper content in each of
the two frames. Wire signals according the the Block
diagram.
- Open the front panel of your VI. Save, and run the VI. Does it work?
|
Finally, some general comments to the Case structure:
- In our example, case.vi, the Case structure
Labels have Boolean values, True and False, and the Selector must then recieve
a Boolean value, too. However, you may use other data types for the Labels and
the Selector, for example an integer type or an enumerate type, making it
possible to select among any number of alternative cases. Once you wire say an
integer signal to the Selector, the Label is automatically adopted to that
data type, getting Labels 0, 1, 2.
- You can edit the Case structure (e.g. removing cases, adding cases,
swapping cases, defining a given case to be Default, which means that this
case is active if the there is no Label match the present Selector value.
[Table of contents]
8.2 For loop
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:
- The N terminal of the For loop defines the number of execution cycle
times. N is set to 5 on the Front panel.
- For demonstration purposes I have placed a Metronome (the Wait Until Next
ms Multiple function) with Wait time of 1000ms = 1s inside the For loop to
make it possible for the user to follow the changes of the values on the Front
panel as the VI runs.
- I have put yellow colour to the For loop just to highlight the loop.
- The i terminal is the For loop index. Its value increases by one, starting
from zero, each time the For loop executes. The i terminal
is available for you to use in your code.
- Arr1 is an array constant which I have manually fed with values.
- The array enters the For loop via two Loop tunnels which are set up in
different ways, just to demonstrate important features of For loops:
- Via a Loop tunnel with setting Enable indexing. In this case, what
comes out from the tunnel is
the value (a scalar) of the array element having the same index number as the
present value of the For loop index. Thus, in our VI the For loop
automatically reads successive array elements as the For loop executes and
displays the element in the x indicator on the Front panel. Since the
- Via a Loop tunnel with setting Disable indexing. In this case, what
comes out from the tunnel is the entire array. In our VI the entire array is
displayed in the Array1 indicator on the Front panel.
You may want to try adding a For loop to a VI yourself:
- Save for_loop.vi as
my_for_loop.vi (in any folder you prefer).
- Open the block diagram of my_for_loop.vi.
- Remove the existing For loop:
Right-click anywhere on the For loop border / Select Remove For Loop.
- Remove broken wires using the keyboard shortcut Ctrl + B.
- Add a For loop to the Block diagram: Right-click
where you want the upper left corner of the For loop to appear / Open the
Structures palette on the Functions palette / Select For Loop on the
palette. Then drag the For loop so that it gets a proper size, cf.
the figure above showing the Block diagram.
Note: Ensure that all elements of interest actually appears, i.e. are
visible, inside the For loop. If an element is not visible inside the
loop, the element is not a part of the loop!
- Wire signals according the the Block
diagram. Set the upper Loop tunnel with Enable Indexing and the other
tunnel with Disable Indexing by right-clicking on the tunnel.
- Open the front panel of your VI. Save the VI. Run the VI. Hopefully, the VI works correctly.
|
[Table of contents]
8.3 Shift register. Feedback node
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.
Shift register
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):
- Save shiftregister.vi as
my_shiftregister.vi (in any folder you prefer).
- Open the block diagram of my_shiftregister.vi.
- Remove the existing Shift register:
Right-click on the right (or the left) Shift register terminal on the
While loop / Select Remove All from the menu.
- Remove broken wires using the keyboard shortcut Ctrl + B.
- Create a Shift register: Right-click on
the right (or the left) border of the While loop / Select Add Shift
Register from the menu.
- Wire signals to the Shift register, cf. the
Block diagram of shiftregister.vi.
- Open the front panel of your VI. Save the VI. Run the VI. Hopefully, the VI works correctly.
|
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
Feedback node
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:
- By replacing an existing Shift register with a Feedback node
- By inserting a Feedback Node from scratch
Let us try both ways.
Creating a Feedback node by replacing an existing Shift register:
- Save shiftregister.vi as
my_feedback_node.vi (in any folder you prefer).
- Open the block diagram of
my_feedback_node.vi.
- Replace the existing Shift register with a Feedback node:
Right-click on the right (or the left) Shift
register / Replace with Feedback node.
- The wires may overlap. To make the individual wires more visible, move
the Feedback node and the wires into and out from the Feedback node
somewhat.
- Open the front panel of your VI. Save the VI. Run the VI. Hopefully, the VI works correctly.
|
Creating a Feedback node from scratch:
- Save shiftregister.vi as
my_feedback_node_scratch.vi (in any folder you prefer).
- Open the block diagram of
my_feedback_node_scratch.vi.
- Remove the existing Shift register:
Right-click on the right (or the left) Shift register terminal on the
While loop / Select Remove All from the menu.
- To create a Feedback node from scratch: Open the Functions palette.
Copy a Feedback node from the Structures palette into the Block diagram
and place it as in shown in this figure.
- Wire a constant of value 2 to the Initializer terminal to the left.
- Open the front panel of your VI. Save the VI. Run the VI. Hopefully, the VI works correctly.
|
[Table of contents]
8.4 SubVIs
What is a SubVI?
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 selecting
in 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.
How to add an existing SubVI to the Block diagram
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.)
- Download
main_vi.vi to any folder you prefer. (This VI misses some code
in the Block diagram, but we will now add what is missing.)
- Open the block diagram of main_vi.vi.
- Download the SubVI sub_vi.vi to the same
folder as above (however, you may download the SubVI to some other folder
if you prefer).
- Add sub_vi.vi to the Block diagram
of main_vi.vi
as follows: Right-click in the Block diagram
where you want to place the SubVI, see the
figure of the resulting Block diagram above / Choose Select a VI in
the Functions palette / Browse to downloaded sub_vi.vi, and drop it into
the Block diagram.
- Connect wires to the SubVI connectors as shown in
the figure of the resulting Block diagram
above.
- Save the VI. Open the front panel. Run the VI. Let's hope it works...
|
How to create a SubVI
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:
- Manually: You start with a blank VI and
develop it further as a SubVI.
- Automatically: You start with some existing
Block diagram code that you want to put into a SubVI, and tell LabVIEW to
create a SubVI based on that code.
In the following only the automatic method will be described in detail.
Let us try to create a SubVI using the automatic method:
- Download level_measurement.vi to any folder
you prefer, and rename it as my_main_vi.vi.
- Open the block diagram of my_main_vi.vi.
- Mark the code that will be put into the
SubVI, see the figure below.
Marking the code that will be put
into a SubVI
- Let LabVIEW create the SubVI automatically with the following menu
selection: . The figure
below shows the resulting Block diagram.
The Block diagram of the main VI with
the SubVI with the preliminary name Untitled 3
- The SubVI automatically gets a name as Untitled 3 (or some other
number). Open the SubVI by double-clicking on it, and then save it with
the name e.g. sub_vi.vi. The name of the SubVI in the Block diagram is
then updated accordingly, see the figure below.
Block diagram containing the SubVI
named sub_vi.vi
- Save the VI. Open the front panel of the VI. Run the VI. It works, hopefully.
|
Here are a few additional comments to SubVIs:
- File saving:
- The ordinary menu just saves
the main VI.
- To save also the SubVI(s), select the menu .
- To save all the involved files (here: the main VI and the SubVIs) into
one LabVIEW Project file (however, LabVIEW project is not described
here): , thereby opening a
Save As dialog window. In this dialog
window, select the Duplicate hierarchy to new
location option, etc.
- Editing the SubVI icon: Open the Icon
Editor as follows: Right-click the SubVI icon which is the icon at the upper
right corner of the Front panel window, and then select
in the context menu, see the figure
below. Now you can edit the icon using the graphical tools at the left part of
the editor.
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.
[Table of contents]
8.5 Logfile writing and reading
Introduction
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.
File formats
The file formats that is supported by the above two File I/O functions are:
- TDMS (Technical Data Management - Streaming) which is are binary files in an
internal LabVIEW file format. The
TDMS format gives more effective and accurate data storage than the LVM format. TDMS files may be
opened in LabVIEW, of course, and in NI DIAdem which is a software tool for
for managing, analyzing, and reporting data in logfiles. TDMS files can save
data in an organized way using a number of Groups and a number of Channels
within a given Group. (The older TDM file format is still supported.)
- LVM (LabVIEW
Measurement) which are text files that contain data that can be read by a human being
because numbers are
represented as text. For example, the number 0.231 is stored as the text (or
string) "0.231". A large benfit of storing numerical data in the text format
is that the file can be opened and displayed in any tool that supports text
files, e.g. MS Word, Notepad, Excel, Matlab, Web browsers. (It may be
necessary to change the file name extension from lvm to e.g. txt or dat before
opening the file in such tools). Thus, text files
provides great portability. However, the text files will be larger than if the
data were written in a binary format, and the data are stored with less
accuracy, but these issues are not important on ordinary PC's, but may be
important on dedicated computers with limited storage.
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.
Writing data to and reading data from a TDMS file
Writing data to a TDMS file
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
Comments:
- The TDSM Write function writes the data continuously to the file.
(It contains a data buffer so that the While loop is not unnecessarily delayed
by the file writing.)
- The Case structure is used to start, and stop, the file writing.
(In the figure the True case is shown. The False case does not contain any
code except the file path wire from the input tunnel to the output tunnel.)
- Three signals - t, u, y - are collected with a Merge Signals function
(which is on the Express / Signal Manipulation palette) before they are
fed into the the TDMS Write function.
- The channel group name and the channel names are written to
the logfile together with the data. This is convenient for later use of the
data, e.g. when opening the data in the DIAdem tool.
- In this example the points of time generated by the Elapsed Time
function are saved as data together with the values of u and y. (If the data
to be saved are Waveform data the time information is contained in the data,
and then it is not necessary to define the time signal explicitely as in the
example above.)
Reading data from a TDMS file
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.
TDMS Viewer
Here are comments to read_from_tdms_file.vi:
- The VI does not contain any While loop since the read from file operation
is done once. (This is the typical situation.)
- The TDMS Read function (on the File
I/O palette) reads data from the logfile. Note that this function has
outputs containing the group name and the channel(s) name(s).
- The data that are read from the file are converted to a ordnary
2-dimensional array containing the three columns of data (t, u, y). The convertion is made by the Convert from Dynamic Data
function which is on the Express / Signal Manipulation palette. This
conversion function can be configured (i.e. selecting the correct data type)
by double-clicking it, cf. the dialog window shown in the figure below.
- The three Index Array functions are used
to extract each of the columns from the data and create 1 dimensional arrays,
labeled Array t (1D), Array u (1D) and Array y (1D).
- The Block diagram also contains code for displaying the array in an array
indicator and to display the size of the array on the Front panel.
- The TDMS Viewer function is a graph tool for quickly plotting the
channels in the logfile.
Dialog window of the Convert from Dynamic Data
function
[Table of contents]
Writing data to and reading data from a LVM file
Writing data to a LVM file
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
Comments:
- In this example the data are writting to a file with the Write To
Measurement File function, which is an Express VI. (The Write to
Spreadsheet File function could also have been used.)
- The boolean (switch) Start_stop_file_writing is used to start
(enable) and stop the continuous file writing. This boolean terminal (control)
is connected to the Enable input to the Write To
Measurement File function.
- Two signals, u and y, are collected with a Merge Signals function
(which is on the Express / Signal Manipulation palette) before they are
fed into the the Write To Measurement File function.
Regarding writing the time signal, cf. the comments about the X Value Columns
in the list of comments below.
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:
- File Name box: Here you can select the file name, but if you have
wired a file name control (or constant) to the File Name input to this Express
VI, the wired name is used instead (this is the case in the example above).
- File Format radio buttons: Here you select the data format: LVM or
TDMS. (The TDM file format also available is a simpler and not so flexible
data format. TDMS is a newer format.) It seems it is not possible to change
the LVM file name extension into e.g. TXT (but you can of course do this
change manually using Windows Explorer).
- Segment Headers: By selecting No headers the files will
contain just plain data, which is convenient if you plan to import your data
into tools as Excel, Matlab, etc. If you choose to have header(s), you will
have to remove the header (perhaps manually) from the data file before you can
load the file into tools mentioned above.
- X Value Columns: The X values are actually the time stamps. By
selecting One column only the file will contain one left column
containing the time stamps. These time stamps are recorded and written to the
file automatically, so you do not have to create any time signal yourself.
- Delimiter: The delimiter is the character that separates columns.
- File Description: The text you write here will appear in the header
of the file, unless you have selected the No headers options mentioned
above.
- Note: There is no way to define the group name or the channels names for
the data in the Write to Measurement File Express VI.
- You may wonder why I did not use the Write To
Measurement File function for writing the data in TDMS format (cf. the
previous section). In stead I used the TDMS Write function. The reason
is that the channel group name and the channel names can not be defined in the Write To
Measurement File function, but if this is not an issue, you may of
course use the Write To
Measurement File function for writing TDMS data.
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
Reading data from a LVM file
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:
- The VI does not contain any While loop since the read from file operation
is done once. (This is the typical situation.)
- The Read From Measurement File
function reads data from the logfile. (By double-clicking the function a
dialog window is opened. The parameters in this window should be
self-explanatory.
- The data that are read from the file are converted to a ordnary
2-dimensional array containing the three columns of data, cf. the logfile1.lvm
shown above. The conversion is made by the Convert from Dynamic Data
function which is on the Express / Signal Manipulation palette. Note
that you can configure the Convert from Dynamic Data function (i.e.
selecting the correct data type) by double-clicking it.
- The three Index Array functions are used
to extract each of the columns from the data and create 1 dimensional arrays,
labeled Array t (1D), Array u (1D) and Array y (1D).
- The Signal indicator is a Graph indicator which is created
by right-clicking on the signal line out from the Expresss VI. The x-axis
shows the time-index (integer). You can configure the Graph via its Property
dialog window so that the x-axis shows time in secons (or minutes etc.).
- The Block diagram also contains code for displaying the array in an array
indicator and to display the size of the array on the Front panel.
8.6 Displaying and analyzing data in DIAdem
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
[Table of contents]
8.7 Plotting in graphs
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.vi. 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:
- The XY Chart indicator is available on the
Controls palette / Graph subpalette.
- In the Block diagram of graph.vi the XY Graph
terminal is placed outside the While loop. This is necessary since the Chart
is to be updated once the While loop has stopped. (The Chart terminal is
however placed inside the While loop since it is to be updates as the While
loop runs.)
- The arrays of y, y_max, y_min, and t are generated in the respective
tunnels on the right border of the While loop. These tunnels are set to
"Enable Indexing" (this is set by right-clicking on the tunnel and selecting
Enable Indexing in the context menu).
- Bundle functions are used to bundle the
t array with the y array, the t array with the y_max array, and the t array
with the y_min array. The output of a Bundle
function is a cluster. Then, these three clusters are collected using a
Build Array function. The resulting array
of clusters of arrays (!) is then wired to the XY Graph terminal.
[Table of contents]
8.8 Structuring VIs using parallel While loops
Often your application implements a number of different tasks to be executed
simultaneously, e.g.
- Reading measurement signals from input devices
- Signal processing, e.f. lowpass filtering
- Simulation
- Calculation of a control signal using a feedback controller, e.g. a PID
controller
- Writing control signals to output devices
- Plotting data in charts on the Front panel
- Saving data to a file
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.
[Table of contents]
8.9 Text-based (C-) programming using Formula node
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_measurement.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:
- Save level_measurement.vi as
my_formula_node.vi.
- Open the block diagram of my_formula_node.vi.
- Remove the Subtract function and the Multiply function.
- Insert a Formula node from the palette in the same area where these functions
were, cf. the figure above.
- Take a brief look at the Help information about the Formula node:
. After reading close the Help window.
- Add an input to the Formula node: . Give the input the name
K by typing "K" at the input tunnel.
- Add two more input named u0 and u, respectively.
- Add two outputs on the right border of the Formula node with names z
and y, respectively.
- Type the textual code inside the Formula node, cf.
the figure above. Remember to end each
expression by semicolon.
- Wire the proper signals to the three inputs, ans wire the output y to
the proper elements, cf. the figure above.
- Save the VI. Run the VI, and adjust the elements on the Front panel. The VI should
behave as before.
|
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:
float z;
z = u-u0;
y = K*z;
[Table of contents]
8.10 Text-based (Matlab-like) mathematics using MathScript
MathScript is a LabVIEW tool for executing textual mathematical commands
or expressions equal to Matlab. Please see the
Introduction to MathScript.
[Table of contents]
8.11 Generating documentation of your VI
You can create a documentation of your VI with various outputs:
- Paper print
- An HTML file which may be shown in any Web browser. Graphics show the
Front panel and the Block diagram of the VI. You can edit the resulting HTML
file in any HTML browser, e.g. Microsoft's FrontPage. The graphics files are
ordinary graphic files.
- A RTF document which can be opened in various editing tools, e.g.
Microsoft Word and Scientific Word.
- A plain text document containing no graphics.
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 the menu. 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:
- Open the level_measurement.vi VI and save
it in any folder.
- Run the VI for a while, and then stop it.
- Generate an HTML document showing the Front panel and the Block
diagram. Select PNG as Image format (see the figure above) and True color
as Color Depth. (There are many dialog windows where you have to select
the proper options. I think the options are quite obvious.). Save the HTML
document in any folder you want.
- Open the HTML document in our browser. It should appear as in
this figure.
|
[Table of contents]
8.12 LabVIEW projects
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.
[Table of contents]
8.13 How to make parts of block diagram disabled?
In some situations it is useful to disable parts of the block
diagram of a VI at development time. Here is a typical example: Assume that the
VI contains functions for data acquisition from an I/O device, and you want to
test your VI without having the device connected to the PC. LabVIEW will then
refuse to run the VI unless you disable the data aquisition code, and it is not
enough to just put the code inside a case of a Case structure.
You can disable, and enable, a part of the block diagram using
the Diagram Disable Structure which is on the Programming / Structures
palette on the Functions palette. You can also use the Conditional Disable
Structure, which is more flexible, but it can be used only when the VI is
included in a LabVIEW project. (The Conditional Disable
Structure is not described in this tutorial.)
As a very simple example, the figures below show the Front panel
and the Block diagram (there are two pictures of the Block diagram) of a simple
VI which gives the user the opportunity to select among real measurement as
acquired with the DAQmx Read function and simulated measurement as
adjusted with the vertical pointer slide named measurement_sim on the
Front panel.
Front panel of
disabled_structure.vi
The Enable subdiagram of the
Diagram Disable Structure in the Block diagram of
disabled_structure.vi
The Disable diagram of the
Diagram Disable Structure in the Block diagram of
disabled_structure.vi
How can you - the programmer - select among the Enable
subdiagram and the Disable subdiagram? You do it by right-clicking on
the label on the top of the Diagram Disable Structure, and selecting the proper
option in the which opens a menu with several options.
Note that you can add several subdiagrams, but only one of these
can be Enabled at any instant of time.
Also note that you can not select among Disable subdigrams and
Enable subdiagrams at run-time. If you need to do that, you may use a Case
structure with proper number of structures.
[Table of contents]
Finn's LabVIEW Page |