Trigger function with precission
조회 수: 4 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (1개)
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
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 Center 및 File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!