Introduction
Virtual Instruments can be structured differently, and it is common to do so depending upon the requirements of the VI. In this lesson, we shall introduce three common architectures and discuss some of pros and cons of each:
- The simple VI architecture.
- The general VI architecture.
- The state machine architecture
The Simple VI Architecture
When creating a simple VI that is only required to perform a very basic and well understood function, a complicated architecture is generally not necessary.
An example may be a VI that takes some inputs from the user, perform some mathematical manipulation on them and then displays the results to the user.
In this example, the VI is executed using the RUN button in the LabVIEW Toolbar and stopped using the ABORT EXECUTION button.
In addition to being adequate for simple tasks, this architecture may also be suitable for VIs that are being created to act as subVIs within a larger application.
Generally, if the VI requires a more complex initialisation procedure (e.g., opening files, initialising hardware, etc.) or a closedown procedure (e.g., saving and closing files, etc.) then the general VI architecture might be more appropriate.
The General VI Architecture
A very common VI architecture is one in which the programme goes through three distinct phases of execution: initialisation, main body and closedown.
- Initialisation – in this first phase the programme may need to open and read configuration files, initialise hardware or prompt the user for required input data.
- Main Body – this second phase executes the main functions of the VI. This section of code is generally encapsulated within a loop so that when the user terminates the program (using a STOP button control or similar), the VI passes control to the Closedown
- Closedown – the final phase of the programme takes care of closing files, modifying configuration files, resetting hardware, reporting results, etc.
In complex General VI Architectures, the order of execution may also be controlled using a ‘Flat Sequence Structure’ to ensure that each phase is executed fully before passing control to the next phase.
General VI Architectures may be determined to be adequate where the order of each phase is expected to be the same on each execution of the VI. Where the execution may vary depending on user interaction, the State Machine Architecture might be more appropriate.
The State Machine Architecture
The State Machine Architecture is one of the most common design implementations for complex LabVIEW applications. Generally, developers turn to State Machine Architectures where various distinguishable events (aka. states) exist or where the order of execution of these states is not the same on each run of the VI. As with the General VI Architecture, the state machine is often started in an Initialisation state and ended by a Closedown state but the number and order of various states between these two are normally variable.
A State Machine Architecture often consist of a Case Structure embedded within a WHILE loop, the Case Structure containing each state of the VI.
In the example presented here, a basic calculator has been produced that contains a different state for each calculation.
The state is controlled by an ‘Enum Control’, which presents the user with a dropdown list of potential calculations. The VI by default may start in an Initialisation state but after that the user may be able to choose between various options: add, subtract, multiply, etc.
Some advantages of such an architecture are:
- The VI is flexible to move from any state to any other depending on the functioning of the VI.
- The VI is comparatively easier to modify and debug as errors are contained with a single sub-diagram of code instead of amongst a large, sprawling Block Diagram.
Some disadvantages of such an architecture are
- The VI is more complex to develop.
- There could be additional bugs introduced by the VI failing to move correctly to a new state or pass data between states.
Exercise 1: Build a State Machine Calculator
In this exercise, students should design a calculator that takes two numbers from the user and provides a result of a calculation applied to those numbers, the calculation selected in accordance with a user input.
- The calculator should be able to undertake the following calculations: A+B, A-B, AxB. A÷B, A2 and B2.
- The calculator should provide an error message to the user if any calculation is invalid, such as dividing by zero.
- The user should be able to select the calculation using a Enum Control drop down menu placed on the Front Panel.
- The user should be able to abort the application by pressing a STOP button placed on the Front Panel.
National Instrumentation’s online reference material pertaining to State Machine Architecture (http://www.ni.com/tutorial/3024/en/) should provide further guidance where needed.