11-11-2012 09:20 PM
Hello,
I am trying to build a state machine, but, I am struggling with understanding the methods for determining which state to transition to next. In other words, I have several states, but, I don't want to go in any particular sequence. If I have states numbered 1 through 10, I want to be able to go 1-4-2-5-6-2-6-1-10 etc. in no particular order. I want the transition to the next state (and actions) to be determined by the streaming data, which can be random and require access to any of my 10 states at any time and in any sequence.
I saw this picture on the "Application Design Patterns: State Machines" white paper, but, it leaves out some important details. How does one create the structure in BLUE shown in 3C? When I try to create this array of enums, all of them are the same. I am not able to make a list of different enum values. In other words, when I type in "2", then, all the values in the array display as "2."
There is a nice, simple video example of a state machine for dispensing soda for $0.15, however, this state machine moves in a single sequence, from 5 cents, to 10 cents to dispense; it does NOT illustrate how to select a state "out of order." I need to understand how a state machine can move from "5 cents" state to dispense directly, with the addition of 10 cents to the "5 cents" state.
Can anyone suggest a really good tutorial on how to make the selector work in a state machine? I have been reading some of the available material on ni.com, but, I can't find a good detailed explanation of how to do it. I remain confused.
Or, can you just explain how they created the BLUE array in the attached picture? Maybe I can figure it out from there.
Thanks,
Dave
Solved! Go to Solution.
11-11-2012 09:22 PM
Drop an array container onto your block diagram.
Drop an enum constant onto your block diagram. (For state machines, it is a good idea to use an enum that has been saved as a Type Def. control.)
Drag the enum into the array container.
11-11-2012 09:44 PM
Hi Ravens fan,
Yes, that is exactly what (I think) I tried to do...please have a look at the attached vi, (obviously in progress) and you will see that I cannot "populate" the elements of the array. I cannot figure out how to enter two different elements in the array. I typed in GREEN, and it shows up in all three boxes. If you choose any of the three boxes and type something else, like RED, then, all the GREENs turn to RED... What is the correct way to populate these?
What am I missing?
Dave
11-11-2012 10:58 PM
HI
can you convert the ablove in 2011 so that I can have look into it.
As far as populating the enum elements in that array what you have to do is create the enum and drag it inside the array.
As you increase the array elements you will get enum only with its deault values in all the elements. There you can choose whatever value you want from the enum.
Find the attached VI.
11-11-2012 11:42 PM - edited 11-11-2012 11:44 PM
dav2010 wrote:What am I missing?
You dropped a new enum that only has one value (green). You probably want to drop the original enum from the left side into the array container. Now you can select one of the four values for each array element.
Alternatively, you can right-click one of the array elements (not the array container!) and select "edit items" to define the available values.
Typically you would define the enum exactly once, then make it a strict type definition and link all instaces on the diagram to it. This way the items update in all locations if you edit it. If you don't do that, you'll soon get a real mess. Currently, all your enums are of a different type. Not good!;)
11-12-2012 06:54 AM
Ah, I see now how to create the array with all the elements I need. Thanks.
Any suggestion on how to learn how to structure a state machine for 10 states that must be called by sensor input in random and changing order? The tutorial about the vending machine requires "step 1, step 2, step 3, finish" but I need to go "step 1, step 3, step1, step 2, step 1, step 3, step 2, step 2"..etc...
How do you structure the case structure and WHILE loop to allow selection of any of the cases possible depending on the input of a sensor that is sensing external stuff and feeding data to the VI and determining the required next case.?
I have already looked at VI for Template ->VI -> From Template -> Frameworks -> Design Patterns -> Standard
State Machine, but this is only 2 cases, and it is not obvious how to extend it to 10 cases.
Thanks,
Dave
11-12-2012 07:39 AM
There is nothing that says that each case can't determine dynamically who is next. The case to be called next should be coming out of the case structure and into a shift regiser. The shift register is then wired to the case selector. This way, each case can determine who gets called next depending on whatever logic you need.
11-12-2012 08:06 AM
Dave, you need to put cases around your next state choice. So if you are always reading an input and that is the only decided on which state to run next then you could even potentially move the read and select the state to run outside of the main states case structure in order to avoid reproducing code (a subVI would be good to keep the code tidy then).
So it would basically run:
State 1
Read input
Determine next state (say 3)
State 3
Read input
Determine next state
State
Read input
Determine next state
And so on...
Of course, if there are considerations based on which state you are in now, if you want faster responses to user inputs, or other interactions to be going on (and don't forget your stop/quit methods) then the other thing to consider, and which may suit your application better is a queued state machine. So check out the producer/consumer loop templates as a starting point.
11-12-2012 09:05 AM
Hi Kathryn,
Yes, this is EXACTLY what I want to accomplish...
So it would basically run:
State 1
Read input
Determine next state (say 3)
State 3
Read input
Determine next state
State
Read input
Determine next state
And so on...
But, I am quite new to programming Labview state machines, and can't figure out how to even start when the sequence of states is random instead of fixed!!! I can do 1,2,3,4, like the vending machine VI example, but, not 1,3,2,2,2,5,2,1 etc. based on external input. Is there some simple example case I can study? I can't believe I am inventing this for the first time.
"select the state to run"
But, HOW do I do this??????? This is exactly the question I am asking...how is this done? Please see the attached 3-state system....how do I hook this up???? I am totally lost here...can you help me understand how to wire this thing?? In this simpe RED/GREEN/BLUE case, my input is just the number a user would input on the front panel.... Apparently, there are two files necessary... a vi and .ctl??? What is .ctl?
Thanks,
Dave
11-12-2012 09:07 AM
Hi,
Do you know of any simple example that illustrates the code for the transitions you are describing. I believe I could follow some existing code that does exactly what you are describing...I can't find it, but, I am sure somebody did this before. I am very new to state machine programming, so, on a very steep learning curve.
Thanks,
Dave