Trigger function with precission

4 views (last 30 days)
Lask
Lask on 8 Sep 2017
Commented: Stephen23 on 8 Sep 2017
Hi all,
I want to execute a function every 200 ms in my script. The precission of the trigger is very important in this case since I'm acquiring samples from a Bluetooth device. I'm currently using the following timer to trigger the function:
my_timer = timer('ExecutionMode','fixedSpacing','Period',0.2,'BusyMode','queue','TimerFcn',{@my_function_callback});
the issue here is, when I execute my_timer.AveragePeriod, it yields 0.2312 s and not 0.200 s (what I was expecting). I'd like to know why the timer is not triggering every 200 ms and if it depends on the tasks my script is performing during the timers period.
Thanks in advance,
Edu

Answers (1)

Guillaume
Guillaume on 8 Sep 2017
Edited: Guillaume on 8 Sep 2017
Mathworks doesn't document how their timer is implemented, therefore it's impossible to tell what the actual resolution and potential jitter is. On Windows, the standard resolution for timers is 15.6 ms. If matlab uses these timers, then you're certainly not going to get better than that and I wouldn't be suprised if your 30 ms error came from that.
The fact that the timers period is specified in seconds is a strong hint that they can't achieve ms accuracy. If you do need that sort of accuracy you may have to use something else than matlab.
  3 Comments
Guillaume
Guillaume on 8 Sep 2017
Achieving 1 ms accuracy for timers on Windows is very hard and I doubt that mathworks have done the effort to get that with their timer.
Windows is not a real-time OS, therefore you never have any guarantee that a timer will execute on time. You can be pretty sure that every now and then, it'll miss a tick or two. As I said, the standard tick resolution is ~15 ms, so it's not surprise that you get an error of ~30 ms.
If you want an accuracy of 1 ms, you may have to switch to a real time OS. Failing that, you'll have to live with the jitter, measure the actual period and correct for it.

Sign in to comment.

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!