LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Figuring out the frequency of the while- and Timed loops

Solved!
Go to solution

I am using CompactRIO for an application and I am getting values from the Main program from different clusters and reading them in another VI in the same project using the shared variables function then saving them in measurement file.
I used a while loop and wanted to check the frequency it's working with and I saw that I am getting around around 2000 samples per minute so its running with around 32Hz ( I calculated it by dividing the number of samples by 60s). I thought having my program under the FPGA Target would let me sample with much higher frequency.
I then used the timed loop to sample with 1KHz but looking at my measurement I am getting one sample per second, I thought I should be getting thousand per second. It's either I am interpreting my measurement wrong or I'm misunderstanding how the sample rate works in the loops.

I added my code in case it helps you to give me a better answer  ( which is very messy, I would be thankful if someone could give me tips how to program in a better way but that's not my main problem now)

0 Kudos
Message 1 of 15
(1,945 Views)

You put the clock of the SCTL to 1kHz, and the period to 1000 ms.

 

1 sec. seems reasonable as a result.

Message 2 of 15
(1,932 Views)

BTW. instead of polling globals, I'd look into FIFO's. Those are lossless and will increase reliability a lot.

 

When polling values, you will loose samples unless the producer and the polling is perfectly synchronized.

 

With FIFO's, your only concern is reading it fast enough on average. If the polling stagnates for a while, it doesn't matter. The FIFO will fill up. If you read it before it fills, all your data is safe.  

0 Kudos
Message 3 of 15
(1,927 Views)

Shouldn't that mean getting thousand samples per second? I am getting only one per second.

0 Kudos
Message 4 of 15
(1,926 Views)

A period of 1 sec. (1000 ms) means 1 sec between samples.

 

1 ms period means 1000 periods per second.

 

1 ms = 0.001 sec.

 

f = 1/T. But f is not the clock cycle. If you want 1000 Hz, you need T = 1/1000. T is the period here, and should be 0.001 sec. or 1 ms.

 

For reference (I'm not making this up 😉:

http://www.differencebetween.net/science/mathematics-statistics/difference-between-period-and-freque...

Message 5 of 15
(1,922 Views)

Thanks a lot for clearing that up for me! I misunderstood the cycle clock and thought its the frequency of the loop. Is the period here any different than setting a wait function in a normal while loop?
also thanks for the tips about using FIFOs

0 Kudos
Message 6 of 15
(1,909 Views)

@Amr95 wrote:

 Is the period here any different than setting a wait function in a normal while loop?


That is conceptually the same.

 

Under the right conditions, a 1 ms wait in a loop will give you a 1 kHz iteration period.

0 Kudos
Message 7 of 15
(1,888 Views)

Thanks again! 
one last question, I now set the frequency to be 1000Hz by following what you said.
But in my measurement file I noticed that only 249 samples were saved per second not 1000, is it due to the fact that I am using shared variables so the data is getting lost in between the cycles? or could it be that the samples are being generated in the main program with a frequency less than 1000Hz?

0 Kudos
Message 8 of 15
(1,884 Views)

@Amr95 wrote:

But in my measurement file I noticed that only 249 samples were saved per second not 1000, is it due to the fact that I am using shared variables so the data is getting lost in between the cycles? or could it be that the samples are being generated in the main program with a frequency less than 1000Hz?


Yes. (It could be either, but i'd guess on the 1st)

Don't sample and write to file in the same loop. Send the sampled data to a queue and have Another loop that writes to file from this queue.

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 9 of 15
(1,879 Views)

@Amr95 wrote:

But in my measurement file I noticed that only 249 samples were saved per second not 1000, is it due to the fact that I am using shared variables so the data is getting lost in between the cycles? or could it be that the samples are being generated in the main program with a frequency less than 1000Hz?


I put my money on the polling loop that is to slow.

 

Setting the timed loop to 1 ms doesn't guarantee 1kHz execution. File IO can be slow, especially writing small amounts of data can be relatively slow.

 

You can check by examining the info inside the timed loop. Comparing Expected start and actual start for example. Simply creating an indicator for the loop counter 'i' often is enough. It should increase 1000 per second, if it's 249 it's wrong.

 

Even if the the main program only generates 249 samples/s, you'd get 1000 samples in your logging loop if it's fast enough. You'd simply get lots of duplicate values (4 on average).

 

Another reason for the FIFO solution. Put things on a FIFO (or queue if it's the same target) and write to file in a slower cycle, but writing more data at once. You wouldn't be able to do that by polling, unless you make a 3rd loop. A loop producing data, a fast loop polling and putting things on a queue, a  3rd loop consuming the queue data. Seems too complicated though.

0 Kudos
Message 10 of 15
(1,877 Views)