LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Async Timer strange behaviour

Hi!

Could someone explain me why in this very simple sample and despites the fact I set the interval to 6 sec the timer start after 10-12 sec??!! Smiley Surprised

The timer runs just fine after the start (i.e. one event each 6sec) but why this" slow" start?

Gauthier.
0 Kudos
Message 1 of 11
(5,089 Views)

Hi!

It is no magic. Timer starts immediately after you set the enable attribute. You can see that the callback function is called if you put a breakpoint.
But, the initial value of the numeric control (0.0) and the value that you write in it in the first timer callback (timer_value, which is 0.0 initally) are the same.
You may change the callback code to

SetCtrlVal (PANEL, PANEL_ASYNC_TIMER, ++timer_value);

or you can change the initial value of the variable: timer_value = 1.0.

Hope this helps Smiley Wink

S. Eren BALCI
IMESTEK
Message 2 of 11
(5,081 Views)
Hi ebalci!

Thx, you solved the trick...

I have an other question. Smiley Tongue

Let's assume there are 2 timers with the same interval now like in the new sample.

I want to start them as following:

t0: start timer1
t0 + 3sec: start timer2

I think I can not use a Sleep between the 2 ASYNC_ATTR_ENABLED since the timers are launched in a separate thread; the sleep is in my main program so it does not affect the start of the timers in the thread:

SetAsyncTimerAttribute (ID_timer, ASYNC_ATTR_ENABLED, 1);
Sleep(3000);
SetAsyncTimerAttribute (ID_timer2, ASYNC_ATTR_ENABLED, 1);

The Sleep(3000) should be in the timer's thread?

Am I right ?
If I am, what could I use to get 2 timers with the same interval but shifted of x seconds?

Thanks for the help! Smiley Happy

0 Kudos
Message 3 of 11
(5,073 Views)

Gauthier, do you *really* need two separate timers for your application? If times are those you are depicting (several seconds per scan) you could maybe accomplish all your tasks with a single timer that scans one per second: something like this pseudocode that runs the timers every 10 seconds (timer 2 routine shifted 3 secs from timer 1 routine)

count++
if count == 3 execute routine for timer 2
if count == 10 {
   execute routine for timer 1
   count = 0
}



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 4 of 11
(5,065 Views)
Thanks for your answer Roberto.

I need to simulate an equipment and it has a cycle of exactly 6sec. In its cycle, a event occurs after 2.5sec exactly.

I have to respect these timings with the best precision (<50 msec would be great, my customer asks for 0 but it's impossible with windows...). That's why I thought I should use 2 timers: one for the cycle and one for the event.

However, I'm open to any suggestion, if someone has a better idea...
0 Kudos
Message 5 of 11
(5,044 Views)
Let;
t0 : cycle-1 start, then
t0+2.5 : event-1
t0+6 : cycle-2 start,
t0+8.5 : event-2
 
So, practically, the event occurs every 6 seconds.
Then, if it is only the "events" that you have to simulate a single 6-second timer will do.
 
Create the timer is suspended mode, use Delay(2.5) for the first event after user clicks START.
Then enable the timer and your timer fires every 6 seconds giving you the "event" moments.
 
I remember reading an article in NI website about the precision of the async timers, and they are very stable as far as Windows timers are concerned.
I am confident that you will get 50msec precision.
S. Eren BALCI
IMESTEK
Message 6 of 11
(5,035 Views)
Thx Ebalci!

I knew I have to set the timers at the same interval...
I was confused because this code doesn't work:

ID_timer = NewAsyncTimer (1.0, -1, 0, TimerCallback, 0);
ID_timer2 = NewAsyncTimer (1.0, -1, 0, Timer2Callback, 0);
GetCtrlVal (PANEL, PANEL_RATE_TIMER, &rate);
SetAsyncTimerAttribute (ID_timer, ASYNC_ATTR_INTERVAL, rate);
SetAsyncTimerAttribute (ID_timer2, ASYNC_ATTR_INTERVAL, rate);


SetAsyncTimerAttribute (ID_timer, ASYNC_ATTR_ENABLED, 1);     
Delay(2.5);
SetAsyncTimerAttribute (ID_timer2, ASYNC_ATTR_ENABLED, 1);

The 2 timers started at the same time... Weird!
Here is the trick:

ID_timer = NewAsyncTimer (1.0, -1, 0, TimerCallback, 0);
ID_timer2 = NewAsyncTimer (1.0, -1, 0, Timer2Callback, 0);
GetCtrlVal (PANEL, PANEL_RATE_TIMER, &rate);

SetAsyncTimerAttribute (ID_timer, ASYNC_ATTR_INTERVAL, rate);
SetAsyncTimerAttribute (ID_timer, ASYNC_ATTR_ENABLED, 1);     
Delay(2.5);
SetAsyncTimerAttribute (ID_timer2, ASYNC_ATTR_INTERVAL, rate);
SetAsyncTimerAttribute (ID_timer2, ASYNC_ATTR_ENABLED, 1);

Can someone explain me why setting the rate before the Delay changes the behaviour of the 2nd timer?!

0 Kudos
Message 7 of 11
(5,032 Views)
I do not think it is about where you set the rate.
It is about the enable part.
Maybe calling the delay function immediately after you enable first timer, somehow, prevents it from getting enabled.
Can you try the following:
 
SetAsyncTimerAttribute (ID_timer, ASYNC_ATTR_ENABLED, 1);
Delay(0.1);
ProcessSystemEvents();
Delay(2.4);
SetAsyncTimerAttribute (ID_timer2, ASYNC_ATTR_ENABLED, 1);


and as a second alternative
 
SetAsyncTimerAttribute (ID_timer, ASYNC_ATTR_ENABLED, 1);
SyncWait (Timer(), 2.5);
SetAsyncTimerAttribute (ID_timer2, ASYNC_ATTR_ENABLED, 1);
then tell me what happens?
S. Eren BALCI
IMESTEK
Message 8 of 11
(5,023 Views)
I just try your 2 suggestions and none of them is working: 2 two timers start at the same moment.
It's strange... I don't understand this behaviour.
I tried many things spend a lot of time before I found this trick... Smiley Sad

A big THX to you for your help anyway! Smiley Very Happy
0 Kudos
Message 9 of 11
(5,021 Views)
To get the resolution you need, why not try this (a small change to Roberto's suggestion):

Use a single async timer with a 0.5 second interval.

[pseudocode]

// Start sequence
count = 0
start timer

// In the timer callback
count++
if count == 5 execute routine for timer 2 // 2.5 seconds
if count == 12 {
execute routine for timer 1 // 6.0 seconds
count = 0
}

Good luck,
Colin.
0 Kudos
Message 10 of 11
(5,000 Views)