LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

how to use a event structure with a state machine

Solved!
Go to solution

First, I would like to inform you that I only work on LabView part time, and have much to learn.  Anything I do learn, I usually forget until I need it again, because I only work on it part time.

 

Using your StopWhileLoopMOD[1].vi, I am trying to put a state machine inside the event structure.  

Related link: http://forums.ni.com/t5/LabVIEW/How-to-stop-while-loop-in-Event-case/td-p/465564/page/2

 

Here is my application:  on the front panel, the user can select any combination of 7 different tests.  I have created cases to perform each step of each test in the correct order, but if the user presses stop, the tests won't stop because some of the cases have a while loop inside the event structure (like you mentioned is a bad idea).  The user should be able to stop the test, reselect tests to perform, and re-start the tests. 

 

When the start button is pressed for the event structure, I need all the cases to run in the proper order, unless stop is pressed.

 

In the past I have indexed an array and used that to run the state machine, but it won't stop immediately.  

 

I have sub VIs that are built in while loops because the outputs of the product needs time to stabilize.  The state machine stops and waits up to a certain number of iterations.  If it passes the test, the while loop stops and the next state starts.  If it takes too long, it exits and reports an error.  Maybe I need to just use the state machine and not an event structure?

Is there a good example of an event structure?

 




metzler CLAD
0 Kudos
Message 1 of 6
(5,359 Views)
Solution
Accepted by topic author metzler

I'm not sure exactly what you are asking, but it sounds like you want to script a bunch of tests and if the user says stop, to immediately stop the current test and abandon the others?  I'm going to assume that you know how to clear the array so that it will abandon the others, so I'm guessing that you are having trouble abandoning the current test?  If this is indeed the case, then the problem is that you are not able to propagate the message from the main VI FP which is the GUI to the sub vi which is the test, where the test may or may not have a GUI (FP visible) of it's own.  Threading was the first thing to come to mind, but this may not be necessary using events.

 

You can do this by passing a refnum of the stop button to the subVI, where you can then add that wait to the event case structure.

 

I've attached 2 VIs, mainvi.vi which is just a loop displays the count*2 (number of seconds passed since running) that will call subvi.vi and then check to see if the stop button is pressed.  mainvi.vi is by no means a state engine, it is just a simple loop for demonstration purposes.  subvi.vi just waits 2 seconds and leaves, it is a better structured state engine with an init state to start a poll case to wait for events and an exit state to clean up.  You can modify this any way you wish to get it to do what you want.  You will note that even if subvi.vi is being executed, it will terminate immediately when the stop button is pressed.

 

Hope this helps.

 

 

A

Download All
Message 2 of 6
(5,345 Views)

 

Thanks for the event structure, that made it easy. 




metzler CLAD
0 Kudos
Message 3 of 6
(5,338 Views)

Hi

 

I've been using Labview for a while now and I always have issues with event structure from time to time when ever I use it in a state machine. When I run the VI it executes the way I want but the stop button doesn't work after I select other boolean controls on the front panel. Am I using event structure wrongly in the code? I've attached the code for someone to take a look.

 

Thanks

0 Kudos
Message 4 of 6
(4,861 Views)

I would say it is a ok way to use the Event structure in a simple program.

 

There are a couple of errors in the code.

In the Vend case:

You never set your lower shift register to zero, you need to do that. Put a zero constant to the output tunnel.

The next case is set to Vend again, change that to "Check UI events".

 

In the Return case:

Next case is set to "Return", change that to "Check UI events".

You need again to set the shift register to zero. Put a zero constant to the output tunnel.

 

And just to be clear, this is a 2 years old thread, that has nothing to do with you program. It would have been better for you to start a new thread. 

0 Kudos
Message 5 of 6
(4,851 Views)

Wadel,

 

First. It is usually better to start a new thread and refer to a related thread than to tack your post at the end of a thread that is two years old and which has been marked solved.

 

Generally the event structure should be located within the VI in such a way that it can execute immediately when an event occurs.  By putting it inside a case structure you prevent it from executing until that case executes.  In particular your Vend case sets the next state to Vend so the state machine remains locked in that state regardless of any events.  Similarly Return locks the state machine in the Return state.

 

If I want to use a single loop architecture with a state machine and an event structure, I usually put the case structure inside the Timeout case of the event structure.  I then enforce two rules: 1. The timeout is <100 ms. 2. No state contains any code which will take longer than ~ 100 ms to run without exiting from the state so that events can be checked.  If a state needs a five second wait for example, it will set a timer and then check it ~ every 100 ms until the time has elapsed and will exit after each check so that events can be processed.

 

Generally a Producer/Consumer (Events) architecture is preferable.  The Producer loop onlyhandles events and the state machine is in the Consumer loop.

 

The Return state should set the shift register to zero, not the Amt deposited indicator. You do not need the local variable.

 

Lynn

0 Kudos
Message 6 of 6
(4,846 Views)