LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Simple example - Break FOR loop inside an event structure based on Front panel event change.

I am running a for loop inside a state machine architecture that contains a FOR loop. How can I break this For loop based on Event change in front panel ??

 

 

Abhilash S Nair

Research Assistant @ Photonic Devices and Systems lab

[ LabView professional Development System - Version 11.0 - 32-bit ]

LabView Gear:
1. NI PXI-7951R & NI 5761
2. The Imaging Source USB 3.0 monochrome camera with trigger : DMK 23UM021

OPERATING SYSTEM - [ MS windows 7 Home Premium 64-bit SP-1 ]
CPU - [Intel Core i7-2600 CPU @ 3.40Ghz ]
MEMORY - [ 16.0 GB RAM ]
GPU - [ NVIDIA GeForce GT 530 ]
0 Kudos
Message 1 of 12
(5,097 Views)

You cannot. The timeout event case will run until everything inside it completes.  Any events received by other cases (such as Stop in your example) will wait until the timeout case finishes.

 

This is basic dataflow. From your posts it appears that you have not fully grasped the concept of dataflow and its implications.

 

To make the state machine work in the timeout event case, it must not have any state (case) which takes longer to execute than the minimum permissible time to respond to an event. This means that you would not include any code like the for loop which you might need to interrupt.  Loops inside loops in state machines should generally be avoided, or used only in situations where you can assure that they will not block.

 

The Producer/Consumer architecture avoids this problem by using parallel loops.

 

Lynn

0 Kudos
Message 2 of 12
(5,092 Views)

I found a work around. It works BUT I dont know if its a correct practice.

 

Abhilash S Nair

Research Assistant @ Photonic Devices and Systems lab

[ LabView professional Development System - Version 11.0 - 32-bit ]

LabView Gear:
1. NI PXI-7951R & NI 5761
2. The Imaging Source USB 3.0 monochrome camera with trigger : DMK 23UM021

OPERATING SYSTEM - [ MS windows 7 Home Premium 64-bit SP-1 ]
CPU - [Intel Core i7-2600 CPU @ 3.40Ghz ]
MEMORY - [ 16.0 GB RAM ]
GPU - [ NVIDIA GeForce GT 530 ]
0 Kudos
Message 3 of 12
(5,088 Views)

That looks very awkward.  It might sort of work, but think it through thoroughly.

 

If the timeout occurs, and the loop starts executing, then you hit the stop button.  The loop will stop.  But the Stop button value change event will queue up and hasn't been handled yet.  So that timeout, stop value change event will run again.  But this time, that loop will run 5 times.  And that stop button won't stop the loop again because the stop button will have already been reset from it being read in the previous (timeout) case.

 

Why do you even need the timeout case For Loop to end early?  That 5 iteration while loop will run almost instantaneously.  There is virtually no time for the stop loop button to even be pressed while the For Loop is executing.

 

I wouldn't do what you are trying to do.

Message 4 of 12
(5,082 Views)

Here is a way to get rid of the nested loops and provide similar functionality.  Note that I still recommend something like Producer/Consumer, but I am showing this so you can see some of what we have been talking about.

 

I "expanded" the "Check While" state to three states: Check While 1, Check While 2, and Check While 3, to replace the 1, 2, and exit states of the nested state machine.

 

The Check While 2 state replaces the state with the for loop. It repeats N times (unless Stop is pressed). After the Nth iteration it goes to Check While 3 which then goes to the outer state machine Exit.

 

I changed the timeout to 500 ms so you can see what it happening.  With 0 delay you never get a chance to try the Stop button.

 

This is still a rather awkward way to build a state machine, but it overcomes some of the limitations of your code.

 

Lynn

Download All
0 Kudos
Message 5 of 12
(5,074 Views)

Okay. Accpeting that the design is not favorable and not advantageous when my program expands. I have begin to follow producer/consumer programming architecture.

 

Please find the attached program which is a simple producer consumer with event structure. I wonder how could I stop looping around the producer and consumer loops over and over again. I am pretty sure that this ENQUEUE ELEMENT loops back to the consumer loop and starts from begining.

 

I hope my first program in PC architecture with State machine and Event structure is correct. 

 

 

Abhilash S Nair

Research Assistant @ Photonic Devices and Systems lab

[ LabView professional Development System - Version 11.0 - 32-bit ]

LabView Gear:
1. NI PXI-7951R & NI 5761
2. The Imaging Source USB 3.0 monochrome camera with trigger : DMK 23UM021

OPERATING SYSTEM - [ MS windows 7 Home Premium 64-bit SP-1 ]
CPU - [Intel Core i7-2600 CPU @ 3.40Ghz ]
MEMORY - [ 16.0 GB RAM ]
GPU - [ NVIDIA GeForce GT 530 ]
0 Kudos
Message 6 of 12
(5,019 Views)

I am sorry its working perfect. Smiley Happy

 

So with this design I just attached, can I freely put a FOR LOOP inside the consumer loop for some calculations. Since its no more inside a EVENT STRUCTURE it should not be a problem right '?

Abhilash S Nair

Research Assistant @ Photonic Devices and Systems lab

[ LabView professional Development System - Version 11.0 - 32-bit ]

LabView Gear:
1. NI PXI-7951R & NI 5761
2. The Imaging Source USB 3.0 monochrome camera with trigger : DMK 23UM021

OPERATING SYSTEM - [ MS windows 7 Home Premium 64-bit SP-1 ]
CPU - [Intel Core i7-2600 CPU @ 3.40Ghz ]
MEMORY - [ 16.0 GB RAM ]
GPU - [ NVIDIA GeForce GT 530 ]
0 Kudos
Message 7 of 12
(5,005 Views)

Sure, you can put a For Loop inside a consumer loop.

 

There are two problems with loops and event structures, and I think you went both ways in your VI's.

1.  Event structures buried in case structures.   Event structures are always queueing up events.  But in the event structure doesn't get into the path of execution to handle the events, your VI might lock up, or potentially even run out of memory.

 

2.  While loops or other long-running or user-interactive loops buried in an event case.  Here, the event case is running for a long time and accumulating other events.  You might even need further user interaction to exit the while loop, but the front panel may be locked to prevent that from happening.

 

Both of these things are aggravted when the events are set to lock the front panel until the event completes.

 

It is important to try to keep your architecture as flat as possible.  And to read the Event Structure Caveats article linked to in one of your message threads.

0 Kudos
Message 8 of 12
(5,002 Views)

Thank you raven. yes I have faced all the issues you just mentioned and learned a lot with the help of this forum.

 

If thats the case ,  Please take a look following VI. Why cant one stop the VI when the iterations are going on in the consumer loop. 

 

Try giving a large iteration number and try stopping it inbetween. It just doesnt break  out. Its similar to front panel lock problem. I believe that the producer loop should help it break out.

 

 

Similar problem with my motor program (same architecture as this VI). Cant stop the motor during execution. Its almost like the stop button doesnt get handled by the producer loop.

Abhilash S Nair

Research Assistant @ Photonic Devices and Systems lab

[ LabView professional Development System - Version 11.0 - 32-bit ]

LabView Gear:
1. NI PXI-7951R & NI 5761
2. The Imaging Source USB 3.0 monochrome camera with trigger : DMK 23UM021

OPERATING SYSTEM - [ MS windows 7 Home Premium 64-bit SP-1 ]
CPU - [Intel Core i7-2600 CPU @ 3.40Ghz ]
MEMORY - [ 16.0 GB RAM ]
GPU - [ NVIDIA GeForce GT 530 ]
0 Kudos
Message 9 of 12
(4,993 Views)

Raven : I found another post of the same problem I am facing by you.

Link: URL

 

Here is a snapshot of the code that is the problem. A for loop inside a consumer loop that doesnt break even when the Event handler in producer loop has occurred.

 

The Outer while loop is for consumer

The inner while is for state machine

the FOR is for running the motor based on certain number of steps.

 

 

Abhilash S Nair

Research Assistant @ Photonic Devices and Systems lab

[ LabView professional Development System - Version 11.0 - 32-bit ]

LabView Gear:
1. NI PXI-7951R & NI 5761
2. The Imaging Source USB 3.0 monochrome camera with trigger : DMK 23UM021

OPERATING SYSTEM - [ MS windows 7 Home Premium 64-bit SP-1 ]
CPU - [Intel Core i7-2600 CPU @ 3.40Ghz ]
MEMORY - [ 16.0 GB RAM ]
GPU - [ NVIDIA GeForce GT 530 ]
0 Kudos
Message 10 of 12
(4,979 Views)