SWITCH expression must be a scalar or a character vector. Error in pathplaninglaila (line 17) switch mode
55 次查看(过去 30 天)
显示 更早的评论
% 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;
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);
end
function moveTo(x,y)
% Send motion commands
disp(['Moving to ' num2str(x) ',' num2str(y)]);
end
0 个评论
回答(2 个)
Dyuman Joshi
2023-12-3
As the error states, the switch expression is expected to be a scalar or character vector. In your case, you are dealing with text data, thus it should be a character vector.
Modify your function selectNewMode to provide the output as a character vector -
function m = selectNewMode()
% Pick random mode
m = randsample({'random','spiral','s-path','wall follow'},1);
m = m{:};
end
Note that you have not defined the variable n, to use it in the spiral case.
Also you have not put a break condition for the while loop.
And the function obstacleDetected paired with the if-else condition seems redundant.
0 个评论
VBBV
2023-12-10
The mode variable used for the switch expression needs a character data type. you need to convert that variable to char mode as shown below. Some variables inside the switch-case statement are not defined and the while loop seems to run endlessly. Use a suitable while loop condition to stop the loop or break statements inside the loop.
% 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';
n = 0; % initialize n
% 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();
mode = char(mode); % convert it to char
end
% Send motion commands
moveTo(x,y);
% Randomly change mode
if rand < 0.1
mode = selectNewMode();
mode = char(mode); % convert to char
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);
end
function moveTo(x,y)
% Send motion commands
disp(['Moving to ' num2str(x) ',' num2str(y)]);
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Develop Apps Using App Designer 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!