Is there a way to reduce Timer period to less than 1 ms?

8 visualizaciones (últimos 30 días)
gujax
gujax el 28 de En. de 2023
Editada: gujax el 31 de En. de 2023
I am trying to reduce timer(‘period’) to less than 0.001 if possible. In this forum link https://www.mathworks.com/matlabcentral/answers/37716-pause-function-in-matlab-for-1-millisecond, few people have suggested using java pause or writing your own pause function but how can I implement these alternatives inside the predefined timer? Or can I redefine timers ‘period’? I need the timer for its start finish and check functionality.The other question I have is why does one need wait? The fact that timer’s callback is running no further Matlab commands are getting executed. The wait is adding another significant time to my process. Thanks
  2 comentarios
Steven Lord
Steven Lord el 30 de En. de 2023
What is your application where you need a timer with periods that small? Are you trying to do something related to real-time processing?
gujax
gujax el 30 de En. de 2023
Editada: gujax el 30 de En. de 2023
Actually I realized I 'perhaps' do not have to shorten the period. I think my problem is something else.
I am wondering if the timer call back function can be updated with incremented variable without re-creating a new timer?
I am running a device (say a piezo mirror) and for every position I am generating data using another object (e.g., tetronix scope or a daq).
So the sequeunce is -Generate a for-loop:
move the mirror to first position(ii)
Run tetronix and record data
Check if the data record is complete
If complete go for the next mirror position(ii+1) and repeat for 10 positions
Data gets taken pretty fast like in <1ms
There are few issues.
If the mirror is not made to move at the correct interval it damages my sample so I got to stick to 1ms of data record and then move the mirror. If mirror move happens in 2 ms then I damage the sample (let us say I do not have other recourse other than moving the mirror).
So I ran the timer as such
t = timer('StartDelay', 0.000, 'Period', 0.001, 'TasksToExecute', 100, 'BusyMode', 'drop', 'ExecutionMode', 'fixedRate');
But each for-loop took 100ms. I found that was because in every for-loop I had to generate a new timer because old timer had to be deleted. Why? Because the timer callback function runs tetronix and provides data file names to save data from tetronix i.e., it needs to be updated for every for-loop of the mirror. And that completely damages my experiment.
So instead I made a timer callback function where I could move the mirror. So now I generate all 10 positions filenames once, no for-loop.
So generate timer function, start, check if first data is written. So period=0.001.
If it is then next callback function is in queue and that next call increments mirror position.No need to generate a new timer.
But then I found I couldn't update the mirror position .
For example in the function below aarg2 moves the mirror using DAQ and I thought I could update the mirror position so that in the 'next' callback a new mirror position will be a new input. That is after a period of 1ms, the next callback is in the queue. And in that next queue if I could affect the mirror position without re-creating a timer, my issue will be resolved.
But alas that is not the case.
function my_callback_fcn(obj, event, argtet,aarg1,aarg2, status)
if strcmp(status, 'START')
command = 'Tetronix_START';
runtetx(command, argtet);
elseif strcmp(status, 'FINISH')
command = 'Tetronix_STOP';
runtetx( command,argtet);
else
command = 'Tetronix_check';
runtetx( command, argtet,aarg1);
disp(aarg2);
write(aarg1,[aarg2 0 0 1]);%aarg1 is DAQ object and aarg2 is the voltage corresponding to position of the mirror
pause(0.04);
aarg2=aarg2+1;
end
So if I started with aarg2=-10Volts
The script ran and showed a series of aarg2=-9 and it wrote all 10 files pretty fast but only in one mirror position

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 28 de En. de 2023
You would need to reimplement timers.
  5 comentarios
Walter Roberson
Walter Roberson el 30 de En. de 2023
I am not sure why a new timer function was being generated? Could you have used shared variables instead?
gujax
gujax el 31 de En. de 2023
Editada: gujax el 31 de En. de 2023
It’s an ADC data acquisition card. It streams input data to a computer memory. So it runs by pre-assigning number of files, their names and trigger and handshake times for every corresponding position of a scanning mirror. These variables are then input to a timer callback. Then from the Matlab’s timer callback function it calls a mex function to start data record for a mirror position. So if I have to synchronize every mirror step to set of data files, I need to call this Mex function with new updated variables every time I change mirror position. I don’t know how can I create a timer function that starts data recording for every mirror position without generating a new timer for each loop!
Here is an example
for I=1:20 % for each mirror pos
Move mirror to pos(I);%DAQ command
P.numberoffiles =100
P.filesize =30K
P.filenames= {code generates 100 filenames}
P.triggertime=100e-3
P.triggertimesforeachfile = vector of times; etc..
t=timer(start,period,etc);
t.start= somefunction(object,event,P):
t.stop= same as above;
And check for status function if the data recording is complete..
start(t);
wait(t);
Some code here….
delete(t);
end
So that ‘somefunction’ calls the ADC’s mex function which uses P variable structure and records data of certain length for certain triggers and store to a file structure. It returns back on completion and script goes through a second loop.
I am not sure how nesting functions would help though it is a good suggestion and I will reflect on it. Thank you

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Troubleshooting in Simulink Real-Time en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by