LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

State Machine Stop

Solved!
Go to solution

Hi

The attached VI does not stop after the VI reads the Stop. It stops after finishing the next loop. Why? I wish to stop the loop right away after the Stop button is pressed or at least after the VI reads the Stop button. How can I do that?

Thank you,

Steve

0 Kudos
Message 1 of 11
(5,209 Views)
Solution
Accepted by topic author Shijie

If I slightly re-arrange your diagram like this...

Parallel Code.png

 

 

Does that make the problem more apparent?

 

How about like this?Parallel Code 2.png

 

 

If you couldn't guess what I'm getting at, LabVIEW will parallelize any code that it can. In this case, there is no data dependency between the case structure and when the stop button gets read. 

 

Odds are, the VI is already waiting when you press the stop button - but the stop button has already been read in that iteration of the overall while loop. There are a couple ways to solve this:

 

1. Put the reading of the 'stop' button after the case structure executes using data dependency (aka sequence structure)

Cons: If the code in the while loop takes a long time to execute, it could appear that the stop button isn't responding to UI events as it will take a few seconds for LabVIEW to read the state of the button. 

2. Have an event structure in the while loop and make the state machine check for events or wait at the event structure when idle (look up the State Machine sample projects or the JKI State Machine good examples of this)

3. If this will eventually turn into a larger application, consider looking at the Queued Message Handler, where you can completely de-couple your UI handling with the processing

 

-Nate

Nathan Murphy
Message 2 of 11
(5,205 Views)

This VI is a simplified case for my application. I need to use the State Machine structure. Any help would be appreciated.

Steve

0 Kudos
Message 3 of 11
(5,203 Views)

I suggest then you look at the State Machine Sample Project or the JKI State Machine and study their use of the event structure. 

 

From the LabVIEW splash screen click on 'Create Project' then look through the templates to find the 'Simple State Machine'

 

Nathan Murphy
Message 4 of 11
(5,198 Views)

Nathan already answered your question. You need to make sure the last thing you do in the loop is read the value of the stop button. As your code is currently written the value of the stop button is one of the first things read. So after you complete whateve code is going on in your case structure the code will go to the next iteration because the stop has already been read and it is false. Force the read of the stop button to occur after the case structure occurs.

 

However in a true state machine I would have state action which specifically checks for the stop condition rather than stopping it in parallel. This way you can invoke any states that are required to shut everything down gracefully and then exit. I would also have a Exit case which is the only way out of the state machine.



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
Message 5 of 11
(5,196 Views)

I usually have a "Check For Events" or an "Idle" state where I have an Event Structure in order to check for button presses, updated values, etc.  And 100% agree with Mark, you should then run specific states to shut everything down properly and then stop the loop.


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 6 of 11
(5,187 Views)

Hi Nathan and Mark,

Thank you very much for your help. The attached VI basically solved my problem. Now I am wondering whether there are any ways to stop the loop right away after the Stop button is pressed. Maybe I am asking too much.

Regards,

Steve

0 Kudos
Message 7 of 11
(5,184 Views)

My real application has the Exit case. The attached VI is just a simplified case. I just want to illustrate the Stop process.

Thank you

0 Kudos
Message 8 of 11
(5,176 Views)

Not really. At least not easily. Remember, within a loop ALL code inside it must run to completion. This means that your for loop must complete before you stop. You would need a much more comprehensive architecture which would allow you to break out of long programming tasks. This is non-trivial and usually unnecessary. If you use a producer consumer patter you can catch the the user wants to stop and provide them with feedback that you will be stopping but some code needs to complete execution. That would be about the easiest way to give immediate feedback to the user that you will be stopping.



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
Message 9 of 11
(5,175 Views)

Hi Mark,

Thank you very much. I'll try that.

Regards,

Steve

0 Kudos
Message 10 of 11
(5,169 Views)