Local and Global Variables in LabVIEW for FRC
This example serves as an introduction to local and global variables, how they are used in the default LabVIEW for FRC Robot Project, and how you might want to use them in your project.
Local variables and global variables may be used to transfer data between locations within the same VI (local variables) or within different VI’s (global variables), breaking the conventional Data Flow Paradigm for which LabVIEW is famous. Thus, they may be useful when, for whatever reason, you cannot wire the value directly from one node to another.
Note: If you only want to pass data between consecutive loop iterations; Miro_T covered this in this post. It should also be noted that the feedback node in LabVIEW may be used as an equivalent to the shift register, although that may be a topic for another day!
Introduction to Local and Global Variables
Local Variables
Local variables may be used within the same VI. Create a local variable by right-clicking a control or indicator on your Front Panel:
You may create a local variable from the Structures palette on the block diagram as well:
Note that when using this method, you will be placing an undefined local variable onto the block diagram. Left-click it to select a front-panel object to correspond to:
In the same manner, you can left-click the local variable to reassign it to a different Front Panel object:
And finally, you can right click on the local variable and select ‘Change to Read’ or ‘Change to Write’ depending on if you want to read the value of the front panel object, or write a new value to it. We are able to perform both read and write conditions regardless of if the front panel object is a control or an indicator.
Global Variables
Global variables are created slightly differently. You can add one to the block diagram from the Structures palette:
Notice that when you double-click it from the block diagram, it opens a separate front panel. This front panel does not have a block diagram, but you can add as many entities to the front panel as you wish and save it as a *.vi file. Below, two string controls are added to the front panel of the global variable. After saving the VI, you can then select which controls or indicators we want to read or write from by left-clicking the global variable from any VI within the project:
Note: Be very careful to avoid race conditions when using local and global variables! Race conditions can occur when you have multiple parts of your code that change a particular value (in this case a local or global variable), but you don’t know which value the end result could be. In the example below, the value of X might be 7 one time, and it might be 3 another time… make sure you know what values you are actually inputting!
For a more thorough explanation of race conditions, see this help document.
How They are Used in the Default LabVIEW for FRC Robot Project
The following discussion uses the Default LabVIEW for FRC Robot Project that comes with the NI FRC Software download. You can find that project by opening LabVIEW and clicking on the ‘FRC roboRIO Project’ as shown below. Having these programs is not necessary to understand this section, but they are referenced.
In the Robot Main VI, global variables for “Enable Vision” and “Image Size” are written to during each its iterations…
…And are read from in each iteration of the Vision Processing VI:
This allows the user, when deploying to Robot Main VI from the LabVIEW Development Environment, to enable/disable vision and change the image size from Robot Main’s Front Panel. This showcases the usefulness of global variables because we can write to them with any VI in the project, and read from them with any VI in the project!
How Can You Use Them in Your Project?
There are endless ways to use local and global variables. If you want to pass data between structures that you can’t wire, you may find local variables to be useful. Or, you may find yourself in a situation where you have values within one VI that you want to access from another VI – this is a prime example of when you would want to use global variables!
For instance, we can look at the block diagram for the Periodic Tasks VI (shown below). Perhaps there is some value we have in the Teleop VI that we want to write. The Teleop VI runs whenever a TeleOp DS packet is received, and can be used to respond to joystick and Driver Station values. If we have a certain task we want to perform whenever the TeleOp DS packet is received, we can write to a global variable from within the Teleop VI, and read this value from within the Periodic Tasks VI. You can then decide what code or values to use in the Periodic Tasks VI, depending on the global variable (in this case, a boolean):
Additional Considerations
While local and global variables are easy to implement, they are still at risk for race conditions, which may output unexpected values. Generally speaking, local variables should be avoided unless absolutely necessary – if you can wire a value from node to node, it may be better!
To communicate data between parallel loops, we can use channel wires in place of local variables.
A more complex, but safer way of implementing global variables would be to use Functional Global Variables.
Conclusions
Congratulations! You now understand how to create local and global variables and how to implement them in LabVIEW. Please leave a comment if there’s anything that doesn’t make sense or if there’s something that we can improve on. We want to make our documentation as helpful as possible, and are constantly updating it.
Have fun, and good luck!