TimeOfArrivalExample.py
Go to the documentation of this file.
1 """
2 GTSAM Copyright 2010-2020, 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 Track a moving object "Time of Arrival" measurements at 4 microphones.
10 Author: Frank Dellaert
11 """
12 # pylint: disable=invalid-name, no-name-in-module
13 
14 from gtsam import (LevenbergMarquardtOptimizer, LevenbergMarquardtParams,
15  NonlinearFactorGraph, Point3, Values, noiseModel, Event,
16  TimeOfArrival)
17 from gtsam_unstable import TOAFactor
18 
19 # units
20 MS = 1e-3
21 CM = 1e-2
22 
23 # Instantiate functor with speed of sound value
24 TIME_OF_ARRIVAL = TimeOfArrival(330)
25 
26 
28  """Create microphones."""
29  height = 0.5
30  microphones = []
31  microphones.append(Point3(0, 0, height))
32  microphones.append(Point3(403 * CM, 0, height))
33  microphones.append(Point3(403 * CM, 403 * CM, height))
34  microphones.append(Point3(0, 403 * CM, 2 * height))
35  return microphones
36 
37 
39  """Create ground truth trajectory."""
40  trajectory = []
41  timeOfEvent = 10
42  # simulate emitting a sound every second while moving on straight line
43  for key in range(n):
44  trajectory.append(
45  Event(timeOfEvent, 245 * CM + key * 1.0, 201.5 * CM, (212 - 45) * CM))
46  timeOfEvent += 1
47 
48  return trajectory
49 
50 
51 def simulate_one_toa(microphones, event):
52  """Simulate time-of-arrival measurements for a single event."""
53  return [TIME_OF_ARRIVAL.measure(event, microphones[i])
54  for i in range(len(microphones))]
55 
56 
57 def simulate_toa(microphones, trajectory):
58  """Simulate time-of-arrival measurements for an entire trajectory."""
59  return [simulate_one_toa(microphones, event)
60  for event in trajectory]
61 
62 
63 def create_graph(microphones, simulatedTOA):
64  """Create factor graph."""
65  graph = NonlinearFactorGraph()
66 
67  # Create a noise model for the TOA error
68  model = noiseModel.Isotropic.Sigma(1, 0.5 * MS)
69 
70  K = len(microphones)
71  key = 0
72  for toa in simulatedTOA:
73  for i in range(K):
74  factor = TOAFactor(key, microphones[i], toa[i], model)
75  graph.push_back(factor)
76  key += 1
77 
78  return graph
79 
80 
82  """Create initial estimate for n events."""
83  initial = Values()
84  zero = Event()
85  for key in range(n):
86  TOAFactor.InsertEvent(key, zero, initial)
87  return initial
88 
89 
91  """Run example with 4 microphones and 5 events in a straight line."""
92  # Create microphones
93  microphones = define_microphones()
94  K = len(microphones)
95  for i in range(K):
96  print("mic {} = {}".format(i, microphones[i]))
97 
98  # Create a ground truth trajectory
99  n = 5
100  groundTruth = create_trajectory(n)
101  for event in groundTruth:
102  print(event)
103 
104  # Simulate time-of-arrival measurements
105  simulatedTOA = simulate_toa(microphones, groundTruth)
106  for key in range(n):
107  for i in range(K):
108  print("z_{}{} = {} ms".format(key, i, simulatedTOA[key][i] / MS))
109 
110  # create factor graph
111  graph = create_graph(microphones, simulatedTOA)
112  print(graph.at(0))
113 
114  # Create initial estimate
115  initial_estimate = create_initial_estimate(n)
116  print(initial_estimate)
117 
118  # Optimize using Levenberg-Marquardt optimization.
119  params = LevenbergMarquardtParams()
120  params.setAbsoluteErrorTol(1e-10)
121  params.setVerbosityLM("SUMMARY")
122  optimizer = LevenbergMarquardtOptimizer(graph, initial_estimate, params)
123  result = optimizer.optimize()
124  print("Final Result:\n", result)
125 
126 
127 if __name__ == '__main__':
128  toa_example()
129  print("Example complete")
Eigen::internal::print
EIGEN_STRONG_INLINE Packet4f print(const Packet4f &a)
Definition: NEON/PacketMath.h:3115
format
std::string format(const std::string &str, const std::vector< std::string > &find, const std::vector< std::string > &replace)
Definition: openglsupport.cpp:226
gtsam_unstable.examples.TimeOfArrivalExample.create_graph
def create_graph(microphones, simulatedTOA)
Definition: TimeOfArrivalExample.py:63
gtsam_unstable.examples.TimeOfArrivalExample.toa_example
def toa_example()
Definition: TimeOfArrivalExample.py:90
gtsam_unstable.examples.TimeOfArrivalExample.create_trajectory
def create_trajectory(n)
Definition: TimeOfArrivalExample.py:38
gtsam_unstable.examples.TimeOfArrivalExample.define_microphones
def define_microphones()
Definition: TimeOfArrivalExample.py:27
gtsam::TimeOfArrival
Time of arrival to given sensor.
Definition: Event.h:87
gtsam::range
Double_ range(const Point2_ &p, const Point2_ &q)
Definition: slam/expressions.h:30
gtsam::NonlinearFactorGraph
Definition: NonlinearFactorGraph.h:55
gtsam::LevenbergMarquardtOptimizer
Definition: LevenbergMarquardtOptimizer.h:35
gtsam_unstable.examples.TimeOfArrivalExample.simulate_one_toa
def simulate_one_toa(microphones, event)
Definition: TimeOfArrivalExample.py:51
gtsam::Event
Definition: Event.h:37
gtsam_unstable.examples.TimeOfArrivalExample.create_initial_estimate
def create_initial_estimate(n)
Definition: TimeOfArrivalExample.py:81
gtsam::Values
Definition: Values.h:65
gtsam::LevenbergMarquardtParams
Definition: LevenbergMarquardtParams.h:35
gtsam::Point3
Vector3 Point3
Definition: Point3.h:38
len
size_t len(handle h)
Get the length of a Python object.
Definition: pytypes.h:2448
gtsam_unstable.examples.TimeOfArrivalExample.simulate_toa
def simulate_toa(microphones, trajectory)
Definition: TimeOfArrivalExample.py:57


gtsam
Author(s):
autogenerated on Wed May 28 2025 03:08:00