Academic Company Events NI Developer Zone Support Solutions Products & Services Contact NI MyNI

Document Type: Tutorial
NI Supported: Yes
Publish Date: Sep 15, 2008


Feedback


Yes No

Related Categories

Related Links - Developer Zone

Related Links - Products and Services

Transferring Data into and out of LabVIEW Statecharts

1 ratings | 4.00 out of 5
Print

Overview

This document describes methods for transferring data in and out of a statechart along with the advantages and disadvantages of each method, ending with a brief discussion on synchronizing triggers and data.

Introduction


The typical process for transferring data into and out of a statechart is to use the Inputs terminal and the Ouputs terminal of the Run Statechart function. However, you can use other methods to transfer data in and out of statecharts. These methods include shared variables, functional global variables, and references. While each method has its advantages and disadvantages, you might want to use one of these methods when the default process for transferring data in and out of statecharts cannot meet one of your application requirements.

 

Statechart Inputs and Outputs

The Run Statechart function has an Inputs terminal and an Outputs terminal for the linked statechart. The Inputs and Outputs terminals are type defined clusters that define the data that you pass into and out of a statechart. The data you pass into a statechart using the Inputs terminal is available in all areas of the statechart and must be defined before a statechart begins its iteration. The Outputs terminal returns the data when the statechart finishes its iteration, therefore this method does not allow you to transfer data in the middle of an iteration (unless you use a VI Server Reference).

Passing all data in and out of the statechart through the terminals can result in a very large Inputs.ctl and Outputs.ctl, which can lead to slow editing. Additionally, all data passes through the statechart with each iteration whether or not it is needed on a particular iteration. Even with these limitations, we recommend this method since it follows the dataflow paradigm, which leads to simpler debugging and minimizes the possibility of data access race conditions. If these limitations affect your application, consider using one of the following methods.

 

References


A reference is best used when you want to update values on front panel controls from within subVIs or statecharts. To update a front panel control using a reference, create a reference to the control and then pass this reference into the statechart as an input. Using a reference allows you to access the data without passing a copy of the entire data set into the statechart. You can configure the reference's properties and methods to update the referenced control directly. This means that you do not have to wait for the statechart to complete an iteration before the control updates. 

 

Shared Variables


A shared variable is a variable that can be any data type (including custom type definitions), lives in a LabVIEW project, and can be accessed by multiple VIs and from multiple computers. To share the shared variable data across a network, you can specify the shared variable type as a network-published shared variable.  

You can access a shared variable within a statechart directly, eliminating the need to pass data using the Inputs and Outputs terminals.  It is important to keep in mind certain considerations when using shared variables, namely race conditions when multiple people access the same variable, data update speeds, and the fact that shared variables are not supported on FPGA targets. Refer to Sharing Live Data Using Shared Variables for more information about using this feature in LabVIEW.

 

Functional Global Variables


A functional global variable is similar to a shared variable; however, a functional global variable is actually a VI that incorporates a While Loop and a shift register to retain values.  The structure of a properly defined functional global variable mitigates race conditions because the structure has specific access functions that control who can read and write to a variable (Get Value) and when the data is allowed to be written (Set Value). Figure 1 shows an implementation of the get value action for a functional global variable, and Figure 2 shows the set value action that allows you to write to the variable. You also can use this VI as a subVI and incorporate it into your main program along with a statechart. Unlike a network-published shared variable, which can be bound to a shared variable on another computer, the functional global variable is limited to a single computer.

 


[+] Enlarge Image

Figure 1. Functional Global Get State

 


[+] Enlarge Image

Figure 2. Functional Global Set State

 

 

Synchronization

An asynchronous statechart has an external trigger queue and it is this external trigger that begins a statechart iteration. To call an asynchronous statechart you first must call the Run Statechart function. This function populates the input data, but the statechart then waits for an external trigger to be sent to begin the statechart's iteration. In this architecture the input data might be out of synch with the trigger.

To synchronize the handling of triggers and the data that you pass to a statechart, you can use a synchronous statechart with your own external queue using the built-in LabVIEW Queue Operations functions. Refer to Figure 3 and Figure 4 for an example of this calling VI architecture.


[+] Enlarge Image

Figure 3. The top loop enqueues the trigger and data together.

 


[+] Enlarge Image

Figure 4. The bottom loop dequeues the trigger and data and sends them to the statechart.

Summary

This document describes several methods for communicating with a statechart. Shared variables, functional global variables, and references offer increased flexibility for your application development. Depending on your application's requirements, these alternative methods for transferring data into and out of a statechart might be useful. Refer to the attached statechart examples to further experiment with these concepts.

 

Related Links

LabVIEW Help: VI Server Reference

LabVIEW Help: Queue Operations Functions

LabVIEW Help: Sharing Live Data Using Shared Variables

LabVIEW Help: Functional Global Variables

LabVIEW Help: Run Statechart Function (Statechart Module)

LabVIEW Help: Statechart Iterations (Statechart Module)

LabVIEW Help: Synchronous and Asynchronous Statecharts (Statechart Module)

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/).