TimeOfArrivalExample.cpp
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010-2020, 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 
26 
27 #include <vector>
28 
29 using namespace std;
30 using namespace gtsam;
31 
32 // units
33 static const double ms = 1e-3;
34 static const double cm = 1e-2;
35 
36 // Instantiate functor with speed of sound value
37 static const TimeOfArrival kTimeOfArrival(330);
38 
39 /* ************************************************************************* */
40 // Create microphones
41 vector<Point3> defineMicrophones() {
42  const double height = 0.5;
43  vector<Point3> microphones;
44  microphones.push_back(Point3(0, 0, height));
45  microphones.push_back(Point3(403 * cm, 0, height));
46  microphones.push_back(Point3(403 * cm, 403 * cm, height));
47  microphones.push_back(Point3(0, 403 * cm, 2 * height));
48  return microphones;
49 }
50 
51 /* ************************************************************************* */
52 // Create ground truth trajectory
53 vector<Event> createTrajectory(size_t n) {
54  vector<Event> trajectory;
55  double timeOfEvent = 10;
56  // simulate emitting a sound every second while moving on straight line
57  for (size_t key = 0; key < n; key++) {
58  trajectory.push_back(
59  Event(timeOfEvent, 245 * cm + key * 1.0, 201.5 * cm, (212 - 45) * cm));
60  timeOfEvent += 1;
61  }
62  return trajectory;
63 }
64 
65 /* ************************************************************************* */
66 // Simulate time-of-arrival measurements for a single event
67 vector<double> simulateTOA(const vector<Point3>& microphones,
68  const Event& event) {
69  size_t K = microphones.size();
70  vector<double> simulatedTOA(K);
71  for (size_t i = 0; i < K; i++) {
72  simulatedTOA[i] = kTimeOfArrival(event, microphones[i]);
73  }
74  return simulatedTOA;
75 }
76 
77 /* ************************************************************************* */
78 // Simulate time-of-arrival measurements for an entire trajectory
79 vector<vector<double>> simulateTOA(const vector<Point3>& microphones,
80  const vector<Event>& trajectory) {
81  vector<vector<double>> simulatedTOA;
82  for (auto event : trajectory) {
83  simulatedTOA.push_back(simulateTOA(microphones, event));
84  }
85  return simulatedTOA;
86 }
87 
88 /* ************************************************************************* */
89 // create factor graph
90 NonlinearFactorGraph createGraph(const vector<Point3>& microphones,
91  const vector<vector<double>>& simulatedTOA) {
93 
94  // Create a noise model for the TOA error
95  auto model = noiseModel::Isotropic::Sigma(1, 0.5 * ms);
96 
97  size_t K = microphones.size();
98  size_t key = 0;
99  for (auto toa : simulatedTOA) {
100  for (size_t i = 0; i < K; i++) {
101  graph.emplace_shared<TOAFactor>(key, microphones[i], toa[i], model);
102  }
103  key += 1;
104  }
105  return graph;
106 }
107 
108 /* ************************************************************************* */
109 // create initial estimate for n events
111  Values initial;
112 
113  Event zero;
114  for (size_t key = 0; key < n; key++) {
115  initial.insert(key, zero);
116  }
117  return initial;
118 }
119 
120 /* ************************************************************************* */
121 int main(int argc, char* argv[]) {
122  // Create microphones
123  auto microphones = defineMicrophones();
124  size_t K = microphones.size();
125  for (size_t i = 0; i < K; i++) {
126  cout << "mic" << i << " = " << microphones[i] << endl;
127  }
128 
129  // Create a ground truth trajectory
130  const size_t n = 5;
131  auto groundTruth = createTrajectory(n);
132 
133  // Simulate time-of-arrival measurements
134  auto simulatedTOA = simulateTOA(microphones, groundTruth);
135  for (size_t key = 0; key < n; key++) {
136  for (size_t i = 0; i < K; i++) {
137  cout << "z_" << key << i << " = " << simulatedTOA[key][i] / ms << " ms"
138  << endl;
139  }
140  }
141 
142  // Create factor graph
143  auto graph = createGraph(microphones, simulatedTOA);
144 
145  // Create initial estimate
146  auto initialEstimate = createInitialEstimate(n);
147  initialEstimate.print("Initial Estimate:\n");
148 
149  // Optimize using Levenberg-Marquardt optimization.
151  params.setAbsoluteErrorTol(1e-10);
152  params.setVerbosityLM("SUMMARY");
153  LevenbergMarquardtOptimizer optimizer(graph, initialEstimate, params);
154  Values result = optimizer.optimize();
155  result.print("Final Result:\n");
156 }
157 /* ************************************************************************* */
const gtsam::Symbol key('X', 0)
NonlinearFactorGraph createGraph(const vector< Point3 > &microphones, const vector< vector< double >> &simulatedTOA)
vector< double > simulateTOA(const vector< Point3 > &microphones, const Event &event)
static const double cm
virtual const Values & optimize()
IsDerived< DERIVEDFACTOR > emplace_shared(Args &&... args)
Emplace a shared pointer to factor of given type.
Definition: FactorGraph.h:196
Factor Graph consisting of non-linear factors.
vector< Event > createTrajectory(size_t n)
noiseModel::Diagonal::shared_ptr model
EIGEN_DONT_INLINE Scalar zero()
Definition: svd_common.h:296
int n
"Time of Arrival" factor
static Cal3_S2 K(500, 500, 0.1, 640/2, 480/2)
Values initial
Definition: BFloat16.h:88
static const double ms
NonlinearFactorGraph graph
int main(int argc, char *argv[])
Values createInitialEstimate(size_t n)
static const SmartProjectionParams params
void print(const std::string &str="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
Definition: Values.cpp:66
Values result
A "Time of Arrival" factor - so little code seems hardly worth it :-)
Definition: TOAFactor.h:28
A nonlinear optimizer that uses the Levenberg-Marquardt trust-region scheme.
Array< double, 1, 3 > e(1./3., 0.5, 2.)
traits
Definition: chartTesting.h:28
Time of arrival to given sensor.
Definition: Event.h:87
Space-time event.
static const TimeOfArrival kTimeOfArrival(330)
void setVerbosityLM(const std::string &s)
vector< Point3 > defineMicrophones()
void insert(Key j, const Value &val)
Definition: Values.cpp:155
Vector3 Point3
Definition: Point3.h:38
static const double timeOfEvent
Definition: testEvent.cpp:36


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:40:01