LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

break a for loop inside Event Structure

I have a VI with event structure that waits for the user command to do the following:
1. move motor x or motor y
2. start DAQ
3. emergency stop all motors

The even structure waits for value change and performs either of the 3 above tasks.  Task 1 and 3 involves sending a simple serial command to the motor.  Task 2 is a more complicated program, it has a for-loop that handles the data acquisition.

The goal of the VI is to 1) move the motor to a particular position, 2) start the DAQ, which involves taking data and simultaneously moving the motor, and 3) abort motors to prevent crashes.

Individually, each task 1-3 works fine.  However, because task (2) involves a lenghy for loop, I can't call on task (3) until (2) is completely executed.  Which defeats the purpose of the loop. 

is there a way to break out of a "for loop"?  maybe by using some sort of a timeout?  I've enclosed a stripped down sample VI, each button executes the tasks 1, 2, or 3.  I'd like to hit <measure> and while it's executing, be able to hit <stop motor> to abort.

0 Kudos
Message 1 of 6
(2,701 Views)

You cannot break out of a FOR loop short of using the Stop function on the application control palette and that'll stop the VI.

You can, with very minor effort, substitute a WHILE loop for a FOR loop and include an additional means of termination other than just loop count.

Assuming you made that change, your VI will have to be redesigned anyway to move the "start measuring" WHILE loop out of the event structure because as long as it is running within that event state, that event state will never complete and the event structure will never get around to checking to see if the STOP button has been pressed.

The "start measuring" WHILE loop could be run in parallel to the while loop used to check for user events -- you would need to communicate with it somehow. Some people use locals and some use globals but queues or notifiers are the better way.

If you rebuild the VI as a state machine it would also be easy enough to use a timeout in the event structure that allowed the check for user input to quickly finish if nothing had come in from the user and to go on to execute one itteration of the "measuring" process before going back for another quick event check. The you can pass data between the states easily with shift register(s).

BTW, before posting a VI to any forum, it's a good idea to check the hierarchy under that VI to see what those of us who download and open the VI will be missing... 

0 Kudos
Message 2 of 6
(2,689 Views)
I would just replace the for loop with a while loop.  You can stop the while loop when the iterator reaches a certain value(like a for loop) or when a stop button is pressed.  I would normally only use for loops to perform operations on an array or other quick operations that are repetitive.  A while loop is much more suited to longer tasks that need the ability to be stopped.
0 Kudos
Message 3 of 6
(2,685 Views)
Thanks for the very informative reply. 

The reason I didn't want to use a while loop, is because right now my measurement vi has a for loop within a second for loop (I'm using 2 motors to do a 2D scan, each for loop for each axis).  So, I don't know how trivial it would be to rewrite that.  However, if I can't figure out a second method, I guess rewriting the loop will be the best option.

Is there a way to tie the stop button with the event structure?  for example, if the user hit the stop button, the VI runs the stop motor event prior to shutting down? I just want to implement a software stop to the VI.
0 Kudos
Message 4 of 6
(2,685 Views)
You can very easily convert a for loop to a while loop. Pop up on the border of the loop and select Replace with While Loop. The VI will be broken until you wire something to the stop condition, but that is the reason for making the change.

Don't use the Abort button (the Stop sign in the menu bar) to stop a VI running a hardware system. That immediately shuts down the VI without allowing you to send Halt commands to the motors, close files, etc. The Abort button should be removed from the menu when the program is deployed to the hardware. (To be complete, I think it may be possible to detect that with the event stucture, but I believe avoiding the need is better).

It is better to put a Stop button on the panel and when it is pressed, execute the shut downcode to put everything in a safe state. Ths is what Warren was referring to (in part) when he recommended a state machine implementation. I also recommend using state machines.

If there is a significant risk of injury to persons or damage to equipment, use an Emergency Stop button in hardware, NEVER in software.

Lynn
0 Kudos
Message 5 of 6
(2,671 Views)


@yoyofella wrote:
Thanks for the very informative reply. 

The reason I didn't want to use a while loop, is because right now my measurement vi has a for loop within a second for loop (I'm using 2 motors to do a 2D scan, each for loop for each axis).  So, I don't know how trivial it would be to rewrite that.  However, if I can't figure out a second method, I guess rewriting the loop will be the best option.

Is there a way to tie the stop button with the event structure?  for example, if the user hit the stop button, the VI runs the stop motor event prior to shutting down? I just want to implement a software stop to the VI.



state machine   State Machine   STATE MACHINE   STATE MACHINE   STATE MACHINE   STATE MACHINE !
 
Read the information here. Find a state machine format that you are comfortable with. Build a state machine template using that style and use that template as the starting point for your more complex VI's. I guarantee that once you get bitten by the state machine bug, you will be having to talk yourself out of using it on the simpler VIs that you put together.
 
I've attached a VI loosely based on what you say you need. Notice how individual primitive states can be dynamically strung together into a program that will do most anything you might need it to do.
 
(and as you get into this, you may find the tunnel wiring wizard found here to be useful)
Message 6 of 6
(2,654 Views)