How to execute the same code for a specific time?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I want to estimate the shaft speed of a motor and to do that I obtain the speed error between the reference speed and the estimated speed. Then, if the error is greater than 10 the estimated speed multiplied by 1/3. if error is less than 10 then the estimated speed multiplied by a different gain. What I am looking for is when the error is > 10 I want the estimated speed multiplied by 1/3 for a specific time for example 1 second and not to be interrupted by anything even if the error becomes less than 10.
0 Commenti
Risposte (1)
Image Analyst
il 21 Gen 2023
Try a while loop. In the loop get the estimated speed and compute the error. Here is a start:
% Demonstration of how to avoid an infinite loop by setting up a failsafe.
% Set up a failsafe
maxIterations = 500; % Way more than you think it would ever need.
loopCounter = 0;
% Now loop until we obtain the required condition: a random number equals exactly 0.5.
% If that never happens, the failsafe will kick us out of the loop so we do not get an infinite loop.
refSpeed = 1000; % Initialize so we can enter the loop the first time.
estimatedSpeed = 1; % Initialize so we can enter the loop the first time.
speedDifference = abs(refSpeed - estimatedSpeed);
while speedDifference > 10 && loopCounter < maxIterations
loopCounter = loopCounter + 1;
fprintf('Iteration #%d.\n', loopCounter)
estimatedSpeed = however you get it;
speedDifference = abs(refSpeed - estimatedSpeed);
if speedDifference > 10
% Somehow change the motor speed.
end
end
% Loop is done
% Alert user if we exited normally, or if the failsafe kicked us out to avoid an infinite loop.
if loopCounter < maxIterations
% Then the loop found the condition and exited early, which means normally.
fprintf('Loop exited normally after %d iterations.\n', loopCounter);
else
% Then the loop never found the condition and exited when the number of iterations
% hit the maximum number of iterations allowed, which means abnormally.
fprintf('Loop exited abnormally after iterating the maximimum number of iterations (%d) without obtaining the exit criteria.\n', maxIterations);
end
fprintf('All done after %d iterations.\n', loopCounter)
I'd recommend you do a bilinear search to home in on the best speed rather than doing the 3* or 1/3* multiplication.
2 Commenti
Vedere anche
Categorie
Scopri di più su Calendar in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!