Functional Global Variable (FGV) Design Patterns in LabVIEW


Functional Global Variable (FGV) is a well-known design pattern in LabVIEW that allows data to be stored and accessed in a controlled manner, avoiding the problems associated with traditional global or local variables.

It is implemented using a non-reentrant, uninitialised shift register in a While Loop inside a non-executing (single-run) While Loop within a LabVIEW VI.

FGVs provide a more robust alternative to standard global variables as they prevent race conditions, allow controlled access, and provide encapsulation.


Why Use a Functional Global Variable?

FGVs are preferred over standard global variables for several reasons:

  • Prevention of race conditions – Unlike standard global variables, which allow uncontrolled access from multiple parts of a programme, FGVs provide controlled access using an action-based approach.
  • Encapsulation and modularity – Encapsulating logic within an FGV makes debugging easier and promotes modularity by localising data access.
  • Efficient memory management – Since an FGV is a VI, it runs only when called, maintaining data in shift registers rather than consuming memory continuously.
  • Better performance – Standard global variables create additional memory copies when read or written, whereas FGVs use a single memory space, improving efficiency.
  • Flexibility – Unlike traditional global variables, an FGV allows additional logic such as filtering, scaling, or logging while accessing the stored data.

Structure of a Functional Global Variable

A basic FGV consists of:

  • While Loop with an uninitialised shift register to preserve data between calls.
  • Case Structure to handle different operations such as reading, writing, or resetting the stored value.
  • Input and output terminals to pass data in and out.

A simple read/write FGV for storing temperature might have:

  • An input called “Action” that accepts values like “Read”, “Write”, or “Reset”.
  • Another input called “New Temperature”, which is only used when writing data.
  • An output called “Current Temperature”, which is only used when reading data.

Inside the VI, the While Loop contains an uninitialised shift register that stores a temperature value. The Case Structure controls what happens based on the selected action. If the action is “Write”, the new temperature is stored in the shift register. If it is “Read”, the stored value is returned. If it is “Reset”, the stored value is reset to a default temperature.


Types of Functional Global Variables

  1. Simple Read/Write FGV – Stores a single value and allows basic read and write operations. This is useful for storing global values like a temperature reading.
  2. Action Engine – Extends the FGV by adding extra operations such as increment, decrement, or logging. This is useful for keeping track of counts or statistics.
  3. Multi-Variable FGV – Stores multiple related values using a cluster inside the shift register. This can be used to store configuration parameters for a data acquisition system.
  4. Object-Oriented FGV – Uses LabVIEW classes to encapsulate data in an FGV-like structure. This is useful for managing multiple device configurations in a scalable way.

Best Practices for FGVs

  • Use enums instead of strings for selecting actions to avoid typos and improve type safety.
  • Keep FGVs non-reentrant to ensure there is only one instance handling the data.
  • Avoid excessive use of FGVs, as too many can lead to poor modularity.
  • Include error handling to ensure reliable data flow.
  • Do not use FGVs for large datasets, as they can be inefficient for storing images or large arrays.

Alternatives to FGVs

While FGVs are useful, other techniques may be more suitable in some cases:

  • Queues and Notifiers – Better for message-based communication between parallel loops.
  • Data Value References (DVRs) – Useful for protecting large datasets from unnecessary memory copies.
  • LabVIEW Object-Oriented Programming (LVOOP) – Provides better encapsulation and scalability than FGVs.
  • Global Variables – Can be used in simple applications where controlled access is not a concern.

Example: Implementing an FGV in LabVIEW

  1. Create a new VI in LabVIEW and add a While Loop with an uninitialised shift register.
  2. Add a Case Structure and connect an Enum control to the selector terminal.
  3. Define cases such as “Read”, “Write”, and “Reset”.
  4. In the “Write” case, store the input data into the shift register.
  5. In the “Read” case, output the stored value from the shift register.
  6. In the “Reset” case, reset the stored value to a default.
  7. Save the VI and use it in multiple locations.

Comparison: FGV vs. Global Variables

  • Encapsulation – Global variables do not encapsulate logic, while FGVs do.
  • Risk of race conditions – High with global variables, low with FGVs.
  • Memory efficiency – Poor with global variables, good with FGVs.
  • Custom logic – Not possible with global variables, but possible with FGVs.
  • Performance – Global variables create extra memory copies, while FGVs store data efficiently.

Conclusion

Functional Global Variables (FGVs) are an essential LabVIEW design pattern for managing global data efficiently while avoiding the risks of standard global variables. They improve memory management, encapsulation, and allow additional logic beyond simple read and write operations.


Leave a Reply

Discover more from Product Development Engineers Ltd

Subscribe now to keep reading and get access to the full archive.

Continue reading