LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Best method/practice to run two loops (read + write) simultaneously in sync

Solved!
Go to solution

Hello,

 

I thought I had a program working that could write and read simultaeously with reasonable accuracy, but have discovered it may not be as reliable as I thought (it sometimes gets out of sync/jumps steps).

 

Is there a good guide, tutorial or recommended method to do this reliably, in sync and efficiently?

Should I use notifiers? and/or timed loops?

 

I have a write loop, which sends steps to some type of equipment (stepper motor or magnet etc) at no more than about 100 Hz.

I have a read loop, which reads data from a detector (eg photodiode, the signal being dependent on the write step), at no more than about 1000 Hz (the read loop runs faster than the write loop)

 

I then pass information from one loop to the other (one loop writes to a group, the other reads from it).

I then save data to a file by saving the contents of the graph.

 

Thanks

 

0 Kudos
Message 1 of 14
(4,890 Views)

Producer consumer loop.  It's all over this website Smiley Happy

 

Can you post your code for review, there are more than one way to solve this requirements set.

 

Perhaps you want to read/write in a 10/1 ratio.  You could use a state machine to run in that order, thus P/C not needed.

-------
Mark Ramsdale
-------
0 Kudos
Message 2 of 14
(4,887 Views)

Do the read and the write have to be truly simultaneous?  What do you consider reasonable accuracy?

 

Is there a reason they can't be put into the same loop?

 

You could look at the Rendezvous functions.  They cause separate sections of code to wait until all the sections arrive at the same rendezvous point in execution.

0 Kudos
Message 3 of 14
(4,879 Views)

Use AI Sample Clock from the read operation to drive a Counter task Configured as a clock divider and the over-flow of the clock can them be used to clock the DO task.

 

I have done the above and it worked fine. In one case I skipped the divider so I could have a one-to-one relationship between stimulus and response that was used in a cyclic voltametry project used to characterize the endorphins in a rats brain.

 

 

Spoiler

Yes I brought that project up only because I like the term "cyclic voltametry". Smiley Tongue

 

 

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 4 of 14
(4,876 Views)

There are two possible explanations for your 'jumps':

 

a) Race condition. Everything works fine, but due to race conditions, you have inconsistent data.

b) You are running in issues because of non-determinism of the OS.

 

I would expect b) to be "in charge" here. You should consider a real time system.

 

Norbert 

Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
0 Kudos
Message 5 of 14
(4,871 Views)
Solution
Accepted by Jeff_Tech

 


@Jeff_Tech wrote:

(1) .... in sync....

(2) ... (the read loop runs faster than the write loop) ...


 

Aren't these two statements a contradiction?

 

There is significant information that you have left out, for example is it sufficient for the two to be in sync or is it also necessary that they occur at a very regular rate.

In what way do the two depend on each other? Is this a control application where the output depends on the input in some way or is it simply a excitation/response pair.

 

Are you doing software timed single point acquisition?

 

Does it need to run for extended periods of time or it it just a finite sweep each time?

 

In any case, the right thing do probably do is run both hardware timed from the same clock. Setup the output array for the AO and arm the AI, then start both running from the same clock (e.g. you could tie the AI conversion to the AO clock). Read the buffered AI data at leasure in a loop that does not need any special timing.

 

Can you show us some code so we get a better feeling what you are doing?

Message 6 of 14
(4,860 Views)

I think his term "sync" refers to the concept called "phase lock loop". I would expect his code to do something like this, if it contains any sync-mechanism at all.....

 

Norbert 

Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
0 Kudos
Message 7 of 14
(4,847 Views)

Hello,

 

Thanks all for your help. The response has been amazing. I will look into P&C, state machines, rendezvous functions(?) etc.

Are there any good summaries/tutorials/samples of these?

 

I thought about posting my code, but it is (needlessly?) complicated.

It is basically a combination of two Labview DAQmx tutorial/samples:

'Cont Acq&Graph Voltage - Int Clk.vi' (the buffered analogue read) and

'Gen voltage update.vi' (the simplest analogue write)

 

They run in separate loops, because they run at different rates. You are right that I shouldn't have said they are in sync, as one does run faster than the other.

 

 

0 Kudos
Message 8 of 14
(4,843 Views)

The program usually runs for the good part of an hour (15 mins up to about an hour).

 

By 'in sync' I meant the rates are kept the same once the run has started, and each must step in time with the other (so if the rates were a fastor of 3 different, one loop would take 3 steps for every 1 of the other).

0 Kudos
Message 9 of 14
(4,831 Views)

look at my post above.

 

By changing the clock task, you can control the ratio of AI to AO.

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 10 of 14
(4,827 Views)