LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

State Machine initialisation question

Hi guys!

 

I'm interested to hear your thoughts on initialising values in a state-machine architecture. Is it preferable to initialise everything outside of the loop before anything is executed (i.e. in a sequence before the loop) or adding an initialisation state and run it as the first thing in the state-machine? Any caveats for any option?

 

Thanks! 

0 Kudos
Message 1 of 7
(2,682 Views)

My preference, particularly recently, is to have an Init State and do the Initialization explicitly as the first step in running the State Machine.  I similarly often have a Final State that handles the cleanup before exiting.  If there is an Error State to catch and report Errors, I may have it seque into the Final State, so that there is a single point of Entry, a single point of Exit.

 

Having said that, my rationale is based on aesthetics, as much as anything else.  It keeps the State Machines looking relatively "clean", with essentially nothing executing "outside" the loop.  Among other things, should you need to stop and restart the State Machine, it is as (conceptually) simple as calling the Final State, then calling the Init State (again).

 

Bob Schor 

Message 2 of 7
(2,655 Views)

Theres not really a right or wrong way to do it.

 

I usually make initialisation a state of the state machine.This way, if you have an error that you cant recover from in the program, at least you can go back to init and start over.

 

With this in mind, somethimes it is beneficial to close any references that are coming from shift registers as part of the init case (like close a VISA session for example). The first time you init and the ref was not valid, these will error out but as long as you handle it there its ok.

 

 

 

Message 3 of 7
(2,655 Views)

 


@cc77 wrote:

Hi guys!

 

I'm interested to hear your thoughts on initialising values in a state-machine architecture. Is it preferable to initialise everything outside of the loop before anything is executed (i.e. in a sequence before the loop) or adding an initialisation state and run it as the first thing in the state-machine? Any caveats for any option?

 

Thanks! 

 

 


Better way go with initialize inside of the loop, it'll help for debugging. 

http://www.ni.com/white-paper/2926/en/

Message 4 of 7
(2,628 Views)

My first state is always "Init" and I initialize everything in the program.

 

NOTHING is initialized outside the loop, here's an example...

initCapture.PNG

 

Also I like to do this so if there is an error in any case it will override and goto the Error case.

I2Capture.PNG

 

========================
=== Engineer Ambiguously ===
========================
Message 5 of 7
(2,597 Views)

Thank you guys for your help! I guess inside a state is the best option (but then it will also depend on the application)

0 Kudos
Message 6 of 7
(2,590 Views)

An advantage of NOT tidying away initialization code inside the loop, and bringing in information via tunnels rather than shift registers, is that it is immediately obvious what things are "static" and unchanging while the loop is running.

Message 7 of 7
(2,589 Views)