-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsteap.m
372 lines (306 loc) · 10.4 KB
/
steap.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
close all
clear
import gtsam.*
import gpmp2.*
map_path = '~/catkin_ws/src/epic_drive_functions/maps/maze.png'
resolution = 0.05
%% small dataset
map_matrix = imread(map_path)
map_matrix = ~(map_matrix)
rows = size(map_matrix, 1);
cols = size(map_matrix, 2);
cell_size = resolution;
origin_x = -5;
origin_y = -5;
origin_point2 = Point2(origin_x, origin_y);
% signed distance field
field = signedDistanceField2D(map_matrix, cell_size);
sdf = PlanarSDF(origin_point2, cell_size, field);
% % plot sdf
figure(2)
plotSignedDistanceField2D(field, 0, 0, cell_size);
title('Signed Distance Field')
%% settings
total_time_sec = 1;
total_time_step = 100; %how many variable factors
check_inter = 5;
delta_t = total_time_sec / total_time_step;
total_check_step = (check_inter + 1)*total_time_step;
% use 2d vehicle dynamics
use_vehicle_dynamics = true;
dynamics_sigma = 0.001;
% robot model
spheres_data = [...
0 0.0 0.0 0.0 0.05];
nr_body = size(spheres_data, 1);
sphere_vec = BodySphereVector;
for i=1:nr_body
sphere_vec.push_back(BodySphere(spheres_data(i,1), spheres_data(i,5), ...
Point3(spheres_data(i,2:4)')));
end
robot = Pose2MobileBaseModel(Pose2MobileBase, sphere_vec);
% GP
Qc = 1 * eye(robot.dof());
Qc_model = noiseModel.Gaussian.Covariance(Qc);
% Obstacle avoid settings
cost_sigma = 0.01;
epsilon_dist = 0.02;
% prior to start/goal
pose_fix = noiseModel.Isotropic.Sigma(robot.dof(), 0.0001);
vel_fix = noiseModel.Isotropic.Sigma(robot.dof(), 0.0001);
% start and end conf
start_pose = Pose2(-3.5, -2, 0);
start_vel = [0, 0, 0]';
end_pose = Pose2(2, 1, pi/2);
end_vel = [0, 0, 0]';
avg_vel = [end_pose.x()-start_pose.x(); end_pose.y()-start_pose.y(); ...
end_pose.theta()-start_pose.theta()] / delta_t;
% plot param
pause_time = total_time_sec / total_time_step;
% % plot start / end configuration
figure(1), hold on
plotEvidenceMap2D(map_matrix, origin_x, origin_y, cell_size);
title('Layout')
plotPlanarMobileBase(robot.fk_model(), start_pose, [0.4 0.2], 'b', 1);
plotPlanarMobileBase(robot.fk_model(), end_pose, [0.4 0.2], 'r', 1);
hold off
%% initial values
init_values = Values;
for i = 0 : total_time_step
key_pos = symbol('x', i);
key_vel = symbol('v', i);
% initialize as straight line in conf space
pose = Pose2(start_pose.x() * (total_time_step-i)/total_time_step + ...
end_pose.x() * i/total_time_step, ...
start_pose.y() * (total_time_step-i)/total_time_step + ...
end_pose.y() * i/total_time_step, ...
start_pose.theta() * (total_time_step-i)/total_time_step + ...
end_pose.theta() * i/total_time_step);
vel = avg_vel;
init_values.insert(key_pos, pose);
init_values.insert(key_vel, vel);
end
%% build graph
graph = NonlinearFactorGraph;
for i = 0 : total_time_step
key_pos = symbol('x', i);
key_vel = symbol('v', i);
% start/end priors
if i==0
graph.add(PriorFactorPose2(key_pos, start_pose, pose_fix));
graph.add(PriorFactorVector(key_vel, start_vel, vel_fix));
elseif i==total_time_step
graph.add(PriorFactorPose2(key_pos, end_pose, pose_fix));
graph.add(PriorFactorVector(key_vel, end_vel, vel_fix));
end
% cost factor
graph.add(ObstaclePlanarSDFFactorPose2MobileBase(key_pos, ...
robot, sdf, cost_sigma, epsilon_dist));
% vehicle dynamics
if use_vehicle_dynamics
graph.add(VehicleDynamicsFactorPose2(key_pos, key_vel, ...
dynamics_sigma));
end
% GP priors and cost factor
if i > 0
key_pos1 = symbol('x', i-1);
key_pos2 = symbol('x', i);
key_vel1 = symbol('v', i-1);
key_vel2 = symbol('v', i);
graph.add(GaussianProcessPriorPose2(key_pos1, key_vel1, ...
key_pos2, key_vel2, delta_t, Qc_model));close all
clear
import gtsam.*
import gpmp2.*
%% small dataset
dataset = generate2Ddataset('MobileMap1');
rows = dataset.rows;
cols = dataset.cols;
cell_size = dataset.cell_size;
origin_point2 = Point2(dataset.origin_x, dataset.origin_y);
% signed distance field
field = signedDistanceField2D(dataset.map, cell_size);
sdf = PlanarSDF(origin_point2, cell_size, field);
% % plot sdf
figure(2)
plotSignedDistanceField2D(field, dataset.origin_x, dataset.origin_y, dataset.cell_size);
title('Signed Distance Field')
%% settings
total_time_sec = 2.0;
total_time_step = 10; %how many variable factors
check_inter = 5;
delta_t = total_time_sec / total_time_step;
total_check_step = (check_inter + 1)*total_time_step;
% use 2d vehicle dynamics
use_vehicle_dynamics = true;
dynamics_sigma = 0.001;
% robot model
spheres_data = [...
0 0.0 0.0 0.0 0.2];
nr_body = size(spheres_data, 1);
sphere_vec = BodySphereVector;
for i=1:nr_body
sphere_vec.push_back(BodySphere(spheres_data(i,1), spheres_data(i,5), ...
Point3(spheres_data(i,2:4)')));
end
robot = Pose2MobileBaseModel(Pose2MobileBase, sphere_vec);
% GP
Qc = 1 * eye(robot.dof());
Qc_model = noiseModel.Gaussian.Covariance(Qc);
% Obstacle avoid settings
cost_sigma = 0.01;
epsilon_dist = 0.2;
% prior to start/goal
pose_fix = noiseModel.Isotropic.Sigma(robot.dof(), 0.0001);
vel_fix = noiseModel.Isotropic.Sigma(robot.dof(), 0.0001);
% start and end conf
start_pose = Pose2(-3, 1, pi/2);
start_vel = [0, 0, 0]';
end_pose = Pose2(3, 3, pi/2);
end_vel = [0, 0, 0]';
avg_vel = [end_pose.x()-start_pose.x(); end_pose.y()-start_pose.y(); ...
end_pose.theta()-start_pose.theta()] / delta_t;
% plot param
pause_time = total_time_sec / total_time_step;
% % plot start / end configuration
figure(1), hold on
plotEvidenceMap2D(dataset.map, dataset.origin_x, dataset.origin_y, cell_size);
title('Layout')
plotPlanarMobileBase(robot.fk_model(), start_pose, [0.4 0.2], 'b', 1);
plotPlanarMobileBase(robot.fk_model(), end_pose, [0.4 0.2], 'r', 1);
hold off
%% initial values
init_values = Values;
for i = 0 : total_time_step
key_pos = symbol('x', i);
key_vel = symbol('v', i);
% initialize as straight line in conf space
pose = Pose2(start_pose.x() * (total_time_step-i)/total_time_step + ...
end_pose.x() * i/total_time_step, ...
start_pose.y() * (total_time_step-i)/total_time_step + ...
end_pose.y() * i/total_time_step, ...
start_pose.theta() * (total_time_step-i)/total_time_step + ...
end_pose.theta() * i/total_time_step);
vel = avg_vel;
init_values.insert(key_pos, pose);
init_values.insert(key_vel, vel);
end
%% build graph
graph = NonlinearFactorGraph;
for i = 0 : total_time_step
key_pos = symbol('x', i);
key_vel = symbol('v', i);
% start/end priors
if i==0
graph.add(PriorFactorPose2(key_pos, start_pose, pose_fix));
graph.add(PriorFactorVector(key_vel, start_vel, vel_fix));
elseif i==total_time_step
graph.add(PriorFactorPose2(key_pos, end_pose, pose_fix));
graph.add(PriorFactorVector(key_vel, end_vel, vel_fix));
end
% cost factor
graph.add(ObstaclePlanarSDFFactorPose2MobileBase(key_pos, ...
robot, sdf, cost_sigma, epsilon_dist));
% vehicle dynamics
if use_vehicle_dynamics
graph.add(VehicleDynamicsFactorPose2(key_pos, key_vel, ...
dynamics_sigma));
end
% GP priors and cost factor
if i > 0
key_pos1 = symbol('x', i-1);
key_pos2 = symbol('x', i);
key_vel1 = symbol('v', i-1);
key_vel2 = symbol('v', i);
graph.add(GaussianProcessPriorPose2(key_pos1, key_vel1, ...
key_pos2, key_vel2, delta_t, Qc_model));
% GP cost factor
for j = 1:check_inter
tau = j * (total_time_sec / total_check_step);
graph.add(ObstaclePlanarSDFFactorGPPose2MobileBase( ...
key_pos1, key_vel1, key_pos2, key_vel2, ...
robot, sdf, cost_sigma, epsilon_dist, ...
Qc_model, delta_t, tau));
end
end
end
%% optimize!
use_trustregion_opt = true;
use_LM_opt = true;
if use_trustregion_opt
parameters = DoglegParams;
parameters.setVerbosity('ERROR');
optimizer = DoglegOptimizer(graph, init_values, parameters);
elseif use_LM_opt
parameters = LevenbergMarquardtParams;
parameters.setVerbosity('ERROR');
optimizer = LevenbergMarquardtOptimizer(graph, init_values, parameters);
else
parameters = GaussNewtonParams;
parameters.setVerbosity('ERROR');
optimizer = GaussNewtonOptimizer(graph, init_values, parameters);
end
optimizer.optimize();
result = optimizer.values();
result.print('Final results')
%% plot final values
plot_inter = check_inter;
if plot_inter
total_plot_step = total_time_step * (plot_inter + 1);
plot_values = interpolatePose2Traj(result, Qc_model, delta_t, plot_inter, 0, total_time_step);
else
total_plot_step = total_time_step;
plot_values = result;
end
figure(4), hold on
plotEvidenceMap2D(dataset.map, dataset.origin_x, dataset.origin_y, cell_size);
for i=0:total_plot_step
p = plot_values.atPose2(symbol('x', i));
plotPlanarMobileBase(robot.fk_model(), p, [0.4 0.2], 'b', 1);
end
hold off;
% GP cost factor
for j = 1:check_inter
tau = j * (total_time_sec / total_check_step);
graph.add(ObstaclePlanarSDFFactorGPPose2MobileBase( ...
key_pos1, key_vel1, key_pos2, key_vel2, ...
robot, sdf, cost_sigma, epsilon_dist, ...
Qc_model, delta_t, tau));
end
end
end
%% optimize!
use_trustregion_opt = true;
use_LM_opt = true;
if use_trustregion_opt
parameters = DoglegParams;
parameters.setVerbosity('ERROR');
optimizer = DoglegOptimizer(graph, init_values, parameters);
elseif use_LM_opt
parameters = LevenbergMarquardtParams;
parameters.setVerbosity('ERROR');
optimizer = LevenbergMarquardtOptimizer(graph, init_values, parameters);
else
parameters = GaussNewtonParams;
parameters.setVerbosity('ERROR');
optimizer = GaussNewtonOptimizer(graph, init_values, parameters);
end
optimizer.optimize();
result = optimizer.values();
result.print('Final results')
%% plot final values
plot_inter = check_inter;
if plot_inter
total_plot_step = total_time_step * (plot_inter + 1);
plot_values = interpolatePose2Traj(result, Qc_model, delta_t, plot_inter, 0, total_time_step);
else
total_plot_step = total_time_step;
plot_values = result;
end
figure(4), hold on
plotEvidenceMap2D(map_matrix, origin_x, origin_y, cell_size);
for i=0:total_plot_step
p = plot_values.atPose2(symbol('x', i));
plotPlanarMobileBase(robot.fk_model(), p, [0.4 0.2], 'b', 1);
end
hold off;