VisualISAM2Example.py
Go to the documentation of this file.
1 """
2 GTSAM Copyright 2010-2018, Georgia Tech Research Corporation,
3 Atlanta, Georgia 30332-0415
4 All Rights Reserved
5 Authors: Frank Dellaert, et al. (see THANKS for the full author list)
6 
7 See LICENSE for the license information
8 
9 An example of running visual SLAM using iSAM2.
10 Author: Duy-Nguyen Ta (C++), Frank Dellaert (Python)
11 """
12 # pylint: disable=invalid-name, E1101
13 
14 from __future__ import print_function
15 
16 import gtsam
17 import gtsam.utils.plot as gtsam_plot
18 import matplotlib.pyplot as plt
19 import numpy as np
20 from gtsam.symbol_shorthand import L, X
21 from gtsam.examples import SFMdata
22 from mpl_toolkits.mplot3d import Axes3D # pylint: disable=W0611
23 
24 
25 def visual_ISAM2_plot(result):
26  """
27  VisualISAMPlot plots current state of ISAM2 object
28  Author: Ellon Paiva
29  Based on MATLAB version by: Duy Nguyen Ta and Frank Dellaert
30  """
31 
32  # Declare an id for the figure
33  fignum = 0
34 
35  fig = plt.figure(fignum)
36  axes = fig.gca(projection='3d')
37  plt.cla()
38 
39  # Plot points
40  # Can't use data because current frame might not see all points
41  # marginals = Marginals(isam.getFactorsUnsafe(), isam.calculateEstimate())
42  # gtsam.plot_3d_points(result, [], marginals)
43  gtsam_plot.plot_3d_points(fignum, result, 'rx')
44 
45  # Plot cameras
46  i = 0
47  while result.exists(X(i)):
48  pose_i = result.atPose3(X(i))
49  gtsam_plot.plot_pose3(fignum, pose_i, 10)
50  i += 1
51 
52  # draw
53  axes.set_xlim3d(-40, 40)
54  axes.set_ylim3d(-40, 40)
55  axes.set_zlim3d(-40, 40)
56  plt.pause(1)
57 
58 
60  plt.ion()
61 
62  # Define the camera calibration parameters
63  K = gtsam.Cal3_S2(50.0, 50.0, 0.0, 50.0, 50.0)
64 
65  # Define the camera observation noise model
66  measurement_noise = gtsam.noiseModel.Isotropic.Sigma(
67  2, 1.0) # one pixel in u and v
68 
69  # Create the set of ground-truth landmarks
70  points = SFMdata.createPoints()
71 
72  # Create the set of ground-truth poses
73  poses = SFMdata.createPoses(K)
74 
75  # Create an iSAM2 object. Unlike iSAM1, which performs periodic batch steps
76  # to maintain proper linearization and efficient variable ordering, iSAM2
77  # performs partial relinearization/reordering at each step. A parameter
78  # structure is available that allows the user to set various properties, such
79  # as the relinearization threshold and type of linear solver. For this
80  # example, we we set the relinearization threshold small so the iSAM2 result
81  # will approach the batch result.
82  parameters = gtsam.ISAM2Params()
83  parameters.setRelinearizeThreshold(0.01)
84  parameters.setRelinearizeSkip(1)
85  isam = gtsam.ISAM2(parameters)
86 
87  # Create a Factor Graph and Values to hold the new data
89  initial_estimate = gtsam.Values()
90 
91  # Loop over the different poses, adding the observations to iSAM incrementally
92  for i, pose in enumerate(poses):
93 
94  # Add factors for each landmark observation
95  for j, point in enumerate(points):
96  camera = gtsam.PinholeCameraCal3_S2(pose, K)
97  measurement = camera.project(point)
98  graph.push_back(gtsam.GenericProjectionFactorCal3_S2(
99  measurement, measurement_noise, X(i), L(j), K))
100 
101  # Add an initial guess for the current pose
102  # Intentionally initialize the variables off from the ground truth
103  initial_estimate.insert(X(i), pose.compose(gtsam.Pose3(
104  gtsam.Rot3.Rodrigues(-0.1, 0.2, 0.25), gtsam.Point3(0.05, -0.10, 0.20))))
105 
106  # If this is the first iteration, add a prior on the first pose to set the
107  # coordinate frame and a prior on the first landmark to set the scale.
108  # Also, as iSAM solves incrementally, we must wait until each is observed
109  # at least twice before adding it to iSAM.
110  if i == 0:
111  # Add a prior on pose x0
112  pose_noise = gtsam.noiseModel.Diagonal.Sigmas(np.array(
113  [0.1, 0.1, 0.1, 0.3, 0.3, 0.3])) # 30cm std on x,y,z 0.1 rad on roll,pitch,yaw
114  graph.push_back(gtsam.PriorFactorPose3(X(0), poses[0], pose_noise))
115 
116  # Add a prior on landmark l0
117  point_noise = gtsam.noiseModel.Isotropic.Sigma(3, 0.1)
118  graph.push_back(gtsam.PriorFactorPoint3(
119  L(0), points[0], point_noise)) # add directly to graph
120 
121  # Add initial guesses to all observed landmarks
122  # Intentionally initialize the variables off from the ground truth
123  for j, point in enumerate(points):
124  initial_estimate.insert(L(j), gtsam.Point3(
125  point[0]-0.25, point[1]+0.20, point[2]+0.15))
126  else:
127  # Update iSAM with the new factors
128  isam.update(graph, initial_estimate)
129  # Each call to iSAM2 update(*) performs one iteration of the iterative nonlinear solver.
130  # If accuracy is desired at the expense of time, update(*) can be called additional
131  # times to perform multiple optimizer iterations every step.
132  isam.update()
133  current_estimate = isam.calculateEstimate()
134  print("****************************************************")
135  print("Frame", i, ":")
136  for j in range(i + 1):
137  print(X(j), ":", current_estimate.atPose3(X(j)))
138 
139  for j in range(len(points)):
140  print(L(j), ":", current_estimate.atPoint3(L(j)))
141 
142  visual_ISAM2_plot(current_estimate)
143 
144  # Clear the factor graph and values for the next iteration
145  graph.resize(0)
146  initial_estimate.clear()
147 
148  plt.ioff()
149  plt.show()
150 
151 
152 if __name__ == '__main__':
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
MatrixXd L
Definition: LLT_example.cpp:6
static Rot3 Rodrigues(const Vector3 &w)
Definition: Rot3.h:243
static shared_ptr Sigmas(const Vector &sigmas, bool smart=true)
Definition: NoiseModel.cpp:270
Vector3 Point3
Definition: Point3.h:35
#define X
Definition: icosphere.cpp:20
size_t len(handle h)
Definition: pytypes.h:1514
static shared_ptr Sigma(size_t dim, double sigma, bool smart=true)
Definition: NoiseModel.cpp:567


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:51:25