http://techteach.no/labview/lv86/labview/
Introduction to
|
Activities are shown in blue boxes as this one. |
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. |
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. |
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:
Open the example level_measurement.vi in LabVIEW. Then play the video level_measurement_front_panel (17 minutes, the video will be opened in new window) which describes the operation and contents of the Front panel of this VI. |
It is assumed that you have the example level_measurement.vi opened in LabVIEW. Then play the video level_measurement_block_diagram (26 minutes, the video will be opened in new window) describes the Block diagram of this VI. |
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: .
It is assumed that the block diagram of level_measurement.vi is opened. Try the various
Help options:
|
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.
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. |
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.) |
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 languagescase.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.)
|
- 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.
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.
|
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):
|
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
Creating a Feedback node by replacing an existing Shift register:
|
Creating a Feedback node from scratch:
|
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.)
|
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.
Let us try to create a SubVI using the automatic method:
Block diagram containing the SubVI
named sub_vi.vi
|
- 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): Save As dialog window. In this dialog window, select the Duplicate hierarchy to new location option, etc. , thereby opening a
- 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.
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.
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.
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.
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
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 buttonWhen 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.
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;The VI is otherwise the same as level_measurement.vi.
y = K*z;
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:
|
z = u-u0;in stead of the somewhat simpler
y = K*z;
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;[Table of contents]
z = u-u0;
y = K*z;
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.
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:
|
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]
No hay comentarios:
Publicar un comentario