binary occupancy map flipped after using A* algorithm

조회 수: 3 (최근 30일)
YAAQOB AL-JAMALI
YAAQOB AL-JAMALI 2025년 3월 11일
댓글: YAAQOB AL-JAMALI 2025년 3월 14일
I am trying to explore A* algorithm which is built-in MATLAB algorithm, but when creating enviornment for simulation and then show the outcome of the planner the result is flipped compared to original map.
the code used as follow:
clearvars;
close all;
% Parameters
x_max = 500;
y_max = 500;
mapRes = 1; % 1 cell per meter (or per unit)
% number of obstacles
numObstacles = input('Enter the number of rectangular obstacles: ');
% Obstacle size
obsWidth = 40;
obsHeight = 40;
% Create empty map
mapMatrix = zeros(y_max, x_max); % Rows = Y, Columns = X
% Add rectangular obstacles
for i = 1:numObstacles
x = randi([1, x_max - obsWidth]);
y = randi([1, y_max - obsHeight]);
mapMatrix(y:(y+obsHeight), x:(x+obsWidth)) = 1; % Occupied = 1
end
% Flip the map vertically so that origin is at bottom-left (for A*)
mapMatrix = flipud(mapMatrix);
% Create binary occupancy map
map = binaryOccupancyMap(mapMatrix, mapRes);
% Flip axis for plotting purposes
figure;
show(map);
set(gca, 'YDir', 'normal'); % Flip Y axis so origin is bottom-left
title('Click to select START (green) and GOAL (red)');
hold on;
% Select start point
disp('Click on the map to select the START point');
[startX, startY] = ginput(1);
start = round([startY, startX]);
plot(start(2), start(1), 'go', 'MarkerSize', 10, 'LineWidth', 2);
% Select goal point
disp('Click on the map to select the GOAL point');
[goalX, goalY] = ginput(1);
goal = round([goalY, goalX]);
plot(goal(2), goal(1), 'ro', 'MarkerSize', 10, 'LineWidth', 2);
% % Flip the map
% mapFliped = flipud(map);
% figure;
% show(mapFliped)
% Plan using A* algorithm
planner = plannerAStarGrid(map);
[path, debugInfo] = plan(planner, start, goal);
% Display results
figure;
show(planner);
% plot(path(:,2), path(:,1))
set(gca, 'YDir', 'normal'); % Flip Y axis again
axis xy;
title('A* Path Planning Result');
% legend('Path','Start','Goal','Explored');
  댓글 수: 4
Walter Roberson
Walter Roberson 2025년 3월 11일
I am suggesting commenting out that line.
YAAQOB AL-JAMALI
YAAQOB AL-JAMALI 2025년 3월 12일
i have commented it out, yet the content of the map still flipped. I do think there is some feature in A* algorithm which cause this but not really sure. If you know a step to overcome such nature, I would appreciate it.

댓글을 달려면 로그인하십시오.

채택된 답변

Cris LaPierre
Cris LaPierre 2025년 3월 13일
You need to specify 'world' as an additional input to plan when start and goal are specified as [x y] in world coordinate frame with origin at bottom-left corner.
With this setting, you do not need to worry about flipping or setting axis Y direction.
% Parameters
x_max = 500;
y_max = 500;
mapRes = 1; % 1 cell per meter (or per unit)
% number of obstacles
numObstacles = 8;%input('Enter the number of rectangular obstacles: ');
% Obstacle size
obsWidth = 40;
obsHeight = 40;
% Create empty map
mapMatrix = zeros(y_max, x_max); % Rows = Y, Columns = X
% Add rectangular obstacles
for i = 1:numObstacles
x = randi([1, x_max - obsWidth]);
y = randi([1, y_max - obsHeight]);
mapMatrix(y:(y+obsHeight), x:(x+obsWidth)) = 1; % Occupied = 1
end
% Create binary occupancy map
map = binaryOccupancyMap(mapMatrix, mapRes);
figure;
show(map);
hold on;
start = [28 48];
plot(start(2), start(1), 'go', 'MarkerSize', 10, 'LineWidth', 2);
goal = [481, 418];
plot(goal(2), goal(1), 'ro', 'MarkerSize', 10, 'LineWidth', 2);
hold off
planner = plannerAStarGrid(map);
[path, debugInfo] = plan(planner, start, goal,'world');
% Display results
figure;
show(planner);
title('A* Path Planning Result');
  댓글 수: 1
YAAQOB AL-JAMALI
YAAQOB AL-JAMALI 2025년 3월 14일
thank you so much, that is really work more better than the way i figure it out.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by