Timer Interrupt
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hey-
I am looking to use matlab to send data over a udp connection in regular intervals.
I've found the timer objects within matlab, and was hoping they would help me, but I cannot get them to do what I would like. I essentially want the equivalent of a clock interrupt to trigger the send command at regular intervals of ~1/60th of a second.
Here is a sample code of what I have been trying:
t = timer('TimerFcn','toc;fprintf(str);tic;','StartDelay', .01);
for x=80:90
start(t);
str=char(x);
wait(t);
end
This results in the following.
PElapsed time is 0.020846 seconds.
QElapsed time is 0.011059 seconds.
RElapsed time is 0.018870 seconds.
SElapsed time is 0.021953 seconds.
TElapsed time is 0.019802 seconds.
UElapsed time is 0.016017 seconds.
VElapsed time is 0.026830 seconds.
WElapsed time is 0.015980 seconds.
XElapsed time is 0.019952 seconds.
YElapsed time is 0.021275 seconds.
If you notice the intervals produced, they are far from regular, varying by 250%. The function itself says it is supposed to have millisecond precision and will not take anything below milliseconds as an input. Is there something I am doing wrong with this to produce the desired results? or is there some other method anyone can suggest?
Thanks for any help you can provide- John
0 Kommentare
Antworten (2)
Daniel Shub
am 15 Feb. 2012
Where in the documentation do you see that it has millisecond precision?
timer doc
Clearly says: "The timer object is subject to the limitations of your hardware, operating system, and software. It is not intended to be used for real-time applications."
If you try and set a precision less than 1 ms:
timer('period', 1.0001);
You get: Warning: Period property is limited to 1 millisecond precision. Sub-millisecond precision will be ignored.
MATLAB timers are simply wrappers for Java timers. I don't think java timers can give you accuracy/precision better than 10 ms. As a "single threaded" environment, MATLAB adds constraints as to when a timer can execute.
0 Kommentare
Jesus Carmona
am 11 Sep. 2014
I tried the following code and got for a StartDelay of 1ms an error of 0.5ms. The accuracy is not better than 1ms. First define the timer. At start it will "tic" and when the delay (in this case of 1ms) has expired then it will "toc" as it is the function assigned in TimerFcn
>> t = timer('StartFcn', 'tic', 'TimerFcn','toc;','StartDelay', .001);
The results are below for 3 executions
>> start(t)
Elapsed time is 0.000421 seconds.
>> start(t)
Elapsed time is 0.000413 seconds.
>> start(t)
Elapsed time is 0.000973 seconds.
Another example, but this time with a longer delay 11ms. (Note that I omitted the start(t) commands in the listing below and showed only the results). The error is is still about 0.5ms in average.
>> t = timer('StartFcn', 'tic', 'TimerFcn','toc;','StartDelay', .011);
Elapsed time is 0.011189 seconds.
Elapsed time is 0.010540 seconds.
Elapsed time is 0.010849 seconds.
Elapsed time is 0.010834 seconds.
Elapsed time is 0.010655 seconds.
Elapsed time is 0.010220 seconds.
Elapsed time is 0.010785 seconds.
Elapsed time is 0.010381 seconds.
Elapsed time is 0.010232 seconds.
Elapsed time is 0.010419 seconds.
Elapsed time is 0.010422 seconds.
The machine you use and its load must have a lot to do with the timer's performance
0 Kommentare
Siehe auch
Kategorien
Mehr zu Downloads finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!