forked from petercorke/robotics-toolbox-matlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PRM.m
389 lines (321 loc) · 13.5 KB
/
PRM.m
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
%PRM Probabilistic RoadMap navigation class
%
% A concrete subclass of the abstract Navigation class that implements the
% probabilistic roadmap navigation algorithm over an occupancy grid. This
% performs goal independent planning of roadmaps, and at the query stage
% finds paths between specific start and goal points.
%
% Methods::
% PRM Constructor
% plan Compute the roadmap
% query Find a path
% plot Display the obstacle map
% display Display the parameters in human readable form
% char Convert to string
%
% Example::
% load map1 % load map
% goal = [50,30]; % goal point
% start = [20, 10]; % start point
% prm = PRM(map); % create navigation object
% prm.plan() % create roadmaps
% prm.query(start, goal) % animate path from this start location
%
% References::
%
% - Probabilistic roadmaps for path planning in high dimensional configuration spaces,
% L. Kavraki, P. Svestka, J. Latombe, and M. Overmars,
% IEEE Transactions on Robotics and Automation, vol. 12, pp. 566-580, Aug 1996.
% - Robotics, Vision & Control, Section 5.2.4,
% P. Corke, Springer 2011.
%
% See also Navigation, DXform, Dstar, PGraph.
% Copyright (C) 1993-2017, by Peter I. Corke
%
% This file is part of The Robotics Toolbox for MATLAB (RTB).
%
% RTB is free software: you can redistribute it and/or modify
% it under the terms of the GNU Lesser General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% RTB is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU Lesser General Public License for more details.
%
% You should have received a copy of the GNU Leser General Public License
% along with RTB. If not, see <http://www.gnu.org/licenses/>.
%
% http://www.petercorke.com
% Peter Corke 8/2009.
classdef PRM < Navigation
properties
npoints % number of sample points
npoints0
distthresh % distance threshold, links between vertices
% must be less than this.
distthresh0 % distance threshold set by constructor option
graph % graph Object representing random nodes
vgoal % index of vertex closest to goal
vstart % index of vertex closest to start
localGoal % next vertex on the roadmap
localPath % set of points along path to next vertex
vpath % list of vertices between start and goal
gpath
end
methods
% constructor
function prm = PRM(varargin)
%PRM.PRM Create a PRM navigation object
%
% P = PRM(MAP, options) is a probabilistic roadmap navigation
% object, and MAP is an occupancy grid, a representation of a
% planar world as a matrix whose elements are 0 (free space) or 1
% (occupied).
%
% Options::
% 'npoints',N Number of sample points (default 100)
% 'distthresh',D Distance threshold, edges only connect vertices closer
% than D (default 0.3 max(size(occgrid)))
%
% Other options are supported by the Navigation superclass.
%
% See also Navigation.Navigation.
% invoke the superclass constructor, it handles some options
prm = prm@Navigation(varargin{:});
% create an empty 2D graph
prm.graph = PGraph(2);
% parse out PRM specific options and save in the navigation object
opt.npoints = 100;
opt.distthresh = 0.3*max(size(prm.occgridnav));
[opt,args] = tb_optparse(opt, varargin);
prm.npoints0 = opt.npoints;
prm.distthresh0 = opt.distthresh;
end
function plan(prm, varargin)
%PRM.plan Create a probabilistic roadmap
%
% P.plan(OPTIONS) creates the probabilistic roadmap by randomly
% sampling the free space in the map and building a graph with
% edges connecting close points. The resulting graph is kept
% within the object.
%
% Options::
% 'npoints',N Number of sample points (default is set by constructor)
% 'distthresh',D Distance threshold, edges only connect vertices closer
% than D (default set by constructor)
% 'movie',M make a movie of the PRM planning
% build a graph over the free space
prm.message('create the graph');
opt.npoints = prm.npoints0;
opt.distthresh = prm.distthresh0; % default is constructor value
opt.animate = false;
opt.movie = [];
opt = tb_optparse(opt, varargin);
prm.npoints = opt.npoints;
prm.distthresh = opt.distthresh; % actual value used is constructor value overridden here
prm.graph.clear(); % empty the graph
prm.vpath = [];
create_roadmap(prm, opt); % build the graph
end
function pp = query(prm, start, goal)
%PRM.query Find a path between two points
%
% P.query(START, GOAL) finds a path (Mx2) from START to GOAL.
%
if prm.graph.n == 0
error('RTB:PRM:noplan', 'query: no plan: run the planner');
end
checkquery(prm, start, goal)
% find the vertex closest to the goal
prm.vgoal = prm.closest(prm.goal);
if isempty(prm.vgoal)
error('RTB:PRM:nopath', 'plan: no path roadmap -> goal: rerun the planner');
end
% find the vertex closest to the start
prm.vstart = prm.closest(prm.start);
if isempty(prm.vstart)
error('RTB:PRM:nopath', 'plan: no path start -> roadmap: rerun the planner');
end
% find a path through the graph
prm.vpath = prm.graph.Astar(prm.vstart, prm.vgoal);
% the path is a list of nodes from vstart to vgoal
% discard the first vertex, since we plan a local path to it
prm.gpath = prm.vpath;
prm.gpath = prm.gpath(2:end);
if nargout > 0
pp = [prm.start prm.graph.coord(prm.vpath) prm.goal]';
end
end
function c = closest(prm, vertex, vcomponent)
% find a node close to v that is:
% - closest
% - in the same component
% - free straight line path
if nargin > 2
component = prm.graph.component(vcomponent);
end
[d,v] = prm.graph.distances(vertex);
c = [];
% test neighbours in order of increasing distance and check for a clear
% path
for i=1:length(d)
if nargin > 2
if prm.graph.component(v(i)) ~= component
continue; % not connected
end
end
if ~prm.testpath(vertex, prm.graph.coord(v(i)))
continue; % no path
end
c = v(i);
break
end
end
% Handler invoked by Navigation.path() to start the navigation process
%
% - find a path through the graph
% - determine vertices closest to start and goal
% - find path to first vertex
% Invoked for each step on the path by path() method.
function n = next(prm, p)
if all(p(:) == prm.goal)
n = []; % signal that we've arrived
return;
end
% we take the next point from the localPath
if numrows(prm.localPath) == 0
% local path is consumed, move to next vertex
if isempty(prm.gpath)
% we have arrived at the goal vertex
% make the path from this vertex to the goal coordinate
prm.localPath = bresenham(p, prm.goal);
prm.localPath = prm.localPath(2:end,:);
prm.localGoal = [];
else
% set local goal to next vertex in gpath and remove it from the list
prm.localGoal = prm.gpath(1);
prm.gpath = prm.gpath(2:end);
% compute local path to the next vertex
prm.localPath = bresenham(p, prm.graph.coord(prm.localGoal));
prm.localPath = prm.localPath(2:end,:);
prm.graph.highlight_node(prm.localGoal);
end
end
n = prm.localPath(1,:)'; % take the first point
prm.localPath = prm.localPath(2:end,:); % and remove from the path
end
function s = char(prm)
%PRM.char Convert to string
%
% P.char() is a string representing the state of the PRM
% object in human-readable form.
%
% See also PRM.display.
% invoke the superclass char() method
s = char@Navigation(prm);
% add PRM specific stuff information
s = char(s, sprintf(' graph size: %d', prm.npoints));
s = char(s, sprintf(' dist thresh: %g', prm.distthresh));
s = char(s, char(prm.graph) );
end
function plot(prm, varargin)
%PRM.plot Visualize navigation environment
%
% P.plot() displays the roadmap and the occupancy grid.
%
% Options::
% 'goal' Superimpose the goal position if set
% 'nooverlay' Don't overlay the PRM graph
%
% Notes::
% - If a query has been made then the path will be shown.
% - Goal and start locations are kept within the object.
opt.overlay = true;
opt.nodes = true;
[opt,args] = tb_optparse(opt, varargin);
% display the occgrid
plot@Navigation(prm, args{:});
if opt.overlay
hold on
prm.graph.plot('componentcolor');
if opt.nodes && ~isempty(prm.vpath)
prm.graph.highlight_path(prm.vpath, ...
'NodeFaceColor', 'y', ...
'NodeEdgeColor', 'k', ...
'EdgeColor', 'k', ...
'EdgeThickness', 2 ...
)
v0 = prm.vpath(1);
p0 = prm.graph.coord(v0);
plot([prm.start(1) p0(1)], [prm.start(2) p0(2)], 'Color', 'k', ...
'LineWidth', 1.5);
vf = prm.vpath(end);
pf = prm.graph.coord(vf);
plot([prm.goal(1) pf(1)], [prm.goal(2) pf(2)], 'Color', 'k', ...
'LineWidth', 1.5);
end
end
% % get the superclass to plot the path
% if nargin > 1
% plot@Navigation(prm, varargin{:});
% end
hold off
set(gcf, 'Color', [1 1 1])
end
end % method
methods (Access='protected')
% private methods
% create the roadmap
function create_roadmap(prm, opt)
a = Animate(opt.movie, 'fps', 5);
for j=1:prm.npoints
% pick a point not in obstacle
while true
x = prm.randi(numcols(prm.occgrid));
y = prm.randi(numrows(prm.occgrid));
if ~prm.isoccupied([x y])
break; % free cell
end
end
new = [x; y];
% add it to the graph
vnew = prm.graph.add_node(new);
% find the closest node already in the graph
[d,v] = prm.graph.distances(new);
% test neighbours in order of increasing distance and check for a clear
% path
for i=1:length(d)
if d(i) > prm.distthresh
continue; % it's too far
end
if ~prm.testpath(new, prm.graph.coord(v(i)))
continue; % no path
end
% add an edge from the found node to new
prm.graph.add_edge(v(i), vnew);
end
if opt.animate || ~isempty(opt.movie)
prm.plot()
if ~isempty(opt.movie)
a.add();
else
pause(1)
end
end
end
end
% test the path from p1 to p2 is entirely in free space
function c = testpath(prm, p1, p2)
p = bresenham(p1, p2);
for pp=p'
if prm.isoccupied(pp)
c = false;
return;
end
end
c = true;
end
end % private methods
end % classdef