LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

State machine architecture

Hi, im still pretty new to labview, I have built a program that is functional just wondering how to make it better and more efficient. Someone told me to use a state machine architecture with my program but not too sure how to implement it/ if it would be beneficial.

Thanks 

0 Kudos
Message 1 of 16
(2,745 Views)

18 is pretty new. You might get better\faster response if you save to a previous version (or even a screenshot).

0 Kudos
Message 2 of 16
(2,740 Views)

First of all read this: Application Design Patterns: State Machines

 

Secondly, start by thinking about the stages your code goes through.  I immediately see "Start up", "Initialize Equipment", "Shutdown". "Take Readings", "Adjust Current".  I would also add a state with a dialog to let the user input the desired settings instead of waiting for the start button to be pressed.


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
Message 3 of 16
(2,710 Views)

what are the benefits of doing this? Essentially just rearranging my program.

0 Kudos
Message 4 of 16
(2,692 Views)

@newman3108 wrote:

what are the benefits of doing this? Essentially just rearranging my program.


The computer couldn't care less of course how things are arranged. "It works, why bother?" is heard often. But that "it works" for the computer (it does what it is told) doesn't mean "it works" for (other) programmers.

 

In general, rearrangements are useful if the changed code better reflect 'reality'. Afterwards, it better tell (other) programmers what the program is doing. It becomes more 'self documenting' (although it should of course follow from a design and be documented).

 

This is only important if you care about maintainability, readability, documentability (is that a word?), traceability, and programmers, including (future) you...

0 Kudos
Message 5 of 16
(2,661 Views)

@newman3108 wrote:

what are the benefits of doing this? Essentially just rearranging my program.


The biggest benefit, in my opinion, it is easier to maintain and add features for the inevitable code creep.

 

Most LabVIEW examples go through a series of Init, Set, Get, etc, all in a linear fashion. Not being a programmer, that is how I modeled my first programs. You hit the run arrow button and let the program go. It was always through the IDE interface, never an exe. In fact, it took me a few years to realize LabVIEW could make exes, I thought everything went through the IDE.

 

Those programs worked, but were a pain to modify. If you have a case structure with a bunch of states, it is much easier to modify the execution order of the states, and add/remove states. It is also easier to turn that type of architecture into an exe.

 

mcduff

0 Kudos
Message 6 of 16
(2,652 Views)

@newman3108 wrote:

what are the benefits of doing this? Essentially just rearranging my program.


Because someday sometime in the future someone might have to expand on your program.

 

And that someone could be YOU!

 

By using a proper architecture from the start, it makes scaling up or adding features a lot easier

 

 

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 7 of 16
(2,640 Views)

In retrospect, I haven't made a state machine in years. It's not the holy grail of architecture, but it is a nice 'step in' architecture.

 

Usually, a SM turns out to be a) an event driven loop or b) a sequence. If it is a sequence, I don't want to implement a SM but a sequence (not a FSS\SSS of course), for the same reason that when it is a SM I don't want to implement a bunch of loose code but a SM...

 

If I really need SM-like functionality, I go for the OO state pattern nowadays. Disclaimer: not the easiest OO pattern...

0 Kudos
Message 8 of 16
(2,600 Views)

One thing not mentioned yet: Normal LabVIEW code is just sequential - you program things to happen one piece after another - Part1 (P1), P2, P3, P4 etc..

If your code shall react on things while running, and then decide what part to execute next (either a result of an calculation or a button pressed), you need a state-machine architecture. This allows arbitrary reordering at run-time, e.g. P1, P2, P2, P3, P1, P4, etc...


Ingo – LabVIEW 2013, 2014, 2015, 2016, 2017, 2018, NXG 2.0, 2.1, 3.0
CLADMSD
0 Kudos
Message 9 of 16
(2,592 Views)

@ikaiser wrote:

If your code shall react on things while running, and then decide what part to execute next (either a result of an calculation or a button pressed), you need a state-machine architecture. This allows arbitrary reordering at run-time, e.g. P1, P2, P2, P3, P1, P4, etc...


'Need' is a bit strong. I do well without SMs. But if that is the situation, a SM would be a good fit.

 

Alternatively, you could simply put an event structure in a loop, skipping the traditional SM case structure and enum all together.

 

If P1, P2, P3 and P4 are not states, but VIs (or better: class methods), an event can simply call those VIs in the desired sequence... No more states required! At least not 'states' as in a traditional state machine. Or 'fake' states can be called with a user event or value signaling event.

 

An event structure in a while loop is of course still some sort of SM, as it is a 'machine', and has 'state' (classes or clusters in shift registers). But traditionally, a LabVIEW state machine is a case in a loop, and an type def enum to switch state.

0 Kudos
Message 10 of 16
(2,583 Views)