Scripting Languages and LabVIEW
In the scientific and academic community, it’s common to use scripting languages for analysis and visualization. NI LabVIEW software offers different options for using scripting languages such as The MathWorks, Inc. MATLAB® software (.m file scripts), CGI, XML, Xmath, INRIA Scilab, and others within the LabVIEW development environment in combination with G graphical programming. Many of the most popular scripting languages (Python, PHP, Perl, Tcl, and so on) on the market offer different methods of connectivity such as a C language API, and there is always a way to integrate them with LabVIEW.
But What’s a “Scripting Language”?
A common definition is that “a scripting language, script language, or extension language is a programming language that controls a software application” [1]. Programs written with a “scripting language” are usually command-based and interpreted (not compiled), a reason for being typically slower than programs created in C, C++, G, and other programming languages that execute compiled (and optimized) code. Scripting languages usually follow a specific syntax, and rules are not very flexible in some cases. However, because these scripting languages are text-based and many of them are based on open standards that use simple rules and commands for executing complex tasks, they have a space in the academic and scientific community. They are also very popular in Web page creation and administration, so computer science and IT professionals commonly use them in Web-related projects.
Consider the options available within the LabVIEW development environment, which allows a developer to use a hybrid approach when creating code: the combination of the G graphical programming language with text-based commands or scripts. These are usually implemented within the LabVIEW development environment through a “node” in which inputs/outputs (variables or constants) are defined to “connect” the node with the G programming language. Within the node, you can use a set of script commands to acquire, analyze, or visualize data. The basic architecture of a script node in LabVIEW is shown in Figure 1. Usually, inputs and outputs are optional but recommended to have some level of interactivity with LabVIEW and the G programming language.
Figure 1. Basic “Architecture” of a Script Node in LabVIEW
Node Options for Using Scripting Language in LabVIEW
You can choose from the following six options to use scripting languages within the LabVIEW development environment:
- Formula node
- Expression node
- LabVIEW MathScript node
- Xmath script node (requires NI MATRIXx)
- MATLAB script node (requires MATLAB)
- INRIA Scilab script node (requires INRIA Scilab)
You probably use script nodes to execute math scripts in LabVIEW. The LabVIEW MathScript, MATLAB, Scilab, and Xmath script nodes are similar to the formula node, but you can use them to import an existing LabVIEW MathScript script written in the MATLAB language syntax or an existing Xmath script in ASCII form and run the script in LabVIEW. As with a formula node, you can pass data to/from the node.
Expression Node
Use the expression node to calculate expressions that contain a single variable. Expression nodes are useful when an expression has only one variable, but they are otherwise complicated.
Expression nodes use the value you pass to the input terminal as the value of the variable. The output terminal returns the value of the calculation. The expression node may not be considered a scripting language, but you can use a small subset of functions and operators directly on the “wires” that connect the nodes in G. It is included here because it is a text-based (expression) option available in LabVIEW.
Consider this simple expression:
x * x + 33 * (x + 5)
The following block diagram uses numeric functions to represent this expression.
Figure 2. Using Numeric Functions to Represent an Expression
You can drastically simplify this code using the expression node. The following code replicates the same functionality of the code shown above:
Figure 3. Using Expression Node to Simplify Code in Figure 2
The expression node supports some polymorphism. The input terminal of an expression node is the same data type as the control or constant you wire to it. The output terminal is the same data type as the input terminal. The data type of the input can be any noncomplex scalar number, array of noncomplex scalar numbers, or cluster of noncomplex scalar numbers. With arrays and clusters, the expression node applies the expression to each element of an input array or cluster.
Formula Node
The formula node is much more extensive and powerful than the expression node and may also not be considered a “scripting language” because it is more oriented toward formula solving and limited to a set of functions and operators. However, it is included in this list because of its similarities with other scripting languages (and with the C language syntax) and because it is a convenient text-based node that you can use to perform mathematical operations on the LabVIEW block diagram.
You do not have to access any external code or applications, and you do not have to wire low-level arithmetic functions to create equations. In addition to text-based equation expressions, the formula node accepts text-based versions of if statements, while loops, for loops, and do loops, which are familiar to C programmers. These programming elements are similar to those you find in C programming but are not identical. Formula nodes are useful for equations that have many variables or are otherwise complicated and for using existing text-based code. You can copy and paste the existing text-based code into a formula node rather than recreating it graphically.
Formula nodes use type checking to make sure that array indexes are numeric data and that operands to the bit operations are integer data. They also check to make sure array indexes are in range. For arrays, an out-of-range value defaults to zero, and an out-of-range assignment defaults to indicate no operation occurs. Formula nodes also perform automatic type conversion. The formula node, shown in Figure 4, is a resizable box similar to the for loop, while loop, case structure, stacked sequence structure, and flat sequence structure. However, instead of containing a subdiagram, the formula node contains one or more C-like statements delimited by semicolons, as in the following example. As with C, you can add comments by enclosing them inside a slash/asterisk pair (/*comment*/) or by preceding them with two slashes (//comment).
Figure 4. Formula Node Example
In Figure 4, an integer (i) and an array (numArry) are first defined; then, a for loop is used for generating 100 random numbers, using the function rand(). Finally, the array is plotted using a LabVIEW waveform graph (see Figure 5).
Figure 5. Data Generated by the Formula Node Script
There is no limit to the number of variables or equations in a formula node, and you can declare and use a variable inside the formula node without relating it to an input or output wire. Variables can be floating-point numeric scalars, whose precision depends on the configuration of your computer. You also can use integers and arrays of numeric values for variables. One point to consider is that the formula node does not support complex data types or the matrix data type. An extensive list of commands and functions are listed in the LabVIEW manual for your reference.
The following code allows you to convert readings from a thermocouple type J (Iron-Constantan), one of the most common thermocouples along with type K thermocouple, and convert it to either the Celsius (C) or Fahrenheit (F) scale.
Table 1 summarizes the voltage-to-degrees coefficients for the NIST polynomial equation:
T = a0 + a1V + a2V2 + ... + anVn
Table 1. Voltage-to-Degrees Coefficients for the NIST Polynomial Equation
For simplicity and demonstration purposes, the following formula node implementation only focuses on the 0 to 76 °C temperature range and the conversion of the voltage (mV) generated by the thermocouple, ignoring the cold-junction compensation (CJC) reading from a different source (usually a thermistor or IC).
Figure 6. Formula Nodes Used for Linearization of Type J Thermocouple Readings
As shown in Figure 6, two formulas nodes are used, one for the NIST polynomial (and coefficients) and another for the Celsius to Fahrenheit conversion (vase structure). A similar approach is used with the reference temperature measured with a thermistor or IC, and for the conversion to other temperature scales (Kelvin, Ranking).
LabVIEW MathScript Node
LabVIEW MathScript is a text-based (scripting) language that you can use to write functions and scripts for use in the LabVIEW MathScript window or MathScript LabVIEW node. The MathScript syntax is similar to the MATLAB language syntax. In LabVIEW MathScript, you generally can execute your scripts written in the MATLAB language syntax. However, the MathScript engine executes the scripts, and the MathScript engine does not support some functions that the MATLAB software supports.
The following table shows LabVIEW data types and the corresponding data types in the MATLAB, Xmath, and MathScript script nodes:
Table 2. LabVIEW Data Types and Their Corresponding Data Types in the MATLAB, Xmath, and MathScript Script Nodes
The LabVIEW MathScript syntax is similar to the MATLAB, Scilab, and Octave scripting language syntax, and the LabVIEW MathScript node has the following format:
Figure 7. LabVIEW MathScript Node Format
You can call functions from a LabVIEW MathScript node. If you call a function from a LabVIEW MathScript node that might change the LabVIEW MathScript search path list at run time or might introduce new variables at run time, a warning glyph (yellow sign), appears on the LabVIEW MathScript node frame.
The following example uses a function for calculating the Fibonacci numbers as specified by the user. First, the G code version is presented; then, the LabVIEW MathScript version is presented (including the “warning glyph” of yellow color).
The Fibonacci numbers are computed according to the following algorithm:
Figure 8. Fibonacci Number Algorithm
The G programming language version is as follows:
Figure 9. Calculating Fibonacci Numbers in G Code
The LabVIEW MathScript code is as follows:
Figure 10. Calculating Fibonacci Numbers in LabVIEW MathScript
The warning glyph in Figure 10 indicates a change of the path used for the *.m file. It is important to emphasize that the LabVIEW Run-Time Engine does not support LabVIEW MathScript nodes with warning glyphs. If a VI includes a LabVIEW MathScript node with a warning glyph, you must remove the warning glyph from the LabVIEW MathScript node before you build a stand-alone application or shared library.
The above LabVIEW MathScript node calls a function F, which is saved in a *.m file with the following script:
You can create his file using the Interactive MathScript Window or an ASCII text editor such as the Microsoft Windows Notepad.
You also can combine the use of LabVIEW MathScript nodes with specialized LabVIEW modules or toolkits, for example, the state-space, transfer function, and zero-pole-gain models of the LabVIEW Control Design and Simulation Module with the LabVIEW MathScript node (see Figure 11) and the Xmath script node. You must have the LabVIEW Control Design and Simulation Module installed to use these models.
Figure 11. LabVIEW MathScript Node Format
Scilab Script Node
Scilab is a numerical computational package developed by researchers from the INRIA and the École nationale des ponts et chaussées (ENPC) in France [1]. Since the creation of the Scilab consortium in May 2003, Scilab has been developed and maintained by the INRIA. It is a high-level programming language because most of its functionality is based on the ability to specify many computations with few lines of code. It does this primarily by abstracting primitive data types to functionally equivalent matrices.
The Scilab license permits commercial and noncommercial distribution/use of unmodified versions of Scilab free of charge and noncommercial distribution/use of modified versions (which must include source code).
As was briefly mentioned above, the Scilab script node uses a syntax similar to MATLAB, Octave, and Xmath. To use this node, you need to install the Scilab scripting language on your computer. Also, you have to install the Scilab LabVIEW Gateway software available free of charge at ni.com as described in this developer's zone document. Once you install the software on your PC, the following menu is available within the LabVIEW development environment:/
Figure 12. Scilab Script Node Example
If you use LabVIEW applications and compiled code (EXEs) that incorporate the Scilab script node, you must make sure that the corresponding library of functions is installed on the machine where the application will run. LabVIEW depends on a DLL to connect to Scilab. As such, target machines that run stand-alone applications built with LabVIEW and that use the Scilab script node must include this DLL in the DLL support folder of the LabVIEW Run-Time Engine.
Related Links
Learn more about LabVIEW MathScript
Download and install the LabVIEW / Scilab Gateway Software for the Scilab Script node
Scientific Computing with Graphical System Design
Scientific Computing with NI LabVIEW
MATLAB® is a registered trademark of The MathWorks, Inc.
Reader Comments | Submit a comment »
Legal
This tutorial (this "tutorial") was developed by National Instruments ("NI"). Although technical support of this tutorial may be made available by National Instruments, the content in this tutorial may not be completely tested and verified, and NI does not guarantee its quality in any way or that NI will continue to support this content with each new revision of related products and drivers. THIS TUTORIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND AND SUBJECT TO CERTAIN RESTRICTIONS AS MORE SPECIFICALLY SET FORTH IN NI.COM'S TERMS OF USE (http://ni.com/legal/termsofuse/unitedstates/us/).
