-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsteap_v3.m
286 lines (258 loc) · 8.14 KB
/
steap_v3.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
close all
clear
import gtsam.*
import gpmp2.*
%%ROS Config
rosshutdown
rosinit
server = rossvcserver('/steap_plan', 'carrot_planner/path_array', @serviceCallback,...
'DataFormat','struct');
req = rosmessage(server);
% Arrays that saves current trajectory
global x_array
global y_array
%% small dataset
dataset = generate2Ddataset('MobileMap1');
rows = dataset.rows;
cols = dataset.cols;
cell_size = dataset.cell_size;
origin = [0, 0, 0];
origin_point3 = Point3(origin');
% sdf
disp('calculating signed distance field ...');
field = signedDistanceField3D(dataset.map, dataset.cell_size);
disp('calculating signed distance field done');
% init sdf
sdf = SignedDistanceField(origin_point3, cell_size, size(field, 1), ...
size(field, 2), size(field, 3));
for z = 1:size(field, 3)
sdf.initFieldData(z-1, field(:,:,z)');
end
%% generate mobile arm
base_T_arm = Pose3(Rot3(eye(3)), Point3([0,0,0]'));
% arm: JACO2 6DOF arm
% alpha = [0]';
% a = [0]';
% d = [0]';
% % theta = [0, 0, 0, 0, 0, 0]';
% % abstract arm
% arm = Arm(0, a, alpha, d);
% % abstract mobile arm
% marm = Pose2MobileArm(arm, base_T_arm);
% % sphere data [id x y z r]
% spheres_data = [...
% 0 0.0 0.0 0.0 0.8
% ];
%
% 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
% arm_model = generateMobileArm('');
% arm model
arm_model = generateMobileArm('SimpleTwoLinksArm');
% GP
Qc = 1 * eye(5);
Qc_model = noiseModel.Gaussian.Covariance(Qc);
% Obstacle avoid settings
cost_sigma = 0.1;
epsilon_dist = 0.1;
% prior to start/goal
pose_fix = noiseModel.Isotropic.Sigma(5, 0.0001);
vel_fix = noiseModel.Isotropic.Sigma(5, 0.0001);
%% settings
error_mode = 0;
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;
% GP
% Qc = 1 * eye(6);
% 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(6, 0.0001);
% vel_fix = noiseModel.Isotropic.Sigma(6, 0.0001);
% start and end conf
[x_ist, y_ist, t_ist] = get_pose_estimate();
start_vector = [x_ist, y_ist, t_ist];
start_pose = Pose2(x_ist, y_ist, t_ist);
start_vel = [0, 0, 0];
start_conf = Pose2Vector(start_pose, zeros(3,1));
end_vector = [8, 5, pi/2];
end_pose = Pose2(8, 5, pi/2);
end_vel = [0, 0, 0]';
end_conf = Pose2Vector(end_pose, zeros(3,1));
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;
%optimization settings
opt_setting = TrajOptimizerSetting(3);
% opt_setting.set_total_step(total_time_step);
% opt_setting.set_total_time(total_time_sec);
% opt_setting.set_conf_prior_model(pose_fix_sigma);
% opt_setting.set_vel_prior_model(vel_fix_sigma);
%
% opt_setting.set_flag_pos_limit(flag_limit);
% opt_setting.set_flag_vel_limit(flag_limit);
% opt_setting.set_joint_pos_limits_up(joint_limit_vec_up);
% opt_setting.set_joint_pos_limits_down(joint_limit_vec_down);
% opt_setting.set_vel_limits(joint_vel_limit_vec);
% opt_setting.set_pos_limit_thresh(joint_limit_thresh);
% opt_setting.set_vel_limit_thresh(joint_vel_limit_thresh);
% opt_setting.set_pos_limit_model(joint_limit_model);
% opt_setting.set_vel_limit_model(joint_vel_limit_model);
%
% opt_setting.set_epsilon(epsilon_dist);
% opt_setting.set_cost_sigma(cost_sigma);
% opt_setting.set_obs_check_inter(check_inter);
%
% opt_setting.set_Qc_model(Qc);
%
% opt_setting.setDogleg();
% test = ISAM2TrajOptimizer2DArm(robot, sdf, opt_setting);
%% initialize
%init traj
init_values = initPose2VectorTrajStraightLine(start_pose, zeros(3,1), end_pose, zeros(3,1), total_time_step);
batch_values = BatchTrajOptimizePose2MobileArm(arm_model, sdf, start_conf, zeros(3,1), end_conf, zeros(3,1), init_values, opt_setting)
marm_inc_inf = ISAM2TrajOptimizerPose2MobileArm(arm_model, sdf, opt_setting);
marm_inc_inf.initFactorGraph(start_conf, zeros(3,1), end_conf, zeros(3,1));
marm_inc_inf.initValues(batch_values);
marm_inc_inf.update();
% result.print('Final results')
%
% %% STEAP
% isam = ISAM2;
%
% figure(4)
% hold on
% plotEvidenceMap2D(dataset.map, dataset.origin_x, dataset.origin_y, cell_size);
%
% xlim([0 10])
% ylim([0 10])
%
% %plot initial trajectory
% plot_inter = check_inter;
% total_plot_step = total_time_step * (plot_inter + 1);
% plot_values = interpolatePose2Traj(result, Qc_model, delta_t, plot_inter, 0, total_time_step);
% plot_trajectory(plot_values, total_plot_step, 'r');
%
% for i = 0 : total_time_step - 1
% key_pos = symbol('x', i+1);
% goal = result.atPose2(key_pos);
%
% plot_inter = check_inter; %interpolate to next time step
% total_plot_step = total_time_step * (plot_inter + 1);
% exec_values = interpolatePose2Traj(result, Qc_model, delta_t, 5, i, i+1);
% plot_trajectory(exec_values, total_plot_step, 'b');
%
% coll_cost = CollisionCostPose2MobileBase2D(robot, sdf, exec_values, opt_setting); %calculate collision cost
%
% if coll_cost ~= 0 && error_mode == 1
% error("At step %i, plan is not collision free (Collision Cost: %i)", i, coll_cost);
% end
%
% %execute Trajectory
% provide_trajectory(exec_values, 6);
% [x_ist, y_ist, t_ist] = send_goal(goal.x, goal.y, goal.theta);
%
% %get current state and use if it was measured recently then
% %update factor graph to perform incremental inference
% plot(x_ist, y_ist, 'O g');
%
% % pose_estimate = Pose2(x, y, t);
% % estimation_noise = noiseModel.Diagonal.Sigmas([0.1; 0.1; 15]);
% % graph.add(PriorFactorPose2(key_pos, pose_estimate, estimation_noise));
% %
% % %optimize again
% % parameters = LevenbergMarquardtParams;
% % parameters.setVerbosity('ERROR');
% % optimizer = LevenbergMarquardtOptimizer(graph, result, parameters);
% % optimizer.optimize();
% % result = optimizer.values();
% %
% % provide_trajectory(result);
% end
%
% %% 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
% %
% % for i=0:total_plot_step
% % if i>0
% % p0_x = plot_values.atPose2(symbol('x', i)).x;
% % p0_y = plot_values.atPose2(symbol('x', i)).y
% %
% % p1_x = plot_values.atPose2(symbol('x', i-1)).x;
% % p1_y = plot_values.atPose2(symbol('x', i-1)).y;
% %
% %
% % % plotPlanarMobileBase(robot.fk_model(), p, [0.4 0.2], 'b', 1);
% % plot([p0_x p1_x], [p0_y p1_y], 'g');
% % end
% % end
%
% %% FUNCTIONS
% %ROS Trajectory Service
% function provide_trajectory(Values, steps)
% global x_array;
% global y_array;
%
% [x_array, y_array] = values_to_array(Values, steps);
% end
%
% function resp = serviceCallback(~,req,resp)
% global x_array;
% global y_array;
% resp.PathXArray(1) = x_array(1);
%
% i = 1;
% while x_array(i) ~= 0 && y_array(i) ~= 0
% resp.PathXArray(i) = x_array(i);
% resp.PathYArray(i) = y_array(i);
% i = i + 1;
% end
% end
%
% %Other Functions
% function plot_trajectory(values, plot_step, color)
% import gtsam.*;
% import gpmp2.*;
% for i = 0 : plot_step
% if i>0
% try
% p0_x = values.atPose2(symbol('x', i)).x;
% p0_y = values.atPose2(symbol('x', i)).y;
%
% p1_x = values.atPose2(symbol('x', i-1)).x;
% p1_y = values.atPose2(symbol('x', i-1)).y;
%
% % plotPlanarMobileBase(robot.fk_model(), p, [0.4 0.2], 'b', 1);
% plot([p0_x p1_x], [p0_y p1_y], color);
% catch
% break;
% end
% end
% end
% end
%
%
%