LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

State machine won't stop

Solved!
Go to solution

Hello pagz,

 

One thing that I'm not sure is doing what you want- Right now you have eight cases- 0/default,1,2,3,4,5,6, and 20.  Since you're using the loop index to determine which case is run, this means you'll end up running case "0" for iterations 7-19.  Since your Write to Measurement File express VI in case 0 is configured to "use next available filename", you'll end up with twenty files, not eight as you mentioned.

 

-Edit-

Hah- seems I typed too slowly.  Six posts in as many minutes!

 

Regards,

Tom L.
0 Kudos
Message 11 of 24
(602 Views)

Thanks a million man. You've no idea of the problems that was causing me!

0 Kudos
Message 12 of 24
(599 Views)

I'm going to point out a several things about this architecture that I don't like and think could be causing you problems.

 

1.  I don't consider what you have a state machine.  A key principle of a state machine is a shift register that will tell the next iteration of the while loop to what case of the case structure,  i.e. "state", to execute next.  Usually, that will involve some decision making at the end of the loop to help determine if any alternative states should be executed in the next iteration.  You are just using the i terminal to determine which case to execute.  What you have is pretty much a while loop alternative to a stacked sequence structure.

 

2.  You only have states defined for 0(default), 1 through 6, and 20.  So iterations 7 through 19 are just going to execute 0.  I bet you think you are returning to state 0 after executing state 20, but you may actually be executing state 7, 8, 9, 10 etc. since they don't exist and will default to state 0.  Since each state takes a long time to run, it will take what feels like for ever to get to state 20 and have the program stop.

 

3.  Very long wait statements in most of your cases.  Why do you have waits of 90 to 190 seconds in your various states.  Each state will take a long time to run.  It will make your program seem to be very slow.  There is no way for a user to stop the program without it reaching the end without aborting it.  One execution of your program will take over 34 minutes to run.

 

3.  Your use of semaphores and single frame sequences to determine exeuction order.  You acquire a semaphore and then execute a VISA write and read.  IN parallel you wait at another semaphore before executing some instrument code and file writing after also waiting a long time of 70 to 190 seconds.  If you wired up things in series you can guarantee the instrument executes after the VISA write and read without needing any semaphores.

 

4.  You also have a memory leak on semaphores.  You create them in each iteration.  You acquire and release.  But you never release the semaphore reference.

 

5.  Since many of your states are similar, you don't need to duplicate all of the code among the different cases.  It seems like the only difference between cases 1 through 6 is the command you write, or the amount of time you wait.  Common code should be in a subVI that is shared among each case.  Or just use a single case and as part of your state machine determine which index of an array of commands you will execute when you enter that case.

 

EDIT:  I took so long creating a thorough write, I missed on timing to get the quick solution message out.Smiley Embarassed

0 Kudos
Message 13 of 24
(584 Views)

@RavensFan wrote:

I'm going to point out a several things about this architecture that I don't like and think could be causing you problems.

 

1.  I don't consider what you have a state machine.  A key principle of a state machine is a shift register that will tell the next iteration of the while loop to what case of the case structure,  i.e. "state", to execute next.  Usually, that will involve some decision making at the end of the loop to help determine if any alternative states should be executed in the next iteration.  You are just using the i terminal to determine which case to execute.  What you have is pretty much a while loop alternative to a stacked sequence structure.

 

2.  You only have states defined for 0(default), 1 through 6, and 20.  So iterations 7 through 19 are just going to execute 0.  I bet you think you are returning to state 0 after executing state 20, but you may actually be executing state 7, 8, 9, 10 etc. since they don't exist and will default to state 0.  Since each state takes a long time to run, it will take what feels like for ever to get to state 20 and have the program stop.

 

3.  Very long wait statements in most of your cases.  Why do you have waits of 90 to 190 seconds in your various states.  Each state will take a long time to run.  It will make your program seem to be very slow.  There is no way for a user to stop the program without it reaching the end without aborting it.  One execution of your program will take over 34 minutes to run.

 

3.  Your use of semaphores and single frame sequences to determine exeuction order.  You acquire a semaphore and then execute a VISA write and read.  IN parallel you wait at another semaphore before executing some instrument code and file writing after also waiting a long time of 70 to 190 seconds.  If you wired up things in series you can guarantee the instrument executes after the VISA write and read without needing any semaphores.

 

4.  You also have a memory leak on semaphores.  You create them in each iteration.  You acquire and release.  But you never release the semaphore reference.

 

5.  Since many of your states are similar, you don't need to duplicate all of the code among the different cases.  It seems like the only difference between cases 1 through 6 is the command you write, or the amount of time you wait.  Common code should be in a subVI that is shared among each case.  Or just use a single case and as part of your state machine determine which index of an array of commands you will execute when you enter that case.

 

EDIT:  I took so long creating a thorough write, I missed on timing to get the quick solution message out.Smiley Embarassed


1. Then why is it popular advice to "change a stacked sequence into a state machine?"  I think it's a rudimentary state machine always executed in the same order, but it can easily be adapted to run states as needed instead.  Would it have been any different if he just had an array of states 0-7 connected to shift register?
edit - ugh, i had a lot more that got wiped when i posted this
I'll just say I thought it was pretty cool for a newbie to construct something this advanced in the first place.  But I do agree with everything else you said.
Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 14 of 24
(571 Views)

It is better than a stacked sequence structure.  You get shift registers so that you don't have to deal with evil sequence locals and backwards wires.

 

But to me, it is still short of being a state machine, even a rudimentary one.  The i terminal wired to a case structure is still effectively the same thing as a stacked sequence in execution.  It executes the cases in order with no ability to vary the order of execution, back up, or skip over a case.

 

Now it is one step closer to a real state machine and would be easier to modify than a stacked sequence so that you can do all of those things typical of a real state machine.  The next major modification is to put in a shift register and have each state determine what is the next state to execute as opposed to having the iteration number determine which state to execute.

Message 15 of 24
(564 Views)

I agree. A key element in the definition of a state machine is that the next state is determined by the results of the current state and possibly external inputs which become available at the time the state completes execution. Code which cannot alter the sequence of execution is not really a state machine.

 

Lynn

0 Kudos
Message 16 of 24
(536 Views)

@johnsold wrote:

I agree. A key element in the definition of a state machine is that the next state is determined by the results of the current state and possibly external inputs which become available at the time the state completes execution. Code which cannot alter the sequence of execution is not really a state machine.

 

Lynn


I would say that it is common, but required, for the current state to determine the next state.  Furthermore, it is also common, but not required, for the next state to be determined by outside influences.

 

My arguement says what is so different between the code that was written and a queued state machine that had all the states queued in an array but none of the states happened to be influenced by either of the two influencing factors mentioned above?

 

But your argument if the above conditions are true, it's not really a state machine?  Then the advice given about turning stacked sequence structures into state machines cannot be true?

 

[edit]

I guess you could make a "true" state machine by simply having the current state influence the next state, but isn't that really cheating because you'll never have a choice of which state to execute next.

[/edit]

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 17 of 24
(528 Views)

A state machine is an architecture where a state determines what the next state to run is.  It is not locked in by the architecture.

 

1.  Stacked Sequence.  Code is locked in.

2.  Case structure driven by i terminal.  Still code is locked in.  Closer to a state machine, but not really.

3.  Case structure with shift register, but the value into the shift register is hard coded as a constant.  IS a state machine since the states determine the next state.  It just so happens that the programmer has it as a 1 to 1 to 1 to 1 path without any actual decisions taking place.  The last state may recall the first.  But even without decisions, the programmer has the ability to start expanding on it and make decisions and alternate paths.

4.  #3, but with decisions.  Now the programmer is taking full advantage of the state machine where there can be diverging paths that are determined within the state.

5.  Queued state machine.  While you think it's not if the programmer preloaded a set of states.  It still is a state machine.  Any state can add a new state at the beginning of the queue to determine what to do next.  Any state, can flush the whole queue and put in a different state.  It could flush the queue, and put in remnants of states from what was in the queue before and one or more other states in whatever order it desires.  It's actually more powerful than #4 because it can set up and control multiple states in a row, but the following states also have the ability to ignore the list.  This amount of flexibility can also create a lot of complications if the programmer isn't careful about how he manages the list of states.

Message 18 of 24
(518 Views)

Hey, guys:

 

Please understand that I am debating you only because I want to be proud of this CLD badge, and any ill-conceived notions I have need to go away quickly.

 

Oh, and in case you didn't know, I truly appreciate your patience.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 19 of 24
(511 Views)

Okay, so following your train of thought, would a numeric constant in a shift register and an incremnting inside each state be closer to or be an actual state machine?

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 20 of 24
(506 Views)