-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.m
More file actions
147 lines (124 loc) · 4.17 KB
/
Copy pathmain.m
File metadata and controls
147 lines (124 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
% Initialize Environment
workspace = [-6, 6, -6, 6]; % Workspace dimensions [xmin, xmax, ymin, ymax]
obstacles = {[2, 2; 4, 2; 4, 4; 2, 4], ... % Obstacle 1
[-1, 0; -1, 2; -4, 2; -4, 0], ... % Obstacle 2
[2, -4; 2, -2; 0, -2; 0, -4]}; % Obstacle 3
n = 4; % Number of links
link_lengths = [2, 1.5, 1, 1]; % Link lengths
qI = [-pi/6, 0, 0, -pi/2]; % Initial configuration
qG = [pi/4, pi/3, -pi/6, pi/2]; % Goal configuration
%% RRT for Serial Manipulator
% RRT Parameters
dq = 0.5; % Step size
tolerance = 1; % Tolerance for reaching the goal
% Create a video writer object to create movie file
video_rrt = VideoWriter('rrt_movie','MPEG-4');
video_rrt.FrameRate = 5; % Set the frame rate
open(video_rrt);
% Plot the environment
figure;
hold on;
xlim(workspace(1:2));
ylim(workspace(3:4));
% Set plot properties
set(gca, 'XTick', workspace(1):1:workspace(2));
set(gca, 'YTick', workspace(3):1:workspace(4));
grid on;
xlabel('X');
ylabel('Y');
title('RRT Path Planning');
% Plot obstacles
obstacle_colors = ['r', 'g', 'c'];
for i = 1:length(obstacles)
obs = obstacles{i};
patch(obs(:,1), obs(:,2), obstacle_colors(i));
end
% Plot initial and goal configurations
plotManipulator(qI, link_lengths, 'k', 'LineWidth', 2); % Intial in Black
plotManipulator(qG, link_lengths, 'y', 'LineWidth', 2); % Goal in Yellow
% Build RRT
[path, V, E] = manipulator_RRT(qI, qG, dq, obstacles, link_lengths, tolerance);
% Plot path
if ~isempty(path)
for i = 1:size(path, 1)
q = path(i, :);
plotManipulator(q, link_lengths, 'b');
pause(1);
frame = getframe(gcf); % Capture the current frame
writeVideo(video_rrt, frame); % Write the frame to the video
end
end
hold off;
axis equal;
% Create obstacle labels for the legend
obstacle_labels = cell(1, length(obstacles));
for i = 1:length(obstacles)
obstacle_labels{i} = sprintf('Obstacle %d', i);
end
legend([obstacle_labels, 'Initial Configuration', 'Goal Configuration','Manipulator Path']);
close(video_rrt); % Close the video writer
%% PRM for Serial Manipulator
% PRM Parameters
n = 1000; % Number of nodes in the roadmap
K = 10; % Number of nearest neighbors to consider
% Create a video writer object to create movie file
video_prm = VideoWriter('prm_movie','MPEG-4');
video_prm.FrameRate = 1; % Set the frame rate
open(video_prm);
% Plot the environment
figure;
hold on;
xlim(workspace(1:2));
ylim(workspace(3:4));
% Set plot properties
set(gca, 'XTick', workspace(1):1:workspace(2));
set(gca, 'YTick', workspace(3):1:workspace(4));
grid on;
xlabel('X');
ylabel('Y');
title('PRM Path Planning');
% Plot obstacles
obstacle_colors = ['r', 'g', 'c'];
for i = 1:length(obstacles)
obs = obstacles{i};
patch(obs(:,1), obs(:,2), obstacle_colors(i));
end
% Plot initial and goal configurations
plotManipulator(qI, link_lengths, 'k', 'LineWidth', 2); % Intial in Black
plotManipulator(qG, link_lengths, 'y', 'LineWidth', 2); % Goal in Yellow
% Build PRM
[path, V, E] = manipulator_PRM(qI, qG, n, K, obstacles, link_lengths);
% Plot path
if ~isempty(path)
for i = 1:size(path, 1)
q = path(i, :);
plotManipulator(q, link_lengths, 'm', 'LineWidth', 2);
pause(1);
frame = getframe(gcf); % Capture the current frame
writeVideo(video_prm, frame); % Write the frame to the video
end
end
hold off;
axis equal;
% Create obstacle labels for the legend
obstacle_labels = cell(1, length(obstacles));
for i = 1:length(obstacles)
obstacle_labels{i} = sprintf('Obstacle %d', i);
end
legend([obstacle_labels, 'Initial Configuration', 'Goal Configuration', 'Manipulator Path']);
close(video_prm); % Close the video writer
%% Function to plot the manipulator
function plotManipulator(q, link_lengths, color, varargin)
x = 0;
y = 0;
plot(x, y, 'o', 'MarkerSize', 5, 'MarkerFaceColor', color);
% Calculate each joint location using forward kinematics
for i = 1:length(q)
x_next = x + link_lengths(i) * cos(sum(q(1:i)));
y_next = y + link_lengths(i) * sin(sum(q(1:i)));
% Create line segment to connect previous and current joint
line([x, x_next], [y, y_next], 'Color', color, varargin{:});
x = x_next;
y = y_next;
end
end