Clear Filters
Clear Filters

How to execute the same code for a specific time?

2 views (last 30 days)
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.

Answers (1)

Image Analyst
Image Analyst on 21 Jan 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.
% 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);
% 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);
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.
saleh shlimet
saleh shlimet on 22 Jan 2023
Thank you for your answer. I do not know if you understood my question. to make it clearer see attached. I have two input variables, estimated speed and reference speed. If the error is less than 10 then the output is estimated speed *1/3. If the error >10 then the output is estimated speed*1/4, but in this case(error>10) I want the output to stick to this value(estimated speed*1/4) for a period of time for example 0.5 second even if the condition changed to error<10. I thought the while loop could sort it out but it failed.

Sign in to comment.


Find more on MATLAB Coder 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!