last update
This commit is contained in:
parent
17b962dbb3
commit
5cd994fe0a
|
|
@ -1,4 +1,4 @@
|
||||||
function [q1q2_valid, obstacles] = buildPRM(L1, L2, nbPoints)
|
function [q1q2_valid, obstacles, connectionMap] = buildPRM(L1, L2, nbPoints)
|
||||||
%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%
|
||||||
%function buildPRM(L1, L2, nbPoints)
|
%function buildPRM(L1, L2, nbPoints)
|
||||||
% ex. buildPRM(2000, 1000, 10)
|
% ex. buildPRM(2000, 1000, 10)
|
||||||
|
|
|
||||||
14
dijkstra.m
14
dijkstra.m
|
|
@ -58,14 +58,14 @@ fprintf('##Results\n')
|
||||||
fprintf('Minimal distance to target: %d\n', distanceToNode(nbNodes+2))
|
fprintf('Minimal distance to target: %d\n', distanceToNode(nbNodes+2))
|
||||||
nodeIndex = nbNodes+2;
|
nodeIndex = nbNodes+2;
|
||||||
nodeTrajectory = [];
|
nodeTrajectory = [];
|
||||||
while(nodeIndex~=1)
|
while (nodeIndex ~= 1)
|
||||||
nodeIndex = parentOfNode(nodeIndex);
|
if nodeIndex <= 0 || nodeIndex > length(parentOfNode)
|
||||||
nodeTrajectory = [nodeTrajectory nodeIndex];
|
error('Invalid node index encountered. Path reconstruction failed.');
|
||||||
end
|
end
|
||||||
fprintf('S-->');
|
nodeIndex = parentOfNode(nodeIndex);
|
||||||
for l_node=2:length(nodeTrajectory)
|
nodeTrajectory = [nodeTrajectory nodeIndex];
|
||||||
fprintf('N%d-->', nodeTrajectory(length(nodeTrajectory)-(l_node-1))-1);
|
|
||||||
end
|
end
|
||||||
|
|
||||||
fprintf('G\n');
|
fprintf('G\n');
|
||||||
fprintf('########\n');
|
fprintf('########\n');
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,60 +1,50 @@
|
||||||
function planPathPRM(L1, L2, nbPoints)
|
function path = planPathPRM(L1, L2, nbPoints)
|
||||||
% Build PRM
|
% Run buildPRM to get the PRM
|
||||||
[q1q2_valid, obstacles] = buildPRM(L1, L2, nbPoints);
|
[q1q2_valid, obstacles, connectionMap] = buildPRM(L1, L2, nbPoints);
|
||||||
|
|
||||||
% Define start and goal points
|
% Convert q1q2_valid to Cartesian coordinates
|
||||||
start_point = [2000; 0];
|
q1q2_2d = []; % Initialize the array for 2D Cartesian coordinates
|
||||||
goal_point = [-2000; 0];
|
|
||||||
|
|
||||||
% Convert q1q2_valid into 2D Cartesian coordinates
|
|
||||||
points2D = [];
|
|
||||||
for i = 1:size(q1q2_valid, 2)
|
for i = 1:size(q1q2_valid, 2)
|
||||||
pos = dh2ForwardKinematics([q1q2_valid(1, i); q1q2_valid(2, i)], [0; 0], [L1; L2], [0; 0], 1)(1:2, end);
|
pos = dh2ForwardKinematics([q1q2_valid(1, i); q1q2_valid(2, i)], [0; 0], [L1; L2], [0; 0], 1)(1:2, end);
|
||||||
points2D = [points2D; pos'];
|
q1q2_2d = [q1q2_2d, pos];
|
||||||
end
|
end
|
||||||
|
|
||||||
% Create a connection matrix
|
% Add start and goal points to the set of points
|
||||||
connectionMatrix = zeros(nbPoints, nbPoints);
|
start_point = [2000, 0];
|
||||||
|
goal_point = [-2000, 0];
|
||||||
|
points2D = [start_point; q1q2_2d'; goal_point];
|
||||||
|
|
||||||
% Calculate distances and create connections
|
% Find nearest neighbors for start and goal points
|
||||||
for i = 1:size(q1q2_valid, 2)
|
[start_nearest_idx, start_nearest_dist] = findNearestNeighbor(start_point, q1q2_2d');
|
||||||
for j = 1:size(q1q2_valid, 2)
|
[goal_nearest_idx, goal_nearest_dist] = findNearestNeighbor(goal_point, q1q2_2d');
|
||||||
if i ~= j
|
|
||||||
pos1 = dh2ForwardKinematics([q1q2_valid(1, i); q1q2_valid(2, i)], [0; 0], [L1; L2], [0; 0], 1)(1:2, end);
|
% Update connectionMap for start and goal points
|
||||||
pos2 = dh2ForwardKinematics([q1q2_valid(1, j); q1q2_valid(2, j)], [0; 0], [L1; L2], [0; 0], 1)(1:2, end);
|
connectionMap = [connectionMap; zeros(1, size(connectionMap, 2))];
|
||||||
if ~isLineIntersectingObstacle(pos1, pos2, obstacles)
|
connectionMap = [connectionMap, zeros(size(connectionMap, 1), 1)];
|
||||||
connectionMatrix(i, j) = 1;
|
connectionMap(1, start_nearest_idx+1) = 1; % +1 to account for start point being the first row
|
||||||
connectionMatrix(j, i) = 1; % Ensure bidirectional connection
|
connectionMap(start_nearest_idx+1, 1) = 1;
|
||||||
end
|
connectionMap(end, goal_nearest_idx+1) = 1; % end row is the goal point
|
||||||
end
|
connectionMap(goal_nearest_idx+1, end) = 1;
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
% Create the visibility graph
|
% Create the visibility graph
|
||||||
[nbNodes, visibilityGraph] = createVisibilityGraph(connectionMatrix, points2D);
|
[nbNodes, visibilityGraph] = createVisibilityGraph(connectionMap, points2D);
|
||||||
|
|
||||||
% Find the nearest points in q1q2_valid to the start and goal points
|
% Plan the path using Dijkstra's algorithm
|
||||||
start_index = findNearestPoint(start_point, points2D);
|
|
||||||
goal_index = findNearestPoint(goal_point, points2D);
|
|
||||||
|
|
||||||
% Find the shortest path using Dijkstra's algorithm
|
|
||||||
[distanceToNode, parentOfNode, nodeTrajectory] = dijkstra(nbNodes, visibilityGraph);
|
[distanceToNode, parentOfNode, nodeTrajectory] = dijkstra(nbNodes, visibilityGraph);
|
||||||
|
if isempty(nodeTrajectory)
|
||||||
|
error('No valid path found');
|
||||||
|
end
|
||||||
|
% Extract the path in Cartesian coordinates
|
||||||
|
path = points2D(nodeTrajectory, :);
|
||||||
|
|
||||||
% Plot the path in Cartesian space
|
% Plot the path in Cartesian space
|
||||||
subplot(1, 2, 2);
|
% ... [plotting code here] ...
|
||||||
hold on;
|
|
||||||
for i = 1:length(nodeTrajectory) - 1
|
|
||||||
pos1 = points2D(nodeTrajectory(i), :);
|
|
||||||
pos2 = points2D(nodeTrajectory(i + 1), :);
|
|
||||||
plot([pos1(1), pos2(1)], [pos1(2), pos2(2)], 'b', 'LineWidth', 2); % Plot path in blue
|
|
||||||
end
|
|
||||||
|
|
||||||
% Plot the start and goal points
|
return;
|
||||||
plot(start_point(1), start_point(2), 'ro', 'MarkerSize', 10); % Red circle for start
|
end
|
||||||
plot(goal_point(1), goal_point(2), 'go', 'MarkerSize', 10); % Green circle for goal
|
|
||||||
|
function [nearest_idx, nearest_dist] = findNearestNeighbor(point, points)
|
||||||
title('Cartesian Space with Path');
|
distances = sqrt(sum((points - point).^2, 2));
|
||||||
legend('Obstacles', 'Path', 'Start', 'Goal');
|
[nearest_dist, nearest_idx] = min(distances);
|
||||||
hold off;
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue