LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Problems closing several loops in a state machine with an event structure

I am having some problems closing three loops running in parallel in a state machine.  I have one loop for an event structure to monitor user controls to change the state.  The other loop is the state machine which will control an actuator, and the third loop is for data aquisition/analysis.  I want to be able to hit the stop button and go to a "shutdown" state where I can home the actuator then close the loops and finally go to the last "shutdown" subdiagriam in the flat sequence structure.  Currently when hitting the stop button it goes into the shutdown state and will end the state machine loop but it appears the other loops are not closing until I hit the stop button again.  Could this be due to using a local variable for the boolean to control the loop conditions? I attached a screenshot of the block diagram.  Any advice would be great!

0 Kudos
Message 1 of 3
(2,937 Views)

Yes, you should not use the local variables.  I would suggest using queues to send messages to the other loops.  Search for Producer/Consumer or Queued Message Handler architectures.  You have most of the framework in place, so it shouldn't be too much work to improve the code.  

Message 2 of 3
(2,931 Views)

You have a classic race condition due to overuse of local variables. If the stop event fires in the upper loop, the local variable connected to the loop termination condition has already been read (it is false!). This means that the upper loop will iterate once more and again wait at the event structure for the next event. At this time, the local variable connected to the loop termination is TRUE, but the loop cannot complete until the event fires again, for example if you press the stop button once more.

 

The correct way is to connect the terminal of the stop button across the right event frame directly to the loop termination terminal. Now it will get the TRUE value correctly once the stop event fires for the first time.

 

LabVIEW does not execute left to right, execution order is determined by the wiring.

 

I also agree with the above message that you should rethink and re-architect your code using established coding guidelines. 

Message 3 of 3
(2,897 Views)