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
(1,171 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
(1,144 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
(1,144 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
(1,117 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

 

-------------------------------------------------------------------
Unfortunately, most readers of this Forum, including some real Experts, have not mastered the skill of being able to read the code that Posters fail to post. If we cannot see "what you did wrong", we are unable to tell you how to fix it. (Bob Schor 28 August 2018)
Message 5 of 7
(1,086 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
(1,079 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
(1,078 Views)