-
Notifications
You must be signed in to change notification settings - Fork 1
/
build_and_solve.m
138 lines (95 loc) · 3.87 KB
/
build_and_solve.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
%% setup the workspace
% clear and close
close all;
clear all;
% add our paths for helper functions
addpath('func');
% create the figure
fh1 = figure(1);
fontsize = 20;
set(gcf,'Color','w');
set(gcf,'PaperPositionMode','auto');
set(gcf,'defaultuicontrolfontsize',fontsize);
set(gcf,'defaultuicontrolfontname','Bitstream Charter');
set(gcf,'DefaultAxesFontSize',fontsize);
set(gcf,'DefaultAxesFontName','Bitstream Charter');
set(gcf,'DefaultTextFontSize',fontsize);
set(gcf,'DefaultTextFontname','Bitstream Charter');
set(gcf,'Position',[0 0 800 800])
%% start loading our data in
% load our data
disp('loading data...');
[nodes,edges] = load_2d_g2o('data/input_INTEL_g2o.g2o');
%[nodes,edges] = load_2d_g2o('data/input_M3500_g2o.g2o');
%[nodes,edges] = load_2d_g2o('data/input_MITb_g2o.g2o');
% plot the before
disp('plotting initial trajectory...');
plot_2d_graph(fh1,nodes,'b');
%% now lets make the optimization problem
disp('creating optimization problem...');
% first ensure our orientations are bounded between -pi and pi
for i=1:length(nodes)
nodes{i} = update_node(nodes{i},[0 0 0]);
end
% we will stop when our update is small
disp('solving optimization problem...');
dx_norm = 99999;
iteration = 0;
while dx_norm > 1e-2
% our matrices for Hx=-b
H = zeros(3*length(nodes),3*length(nodes));
b = zeros(3*length(nodes),1);
% loop through each edge
for i=1:length(edges)
% our edge
edge = edges{i};
% get the nodes (and thus our linearization points)
node1 = get_node(nodes, edge.id1);
node2 = get_node(nodes, edge.id2);
% orientation error
R_1toG = rot2(node1.state(3));
R_2toG = rot2(node2.state(3));
R_2to1 = rot2(edge.meas(3));
%err_theta = log2(R_2to1'*R_1toG'*R_2toG);
err_theta = wrap2pi(edge.meas(3) - wrap2pi(node2.state(3)-node1.state(3)));
% position error
p_1inG = node1.state(1:2)';
p_2inG = node2.state(1:2)';
p_2in1 = edge.meas(1:2)';
err_pos = p_2in1 - R_1toG'*(p_2inG - p_1inG);
% Jacobian of current relative in respect to NODE 1
dx1_dtheta1 = -sin(node1.state(3))*(node2.state(1)-node1.state(1)) + cos(node1.state(3))*(node2.state(2)-node1.state(2));
dy1_dtheta1 = -cos(node1.state(3))*(node2.state(1)-node1.state(1)) - sin(node1.state(3))*(node2.state(2)-node1.state(2));
Aij = [-cos(node1.state(3)) -sin(node1.state(3)) dx1_dtheta1;
sin(node1.state(3)) -cos(node1.state(3)) dy1_dtheta1;
0 0 -1];
% Jacobian of current relative in respect to NODE 2
Bij = [cos(node1.state(3)) sin(node1.state(3)) 0;
-sin(node1.state(3)) cos(node1.state(3)) 0;
0 0 1];
% update our information
id1 = edge.id1;
id2 = edge.id2;
H(3*id1+1:3*id1+3,3*id1+1:3*id1+3) = H(3*id1+1:3*id1+3,3*id1+1:3*id1+3) + Aij'*edge.info*Aij;
H(3*id1+1:3*id1+3,3*id2+1:3*id2+3) = H(3*id1+1:3*id1+3,3*id2+1:3*id2+3) + Aij'*edge.info*Bij;
H(3*id2+1:3*id2+3,3*id1+1:3*id1+3) = H(3*id2+1:3*id2+3,3*id1+1:3*id1+3) + Bij'*edge.info*Aij;
H(3*id2+1:3*id2+3,3*id2+1:3*id2+3) = H(3*id2+1:3*id2+3,3*id2+1:3*id2+3) + Bij'*edge.info*Bij;
% update our error terms
b(3*id1+1:3*id1+3,1) = b(3*id1+1:3*id1+3,1) + Aij'*edge.info*[err_pos; err_theta];
b(3*id2+1:3*id2+3,1) = b(3*id2+1:3*id2+3,1) + Bij'*edge.info*[err_pos; err_theta];
end
% fix the first node to be known
H(1:3,1:3) = H(1:3,1:3) + 1e6*eye(3);
% solve the linear system
x = H\b;
dx_norm = norm(x);
iteration = iteration + 1;
fprintf(' + iter %d with delta = %.4f\n',iteration,dx_norm);
% update our nodes
for i=1:length(nodes)
nodes{i} = update_node(nodes{i},x(3*(i-1)+1:3*(i-1)+3,1));
end
end
% plot after update
disp('plotting after update...');
plot_2d_graph(fh1,nodes,'r');