Clear Filters
Clear Filters

I want my code conditions checked every 1/2000th of a second, how can I do that?

63 views (last 30 days)
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 Comments
Aaqib Ahmad
Aaqib Ahmad on 3 Aug 2024 at 23:41
@dpb dear sir/ma'am the code you provided is for case statements, will it work in my code where I have only if conditions that I am checking?
dpb
dpb on 4 Aug 2024 at 1:45
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...

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 3 Aug 2024 at 16:52
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 Comments
dpb
dpb on 3 Aug 2024 at 20:39
Edited: dpb on 4 Aug 2024 at 13:39
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
dpb on 3 Aug 2024 at 22:44
With my results above, I thought "just maybe!" and tried
PAUSE_SEC=2/1000;
hT=timer('period',PAUSE_SEC,'executionmode','fixedRate','timerfcn',@timercallback)
hT =
Timer Object: timer-1 Timer Settings ExecutionMode: fixedRate Period: 0.002 BusyMode: drop Running: off Callbacks TimerFcn: @timercallback ErrorFcn: '' StartFcn: '' StopFcn: ''
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.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements 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!