-
Notifications
You must be signed in to change notification settings - Fork 1
/
2d_bearing_only_slam_least_squares.m
208 lines (167 loc) · 7.31 KB
/
2d_bearing_only_slam_least_squares.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
clc
clear all
close all
%==============================================================================
%:::::::: IMPORTS :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
%==============================================================================
addpath './g2o_wrapper'
source 'utilities/utils.m'
source 'utilities/least_squares_utils.m'
source 'utilities/plot_utils.m'
%==============================================================================
%:::::::: LOAD GROUND TRUTH :::::::::::::::::::::::::::::::::::::::::::::::::::
%==============================================================================
fprintf('\nLoading g2o ground truth data... ');
# load ground truth dataset
[landmarks, poses, transitions, observations] = loadG2o('dataset/slam2D_bearing_only_ground_truth.g2o');
poses = poses(2:end);
landmarks = landmarks(2:end);
transitions = transitions(2:end);
observations = observations(2:end);
# define some global variables
global num_poses = length(poses);
global num_landmarks = length(landmarks);
global pose_dim = 3;
global landmark_dim = 2;
global sys_size = num_poses*pose_dim + num_landmarks*landmark_dim;
global num_transitions = length(transitions);
global num_observations = length(observations);
global num_measurements = 0;
for (i = 1:num_observations)
num_measurements = num_measurements + length(observations(i).observation);
endfor
# state initialization
global XR_truth = zeros(3,3,num_poses);
global XL_truth = zeros(2,num_landmarks);
for (i = 1:num_poses)
XR_truth(:,:,i) = v2t([poses(i).x poses(i).y poses(i).theta]);
pose_id_2_index(:,i) = poses(i).id;
endfor
# landmark_id_2_index maps the landmark_id to its own index
global landmark_id_2_index = zeros(1,num_landmarks);
for (i = 1:num_landmarks)
XL_truth(:,i) = [landmarks(i).x_pose landmarks(i).y_pose];
landmark_id_2_index(:,i) = landmarks(i).id;
endfor
fprintf('Done.\n');
%==============================================================================
%:::::::: LOAD INITIAL GUESS ::::::::::::::::::::::::::::::::::::::::::::::::::
%==============================================================================
fprintf('\nLoading g2o initial guess data... ');
# load initial guess dataset
[landmarks, poses, transitions, observations] = loadG2o('dataset/slam2D_bearing_only_initial_guess.g2o');
poses = poses(2:end);
transitions = transitions(2:end);
observations = observations(2:end);
% state initialization
global XR_guess = zeros(3,3,num_poses);
global XL_guess = zeros(2,num_landmarks);
for (i = 1:num_poses)
XR_guess(:,:,i) = v2t([poses(i).x poses(i).y poses(i).theta]); # store initial guess
pose_id_2_index(:,i) = poses(i).id;
endfor
fprintf('Done.\n');
%==============================================================================
%:::::::: PARSE POSES :::::::::::::::::::::::::::::::::::::::::::::::::::::::::
%==============================================================================
fprintf('\nParsing data... \n');
fprintf('|--> parse pose edges\n');
global ZR = zeros(3,3,num_transitions);
global ass_ZR = zeros(2,num_transitions);
for (i = 1:num_transitions)
ZR(:,:,i) = v2t(transitions(i).v);
pose_i_id = transitions(i).id_from;
pose_j_id = transitions(i).id_to;
pose_i_index = getIndex(pose_id_2_index,pose_i_id);
pose_j_index = getIndex(pose_id_2_index,pose_j_id);
ass_ZR(:,i) = [pose_i_index pose_j_index]';
endfor
%==============================================================================
%:::::::: PARSE LANDMARKS :::::::::::::::::::::::::::::::::::::::::::::::::::::
%==============================================================================
fprintf('|--> parse landmark edges\n');
global ZL = zeros(1,num_measurements);
global ass_ZL = zeros(2,num_measurements);
m = 1;
for (i = 1:num_observations)
pose_id = observations(i).pose_id;
pose_index = getIndex(pose_id_2_index,pose_id);
num_observed = length(observations(i).observation);
for (j = 1:num_observed)
landmark_id = observations(i).observation(j).id;
landmark_index = getIndex(landmark_id_2_index,landmark_id);
ZL(m) = observations(i).observation(j).bearing;
ass_ZL(:,m) = [pose_index;landmark_index];
m = m + 1;
endfor
endfor
fprintf('Done.\n');
%==============================================================================
%:::::::: LINEAR TRIANGULATION ::::::::::::::::::::::::::::::::::::::::::::::::
%==============================================================================
fprintf('\nLinear triangulation... ');
fprintf('\n|--> compute observations for each landmark\n');
[out,indices] = sort(ass_ZL(2,:));
landmarks_ids_list = unique(out);
observations_per_landmarks = zeros(size(landmarks_ids_list));
% for each landmark get the pair of meaurements with the best parallax
fprintf('|--> compute best measuraments by parallax\n');
fprintf("|--> triangulate\n")
for (landmark_id = landmarks_ids_list)
bearing.pose_id = ass_ZL(1,:)(find(ass_ZL(2,:)==landmark_id));
bearing.val = ZL(find(ass_ZL(2,:)==landmark_id));
for i = 1:length(bearing.val)
bearing.pose(:,i) = t2v(XR_guess(:,:,bearing.pose_id(i)));
bearing.val(i) = bearing.val(i) + bearing.pose(3,i);
bearing.val(i) = atan2(sin(bearing.val(i)),cos(bearing.val(i)));
endfor
observations_per_landmarks(landmark_id) = sum(out==landmark_id);
box_minus_matrix = zeros(observations_per_landmarks(landmark_id),observations_per_landmarks(landmark_id));
for (i = 1:observations_per_landmarks(landmark_id)-1)
for (j = (i+1):observations_per_landmarks(landmark_id))
box_minus_matrix(i,j) = boxMinus(bearing.val(i),bearing.val(j));
if (box_minus_matrix(i,j) > pi/2)
box_minus_matrix(i,j) = pi - box_minus_matrix(i,j);
endif
endfor
endfor
[pose0_id, pose1_id] = find(box_minus_matrix==max(max(box_minus_matrix)));
if(observations_per_landmarks(landmark_id)==1)
XL_guess(:,landmark_id) = bearing.pose(1:2,1)+[cos(bearing.pose(3,1)); sin(bearing.pose(3,1))];
else
p00 = bearing.pose(1:2,pose0_id);
phi0 = bearing.val(pose0_id);
p01 = p00 + [cos(phi0); sin(phi0)];
p10 = bearing.pose(1:2,pose1_id);
phi1 = bearing.val(pose1_id);
p11 = p10 + [cos(phi1); sin(phi1)];
XL_guess(:,landmark_id) = getLinesIntersection(p00,p01, p10,p11);
endif
clear bearing
endfor
fprintf('Done.\n');
%==============================================================================
%:::::::: RUN GAUSS-NEWTON ::::::::::::::::::::::::::::::::::::::::::::::::::::
%==============================================================================
% number of iterations
global num_it = 15;
% ENABLE/DISABLE stop algorithm whenever chi evolution stalls
global cut_exec = false;
% damping coefficient
global damp_coeff = 1e-6;
fprintf("\nRun Gauss-Newton algorithm... ");
[XR, XL, chi_r, chi_l, H] = ...
GaussNewtonAlgorithm(XR_guess,XL_guess,ZL,ZR,ass_ZR,ass_ZL,num_it,cut_exec,damp_coeff);
fprintf("\nDone.\n");
%==============================================================================
%:::::::: Plot and save :::::::::::::::::::::::::::::::::::::::::::::::::::::::
%=============================================================================
mkdir figures;
slam_fig = plotSLAM(XR_truth, XR_guess, XR, XL_truth, XL_guess, XL);
saveas(slam_fig, "figures/slam", "png");
chi_fig = plotChi(chi_r, chi_l);
saveas(chi_fig, "figures/chi", "png");
H_fig = plotH(H);
saveas(H_fig, "figures/H", "png");
fprintf("SLAM results saved in /figures.\n");
%==============================================================================