Code occasionally gets stuck in a for loop iteration and takes ~50x longer to complete iteration

3 views (last 30 days)
Hi,
I have a function that uses the DAQ toolbox to send a sinusoidal loading profile to a motor while gathering force and displacement data from sensors. An excerpt from my code that is causing the issue is below.
If its running correctly, each loop iteration should take more or less the exact same amount of time, but occasionaly this is not the case. Recently, I ran the code and it took much longer during a loop iteration than usual (about 0.5s instead of the usual 0.01s) about 3.96 seconds (and again the next time I ran it after 2.96s) into the sinusoid which meant that the motor got the message to change speed/direction too late and it overloaded my samples before continuing with the next iteration (all of the other ~345,000 iterations occured at correct speed). This is the first time I've ran into this issue in the last few months of regular use, but I have ran into this issue a while ago so this doesn't seem like a one time event. I also don't believe this is a hardware issue since a previous program we used to control them (written in C++ but unable to be used anymore) didn't have this issue. Any ideas what is causing this issue and how I can go about fixing it?
% Start loading
obj.ctr.Frequency = freqWForm(iter);
write(obj.ports,[1 1 1 0 directionArray(iter)])
start(obj.c, "Continuous")
t = tic;
% Usual variable values
% obj.samplesPerCycle = 96
% obj.cycles = 3600
% timePerSample = 0.01 (technically 1/96)
for i = 1:obj.samplesPerCycle*obj.cycles
% Start timer for this loop iteration
tic;
% Every 8 loop iterations update the speed and/or direction of the motor
if i > 1 && mod(i,(obj.samplesPerCycle/12)) == 1
obj.ctr.Frequency = freqWForm(iter);
write(obj.ports,[1 1 1 0 directionArray(iter)])
iter = iter + 1;
end
% Collect and store data
[a,~] = read(d,"OutputFormat","Matrix");
dataOutput(i,2:end) = a;
dataOutput(i,2) = (dataOutput(i,2)-dispOffset)*dispScale;
dataOutput(i,3) = (dataOutput(i,3)-forceOffset)*forceScale;
dataOutput(i,1) = toc(t);
% Pauses for the remaining time allocated to loop iteraion
pTime = timePerSample - toc;
if pTime > 0
pauses(pTime) %Uses a more precise pause function than default one
end
end
% Stop motor after finishing sinusoid
stop(d)
stop(obj.c)
write(obj.ports, [0 0 0 0 0])
stop(obj.ports)
Thanks for your help!

Answers (1)

Ayush
Ayush on 6 Sep 2023
Hi Sam,
I understand that your code occasionally experiences significant delays during loop iterations using the DAQ toolbox to control a motor and collect sensor data. These delays result in late speed and direction changes for the motor, causing sample overload. The cause of the delays is unknown, and you are seeking ideas on what might be causing the issue and how to resolve it.
Looking at the code provided and considering the potential issues you mentioned, it appears that there could be two main areas causing the delays: mixing foreground/background signal generation and potential timing calculation errors.
  1. Foreground/Background Signal Generation: The issue might arise if there is a mix-up between foreground and background signal generation. If you are using continuous output mode with a large buffer, new data will not be written until the buffer is empty. This can cause delays in sending commands to the motor and affect the timing of your loop iterations. Refer to the options for the “start” command and ensure that you are using the appropriate settings for your signal generation requirements.
  2. Timing Calculation Errors: The timing calculations in your code might be incorrect, leading to unexpected delays. Ensure that the timing calculations such as “timePerSample” and “pTime” accurately reflect the desired time intervals. Verify that the units and calculations are correct to avoid any miscalculations that could cause delays.
To learn more about foreground/background signal generation and the “start” command, please refer to the MathWorks documentation links below:
  1. https://www.mathworks.com/help/daq/generate-signals-in-the-background-continuously.html[AS1] .
  2. https://www.mathworks.com/help/daq/daq.interfaces.dataacquisition.start.html[AS2]
Hope this helps!

Categories

Find more on Data Acquisition Toolbox Supported Hardware in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!