LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

State Machine Demo

Hi Folks,
 
I am a newbiew LabVIEW developer studying & working towards my CLAD certification.
I already took LabVIEW Basics I & II and I decided to make a simple state machine (please see attached VI).
I was hopping if there is anyone out there who find a way for the VI be optimized so I can see my weakness.
 
ThanX in ADVANCE!!!!!Smiley Happy
0 Kudos
Message 1 of 21
(4,188 Views)
goodness.  It is certainly better than my early code when I started in 1993.  But having evolved my code over a couple of years.  Your implementation, while functional is overly complicated.  My preference is to use the queued message handler, although it has been evolved over several years.  whether u choose to use that or the standard statemachine  template, the cases should be named... and the names should be descriptive to what the state (or case actually does).  this way, other the code can be shared with other developers. 
One other trick I learned, is to use a single cluster to pass through all of the states, this way you can add additional local variables very easily.   In the morning, I will send you a quick example that you may be able to utilize for your example to improve readability.
In the end though, it is ur personal preference and if it works, well then it is right.  Just may not be the best implemenation.  And I think this is one example of that.

Paul
Message 2 of 21
(4,172 Views)
Overly complicated and convoluted is the word.
  • I haven't even tried to undestand what you are juggling with all these string operations, but there's gotta be a better way!
  • Case 4 and 5 are identical except for a diagram constant. They can be comined into one case.
  • "state operation" is a control, but you are constantly writing to it via local variables. This mean it cannot be reliably operated, because the set value could be reset before it is used due to race conditions.
  • It is confusing that you replace the "state" of a typical state machine with a "state operation", while the "state" is actually in a string that does not really influnece operation. Whatever you have is not a state machine.
  • In state operation=2 you delete from the 2D string array, but later you always seem to iterate over 9 elements (one too many to begin with) in the inner while loop.
  • Since the 2D string array can shrink but never grow, you might stop the program once its size reaches zero.
  • You write to a local variable in virtually each state, it would be more reasonable to pull this local variable write before the case structure.

The attached quick modification illustrates some of the above points. This is not a finished or even reasonable program. It would need a complete rewrite.

Message 3 of 21
(4,171 Views)
seems altenbach has beaten me to the post for an example.  If you open a new VI and click on the file menu -> NEW... expand the templates and frameworks there is both a statemachine and a queued message handler template. That is the best place to start, and what will be covered on your test.

Paul
Message 4 of 21
(4,134 Views)
"Overly complicated and convoluted is the word ..."
 
But at least labVexpert's VI WORKS. Smiley Tongue
PaulG.
Retired
0 Kudos
Message 5 of 21
(4,111 Views)
PaulG said
"But at least labVexpert's VI WORKS."

So do a lot of Rube Goldberg VIs. Smiley Wink
0 Kudos
Message 6 of 21
(4,106 Views)

WOW!

Thanks a lot folks. I wasn't expecting any response until a week or two later.
I am very impressed with this FORUM and most importantly the members.
I just learned a whole lot from your MOD altenbach , so thanks a lot once again.
I never new you can combine conditions in within a Case Structure and
I like the fact where you moved the location for the State Operation.
I forgot to mention this earlier but I actually wrote this code in a much better
fashion in C. I was basically trying to do the same program with LabVIEW.
Nevertheless, I knew it wasn't going to be perfect but I want to have some functional
program that I can learn from. I knew my program was complicated but I was hoping
from another set of eyes to have a look at. Anyways, I already feel indebted to you folks
cause I really learned a lot already.

Once again, ThanX a lot.

0 Kudos
Message 7 of 21
(4,071 Views)
Hi Folks,
 
I am back after studying some more and trying to take advantage of all the things I learned so far.
Anyways, I am improved my program a whole lot and changed it drastically.
Hence, I wanted to get some feedback on what can be done to improve it programatically.
My certification exam for CLAD is going to be this Friday or next Monday so any tips for the exam
would be helpful.
 
Once again, thank you all for your time and input in ADVANCE!
 
labVexpert2B
0 Kudos
Message 8 of 21
(3,997 Views)
Using dataflow would eliminate the need for the sequence structure in the upper loop. Just wire the error clusters. Do you want to continually enqueue the same element repeatedly or only when it changes? The way it is now, if the lower loop is slower than the upper loop, the queue would grow until a memory problem causes the program or the OS to choke.

Use the State Operation enum for the queue datatype. Then the case selector label will show the state operation names, self-documenting the code. Your use of free text is good for situations where the self-documentation is not available or is not sufficiently explanatory.

Similarly, I prefer to use a typedefed enum for the state itself rather than strings. It eliminates spelling problems, facilitates ease of creation of constants, and makes adding or changing states much less error prone.

The lower loop should have a wait. Either in the Default state or by wiring a non-negative timeout value to the Dequeue Element function.

Using the Value property node is the slowest way of programmatically updating a control and it forces a thread switch to the UI thread. There are many threads on the Forum which discuss this.

Lynn
Message 9 of 21
(3,986 Views)

> Withregards to the sequence structure, I removed it cause I totally forgot about the error cluster.

> I actually want to enque the Ring Data only when it changes and to revert back to the default after making a selection.
   I am not sure how to implement this. Withregards to the the defualt State being selected, I thought that using property node
   would much more efficient than using a local variable. Is there any other way of implementing this?

> I know it would have been better to used Enum but I already started with the Ring and didn't want to adjust that for now.

Hopefully this is much better.

0 Kudos
Message 10 of 21
(3,949 Views)