where to put the break condition for the while loop
8 views (last 30 days)
Show older comments
% Parameters
spacing = 10; % spiral spacing
delta_theta = pi/4; % spiral angle increment
pass_width = 10; % S-path pass width
% Initialize position
x = 0;
y = 0;
n=0;
% Initialize path mode
mode = 'random';
% Main loop
while true
% Get next position based on mode
switch mode
case 'random'
dx = rand*10 - 5;
dy = rand*10 - 5;
case 'spiral'
n = n+1;
r = n*spacing;
theta = n*delta_theta;
dx = r*cos(theta);
dy = r*sin(theta);
case 's-path'
if mod(y, pass_width)==0
dy = pass_width;
else
dy = -pass_width;
end
case 'wall follow'
% Check sensors and turn if needed
% ...
dx = 1;
end
% Update position
x = x + dx;
y = y + dy;
% Check for obstacles
if obstacleDetected(x,y)
% Backup and change path
x = x - dx;
y = y - dy;
mode = selectNewMode();
end
% Send motion commands
moveTo(x,y);
% Randomly change mode
if rand < 0.1
mode = selectNewMode();
end
end
% Helper functions
function out = obstacleDetected(~,~)
% Check map for obstacle at x,y
out = false;
end
function m = selectNewMode()
% Pick random mode
m = randsample({'random','spiral','s-path','wall follow'},1);
m = m{:};
end
function moveTo(x,y)
% Send motion commands
disp(['Moving to ' num2str(x) ',' num2str(y)]);
end
2 Comments
Les Beckham
on 3 Dec 2023
Edited: Les Beckham
on 3 Dec 2023
You need to explain the logic that you are trying to implement much more clearly. What conditions should cause the loop to exit?
Also, if you have a specific question about something that isn't working as you expect, please explain.
It appears that at least some of your functions are just placeholders at this point. For example, obstacleDetected() is ignoring its input arguments and always returning false. Perhaps that is why this is getting stuck in the while loop?
Answers (1)
Image Analyst
on 3 Dec 2023
You need to decide when to get out of the loop. This can be either
- after a certain number of iterations,
- or when mode is not one of the strings you listed,
- or when x or y gets outside of some range,
- or maybe some other condition.
Your while should have an otherwise case
switch mode
% Put other cases first, then
otherwise
break % out of while loop.
end
In general, while's should always have a failsafe construction to prevent an infinite loop. Example:
% Demonstration of how to avoid an infinite loop by setting up a failsafe.
% Set up a failsafe
maxIterations = 100; % 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.
r = nan; % Initialize so we can enter the loop the first time.
while (r ~= 0.5) && loopCounter < maxIterations
loopCounter = loopCounter + 1;
fprintf('Iteration #%d.\n', loopCounter)
r = rand;
end
% 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)
Of course you can replace the r~=0.5 with whatever your actual condition is, and adjust maxIterations also for your situation.
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements 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!