From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to Snyc two While loop?

Hi Friends,

 

I have tried to sync two while loops, but when I stop the bottom they show different time. I think time must be same since both loop have same number process to execute.

 

a. How can I sync my while loop clock? so that during the measurement I can get the same time for reading two different data input from two different while loop?

b. There is difference in time between two while loop, Can we accept that much error?  

c. I have read some previous thread they say,  if there is some data exchange (like there is output variable from one loop to the other) between two loops then they will never sync. Is that true? 

d. I tried to put them in one big while loop then the time for the lower one takes more and making negative time difference. when i remove the big while loop then upper loop takes more time in-compare to the lower one.  since the time i am measuring is not the execution time why there is a difference?

 

I have enclosed the block diagram? Please look for your suggestion?

2.png

 

 

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

I am not quite sure what the use case is for this but I will answer as best I can.

 

Your VI looks like it will run forever because you have wired false constants into the loop condition terminals. When you say that you are stopping your loop are you pressing the abort button?

 

You have nothing slowing down your loops so they are running as fast as they can, probably using two cores of your CPU, due to non-deterministic nature of windows CPU allocation it is unlikely that both of these loops are running at the same speed, which is why you are seeing a different numbers in your Numeric and Numeric 2 indicators.

 

I think you have misunderstood how local variables work in LabVIEW. The local variable is a data link to a front panel item (indicator/control). You do not need to write to the local varaible within the while loop. Also you have not defined your dataflow very well so the Difference in Time indicator is evaluated right at the beginning of running the VI probably with the last values from when you last aborted the vi. It is possible that the values of Time 1 and 2 could have been overwritten by the time that it has been evaluated but is very unlikely.

 

I have modified your VI to something a little better. As I mentioned at the beginning I am not 100% sure what you mean by syncing your while clocks, but I suspect that using the wait until next multiple would achieve what you are after. Read the detailed help for more details about what this does.

 

Answers to your questiosn:

a)Use the wait until next multiple function

b)We don't know how nmuch error you can accept, only you can answer that depending upon your application. I suspect that you are seeing differences in time because of your use of the abort button

c) I think you are talking about race conditions here, in which case, yes that is right. Be very careful transmitting data between while loops with local variables.

d) Putting them both in a while loop shouldn't make a difference, although it sounds like some nuances of the LabVIEW compiler. It shouldn't affect you if you follow the dataflow paradigm of LabView properly though.

Message 2 of 6
(2,953 Views)

You cannot rely on independently running while loops to be synchronous. You need an active mechanism.

 

You might get better results using timed loops, you can even synchronize their start.

 

Just from looking at your code, you don't seem very experienced (Your difference calculation executes in parallel to the rest of the code, so the difference is unpredictable, even if the loops are fully synchronized), so my guess is that you might be barking up the wrong tree with your questions.

 

If things need to be synchronized, maybe you can place all code into one single while loop, eliminating the current two?

On a non-RT system such as windows, loop precision is not guaranteed anyway. What are these loops actually doing in your real application?

If this involves DAQ IO, maybe you can run two hardware timed tasks from the same clock.

 

Since you have no wait, your loops might take turns spinning many times before the other loop gets a chance. Have a look at this old discussion and the example two posts down from it.

 

In any case, take a step back and explain on a higher level what you are trying to achieve. There is probably a much better solution than trying to herd cats several while loops (see also).

 

0 Kudos
Message 3 of 6
(2,929 Views)

Occurrences. The first thing I learned form the Synchronization Palette.

 

Mock-up below.

 

snip.png

mcduff

Message 4 of 6
(2,921 Views)

@mcduff wrote:

Occurrences. The first thing I learned form the Synchronization Palette.

 


... only if you can guarantee that the code in each lower loop can complete in 100ms 100% of the time (or whatever the desired pace is. Remember, the original post had greedy loops with no waits whatsoever). 😄

 

(But yes, this was one of the many "active mechanism" I had in mind earlier)

Message 5 of 6
(2,917 Views)

@altenbach wrote:

@mcduff wrote:

Occurrences. The first thing I learned form the Synchronization Palette.

 


... only if you can guarantee that the code in each lower loop can complete in 100ms 100% of the time (or whatever the desired pace is. Remember, the original post had greedy loops with no waits whatsoever). 😄


Correct. I think of synchronization between loops as a trigger, what happens in between triggers obviously effects the synchronization.

 

mcduff

0 Kudos
Message 6 of 6
(2,913 Views)