LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to stop two or more while loops running at different speed at the same time?

Solved!
Go to solution

Hello everyone!

I have different while loops that run at a different speed; the executive speed is controlled by a wait timer.

I would like to stop all while loops at the same time so I used a local variable "STOP" for each loop.

However, I found that because one loop is faster so when the STOP button is triggered, other loops may not be able to stop at the same time.

How to stop two or more while loops running at different speed at the same time? Thank you!

RaymondZhao2017_1-1609804615436.png

 

 

0 Kudos
Message 1 of 14
(293 Views)

In LabVIEW, data flows out of the control as soon as possible. This means that your STOP button and corresponding local variables, are read at the beginning of each loop. What you need is something that interrupts the loop or times out after your specified wait time. You can try an event structure, notifier, or queue, and see which one you like the best. They all have "timeout" inputs which will replace your "wait (ms)" functions.

0 Kudos
Message 2 of 14
(286 Views)

Of course the question is why you would need all these loops? You can do the same thing (and with better synchronization and less potential of race conditions and without the need for local variables) by using one single loop running at 100Hz and updating certain values only at certain multiples of the iteration terminal.

 

Obviously, your code is quite a silly example. Can you give us more background on what you are actually trying to do? What are typical values for the controls? What is the purpose of it all?


LabVIEW Champion. It all comes together in GCentral GCentral
What does "Engineering Redefined" mean??
0 Kudos
Message 3 of 14
(221 Views)

Here is an easy way to do what you are looking for using event structures. Some will say that you should not have multiple event structures but in this case it is very useful. The timeout for the event structure makes it work with different times but execute when you want it to. It also make it so if you press the exit button that all loops shut down even if the timeout has not elapsed.

 

Things can get more interesting from here (good and bad).

 

Multi Loop Stop with different time.png

 

Tim
GHSP
Message 4 of 14
(215 Views)

@aeastet wrote:

Things can get more interesting from here (good and bad). 


I would immediately recommend the following changes:

 

  • All "stop:mouse up" events should be "stop:value changed" events. (Since it is "latch when released", you can press down, move off, and release and it will not trigger. "Mouse up" events are only useful to e.g. trigger over indicators and disabled controls and thus somewhat dangerous)
  • The stop terminal needs to be in one of its event cases so the latch action properly resets the button to false when read by the code.

LabVIEW Champion. It all comes together in GCentral GCentral
What does "Engineering Redefined" mean??
0 Kudos
Message 5 of 14
(202 Views)

@altenbach wrote:

@aeastet wrote:

Things can get more interesting from here (good and bad). 


I would immediately recommend the following changes:

 

  • All "stop:mouse up" events should be "stop:value changed" events. (Since it is "latch when released", you can press down, move off, and release and it will not trigger. "Mouse up" events are only useful to e.g. trigger over indicators and disabled controls and thus somewhat dangerous)
  • The stop terminal needs to be in one of its event cases so the latch action properly resets the button to false when read by the code.

If you use the "stop:value changed event" you have to be aware that you are going to get an event for the down and up for the button push. You will effectively get two events for one button push. For the exit that is not a problem. If you are doing something more complicated you are going to need to handle the true and false condition. I use the mouse up event all of the time and do not have any issues. You just need to understand how it works. If that does not work then I use the value change. There are definitely times I would use the values change but for most button pushes I would not use the values change event.

 

Placing the boolean control inside of an event is a good idea but not really necessary for the example I gave you. Like I said you can go from here and things could get more complex and good or bad things can happen depending on what you are trying to do. Everything you do in your code will depend on what you end result needs to be. If you are going to turn this into a bigger program then it would be recommended to make sure you handle this correctly.

Tim
GHSP
0 Kudos
Message 6 of 14
(195 Views)
Solution
Accepted by topic author RaymondZhao2017

@aeastet wrote:
If you use the "stop:value changed event" you have to be aware that you are going to get an event for the down and up for the button push.

This is incorrect and only applies to "switch until" mechanical actions (which are rarely used). For latch action Booleans, you only get exactly one event trigger per press/release action. (same for plain switch actions).

 


@aeastet wrote:
Placing the boolean control inside of an event is a good idea but not really necessary for the example I gave you.

The relatively ugly side effect is that after stopping, the stop button is still down until the next run and if you have anything connected to it (yes, you don't in this example), that sink will receive a TRUE on the next run. If you would now mindlessly do a "make current values default", that stop button will be now true by default and only temporarily switch to false when pressed. Can of worms to debug 😉 (In my code below, the top loop would now always immediately stop)

 

As an additional comment, I would eliminate the event structure in the fastest loop (as long as it is reasonably fast), use the 10ms wait, and wire the stop button directly to the stop terminal. Less code!

 

altenbach_0-1609882853387.png

 

 


LabVIEW Champion. It all comes together in GCentral GCentral
What does "Engineering Redefined" mean??
Message 7 of 14
(189 Views)

@altenbach wrote:

You can do the same thing (and with better synchronization and less potential of race conditions and without the need for local variables) by using one single loop running at 100Hz and updating certain values only at certain multiples of the iteration terminal.


Here is a very simple example for that. (note the use of arrays, legends, and the absence of local variables)

 

altenbach_1-1609881918168.png

 


LabVIEW Champion. It all comes together in GCentral GCentral
What does "Engineering Redefined" mean??
0 Kudos
Message 8 of 14
(183 Views)

Dear altenbach,

I need one loop do data acquisition and controls, which is running at a maximum speed.

I also have moving average functions and other post-processing functions that I would like to run at a slower speed. 

That is why I need some different speed while loops.

0 Kudos
Message 9 of 14
(176 Views)

@RaymondZhao2017 wrote:

I need one loop do data acquisition and controls, which is running at a maximum speed.


OK, so you need exactly two loops. One for the time critical stuff and one for the post processing.

 

Your current architecture is a nightmare due to race conditions caused by overuse of local variables. Do you care if data is permanently lost?


LabVIEW Champion. It all comes together in GCentral GCentral
What does "Engineering Redefined" mean??
0 Kudos
Message 10 of 14
(169 Views)