Trigger function with precission

10 次查看(过去 30 天)
Lask
Lask 2017-9-8
评论: Stephen23 2017-9-8
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

回答(1 个)

Guillaume
Guillaume 2017-9-8
编辑:Guillaume 2017-9-8
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 个评论
Guillaume
Guillaume 2017-9-8
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by