-
Notifications
You must be signed in to change notification settings - Fork 19
/
pose_graph.py
39 lines (32 loc) · 1.17 KB
/
pose_graph.py
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
import g2o
import numpy as np
class PoseGraphOptimization(g2o.SparseOptimizer):
def __init__(self):
super().__init__()
solver = g2o.BlockSolverSE2(g2o.LinearSolverCholmodSE2())
solver = g2o.OptimizationAlgorithmLevenberg(solver)
super().set_algorithm(solver)
def optimize(self, max_iterations=20):
super().initialize_optimization()
super().optimize(max_iterations)
def add_vertex(self, id, pose, fixed=False):
v_se2 = g2o.VertexSE2()
v_se2.set_id(id)
v_se2.set_estimate(pose)
v_se2.set_fixed(fixed)
super().add_vertex(v_se2)
def add_edge(self, vertices, measurement,
information=np.identity(3),
robust_kernel=None):
edge = g2o.EdgeSE2()
for i, v in enumerate(vertices):
if isinstance(v, int):
v = self.vertex(v)
edge.set_vertex(i, v)
edge.set_measurement(measurement)
edge.set_information(information)
if robust_kernel is not None:
edge.set_robust_kernel(robust_kernel)
super().add_edge(edge)
def get_pose(self, id):
return self.vertex(id).estimate()