forked from petercorke/robotics-toolbox-matlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lattice.m
492 lines (406 loc) · 16.8 KB
/
Lattice.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
%Lattice Lattice planner navigation class
%
% A concrete subclass of the abstract Navigation class that implements the
% lattice planner navigation algorithm over an occupancy grid. This
% performs goal independent planning of kinematically feasible paths.
%
% Methods::
% Lattice 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
%
% Properties (read only)::
% graph A PGraph object describign the tree
%
% Example::
%
% lp = Lattice(); % create navigation object
% lp.plan('iterations', 8) % create roadmaps
% lp.query( [1 2 pi/2], [2 -2 0] ) % find path
% lp.plot(); % plot the path
%
% References::
%
% - Robotics, Vision & Control, Section 5.2.4,
% P. Corke, Springer 2016.
%
%
% See also Navigation, DXform, Dstar, PGraph.
% Notes::
% - The lattice is stored as a 3D PGraph object with coordinates x,y,theta
% where theta is stored as a multiple of pi/2. This was probably a bad
% design decision, it complicates the code a lot.
% - Using the Lattice distance metric in PGraph gives different A* results,
% valid path, same cost, just different. Blah.
% 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 Lattice < Navigation
properties
iterations % number of iterations
cost % segment cost
% must be less than this.
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
grid
root
end
methods
% constructor
function lp = Lattice(varargin)
%Lattice.Lattice Create a Lattice navigation object
%
% P = Lattice(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::
% 'grid',G Grid spacing in X and Y (default 1)
% 'root',R Root coordinate of the lattice (2x1) (default [0,0])
% 'iterations',N Number of sample points (default Inf)
% 'cost',C Cost for straight, left, right (default [1,1,1])
% 'inflate',K Inflate all obstacles by K cells.
%
% Other options are supported by the Navigation superclass.
%
% Notes::
% - Iterates until the area defined by the map is covered.
%
% See also Navigation.Navigation.
% invoke the superclass constructor, it handles some options
lp = lp@Navigation(varargin{:});
% create an empty graph over SE2
lp.graph = PGraph(3, 'distance', 'SE2');
% parse out Lattice specific options and save in the navigation object
opt.grid = 1;
opt.root = [0 0 0]';
opt.iterations = Inf;
opt.cost = [1 1 1];
[lp,args] = tb_optparse(opt, varargin, lp);
end
function plan(lp, varargin)
%Lattice.plan Create a lattice plan
%
% P.plan(OPTIONS) creates the lattice by iteratively building a tree of
% possible paths. The resulting graph is kept within the object.
%
% Options::
% 'iterations',N Number of sample points (default Inf)
% 'cost',C Cost for straight, left, right (default [1,1,1])
%
% Default parameter values come from the constructor
opt.iterations = lp.iterations;
opt.cost = lp.cost;
[opt,args] = tb_optparse(opt, varargin);
if isempty(lp.occgridnav) && isinf(opt.iterations)
error('RTB:Lattice:badarg', 'If no occupancy grid given then iterations must be finite');
end
lp.iterations = opt.iterations;
lp.cost = opt.cost;
% check root node sanity
if isempty(lp.root)
error('no root node specified');
end
switch length(lp.root)
case 2
lp.root = [lp.root(:); 0];
case 3
lp.root = lp.root(:);
otherwise
error('root must be 2- or 3-vector');
end
if lp.isoccupied(lp.root(1:2))
error('root node cell is occupied')
end
% build a graph over the free space
lp.message('create the graph');
lp.graph.clear(); % empty the graph
create_lattice(lp); % build the graph
fprintf('%d nodes created\n', lp.graph.n);
end
function pp = query(lp, start, goal)
%Lattice.query Find a path between two poses
%
% P.query(START, GOAL) finds a path (Nx3) from pose START (1x3)
% to pose GOAL (1x3). The pose is expressed as [X,Y,THETA].
%
if nargin < 3
error('must specify start and goal');
end
% set the goal coordinate
lp.goal = goal;
lp.start = start;
% convert angles to multiple of pi2
start(3) = round(start(3)*2/pi);
goal(3) = round(goal(3)*2/pi);
lp.vstart = lp.graph.closest(start, 0.5);
lp.vgoal = lp.graph.closest(goal, 0.5);
if isempty(lp.vstart)
error('Lattice:badarg', 'start configuration not in lattice');
end
if isempty(lp.vgoal)
error('Lattice:badarg', 'goal configuration not in lattice');
end
% find path through the graph using A* search
[lp.vpath,cost] = lp.graph.Astar(lp.vstart, lp.vgoal, 'directed');
fprintf('A* path cost %g\n', cost);
p = lp.graph.coord(lp.vpath);
if nargout > 0
pp = p';
pp(:,3) = angdiff( pp(:,3) * pi/2 );
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(lp, p)
end
function s = char(lp)
%Lattice.char Convert to string
%
% P.char() is a string representing the state of the Lattice
% object in human-readable form.
%
% See also Lattice.display.
% invoke the superclass char() method
s = char@Navigation(lp);
% add Lattice specific stuff information
s = char(s, sprintf(' grid spacing: %d', lp.grid));
s = char(s, sprintf(' costs [%d,%d,%d]', lp.cost));
s = char(s, sprintf(' iterations %d', lp.iterations));
s = char(s, sprintf(' Graph:'));
s = char(s, char(lp.graph) );
end
function plot(lp, varargin)
%Lattice.plot Visualize navigation environment
%
% P.plot() displays the occupancy grid with an optional distance field.
%
% Options::
% 'goal' Superimpose the goal position if set
% 'nooverlay' Don't overlay the Lattice graph
% get standard stuff done by the superclass
plot@Navigation(lp, varargin{:});
opt.nooverlay = false;
[opt,args] = tb_optparse(opt, varargin);
if ~opt.nooverlay
hold on
lp.showlattice(varargin{:});
hold off
end
if ~isempty(lp.vpath)
% highlight the path
hold on
lp.highlight(args{:});
hold off
end
grid on
end
% function path = animate(lp, varargin)
% path = [];
% for k=1:length(lp.vpath)-1
% v1 = lp.vpath(k);
% v2 = lp.vpath(k+1);
%
% seg = drivearc(lp, [v1, v2], 10);
% path = [path seg(:,1:end-1)];
% end
% path = [path seg(:,end)];
%
% end
end % method
methods (Access='protected')
% private methods
% create the lattice
function create_lattice(lp)
% add the root node
root = lp.graph.add_node( lp.root );
% possible destinations in root node frame
% x direction is forward
% orientation represented by integer 0-3 representing multiples of pi/2
d = lp.grid;
destinations = [
d d d % x
0 d -d % y
0 1 3 % theta *pi/2
];
% now we iterate, repeating this patter at each leaf node
iteration = 1;
while iteration <= lp.iterations
additions = 0;
for node = find(lp.graph.connectivity_out == 0) % foreach leaf node
% get the pose of this node
pose = lp.graph.coord(node);
xys = pose(1:2); heading = pose(3);
% transform the motion directions to this pose and b
xy = bsxfun(@plus, xys, homtrans(rot2(heading*pi/2), destinations(1:2,:)));
theta = mod(heading+destinations(3,:), 4);
newDestinations = [xy; theta];
% now add paths to these new poses
for i=1:numcols(destinations)
% check to see if a node for this pose already exists
v = lp.graph.closest(newDestinations(:,i), 0.5);
if isempty(v)
%node doesn't exist
if ~lp.isoccupied(newDestinations(1:2,i))
% it's not occupied
% add a new node and an edge
nv = lp.graph.add_node( newDestinations(:,i), node, lp.cost(i));
lp.graph.add_edge(node, nv, lp.cost(i));
additions = additions + 1;
end
else
% node already exists, add an edge
lp.graph.add_edge(node, v, lp.cost(i));
additions = additions + 1;
end
end
end
iteration = iteration + 1;
if additions == 0
break; % no more nodes can be added to the space
end
end
end
% Display the lattice, possible arcs, and start/goal markers if relevant
function showlattice(lp, varargin)
lineopt = {'Linewidth', 0.2, 'Color', [0.5 0.5 0.5]};
markeropt = {'bo', 'MarkerSize', 4, 'MarkerFaceColor', 'b'};
p = lp.graph.coord();
th = p(3,:);
th(th == 3) = -1;
plot3(p(1,:), p(2,:), th*pi/2, markeropt{:});
xlabel('x'); ylabel('y'); zlabel('\theta')
grid on
hold on
plot3(lp.root(1), lp.root(2), lp.root(3), 'ko', 'MarkerSize', 8);
view(0,90);
axis equal
rotate3d
% draw the lattice
for e=1:lp.graph.ne
v = lp.graph.vertices(e); % get the vertices of the edge
drawarc(lp, v, lineopt);
end
end
function highlight(lp, p)
if nargin > 1
assert(numcols(p)==3, 'path must have 3 columns');
for i=1:numrows(p)
vpath(i) = lp.graph.closest(p(i,:) );
end
else
vpath = lp.vpath;
end
% highlight the path
for k=1:length(vpath)-1
v1 = vpath(k);
v2 = vpath(k+1);
drawarc(lp, [v1, v2], {'Linewidth', 3, 'Color', 'r'});
end
end
% draw an arc
function drawarc(lp, v, lineOpts)
g = lp.graph;
% use lower resolution if lots of arcs
if lp.iterations < 4
narc = 20;
elseif lp.iterations < 10
narc = 10;
else
narc = 5;
end
v1 = v(1); v2 = v(2);
p1 = g.coord(v1);
p2 = g.coord(v2);
% frame {N} is start of the arc
theta = p1(3)*pi/2; % {0} -> {N}
T_0N = SE2(p1(1:2), theta);
dest = round( T_0N.inv * p2(1:2) ); % in {N}
if dest(2) == 0
% no heading change, straight line segment
th = [p1(3) p2(3)];
th(th == 3) = -1;
plot3([p1(1) p2(1)], [p1(2) p2(2)], th*pi/2, lineOpts{:});
else
% curved segment
c = T_0N * [0 dest(2)]';
th = ( linspace(-dest(2)/lp.grid, 0, narc) + p1(3) )*pi/2;
x = lp.grid*cos(th) + c(1);
y = lp.grid*sin(th) + c(2);
th0 = p1(3);
th0(th0==3) = -1;
thf = p2(3);
thf(thf==3) = -1;
plot3(x, y, linspace(th0, thf, narc)*pi/2, lineOpts{:});
end
end
% % this doesn't work quite properly...
% function path = drivearc(lp, v, narc)
% g = lp.graph;
%
%
% v1 = v(1); v2 = v(2);
% p1 = g.coord(v1); p1(3) = p1(3)*pi/2;
% p2 = g.coord(v2); p2(3) = p2(3)*pi/2;
%
% path = [];
%
% % frame {N} is start of the arc
% theta = p1(3); % {0} -> {N}
% T_0N = SE2(p1(1:2), theta);
%
% dest = round( T_0N.inv * p2(1:2) ); % in {N}
%
% if dest(2) == 0
% % no heading change, straight line segment
%
% for s=linspace(0, 1, narc)
% path = [path (1-s)*p1 + s*p2];
% end
% else
% % curved segment
% c = T_0N * [0 dest(2)]';
%
% th = ( linspace(-dest(2)/lp.grid, 0, narc) + p1(3) );
%
% x = lp.grid*cos(th) + c(1);
% y = lp.grid*sin(th) + c(2);
%
%
% th0 = p1(3);
% % % th0(th0==3) = -1;
% thf = p2(3);
% % % thf(thf==3) = -1;
% path = [path [x; y; linspace(th0, angdiff(thf,th0)+th0, narc)] ];
% end
% end
end % private methods
end % classdef