Home > Software engineering >  start 2 timers simultaneously in a matlab function
start 2 timers simultaneously in a matlab function

Time:01-30

I have a program with 2 timer functions like this (p_maj_input and p_maj_output_motion are equal so the period of both timer is the same):

maj_input = timer('ExecutionMode','fixedRate','Period',p_maj_input,'TasksToExecute', ...
    floor(Tmeasurement/p_maj_input)-1,'TimerFcn',{@time_append,p_maj_input,HR_all,freq_HR, ...
    BVP_all,freq_BVP,TEMP_all,freq_TEMP,ACC_x_all,ACC_y_all,ACC_z_all,freq_ACC,EDA_all,freq_EDA, ...
    folder_all,all_dir,num_dir}); start(maj_input);

maj_output_motion=timer('ExecutionMode','fixedRate','Period',p_maj_output_motion,'TasksToExecute', ...
    floor(Tmeasurement/p_maj_output_motion)-1,'TimerFcn',{@output_motion_append, ...
    freq_MOTION,freq_mvt_score,freq_walk,p_maj_output_motion,p_maj_output_walk,folder_all,all_dir,num_dir});%,'StartDelay',min(5,p_maj_output_motion)); %startDelay must be min 5 for walk detection start(maj_output_motion);

In each timer callback function there is a loop over subfolders contained in a folder that is selected at the beginning of the program.

output_motion_append(obj,event,freq_MOTION,freq_mvt_score,freq_walk,p_maj_output_motion,p_maj_output_walk,folder_all,all_dir,num_dir)
    fprintf('motion %d\n',obj.TasksExecuted)
    toc
    for folder_index=1:num_dir
        [folder_original,folder_fictive] = subfolderFunction(all_dir, folder_all, folder_index);
        fileName=strcat(folder_fictive,'\ACC.csv');
        [ACC_time, ACC_x, ACC_y, ACC_z] = loadValuesE4_acc(fileName);
       
% Motion Amount        
        [agitation,agitation_ts] = identifyAgitation(ACC_x,ACC_y,ACC_z,ACC_time);
        agitation=agitation';

        len1=length(agitation);
        if len1<=freq_MOTION*p_maj_output_motion
            i_init1=1;
        elseif len1>freq_MOTION*p_maj_output_motion
            i_init1=len1-freq_MOTION*p_maj_output_motion 1;
        end
        
        writematrix([agitation(i_init1:len1)],strcat(folder_fictive,'\MOTION_output.csv'),'WriteMode','Append');
        
     writematrix([mvt_score(i_init2:len2)],strcat(folder_fictive,'\neurologicScore_output.csv'),'WriteMode','Append');

    end
    
end

Everything works fine if the number of subfolders is lower than 4 : the good values appear in the files on which is carried out the writematrix function. And the timer callback function are are called one after the other, so both timer work simultaneously. However if there are 5 subfolders or more, it is not the good values and using the debugging I noticed that the first callback function is triggered the number of 'TasksToExecute', and then only the second callback function seems to be called. That is to say the 2 timers don't work simultaneously.

I have tried to increase p_maj_input and p_maj_output_motion to see if the problem is that matlab can't finish to run before another timer callback function is called but still for 5 subfolders I get the same problem.

Does anyone know where my problem is coming from?

CodePudding user response:

This behavior occurs because one timer hasn't finished executing by the time it triggers again, so the second timer never has a chance to execute until the first timer is finished. If you change the ExecutionMode from 'fixedRate' to 'fixedSpacing', then you'll guarantee that there's time for the second timer to execute.

function duelingTimers()
disp('With ExecutionMode = fixedRate')
t1 = timer('ExecutionMode','fixedRate','Period',0.1,'TasksToExecute',5,'TimerFcn',@timerFcn1);
t2 = timer('ExecutionMode','fixedRate','Period',0.1,'TasksToExecute',5,'TimerFcn',@timerFcn2);
cleanup = onCleanup(@()delete([t1,t2]));

    function timerFcn1(~,~)
        pause(0.2)
        disp('Timer 1');
    end
    function timerFcn2(~,~)
        pause(0.2)
        disp('Timer 2');
    end

start(t1),start(t2)

wait(t1)
wait(t2)

disp(newline)
disp('With ExecutionMode = fixedSpacing')
t1.ExecutionMode = 'fixedSpacing';
t2.ExecutionMode = 'fixedSpacing';

start(t1),start(t2)

wait(t1)
wait(t2)
end
  • Related