LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Timed Loop Vs While loop

Solved!
Go to solution

I just wanted to create a standard AI data acquisition code that can be used with required modifications in  different projects.

Looking at the scheme i am sure there will be lots of eyebrows that go up as to why a Timed Loop is used instead of a while loop. I am coming to that point only .. I just ran a serious of rough evaluations on a Intel Pentium dual core PC @1.8 GHz making sure no other application was running apart from the OS related code ( WIN 7 ) . Primarily i was interested to find out if running Timed Loop on a non RTOS system was imposing any additional load on the CPU in comparison to a simple while loop.  Following were the observations : ( I dont claim these to be any kind of bench mark .. just a relative comparison and thats it ) Card used is a PCI-6229. 

VI not running : CPU Usage 0 to 1%

While Loop :

VI Starts to run : 5 to 7% 

Display Starts to scroll : 12 to 15%

FP minimized : 0 to 1% 

Loop timing fluctuating between 49 to 50ms

 

Timed Loop :

VI Starts to run : 5 to 7% 

Display Starts to scroll : 12 to 13%

FP minimized : 0 to 1% 

And the Loop timing was rock steady at 50ms

 

( In both cases the CPU was pushed around more by the display update than the code itself) 

 

So quite really why is it that the Timed Loop is seen as a strict no no for non RTOS systems ? In fact there is no specific LV guideline ( that i have seen ) says that Timed Loops are only for RTOS code. 

 

I just want to know if there is anything more than the demand on CPU that is a concern when using Timed loops on regular OS.

 

AI_HW Triggered.png

  

Raghunathan
LabVIEW to Automate Hydraulic Test rigs.
0 Kudos
Message 1 of 7
(6,121 Views)

Hi Raghunathan,

 

you may use the TWL on a non-RT system - but most often it will not help to solve any problems better then a simple while loop with a wait inside…

 

On your specific case:

- As you use DAQmx with a strict sample rate and you also read a know block size the loop iteration time is ONLY defined by DAQmxRead function! It will wait for the samples to arrive - and this will take always the very same time as this is hardware-triggered!

- I would rather use a producer-consumer scheme to decouple DAQ loop from data processing and data display. This way your DAQ loop will also be "rock steady" as much as possible…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 2 of 7
(6,116 Views)
Solution
Accepted by topic author MogaRaghu

You are waiting more than anything, so I would not expect any real differences in your CPU usages here.  However, some things the Timed Loop does:

1. Sets everything inside of the Timed Loop to be a single threaded process.  You cannot do anything in parallel inside of the loop.

2. Sets a priority to the loop that I have seen mess with Windows (starve OS processes that the rest of my application needed).

3. Generally adds more overhead for little to no gain on Windows.  Especially in your case, as was already stated, the DAQmx Read will be what determines your loop rate.  So just keep it simple and use the standard While loop.

 

The Timed Loop was designed for "time critical" code.  Again, your DAQ is already handling anything that is time critical in the hardware (mainly the sample rate).  So we don't really care about the software being time critical.  The software just has to be fast enough to keep the buffer from getting full.  If you are doing a control loop, then Windows just throws up a huge safety concern, so that should be moved to a RT or FPGA.  So, again, the Timed Loop just does not make sense in a non-RT environment.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 3 of 7
(6,099 Views)

Thanks to both of you @GerdW and @crossrulz. 

Inside of a Timed Loop parallel processing is not possible ! Well its this kind of an insight I was after ... such finer details are not detailed in normal documents or even if done they don't register as in a context sensitive discussion. 

Also if there was one thing that I "thought" was an advantage of TL over WL is its ability to be prioritized !! So when I have the TL running in MAIN as the main data producer ( gets data from AI via DAQMx) I set its priority as 100. And the rest of TL in Sub Vis which consume this data are set for 75, 50, 25 priority depending on requirement.

 

But what I gather is ... its better to leave these to  LV and OS to worry about by using simple WL and not constrain the setup unnecessarily  

 

And one last thing .. you have seen the way the AI data is collected....So which do you think is the best way to pass the same to another While loop in a Sub Vi ? The options ( that I know ) are :

1. Functional Globals

2. Globals

3. Queue

4. Shared Variables

( Missing an occasional data is not a big concern as the process ( like temperature ) does not vary so fast ) 

Raghunathan
LabVIEW to Automate Hydraulic Test rigs.
0 Kudos
Message 4 of 7
(6,064 Views)

Use queues.

You forgot locals, notifiers and channels…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 5 of 7
(6,055 Views)

Yeah ... Queues . I am big fan of them.  Till date have never tried Notifiers or Channels ... maybe will look at them when time permits . 

 

Thanks

Raghunathan
LabVIEW to Automate Hydraulic Test rigs.
0 Kudos
Message 6 of 7
(6,043 Views)

@GerdW wrote:

Use queues.

You forgot locals, notifiers and channels…


The Streaming channel would work (equivalent to a queue).  Depending on where you are sending the data, I have also used User Events.  But the queue would be my first choice.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 7 of 7
(6,031 Views)