SmartRangeExample_plaza2.cpp
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
19 // Both relative poses and recovered trajectory poses will be stored as Pose2 objects
20 #include <gtsam/geometry/Pose2.h>
21 
22 // Each variable in the system (poses and landmarks) must be identified with a unique key.
23 // We can either use simple integer keys (1, 2, 3, ...) or symbols (X1, X2, L1).
24 // Here we will use Symbols
25 #include <gtsam/inference/Symbol.h>
26 
27 // We want to use iSAM2 to solve the range-SLAM problem incrementally
28 #include <gtsam/nonlinear/ISAM2.h>
29 
30 // iSAM2 requires as input a set set of new factors to be added stored in a factor graph,
31 // and initial guesses for any new variables used in the added factors
33 #include <gtsam/nonlinear/Values.h>
34 
35 // We will use a non-liear solver to batch-inituialize from the first 150 frames
37 
38 // In GTSAM, measurement functions are represented as 'factors'. Several common factors
39 // have been provided with the library for solving robotics SLAM problems.
41 #include <gtsam/sam/RangeFactor.h>
42 
43 // To find data files, we can use `findExampleDataFile`, declared here:
44 #include <gtsam/slam/dataset.h>
45 
46 // Standard headers, added last, so we know headers above work on their own
47 #include <fstream>
48 #include <iostream>
49 
50 using namespace std;
51 using namespace gtsam;
52 namespace NM = gtsam::noiseModel;
53 
54 // data available at http://www.frc.ri.cmu.edu/projects/emergencyresponse/RangeData/
55 // Datafile format (from http://www.frc.ri.cmu.edu/projects/emergencyresponse/RangeData/log.html)
56 
57 // load the odometry
58 // DR: Odometry Input (delta distance traveled and delta heading change)
59 // Time (sec) Delta Dist. Trav. (m) Delta Heading (rad)
60 typedef pair<double, Pose2> TimedOdometry;
61 list<TimedOdometry> readOdometry() {
62  list<TimedOdometry> odometryList;
63  string drFile = findExampleDataFile("Plaza2_DR.txt");
64  ifstream is(drFile);
65  if (!is) throw runtime_error("Plaza2_DR.txt file not found");
66 
67  while (is) {
68  double t, distance_traveled, delta_heading;
69  is >> t >> distance_traveled >> delta_heading;
70  odometryList.push_back(
71  TimedOdometry(t, Pose2(distance_traveled, 0, delta_heading)));
72  }
73  is.clear(); /* clears the end-of-file and error flags */
74  return odometryList;
75 }
76 
77 // load the ranges from TD
78 // Time (sec) Sender / Antenna ID Receiver Node ID Range (m)
79 typedef std::tuple<double, size_t, double> RangeTriple;
80 vector<RangeTriple> readTriples() {
81  vector<RangeTriple> triples;
82  string tdFile = findExampleDataFile("Plaza2_TD.txt");
83  ifstream is(tdFile);
84  if (!is) throw runtime_error("Plaza2_TD.txt file not found");
85 
86  while (is) {
87  double t, sender, receiver, range;
88  is >> t >> sender >> receiver >> range;
89  triples.push_back(RangeTriple(t, receiver, range));
90  }
91  is.clear(); /* clears the end-of-file and error flags */
92  return triples;
93 }
94 
95 // main
96 int main(int argc, char** argv) {
97 
98  // load Plaza1 data
99  list<TimedOdometry> odometry = readOdometry();
100 // size_t M = odometry.size();
101 
102  vector<RangeTriple> triples = readTriples();
103  size_t K = triples.size();
104 
105  // parameters
106  size_t incK = 50; // minimum number of range measurements to process after
107  bool robust = false;
108 
109  // Set Noise parameters
110  Vector priorSigmas = Vector3(0.01, 0.01, 0.01);
111  Vector odoSigmas = Vector3(0.05, 0.01, 0.2);
112  double sigmaR = 100; // range standard deviation
113  const NM::Base::shared_ptr // all same type
114  priorNoise = NM::Diagonal::Sigmas(priorSigmas), //prior
115  odoNoise = NM::Diagonal::Sigmas(odoSigmas), // odometry
116  gaussian = NM::Isotropic::Sigma(1, sigmaR), // non-robust
117  tukey = NM::Robust::Create(NM::mEstimator::Tukey::Create(15), gaussian), //robust
118  rangeNoise = robust ? tukey : gaussian;
119 
120  // Initialize iSAM
121  ISAM2 isam;
122 
123  // Add prior on first pose
124  Pose2 pose0 = Pose2(-34.2086489999201, 45.3007639991120, -2.02108900000000);
125  NonlinearFactorGraph newFactors;
126  newFactors.addPrior(0, pose0, priorNoise);
127 
128  // initialize points (Gaussian)
129  Values initial;
130  initial.insert(symbol('L', 1), Point2(-68.9265, 18.3778));
131  initial.insert(symbol('L', 6), Point2(-37.5805, 69.2278));
132  initial.insert(symbol('L', 0), Point2(-33.6205, 26.9678));
133  initial.insert(symbol('L', 5), Point2(1.7095, -5.8122));
134  Values landmarkEstimates = initial; // copy landmarks
135  initial.insert(0, pose0);
136 
137  // set some loop variables
138  size_t i = 1; // step counter
139  size_t k = 0; // range measurement counter
140  Pose2 lastPose = pose0;
141  size_t countK = 0;
142 
143  // Loop over odometry
144  gttic_(iSAM);
145  for(const TimedOdometry& timedOdometry: odometry) {
146  //--------------------------------- odometry loop -----------------------------------------
147  double t;
148  Pose2 odometry;
149  std::tie(t, odometry) = timedOdometry;
150 
151  // add odometry factor
152  newFactors.push_back(
154  NM::Diagonal::Sigmas(odoSigmas)));
155 
156  // predict pose and add as initial estimate
157  Pose2 predictedPose = lastPose.compose(odometry);
158  lastPose = predictedPose;
159  initial.insert(i, predictedPose);
160  landmarkEstimates.insert(i, predictedPose);
161 
162  // Check if there are range factors to be added
163  while (k < K && t >= std::get<0>(triples[k])) {
164  size_t j = std::get<1>(triples[k]);
165  double range = std::get<2>(triples[k]);
166  RangeFactor<Pose2, Point2> factor(i, symbol('L', j), range, rangeNoise);
167  // Throw out obvious outliers based on current landmark estimates
168  Vector error = factor.unwhitenedError(landmarkEstimates);
169  if (k <= 200 || std::abs(error[0]) < 5)
170  newFactors.push_back(factor);
171  k = k + 1;
172  countK = countK + 1;
173  }
174 
175  // Check whether to update iSAM 2
176  if (countK > incK) {
177  gttic_(update);
178  isam.update(newFactors, initial);
179  gttoc_(update);
180  gttic_(calculateEstimate);
181  Values result = isam.calculateEstimate();
182  gttoc_(calculateEstimate);
183  lastPose = result.at<Pose2>(i);
184  // update landmark estimates
185  landmarkEstimates = Values();
186  landmarkEstimates.insert(symbol('L', 1), result.at(symbol('L', 1)));
187  landmarkEstimates.insert(symbol('L', 6), result.at(symbol('L', 6)));
188  landmarkEstimates.insert(symbol('L', 0), result.at(symbol('L', 0)));
189  landmarkEstimates.insert(symbol('L', 5), result.at(symbol('L', 5)));
190  newFactors = NonlinearFactorGraph();
191  initial = Values();
192  countK = 0;
193  }
194  i += 1;
195  //--------------------------------- odometry loop -----------------------------------------
196  } // end for
197  gttoc_(iSAM);
198 
199  // Print timings
200  tictoc_print_();
201 
202  // Write result to file
203  Values result = isam.calculateEstimate();
204  ofstream os2("rangeResultLM.txt");
205  for (const auto& [key, point] : result.extract<Point2>())
206  os2 << key << "\t" << point.x() << "\t" << point.y() << "\t1" << endl;
207  ofstream os("rangeResult.txt");
208  for (const auto& [key, pose] : result.extract<Pose2>())
209  os << key << "\t" << pose.x() << "\t" << pose.y() << "\t" << pose.theta() << endl;
210  exit(0);
211 }
212 
relicense.update
def update(text)
Definition: relicense.py:46
Pose2.h
2D Pose
gtsam::RangeFactor
Definition: sam/RangeFactor.h:35
gtsam::ISAM2
Definition: ISAM2.h:45
pose
static const Pose3 pose(Rot3(Vector3(1, -1, -1).asDiagonal()), Point3(0, 0, 0.5))
gtsam::noiseModel::Base::shared_ptr
std::shared_ptr< Base > shared_ptr
Definition: NoiseModel.h:60
gtsam::noiseModel::Robust::Create
static shared_ptr Create(const RobustModel::shared_ptr &robust, const NoiseModel::shared_ptr noise)
Definition: NoiseModel.cpp:704
LevenbergMarquardtOptimizer.h
A nonlinear optimizer that uses the Levenberg-Marquardt trust-region scheme.
initial
Values initial
Definition: OdometryOptimize.cpp:2
gtsam::LieGroup::compose
Class compose(const Class &g) const
Definition: Lie.h:48
RangeFactor.h
Serializable factor induced by a range measurement.
simple::pose0
static Pose3 pose0
Definition: testInitializePose3.cpp:56
RangeTriple
std::tuple< double, size_t, double > RangeTriple
Definition: SmartRangeExample_plaza2.cpp:79
gtsam::noiseModel::Diagonal::Sigmas
static shared_ptr Sigmas(const Vector &sigmas, bool smart=true)
Definition: NoiseModel.cpp:270
odoNoise
SharedDiagonal odoNoise
Definition: testGaussianISAM2.cpp:40
gtsam::Vector3
Eigen::Vector3d Vector3
Definition: Vector.h:43
os
ofstream os("timeSchurFactors.csv")
gtsam::Vector
Eigen::VectorXd Vector
Definition: Vector.h:38
result
Values result
Definition: OdometryOptimize.cpp:8
gtsam::NonlinearISAM::update
void update(const NonlinearFactorGraph &newFactors, const Values &initialValues)
Definition: NonlinearISAM.cpp:35
gtsam::range
Double_ range(const Point2_ &p, const Point2_ &q)
Definition: slam/expressions.h:30
odometry
Pose2 odometry(2.0, 0.0, 0.0)
gtsam_unstable.tests.test_ProjectionFactorRollingShutter.point
point
Definition: test_ProjectionFactorRollingShutter.py:25
gtsam::Values::at
const ValueType at(Key j) const
Definition: Values-inl.h:261
dataset.h
utility functions for loading datasets
gtsam::Values::extract
std::map< Key, ValueType > extract(const std::function< bool(Key)> &filterFcn=&_truePredicate< Key >) const
Definition: Values-inl.h:105
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
gttoc_
#define gttoc_(label)
Definition: timing.h:250
TimedOdometry
pair< double, Pose2 > TimedOdometry
Definition: SmartRangeExample_plaza2.cpp:60
BetweenFactor.h
gttic_
#define gttic_(label)
Definition: timing.h:245
isam
NonlinearISAM isam(relinearizeInterval)
gtsam::NonlinearFactorGraph
Definition: NonlinearFactorGraph.h:55
gtsam::NonlinearFactorGraph::addPrior
void addPrior(Key key, const T &prior, const SharedNoiseModel &model=nullptr)
Definition: NonlinearFactorGraph.h:199
gtsam::tictoc_print_
void tictoc_print_()
Definition: timing.h:268
gtsam::Pose3::y
double y() const
get y
Definition: Pose3.h:297
gtsam::symbol
Key symbol(unsigned char c, std::uint64_t j)
Definition: inference/Symbol.h:139
Symbol.h
gtsam::Point2
Vector2 Point2
Definition: Point2.h:32
priorNoise
auto priorNoise
Definition: doc/Code/OdometryExample.cpp:6
readTriples
vector< RangeTriple > readTriples()
Definition: SmartRangeExample_plaza2.cpp:80
key
const gtsam::Symbol key('X', 0)
ISAM2.h
Incremental update functionality (ISAM2) for BayesTree, with fluid relinearization.
TimedOdometry
std::pair< double, Pose2 > TimedOdometry
Definition: RangeISAMExample_plaza2.cpp:78
gtsam::ExpressionFactor< double >::unwhitenedError
Vector unwhitenedError(const Values &x, OptionalMatrixVecType H=nullptr) const override
Definition: ExpressionFactor.h:104
Values
std::vector< float > Values
Definition: sparse_setter.cpp:45
gtsam
traits
Definition: chartTesting.h:28
error
static double error
Definition: testRot3.cpp:37
K
#define K
Definition: igam.h:8
NonlinearFactorGraph.h
Factor Graph consisting of non-linear factors.
gtsam::FactorGraph::push_back
IsDerived< DERIVEDFACTOR > push_back(std::shared_ptr< DERIVEDFACTOR > factor)
Add a factor directly using a shared_ptr.
Definition: FactorGraph.h:147
gtsam::Values
Definition: Values.h:65
std
Definition: BFloat16.h:88
gtsam::noiseModel
All noise models live in the noiseModel namespace.
Definition: LossFunctions.cpp:28
main
int main(int argc, char **argv)
Definition: SmartRangeExample_plaza2.cpp:96
gtsam::Values::insert
void insert(Key j, const Value &val)
Definition: Values.cpp:155
gtsam::Pose3::x
double x() const
get x
Definition: Pose3.h:292
initial
Definition: testScenarioRunner.cpp:148
gtsam::noiseModel::Isotropic::Sigma
static shared_ptr Sigma(size_t dim, double sigma, bool smart=true)
Definition: NoiseModel.cpp:594
abs
#define abs(x)
Definition: datatypes.h:17
readOdometry
list< TimedOdometry > readOdometry()
Definition: SmartRangeExample_plaza2.cpp:61
gtsam::findExampleDataFile
GTSAM_EXPORT std::string findExampleDataFile(const std::string &name)
Definition: dataset.cpp:70
align_3::t
Point2 t(10, 10)
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Values.h
A non-templated config holding any types of Manifold-group elements.
gtsam::Pose2
Definition: Pose2.h:39
gtsam::BetweenFactor
Definition: BetweenFactor.h:40


gtsam
Author(s):
autogenerated on Tue Jun 25 2024 03:02:53