LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Time elapsed between each iteration

Solved!
Go to solution

Hi,

 

I am trying to implement a control system using two while loops, for each iteration I want to write the elapsed time for one iteration. Moreover, I want to write each time lapse to an array. I tried using Elapsed Time. vi but I could get so far is the elapsed time for the whole execution. I know this should be simple but I need help.

 

Thanks,

 

Serkut. 

0 Kudos
Message 1 of 13
(11,603 Views)
Solution
Accepted by topic author serkut17

(It does not sound like such a good idea to record the elapsed times to an array forever. At one point you'll run out of memory. ;))

 

Still, here's a quick draft of some code that measures and records the loop time to an array. see if it makes sense. (LabVIEW 2010)

Message 2 of 13
(11,590 Views)

Hi,

 

I also am trying to measure the time elapsed between voltage measurements.  I am confused in your code, though, by the first value that the tick count outputs.  In the first iteration, the elapsed time is zero because the first value is subtracted from itself.  Why isn't the first value zero?  Afterall, what time has elapsed before the tick count outputs a value?

 

I want to measure 2 voltage values, and then know the time in between the two measurements.  I have tried setting up a while loop with shift registers to do this but, I am not sure if I am getting an accurate time.  I am not sure about the start and stop of the timer.  What is the ELAPSED TIME function actually measuring?  Can you look at the attached code and suggest how to measure the time elapsed between voltage measurements?

 

I need to get the change in voltage divided by time, sort of like a velocity and use this "voltage velocity" inside the WHILE loop, in a different calculation that is not shown in the attached vi for simplicity.

 

Also, I need the time in milliseconds.

 

Thanks,

Dave

 

 

0 Kudos
Message 3 of 13
(11,531 Views)

 

I made two changes to altenbach's example so that it gives the expected value on the first iteration (see attachment)

 

Can't look at your code (haven't loaded 2011 yet) but I would not recommend using the Elapsed Time function. It is evil Smiley Wink.

Ok, the Elapsed Time function has it's place but also has many side-effects and isn't very accurate.

 

Use the Tick Count (ms) function to get millisecond resolution.

 

Now you seem to know what you want to do (get a start time, get an end time, then subtract end from start for elapsed time), but you are having trouble understanding how to get the start and end to happen at the right times.

 

To do this you need to use data flow to control when times are acquired.

 

If you can run a wire from the first read to the second read - you can use a flat sequence structure to force the desired data flow (see Example 1).

 

If you can't run a wire - use a simple Action Engine Timer (see Example 2).

 

 Read Interval.png

Attached is a ms timer that can be used as shown above.

 

steve 

 

 

--------------------------------------------------------------------------------------------------------------------------
Help the forum when you get help. Click the "Solution?" icon on the reply that answers your
question. Give "Kudos" to replies that help.
--------------------------------------------------------------------------------------------------------------------------
Download All
Message 4 of 13
(11,506 Views)

OK, thank you so much for the advice.  

 

With respect to your modified vi, "Record Loop Times" are you suggesting that I put my DAQmx READ vi inside the sequence structure?  If so, would it go in the second box, where the TICK COUNT is found? 

 

Or, instead, is this sequence/shift register structure measuring the total execution time of the WHILE loop independent of the the DAQmx vi?  In this case, there should be nothing placed in the sequence structure. Will the attached code deliver the time between voltage measurements?

0 Kudos
Message 5 of 13
(11,491 Views)

If you saved your code for LV2010 (File>>Save for previous version) I could look at it.

 

A few concepts to keep in mind:

 

  • A function can only run when all of it's inputs are valid. This is also true for frames in a sequence structure.
  • If a function (like Tick Count (ms)) has no inputs, you have to tie it to something that does have inputs to control it's execution.
  • If the flow of execution reaches two functions that have valid inputs you can't guarantee which will execute first.

execution.png

--------------------------------------------------------------------------------------------------------------------------
Help the forum when you get help. Click the "Solution?" icon on the reply that answers your
question. Give "Kudos" to replies that help.
--------------------------------------------------------------------------------------------------------------------------
0 Kudos
Message 6 of 13
(11,479 Views)

I saved this into 2010.  Can you see it?  Sorry about the screwy filename...there is some bug, I have learned from NI Telehpone support with the Save as previous versions options....

Dave

0 Kudos
Message 7 of 13
(11,468 Views)

After looking at your code and getting a better idea of what you are trying to do, I see these issues :

 

1) The first time through your loop the calculated data is always wrong since you need 2 measurements

2) The time it takes for Windows to ask for and get the DAQ data is indeterminate (obviously why you wanted to time it)

3)  Making accurate measurements around 5 ms is not super reliable using the Tick Count function (1ms resolution is 20% of 5 ms)

 

I'd like to suggest doing the measurements a bit differently.

 

I see that you are using a 1000hz sample clock. This gives you a sample every 1 ms.

If you want to look at voltage measurements that are 5 ms apart, take 5 samples at a time and compare the first and last samples.

 

This solves the issues I listed above:

1) The first time the loop executes your calculated data is correct (since both measurements are available)

2) You don't care how long windows takes - you know the time difference between readings - now elapsed time can be a constant in your program

3) You are using the DAQ sample clock for timing accuracy which is much better than anything you can do in Windows

 

Attached is your program modified to use this approach. Since both measurements are available in each iteration of the loop, you don't need shift registers anymore. I could only test it using a simulated DAQ device so I can't be sure all the settings are correct.

 

I hope this helps,

steve

 

--------------------------------------------------------------------------------------------------------------------------
Help the forum when you get help. Click the "Solution?" icon on the reply that answers your
question. Give "Kudos" to replies that help.
--------------------------------------------------------------------------------------------------------------------------
0 Kudos
Message 8 of 13
(11,452 Views)

Hi Steven,

Thanks so much for the new code.  Let me offer a couple of ideas.  I'm quite new to Labview, so, I appreciate your patience.  I make LOTS of mistakes.

 

1.) I apologize if I lead you to believe that I need a measurement every 5ms.  In fact, the device we are trying to control with this vi can accept commands at about 100 ms intervals, so, we need to measure no more than 10 voltage values per second.  The minimum time we would need between measurements is about 100ms or more.  Also, the voltage measurement device has a maximum speed of 14s/sec, so, the rates listed in your code far exceed the hardware capability.  Sorry I didn't detail that for you.  Again, I am struggling to learn how timing works in Labview.

 

2.) You are right that the first calculation will always be wrong because the first measurement doesn't exist yet.  I prefer to eliminate that issue, although, our mechanical system moves sufficiently slow that we might be able to live with this issue.

 

3.)The 1000hz sample clock is the default that Labview puts in.  I can change it.  Actually during my testing, (because of our slow sampling rate requirement) I was setting the sampling rate to 10Hz.  Sorry I didn't indicate that when I submitted the code to you.  We have been using 10 for rate, and 3 for samples/channel.  The purpose of this vi is to generate motion control commands based on the changing voltage measurements.  The controller for our motion device cannot accept commands any faster than 10 per second, so, we don't want to be calculating and sending commands any faster than it can accept.

 

Would you mind answering a few questions to help me up the learning curve?

 

1.) Given the slower sampling speed that you were applying, can I simply change the "Time Elapsed" to 100 and get the result I want?  Is the "Time Elapsed" function dictating the time between voltage measurements?

 

2.)  Lastly, I am studying the vi you sent, and I cannot understand where the time difference is coming from.  It appears that there are 5 samples being collected at 5ms intervals, which would make sample 1 and sample 5 separated by 25ms, but, your comment says samples 1 and 5 are separated by 5ms.

 

3.) Also, is the DAQmx Read function outputting a 1-D array?

 

4.) Will this code measure the time difference between successive voltage measurements?  If I understand it correctly now, it appears to return the time difference between samples 1 and 5.  On subsequent sampling sets, does it return the time difference between sample 5 and 1?  Or does it start over with sample 1 through 5 and act only that array?  In other words, what I need is for every measurement to be compared to the previous one like this...

 

Measure V1

Measure V2

Subtract V2-V1

Measure V3

Subtract V3-V2

Measure V4

Subtract V4-V3

Measure V5

Subtract V5-V4

Measure V6

Subtract V6-V5

Measure V7

Subtract V7-V6

Measure V8

Subtract V8-V7 etc. 

 

And of course, we want to know the time between each voltage measurement so we can calculate the rate of change etc..

 

Will the code you offered do this?  As I read your vi, it appears to do this...correct me if I am wrong...

Measure V1

Measure V2

Measure V3

Measure V4

Measure V5

Subtract V5-V1

Measure V6

Measure V7

Measure V8

Measure V9

Measure V10

Subtract V10-V6...is this a correct understanding of your code?  If so, I am losing the connection between V5 and V6.

 

Thanks again for your help.  I really need it!!!

 

Dave

0 Kudos
Message 9 of 13
(11,438 Views)

Dave,

 

After seeing that your timing requirements are not extreme, I decided to go back to the way you were taking the measurements.

The attached version may fit your needs.
 
The first time through issue is solved by taking an initial reading on the first iteration of the loop.
Elapsed time is calculated as: The start time of the current voltage reading minus the start time of the previous voltage reading.

 

I added a control that lets you set the desired Elapsed Time called "Voltage Read Interval (ms)" (Named this way because that's

what it sets). This time will be achieved as long as all functions in the loop execute faster than this setting. In any event the

actual time it took is measured. 

 

NOTE: When you add your motion control code to the loop, it will execute at the same rate as the voltage measurements.
 
So if you set the "Voltage Read Interval" to 100 (ms), this is what should happen:

 

a) Take Initial Voltage Measurement. (This becomes the first Previous Voltage Reading.)
b) 100 ms after the  Previous Voltage Reading was started, Get the Current Voltage Reading
c) Do calculations and send motor control command
d) The Current Voltage Reading becomes the Previous Voltage Reading (through a shift register). 

e) Goto "b)"

 

Check it out,
steve

-------------------------

>Hi Steven,
 
>Thanks so much for the new code.  Let me offer a couple of ideas.  I'm quite new to Labview, so, I appreciate your patience. 
>I make LOTS of mistakes.

 

 Mistakes are how we learn. I figure if I'm not making mistakes I'm just not trying hard enough.  
 
>1.) I apologize if I lead you to believe that I need a measurement every 5ms.  In fact, the device we are trying to control with

>this vi can accept commands at about 100 ms intervals, so, we need to measure no more than 10 voltage values per second. 

>The minimum time we would need between measurements is about 100ms or more.  Also, the voltage measurement device

>has a maximum speed of 14s/sec, so, the rates listed in your code far exceed the hardware capability.  Sorry I didn't detail that

>for you.  Again, I am struggling to learn how timing works in Labview.
 
Hey I struggle with LabVIEW timing too at times - and I've been doing it awhile. Practice and lots of testing help a lot.
And of course asking questions.
 
>2.) You are right that the first calculation will always be wrong because the first measurement doesn't exist yet.  I prefer to
>eliminate that issue, although, our mechanical system moves sufficiently slow that we might be able to live with this issue.

 

You don't have to live with it. There's a simple fix. Just take an extra initial measurement in the first iteration of your loop.
 See attached version.
 
>3.)The 1000hz sample clock is the default that Labview puts in.  I can change it.  Actually during my testing, (because of our slow
>sampling rate requirement) I was setting the sampling rate to 10Hz.  Sorry I didn't indicate that when I submitted the code to you. 
>We have been using 10 for rate, and 3 for samples/channel.  The purpose of this vi is to generate motion control commands based

>on the changing voltage measurements.  The controller for our motion device cannot accept commands any faster than 10 per

>second, so, we don't want to be calculating and sending commands any faster than it can accept.
 
 Useful info.
 
>Would you mind answering a few questions to help me up the learning curve?
 
 I'll try.
 
>1.) Given the slower sampling speed that you were applying, can I simply change the "Time Elapsed" to 100 and get the result I

>want? 
>Is the "Time Elapsed" function dictating the time between voltage measurements?
 
 To the first part - very close, but not quite - your tick count and wait weren't quite right - see the new version .
 To the second part - well actually it's the wait (ms) function - the Time Elapsed indicator "reads" the time between voltage

 measurements.  
 
>2.)  Lastly, I am studying the vi you sent, and I cannot understand where the time difference is coming from.  It appears that there
>are 5 samples being collected at 5ms intervals, which would make sample 1 and sample 5 separated by 25ms, but, your comment

>says samples 1 and 5 are separated by 5ms.
 
 The collected 5 samples were taken by the DAQ at 1 ms (1000hz) intervals. 5 ms later it would get another group of 5 samples that

 were acquired at 1 ms intervals.
 
>3.) Also, is the DAQmx Read function outputting a 1-D array?
 Yes (I thought you said lastly on #2 Smiley Wink)

--------------------------------------------------------------------------------------------------------------------------
Help the forum when you get help. Click the "Solution?" icon on the reply that answers your
question. Give "Kudos" to replies that help.
--------------------------------------------------------------------------------------------------------------------------
0 Kudos
Message 10 of 13
(11,397 Views)