I want my code conditions checked every 1/2000th of a second, how can I do that?
2 次查看(过去 30 天)
显示 更早的评论
I have multiple if conditions that is being checked to provide pulse and gate signals to switches I want that these conditions be checked every 1/2000th of a second. I tried for loop and tic toc methods but the condition is being checked at a frequency of 200kHz. I am not able to solve this issue kindly help
5 个评论
dpb
2024-8-4
Sure, just replace the guts with yours; it is phantom code to illustrate the structure of the loop...but, depending upon the tests, you might consider replacing a bunch of "if...elseif...elseif...else...end" with a switch construct. Just depends on details you've not provided...
回答(1 个)
Walter Roberson
2024-8-3
You will not be able to pause() more accurately than 1/100 of a second on Windows. (On Mac it is about 1/1000 of a second.)
Therefore you will need to "busy-wait"
start_time = tic();
while toc(start_time) < 1/2000
%idle
end
2 个评论
dpb
2024-8-3
编辑:dpb
2024-8-4
N=1000;
i=0;
t=zeros(N,1);
WAIT_MSEC=0.5; % the desired time delay, msec
PAUSE_SEC=WAIT_MSEC/1000; % convert to sec for pause()
continueFlag=true; % make it go...
while(continueFlag)
t0=tic;
pause(PAUSE_SEC) % wait for the desired time; may have to adjust...
i=i+1; t(i)=toc(t0);
continueFlag=(i<N); % make it stop...
end
t=t*1000; % back to msec for display...
[min(t) max(t) mean(t)]
ans =
0.5019 0.8487 0.5072
histogram(t)
hAx=gca; hAx.YScale='log'; hAx.YLim=[0.1 N]; grid on
does reasonably well although as is known, it isn't reliable...nor, of course, will be the polled waiting solution...
I was thinking I read somewhere (Yair, maybe???) quite some time ago that TMW changed over to using the Windows high resolution timer...seems to be the case.
It appears can go to about 50 usec resolution ...for it I got
t=t*1000;[min(t) max(t) mean(t)]
ans =
0.0518 0.7194 0.0551
for N=10000. The most frequent however, was in the third histogram bin and the two most prevalent were the 2nd and 3rd, about 36% and 52%, respectively. And, of course, the loop has no real work in it...
ADDENDUM:
The above is a delay after the code runs; if the intent is to have the code execute on the 0.005 cycle, then the delay time must be shortened by the average run time of the functional code; that, of course, then means the difference in execution time between branches also adds jitter.
All in all, unless "on or about" is good enough, it needs a hardware interrupt scheme...
dpb
2024-8-3
With my results above, I thought "just maybe!" and tried
PAUSE_SEC=2/1000;
hT=timer('period',PAUSE_SEC,'executionmode','fixedRate','timerfcn',@timercallback)
Unfortunately, whether MATLAB could physically do it or not, it's been neutered... the above (per documentation, just hoping maybe it would/could do more)..
That's interesting; on Windows (R2021b) the above results in
Warning: Period property is limited to 1 millisecond precision. Sub-millisecond precision will be ignored.
> In timer/set.Period (line 365)
In timer (line 159)
So, can we believe the non-Windows platform??? That won't help @Aaqib Ahmad, probably, even if so, but would be interesting to know.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!