From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

state machine enum decision

When I try to design a state machine enum,

I can make a normal enum.

but Sometimes I read that people use an array of enum..

sometime people use Queues...

 

anyone can provide advices on these options?

 

Thanks so much,.

0 Kudos
Message 1 of 17
(2,709 Views)

Hi idjuven,

I think of a state machine as a flow where the previous state, along with some boolean logic or results determines the next state. If you have a predetermined order for several states, this is called a sequencer. 

If your VI generally looks like a state machine, but you have a couple states that always need to be called together in a certain order, then those are good candidates to turn into SubVIs and consolidate into a single state.

 

For a simple sequencer, you can make an array of your case enum and feed it into a for loop. This begins to look a lot like a stacked sequence structure, although you can use shift registers and have the ability to conditionally stop it before all cases are run.

 

If you use a queue to hold your states, I would follow the practice where only certain states can add to the queue. (In JKI State Machine these would be called Macro states). Otherwise, you will find yourself quickly confused by where states are becoming enqueued, why your VI is getting stuck in a loop, and generally lose the flow of the state machine.

0 Kudos
Message 2 of 17
(2,700 Views)

Thanks so much..

 

but since a normal enum will work, why do they use a 1D array of enum (place the normal enum in an emply array)?? is it because in some states, it will require state manipulations (i.e. the next state is not defined)?

0 Kudos
Message 3 of 17
(2,679 Views)

From your description, it sounds like there would be no reason to do that. Can you post an example in case I'm picturing it wrong?

0 Kudos
Message 4 of 17
(2,675 Views)

this is the practice for the CLD exam..

 

please see their solution.. I don't think it needs to be so complicated..

0 Kudos
Message 5 of 17
(2,672 Views)

Sorry I am using LV 2016 and those are saved for LV 2018. You will have to backsave in order for me to open them.

0 Kudos
Message 6 of 17
(2,670 Views)
0 Kudos
Message 7 of 17
(2,667 Views)

@idjuven1 wrote:

Thanks so much..

 

but since a normal enum will work, why do they use a 1D array of enum (place the normal enum in an emply array)?? is it because in some states, it will require state manipulations (i.e. the next state is not defined)?


In the "Check Time" case they actually "queue" up to actions to the state machine. If "Has Time Elapsed" is false, they "queue" up the actions "Run State" and "Idle" to the array. The array in this example is basically a queue. In this particular example there is only one situation where they queue up multiple actions so I can see why you might wonder why they are using an array. It is there so that multiple actions can be queued up if necessary such as the "Check Time" case does.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
0 Kudos
Message 8 of 17
(2,658 Views)

Hi idjuven,

I agree with you, there is no good reason to use an array of enums here. Maybe the creator did not know about LabVIEW queue functions, but it seems like anyone training for the CLD should be aware of them. All of the array functions could easily be replaced by queue functions, but I would probably just tweak the logic a tiny bit so that the one case where they want to enqueue 2 states instead of 1 is not necessary, and you can just use a single enum instead. 

For anyone interested in how the array of enums is being used like a queue in this solution, here is a small clipping:

Capture.PNG

0 Kudos
Message 9 of 17
(2,656 Views)

@Gregory wrote: All of the array functions could easily be replaced by queue functions, but I would probably just tweak the logic a tiny bit so that the one case where they want to enqueue 2 states instead of 1 is not necessary, and you can just use a single enum instead.

 


There are a bunch of array constants in there that will completely overwrite what was already in the array.  So a Flush Queue and then an Enqueue would be required for that.  Personally, I think this example could be improved by turning it "inside-out": put the Event Structure on the outside and the state case structure in the timeout case.  Then you could get away with just a single enum (no arrays).


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
0 Kudos
Message 10 of 17
(2,645 Views)