Pose3SLAMExampleExpressions_BearingRangeWithTransform.cpp
Go to the documentation of this file.
1 
10 #include <gtsam/slam/expressions.h>
13 #include <gtsam/nonlinear/Values.h>
14 #include <examples/SFMdata.h>
15 
16 using namespace gtsam;
17 
19 
20 /* ************************************************************************* */
21 int main(int argc, char* argv[]) {
22 
23  // Move around so the whole state (including the sensor tf) is observable
24  Pose3 init_pose = Pose3();
25  Pose3 delta_pose1 = Pose3(Rot3().Yaw(2*M_PI/8).Pitch(M_PI/8), Point3(1, 0, 0));
26  Pose3 delta_pose2 = Pose3(Rot3().Pitch(-M_PI/8), Point3(1, 0, 0));
27  Pose3 delta_pose3 = Pose3(Rot3().Yaw(-2*M_PI/8), Point3(1, 0, 0));
28 
29  int steps = 4;
30  auto poses = createPoses(init_pose, delta_pose1, steps);
31  auto poses2 = createPoses(init_pose, delta_pose2, steps);
32  auto poses3 = createPoses(init_pose, delta_pose3, steps);
33 
34  // Concatenate poses to create trajectory
35  poses.insert( poses.end(), poses2.begin(), poses2.end() );
36  poses.insert( poses.end(), poses3.begin(), poses3.end() ); // std::vector of Pose3
37  auto points = createPoints(); // std::vector of Point3
38 
39  // (ground-truth) sensor pose in body frame, further an unknown variable
40  Pose3 body_T_sensor_gt(Rot3::RzRyRx(-M_PI_2, 0.0, -M_PI_2), Point3(0.25, -0.10, 1.0));
41 
42  // The graph
44 
45  // Specify uncertainty on first pose prior and also for between factor (simplicity reasons)
46  auto poseNoise = noiseModel::Diagonal::Sigmas((Vector(6)<<0.3,0.3,0.3,0.1,0.1,0.1).finished());
47 
48  // Uncertainty bearing range measurement;
49  auto bearingRangeNoise = noiseModel::Diagonal::Sigmas((Vector(3)<<0.01,0.03,0.05).finished());
50 
51  // Expressions for body-frame at key 0 and sensor-tf
52  Pose3_ x_('x', 0);
53  Pose3_ body_T_sensor_('T', 0);
54 
55  // Add a prior on the body-pose
56  graph.addExpressionFactor(x_, poses[0], poseNoise);
57 
58  // Simulated measurements from pose
59  for (size_t i = 0; i < poses.size(); ++i) {
60  auto world_T_sensor = poses[i].compose(body_T_sensor_gt);
61  for (size_t j = 0; j < points.size(); ++j) {
62 
63  // This expression is the key feature of this example: it creates a differentiable expression of the measurement after being displaced by sensor transform.
64  auto prediction_ = Expression<BearingRange3D>( BearingRange3D::Measure, Pose3_('x',i)*body_T_sensor_, Point3_('l',j));
65 
66  // Create a *perfect* measurement
67  auto measurement = BearingRange3D(world_T_sensor.bearing(points[j]), world_T_sensor.range(points[j]));
68 
69  // Add factor
70  graph.addExpressionFactor(prediction_, measurement, bearingRangeNoise);
71  }
72 
73  // and add a between factor to the graph
74  if (i > 0)
75  {
76  // And also we have a *perfect* measurement for the between factor.
77  graph.addExpressionFactor(between(Pose3_('x', i-1),Pose3_('x', i)), poses[i-1].between(poses[i]), poseNoise);
78  }
79  }
80 
81  // Create perturbed initial
83  Pose3 delta(Rot3::Rodrigues(-0.1, 0.2, 0.25), Point3(0.05, -0.10, 0.20));
84  for (size_t i = 0; i < poses.size(); ++i)
85  initial.insert(Symbol('x', i), poses[i].compose(delta));
86  for (size_t j = 0; j < points.size(); ++j)
87  initial.insert<Point3>(Symbol('l', j), points[j] + Point3(-0.25, 0.20, 0.15));
88 
89  // Initialize body_T_sensor wrongly (because we do not know!)
90  initial.insert<Pose3>(Symbol('T',0), Pose3());
91 
92  std::cout << "initial error: " << graph.error(initial) << std::endl;
94  std::cout << "final error: " << graph.error(result) << std::endl;
95 
96  initial.at<Pose3>(Symbol('T',0)).print("\nInitial estimate body_T_sensor\n"); /* initial sensor_P_body estimate */
97  result.at<Pose3>(Symbol('T',0)).print("\nFinal estimate body_T_sensor\n"); /* optimized sensor_P_body estimate */
98  body_T_sensor_gt.print("\nGround truth body_T_sensor\n"); /* sensor_P_body ground truth */
99 
100  return 0;
101 }
102 /* ************************************************************************* */
gtsam::NonlinearOptimizer::optimize
virtual const Values & optimize()
Definition: NonlinearOptimizer.h:98
gtsam::between
Expression< T > between(const Expression< T > &t1, const Expression< T > &t2)
Definition: nonlinear/expressions.h:17
gtsam::createPoints
std::vector< Point3 > createPoints()
Create a set of ground-truth landmarks.
Definition: SFMdata.h:43
LevenbergMarquardtOptimizer.h
A nonlinear optimizer that uses the Levenberg-Marquardt trust-region scheme.
initial
Values initial
Definition: OdometryOptimize.cpp:2
gtsam::noiseModel::Diagonal::Sigmas
static shared_ptr Sigmas(const Vector &sigmas, bool smart=true)
Definition: NoiseModel.cpp:291
gtsam::ExpressionFactorGraph
Definition: ExpressionFactorGraph.h:29
gtsam::Vector
Eigen::VectorXd Vector
Definition: Vector.h:38
result
Values result
Definition: OdometryOptimize.cpp:8
gtsam::compose
Expression< T > compose(const Expression< T > &t1, const Expression< T > &t2)
Definition: nonlinear/expressions.h:23
gtsam::Expression
Definition: Expression.h:47
gtsam::Pose3::print
void print(const std::string &s="") const
print with optional string
Definition: Pose3.cpp:152
gtsam::NonlinearFactorGraph::error
double error(const Values &values) const
Definition: NonlinearFactorGraph.cpp:170
gtsam::Values::at
const ValueType at(Key j) const
Definition: Values-inl.h:261
gtsam::print
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
gtsam.examples.PlanarManipulatorExample.delta
def delta(g0, g1)
Definition: PlanarManipulatorExample.py:45
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
gtsam::Rot3
Rot3 is a 3D rotation represented as a rotation matrix if the preprocessor symbol GTSAM_USE_QUATERNIO...
Definition: Rot3.h:58
gtsam::Pose3
Definition: Pose3.h:37
gtsam::Rot3::Rodrigues
static Rot3 Rodrigues(const Vector3 &w)
Definition: Rot3.h:240
main
int main(int argc, char *argv[])
Definition: Pose3SLAMExampleExpressions_BearingRangeWithTransform.cpp:21
ExpressionFactorGraph.h
Factor graph that supports adding ExpressionFactors directly.
gtsam::LevenbergMarquardtOptimizer
Definition: LevenbergMarquardtOptimizer.h:35
Symbol.h
BearingRange3D
BearingRange< Pose3, Point3 > BearingRange3D
Definition: Pose3SLAMExampleExpressions_BearingRangeWithTransform.cpp:18
M_PI_2
#define M_PI_2
Definition: mconf.h:118
gtsam::createPoses
std::vector< Pose3 > createPoses(const Pose3 &init=Pose3(Rot3::Ypr(M_PI_2, 0, -M_PI_2), {30, 0, 0}), const Pose3 &delta=Pose3(Rot3::Ypr(0, -M_PI_4, 0), {sin(M_PI_4) *30, 0, 30 *(1 - sin(M_PI_4))}), int steps=8)
Definition: SFMdata.h:62
gtsam::Point3_
Expression< Point3 > Point3_
Definition: slam/expressions.h:36
gtsam
traits
Definition: SFMdata.h:40
gtsam::Values
Definition: Values.h:65
expressions.h
Common expressions for solving geometry/slam/sfm problems.
gtsam::BearingRange
Definition: BearingRange.h:52
gtsam::Pose3_
Expression< Pose3 > Pose3_
Definition: slam/expressions.h:39
gtsam::Point3
Vector3 Point3
Definition: Point3.h:38
initial
Definition: testScenarioRunner.cpp:148
BearingRange.h
Bearing-Range product.
M_PI
#define M_PI
Definition: mconf.h:117
graph
NonlinearFactorGraph graph
Definition: doc/Code/OdometryExample.cpp:2
SFMdata.h
Simple example for the structure-from-motion problems.
gtsam::Rot3::RzRyRx
static Rot3 RzRyRx(double x, double y, double z, OptionalJacobian< 3, 1 > Hx={}, OptionalJacobian< 3, 1 > Hy={}, OptionalJacobian< 3, 1 > Hz={})
Rotations around Z, Y, then X axes as in http://en.wikipedia.org/wiki/Rotation_matrix,...
Definition: Rot3M.cpp:84
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
measurement
static Point2 measurement(323.0, 240.0)
Values.h
A non-templated config holding any types of Manifold-group elements.
gtsam::NonlinearFactorGraph::addExpressionFactor
void addExpressionFactor(const SharedNoiseModel &R, const T &z, const Expression< T > &h)
Definition: NonlinearFactorGraph.h:187
gtsam::Symbol
Definition: inference/Symbol.h:37


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:03:36