FixedLagSmootherExample.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 Demonstration of the fixed-lag smoothers using a planar robot example
10 and multiple odometry-like sensors
11 Author: Frank Dellaert (C++), Jeremy Aguilon (Python)
12 """
13 
14 import numpy as np
15 
16 import gtsam
17 import gtsam_unstable
18 
20  """
21  Runs a batch fixed smoother on an agent with two odometry
22  sensors that is simply moving to the
23  """
24 
25  # Define a batch fixed lag smoother, which uses
26  # Levenberg-Marquardt to perform the nonlinear optimization
27  lag = 2.0
28  smoother_batch = gtsam_unstable.BatchFixedLagSmoother(lag)
29 
30  # Create containers to store the factors and linearization points
31  # that will be sent to the smoothers
32  new_factors = gtsam.NonlinearFactorGraph()
33  new_values = gtsam.Values()
34  new_timestamps = gtsam_unstable.FixedLagSmootherKeyTimestampMap()
35 
36  # Create a prior on the first pose, placing it at the origin
37  prior_mean = gtsam.Pose2(0, 0, 0)
38  prior_noise = gtsam.noiseModel.Diagonal.Sigmas(np.array([0.3, 0.3, 0.1]))
39  X1 = 0
40  new_factors.push_back(gtsam.PriorFactorPose2(X1, prior_mean, prior_noise))
41  new_values.insert(X1, prior_mean)
42  new_timestamps.insert((X1, 0.0))
43 
44  delta_time = 0.25
45  time = 0.25
46 
47  while time <= 3.0:
48  previous_key = int(1000 * (time - delta_time))
49  current_key = int(1000 * time)
50 
51  # assign current key to the current timestamp
52  new_timestamps.insert((current_key, time))
53 
54  # Add a guess for this pose to the new values
55  # Assume that the robot moves at 2 m/s. Position is time[s] * 2[m/s]
56  current_pose = gtsam.Pose2(time * 2, 0, 0)
57  new_values.insert(current_key, current_pose)
58 
59  # Add odometry factors from two different sources with different error
60  # stats
61  odometry_measurement_1 = gtsam.Pose2(0.61, -0.08, 0.02)
62  odometry_noise_1 = gtsam.noiseModel.Diagonal.Sigmas(
63  np.array([0.1, 0.1, 0.05]))
64  new_factors.push_back(gtsam.BetweenFactorPose2(
65  previous_key, current_key, odometry_measurement_1, odometry_noise_1
66  ))
67 
68  odometry_measurement_2 = gtsam.Pose2(0.47, 0.03, 0.01)
69  odometry_noise_2 = gtsam.noiseModel.Diagonal.Sigmas(
70  np.array([0.05, 0.05, 0.05]))
71  new_factors.push_back(gtsam.BetweenFactorPose2(
72  previous_key, current_key, odometry_measurement_2, odometry_noise_2
73  ))
74 
75  # Update the smoothers with the new factors. In this case,
76  # one iteration must pass for Levenberg-Marquardt to accurately
77  # estimate
78  if time >= 0.50:
79  smoother_batch.update(new_factors, new_values, new_timestamps)
80  print("Timestamp = " + str(time) + ", Key = " + str(current_key))
81  print(smoother_batch.calculateEstimatePose2(current_key))
82 
83  new_timestamps.clear()
84  new_values.clear()
85  new_factors.resize(0)
86 
87  time += delta_time
88 
89 
90 if __name__ == '__main__':
92  print("Example complete")
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
return int(ret)+1
Definition: pytypes.h:928
static shared_ptr Sigmas(const Vector &sigmas, bool smart=true)
Definition: NoiseModel.cpp:270


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:42:04