Example Code

Basic Functional Global Variable Example

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

Code and Documents

Attachment

Overview

This VI demonstrates a simple usage of Functional Global Variables in incrementing and displaying a number.

 

 

Description

Functional global variables are VIs that use loops with uninitialized shift registers to hold global data. A functional global variable usually has an action input parameter that specifies which task the VI performs.

The VI uses an uninitialized shift register in a While Loop to hold the result of the operation. The following illustration shows a functional global variable that implements a simple count global variable. The actions in this example are initialize, read, increment, and decrement.

 

Every time you call the VI (Call Functional Global.vi), the block diagram in the loop runs exactly once. Depending on the action parameter, the case inside the loop initializes, does not change, incrementally increases, or incrementally decreases the value of the shift register.  Although you can use functional global variables to implement simple global variables, they are especially useful when implementing more complex data structures, such as a stack or a queue buffer. You also can use functional global variables to protect access to global resources, such as files, instruments, and data acquisition devices, that you cannot represent with a global variable.

 

 

Requirements

LabVIEW 2012 (or compatible)

 

 

Steps to Implement or Execute Code

To make this example work with your code, or as a standalone application:

 

  1. Download the attached folder
  2. Open the "Functional Global Variable_LV2012_NI Verified.lvproj" Project
  3. Open the Call Functional Global Variable.vi
  4. Run the VI
  5. Initialize the value of the variable in the "Initialize to" control
  6. Click on Initialize to store the value in the buffer
  7. Click on Increment to Update the value in the buffer
  8. Click on Get to display the value stored in the buffer

 

Additional Information or References

FVG used a SubVI

FGV Usage BD.PNG

 

 Block Diagram of FGV
FGV BD.PNG

 

**This document has been updated to meet the current required format for the NI Code Exchange.**

Todd S.
LabVIEW Community Manager
National Instruments

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
Ben
Knight of NI Knight of NI
Knight of NI
on

RE; The AE shown above.

All controls and indicators should be on the root of the diagram and not inside the structures for best performance. Please see the "Clear as Mud"  thread to read where Greg McKaskle explained how LV can optimize performance when it can determine if data is un-modified in a sub-VI.

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
wildcatherder
Active Participant
Active Participant
on

How would Ben suggest modifying the example to implement this performance-enhancing stricture?  I have read the referenced discussion and I don't see how.  It seems the essential feature of the Functional Global Variable is the use of an uninitialized shift register.  I don't see any way to get the initial value into this example from outside the loop without initializing the shift register.

It is also not clear to me what makes this "global". My understanding is that there must be a convention that this variable will not be modified elsewhere, which is implied from the, presumably, all-inclusive list of Action cases.

Having downloaded the example, I see that it no longer looks like the image shown above. It has been modified to introduce the initial value from outside the loop.

Modified Functional Global Variable.png

Ben
Knight of NI Knight of NI
Knight of NI
on

http://forums.ni.com/t5/LabVIEW/Community-Nugget-4-08-2007-Action-Engines/m-p/503801W

Why did this document jsut change?

See the link above for my Nugget on Action Engines.

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
sumit04
Member
Member
on

Pros and cons if instead of while loop i used for loop with n connected to 1 

Best Regards
Sumit Memane
CLAD, CTD
crossrulz
Knight of NI Knight of NI
Knight of NI
on

sumit04,

They are pretty much equivalent.  Most people will recognize the While loop setup a lot faster than the FOR loop setup.  Personally, I prefer the "No Loop" option by using a Feedback Node.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
JRiggles
Member
Member
on

@crossrulz

 

Would you be willing to provide an example of a "no loop" FGV? A picture is fine, I'm just curious about this approach. Thanks!

crossrulz
Knight of NI Knight of NI
Knight of NI
on

@JRiggles,

I have an example at this article: A Look at Race Conditions


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
JRiggles
Member
Member
on

@crossrulz

Thanks a lot, I'll check it out