Filter löschen
Filter löschen

where to put the break condition for the while loop

1 Ansicht (letzte 30 Tage)
Omar am 3 Dez. 2023
Beantwortet: Image Analyst am 3 Dez. 2023
% 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;
% 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;
dy = -pass_width;
case 'wall follow'
% Check sensors and turn if needed
% ...
dx = 1;
% 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();
% Send motion commands
% Randomly change mode
if rand < 0.1
mode = selectNewMode();
% Helper functions
function out = obstacleDetected(~,~)
% Check map for obstacle at x,y
out = false;
function m = selectNewMode()
% Pick random mode
m = randsample({'random','spiral','s-path','wall follow'},1);
m = m{:};
function moveTo(x,y)
% Send motion commands
disp(['Moving to ' num2str(x) ',' num2str(y)]);
  2 Kommentare
Les Beckham
Les Beckham am 3 Dez. 2023
Bearbeitet: Les Beckham am 3 Dez. 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?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Image Analyst
Image Analyst am 3 Dez. 2023
You need to decide when to get out of the loop. This can be either
  1. after a certain number of iterations,
  2. or when mode is not one of the strings you listed,
  3. or when x or y gets outside of some range,
  4. or maybe some other condition.
Your while should have an otherwise case
switch mode
% Put other cases first, then
break % out of while loop.
Do not use mode as the name of your variable. It is the name of a built-in function mode
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;
% 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)
Of course you can replace the r~=0.5 with whatever your actual condition is, and adjust maxIterations also for your situation.


Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by