Recent

Author Topic: TTimer accuracy at 20ms - vs. Epik timer  (Read 16567 times)

dbaxter

  • New Member
  • *
  • Posts: 13
TTimer accuracy at 20ms - vs. Epik timer
« on: September 27, 2012, 06:10:26 am »
I need a pretty high accuracy timer in an application. I looked, but could not find any documentation about how well ttimer works in the 15-20 msec interval range. The Delphi ttimer is not good at that range and I don't know if that design carried over to Lazarus. There are several comments that Epik has the accuracy, but no ontimer event. Can anyone share an example of using a thread or something to get an ontimer event for Epik?

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: TTimer accuracy at 20ms - vs. Epik timer
« Reply #1 on: September 27, 2012, 06:48:12 am »
If you want to make a custom timer, you can use onIdle event of TApplicationProperties. It's a component on Additional tab. Set Done:=false; and do tasks. What these tasks are, you might get hints from my game engines Idle()-function at:
http://code.google.com/p/nxpascal/source/browse/trunk/src/nxGame.pas#106

That Idle is designed to run on same intervals with millisecond accuracy. But if you need long term time accuracy, you should base it on TDateTime Now() funtion instead of GetTickCount().

I'm sure there's also examples around on how to make a TThread, same timing rules apply to it, except that you propably don't need to call Sleep(), not sure. Put it on if it's using CPU like no tomorrow  ;)

avra

  • Hero Member
  • *****
  • Posts: 2586
    • Additional info
Re: TTimer accuracy at 20ms - vs. Epik timer
« Reply #2 on: September 27, 2012, 02:06:06 pm »
If you are developing for Windows, then you should know that having a regular timer interval less then about 10ms is not working (e.g. setting 1ms and 10ms has the same effect). If you need <10ms then use custom multimedia high precision timers which I think do not ship with Lazarus by default. More info here: http://www.lazarus.freepascal.org/index.php/topic,16530.0.html
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

dbaxter

  • New Member
  • *
  • Posts: 13
Re: TTimer accuracy at 20ms - vs. Epik timer
« Reply #3 on: September 27, 2012, 03:59:45 pm »
I'm aware of the limits of the Windows timer and if one is looking for an excellent replacement, try the TSVAtimer component. I should have noted that I am working with OSX Mountain Lion this time.
Thank you, user137, but I was trying to avoid putting the burden of checking the elapsed time on the main program with a loop, and instead use an OnTimer event so the burden is on the component.

Ttrindade

  • Newbie
  • Posts: 1
Re: TTimer accuracy at 20ms - vs. Epik timer
« Reply #4 on: October 02, 2012, 04:56:48 pm »
As my development uses linux, this solution is not feasible in my case...
Follow on search of an OnTimer event using the EpikTimer and i ask the same question:

Can anyone share an example of using a thread or something to get an ontimer event for Epik?

thanks

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: TTimer accuracy at 20ms - vs. Epik timer
« Reply #5 on: October 02, 2012, 05:41:07 pm »
Thank you, user137, but I was trying to avoid putting the burden of checking the elapsed time on the main program with a loop, and instead use an OnTimer event so the burden is on the component.
Depends what you define "burden" as. If you mean weight on cpu, then it's always more efficient to use manual timing as i described. Sleep(1) is good to put while waiting for next tick. You can have timing at precisely 20 millisecond intervals with 0% cpu effective use if there's not many tasks per tick.

Or if "burden" means just code structuring, you might want to make it a TThread and implement it in other unit. That way code in main program becomes minimal.

For what i read about Epiktimer, it is not designed to be timer for events http://wiki.freepascal.org/EpikTimer
You need to use something else.

If you want to use threads, you can check there: http://wiki.freepascal.org/Multithreaded_Application_Tutorial

mas steindorff

  • Hero Member
  • *****
  • Posts: 576
Re: TTimer accuracy at 20ms - vs. Epik timer
« Reply #6 on: October 02, 2012, 07:35:36 pm »
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

Ocye

  • Hero Member
  • *****
  • Posts: 518
    • Scrabble3D
Re: TTimer accuracy at 20ms - vs. Epik timer
« Reply #7 on: October 04, 2012, 12:04:34 pm »
Lazarus 1.7 (SVN) FPC 3.0.0

dbaxter

  • New Member
  • *
  • Posts: 13
Re: TTimer accuracy at 20ms - vs. Epik timer
« Reply #8 on: October 04, 2012, 04:26:42 pm »
Ocye, you beat me to the posting. I did just discover that, like Dorothy in the Wizard of Oz - I had the power all along. For others:
1) put cthreads and fptimer in your Uses clause
2) add these to your program declarations:
      FTimer: TFPTimer;
      procedure DOTick(sender:TObject);
3) add these to the form create procedure:
     FTimer := TFPTimer.create(self);
     FTimer.OnTimer := @DoTick;
     FTimer.interval := 20;  // optional here, could be set later
     FTimer.enabled := false;  // if you want to start it later
4) what you want the timer to do goes in DoTick;
5) in FormClose put FTimer.Free;



     

Ocye

  • Hero Member
  • *****
  • Posts: 518
    • Scrabble3D
Re: TTimer accuracy at 20ms - vs. Epik timer
« Reply #9 on: October 05, 2012, 01:02:15 pm »
Dorothy in the Wizard of Oz
:)

Running a timer within a thread does not improve accuracy to a certain level. In good old times with DOS the screen refresh was used for timing. But with multithreading OS you will always depend on system load. If you really need exact 20ms you could switch to another runlevel (I assume you run Linux since cthread is not needed at Windows). On the other hand all input has buffer, polls, and chatter issues which produces deviation in measures too.

I'm very comfortable with fpTimer counting seconds with sufficient reproducibility for users.
Lazarus 1.7 (SVN) FPC 3.0.0

mas steindorff

  • Hero Member
  • *****
  • Posts: 576
Re: TTimer accuracy at 20ms - vs. Epik timer
« Reply #10 on: October 05, 2012, 07:50:36 pm »
Running a timer within a thread does not improve accuracy to a certain level.

I'm very comfortable with fpTimer counting seconds with sufficient reproducibility for users.

I've recently kick up my requirement for accuracy (5 mS) and have found your statement may be True!  I'll have to try the FpTimer and see if it improves my timing.  It may be that all I need to do is to set the thread priority higher that default

 I don't see where this timer is in a different thread from the link.  Is FpTimer and the default TTimer the same thing?
How does it handle delayed processing of of the OnTimer event when this event is in the main thread?
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

dbaxter

  • New Member
  • *
  • Posts: 13
Re: TTimer accuracy at 20ms - vs. Epik timer
« Reply #11 on: October 05, 2012, 11:09:49 pm »
I feel so knowledgeable about this now. If you look at another posting - http://lazarus.freepascal.org/index.php/topic,18343.0.html you will see where I posted what is, essentially, the code for FPTimer. It is not like TTimer. The thread priority of FPTimer can be set higher than that of the rest of your program and if you look closely, it accounts for the amount of time it takes to perform your OnTimer procedure.

avra

  • Hero Member
  • *****
  • Posts: 2586
    • Additional info
Re: TTimer accuracy at 20ms - vs. Epik timer
« Reply #12 on: October 11, 2012, 09:53:28 am »
This TTimer alternative code might be interesting to someone:
http://sergworks.wordpress.com/2010/04/06/a-better-timer-for-a-delphi-programmer
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

 

TinyPortal © 2005-2018