Deploying Text-Based Math to Real-Time Hardware with the LabVIEW MathScript RT Module
Overview
NI LabVIEW is an interactive development platform built on graphical dataflow programming (G) that provides an intuitive flowchart representation for science and engineering applications. LabVIEW offers unrivaled integration with custom and commercial off-the-shelf hardware for test and measurement or embedded design applications. By offering a single environment that is a framework for combining graphical and textual code, LabVIEW gives you the freedom to integrate multiple approaches for programming, analysis, and algorithm development. LabVIEW MathScript adds math-oriented, textual programming to LabVIEW through a native compiler for .m files. This functionality, originally included in the LabVIEW Full and Professional development systems, has been reengineered for optimal performance in a real-time OS. NI made structural changes to the underlying MathScript engine to ensure the most optimized compiled code, which resulted in inclusion of this technology in the LabVIEW MathScript RT Module for LabVIEW 2009. This tutorial explores how you can use the LabVIEW MathScript RT Module to deploy your custom .m files to real-time hardware.
Table of Contents
Working with MathScript
The LabVIEW MathScript RT Module provides two interfaces for working with the MathScript engine. The MathScript Window offers an interactive interface in which you can load, save, design, and execute your .m files. It is designed for concept exploration through a command-line interface that allows you to enter commands one at a time or through the building of batch scripts in a simple text editor window. Figure 1 displays the MathScript Window, which you can access from the LabVIEW menus by choosing Tools»MathScript Window.
Figure 1. The MathScript Window
Read Part 2 of Developing Algorithms Using LabVIEW MathScript to learn more about using the MathScript Window.
The other LabVIEW MathScript RT Module interface is the MathScript Node, the blue rectangular region shown in Figure 2. With this node, you can run your .m file scripts from your VIs as well as insert textual algorithms into a VI and then use the LabVIEW graphical programming environment to instrument the scripts by adding knobs, slides, buttons, graphics, and other user controls and indicators.

Figure 2. The MathScript Node
Located on the Programming»Structures palette, the MathScript Node helps you integrate your .m files into a LabVIEW VI to combine the benefits of graphical and textual programming into one solution. Read Part 1 of Developing Algorithms Using LabVIEW MathScript to learn more about using the MathScript Node.
Debugging Your MathScript Code
The LabVIEW MathScript RT Module features a wide array of tools to help you debug your custom .m files when integrated into a LabVIEW block diagram.
LabVIEW MathScript Probe
With the LabVIEW MathScript probe, you can debug your LabVIEW applications by investigating unexpected results in the MathScript Node. You can use the LabVIEW MathScript probe to view the data in a script in a MathScript Node as a VI runs. The MathScript probe displays a list of all the variables you define in the script and previews variables you select. Deploy the MathScript probe by choosing Probe from the right-click menu of the MathScript Node.
Debugging Region
The MathScript Node contains full debugging capabilities, including the following:
- Breakpoints
- Single stepping
- Execution highlighting
The gray region on the left side of the MathScript Node, used to interact with these tools, displays the following:
- Red error glyphs next to lines of the script that contain an error
- Warning glyphs
- Breakpoints

Figure 3. Debugging with the MathScript Node
Simply hover next to the line you want to set or remove a breakpoint from and left-click with the mouse. Just like breakpoints in graphical code, the block diagram brings the specific line of code where the breakpoint exists into key focus when the VI pauses. You can then implement single stepping and execution highlighting using the buttons on the LabVIEW toolbar. The Error List window also lists the lines of code where an error exists. When you select the error within this window and click the Show Error button, LabVIEW highlights the line of script that contains the error.
Script Highlighting
Script highlighting uses colors to distinguish between different parts of a script in a MathScript Node. These colors improve the readability of the script and help you debug a script that contains errors or returns unexpected data. The MathScript Node offers two methodologies for highlighting your script.
Syntax highlighting distinguishes between different portions of your script based on the type of call. Built-in functions, user-defined functions, constants, operators, and variables can all have distinct colors associated with them. For example, you can use script highlighting to see when a user-defined function or a variable overrides a built-in MathScript function.
Data type highlighting distinguishes between different portions of a script based on their data type. Each data type, such as string, Boolean, integer, and double, has the same color associated with it as LabVIEW. The formatting of that color, such as italic, bold, or both, depicts the dimension of the data (scalar, 1D array, 2D array, matrix). You can simply click outside the node to apply colors and font attributes when data type highlighting is enabled.
To enable either syntax highlighting or data type highlighting for a particular MathScript Node, right-click inside the node and select Script Highlighting»Syntax or Script Highlighting»Data Types from the shortcut menu. Syntax highlighting is enabled by default. You cannot enable both syntax and data type highlighting for the same MathScript Node.
Deploying Your Code to Real-Time Targets
The newest feature of the LabVIEW MathScript RT Module is the ability to deploy custom your .m files to any LabVIEW Real-Time target, including NI CompactRIO, Compact FieldPoint, and PXI hardware. As depicted in Figure 4, deploying a math script to real-time hardware is as simple as dragging and dropping.

Figure 4. Deploying a .m file to Real-Time Hardware
Just like any LabVIEW VI, a LabVIEW project provides the interface for deploying to hardware. Simply save your VI and then drag it under the real-time target in your LabVIEW Project Explorer.
Unlike many third-party text-based math languages, there is no conversion or translation necessary when deploying your code to hardware. The LabVIEW MathScript engine compiles the embedded scripts, propagating strictly typed data throughout the underlying code. This typing and propagation allows the LabVIEW compiler to optimize the underlying LabVIEW code for the real-time OSs that run on NI real-time hardware. Because LabVIEW compiles the code instead of interpreting it, LabVIEW developers reap the benefits through edit-time error checking. The LabVIEW compiler is able to identify both semantic and syntax errors throughout the code prior to pressing the Run button.
LabVIEW MathScript RT Module Determinism
The phrase "deploying to real-time hardware" has different meanings for many developers. When the typical developer "deploys to real-time hardware", they expect a certain level of determinism. While a typical developer certainly can develop deterministic scripts with the LabVIEW MathScript RT Module, the 2009 release contains a subset of functions and operators that introduce jitter into the application that can be unacceptable for applications with real-time requirements. However, there is no real feedback to you from within the product indicating this one way or the other.
While only a subset of applications actually require the math script to be run in the time-critical loop, this functionality will help to further simplify the process of deploying your custom .m files to real-time hardware.
The Service Pack 1 release of the LabVIEW MathScript RT Module, targeted at February 2010, is expected to deliver full determinism. This includes:
- Core language determinism
- Functional determinism
- Guidelines for developing deterministic scripts
- In-node warnings for expected non-deterministic behavior
Contact your local Field Engineer with any questions, comments or concerns related to this functionality.
To learn how you can use the LabVIEW MathScript RT Module to develop real-time mathematics algorithms, read Developing Real-Time Math Algorithms with NI LabVIEW.
Additional Resources
Inside the LabVIEW MathScript RT Module
Developing Algorithms Using LabVIEW MathScript: Part 1 – The LabVIEW MathScript Node
Developing Algorithms Using LabVIEW MathScript: Part 2 – The LabVIEW MathScript Window
LabVIEW MathScript Example Gallery
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/).

