| 
									
										
										
										
											2011-08-20 05:27:47 +08:00
										 |  |  | /* ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-11 22:39:48 +08:00
										 |  |  |  * GTSAM Copyright 2010, Georgia Tech Research Corporation, | 
					
						
							| 
									
										
										
										
											2011-08-20 05:27:47 +08:00
										 |  |  |  * Atlanta, Georgia 30332-0415 | 
					
						
							|  |  |  |  * All Rights Reserved | 
					
						
							|  |  |  |  * Authors: Frank Dellaert, et al. (see THANKS for the full author list) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * See LICENSE for the license information | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-14 11:23:14 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file elaboratePoint2KalmanFilter.cpp | 
					
						
							| 
									
										
										
										
											2011-08-20 05:27:47 +08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * simple linear Kalman filter on a moving 2D point, but done using factor graphs | 
					
						
							| 
									
										
										
										
											2011-08-27 20:27:13 +08:00
										 |  |  |  * This example manually creates all of the needed data structures | 
					
						
							| 
									
										
										
										
											2011-08-20 05:27:47 +08:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2011-10-14 11:23:14 +08:00
										 |  |  |  * @date Aug 19, 2011 | 
					
						
							|  |  |  |  * @author Frank Dellaert | 
					
						
							|  |  |  |  * @author Stephen Williams | 
					
						
							| 
									
										
										
										
											2011-08-20 05:27:47 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-11 10:26:22 +08:00
										 |  |  | #include <gtsam/nonlinear/PriorFactor.h>
 | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  | #include <gtsam/slam/BetweenFactor.h>
 | 
					
						
							| 
									
										
										
										
											2013-08-19 23:32:16 +08:00
										 |  |  | #include <gtsam/inference/Symbol.h>
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:33 +08:00
										 |  |  | #include <gtsam/linear/GaussianBayesNet.h>
 | 
					
						
							|  |  |  | #include <gtsam/linear/GaussianFactorGraph.h>
 | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  | #include <gtsam/linear/NoiseModel.h>
 | 
					
						
							|  |  |  | #include <gtsam/geometry/Point2.h>
 | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  | #include <gtsam/base/Vector.h>
 | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-05 00:43:51 +08:00
										 |  |  | #include <cassert>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-20 05:27:47 +08:00
										 |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							| 
									
										
										
										
											2025-02-21 06:12:46 +08:00
										 |  |  | using symbol_shorthand::X; | 
					
						
							| 
									
										
										
										
											2011-08-20 05:27:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | int main() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   // [code below basically does SRIF with Cholesky]
 | 
					
						
							| 
									
										
										
										
											2011-08-20 05:27:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  |   // Create a factor graph to perform the inference
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  |   GaussianFactorGraph::shared_ptr linearFactorGraph(new GaussianFactorGraph); | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Create the desired ordering
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  |   Ordering::shared_ptr ordering(new Ordering); | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Create a structure to hold the linearization points
 | 
					
						
							| 
									
										
										
										
											2012-02-03 00:16:46 +08:00
										 |  |  |   Values linearizationPoints; | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   // Ground truth example
 | 
					
						
							|  |  |  |   // Start at origin, move to the right (x-axis): 0,0  0,1  0,2
 | 
					
						
							|  |  |  |   // Motion model is just moving to the right (x'-x)^2
 | 
					
						
							|  |  |  |   // Measurements are GPS like, (x-z)^2, where z is a 2D measurement
 | 
					
						
							|  |  |  |   // i.e., we should get 0,0  0,1  0,2 if there is no noise
 | 
					
						
							| 
									
										
										
										
											2011-08-20 05:27:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  |   // Create new state variable
 | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   ordering->push_back(X(0)); | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Initialize state x0 (2D point) at origin by adding a prior factor, i.e., Bayes net P(x0)
 | 
					
						
							|  |  |  |   // This is equivalent to x_0 and P_0
 | 
					
						
							|  |  |  |   Point2 x_initial(0,0); | 
					
						
							| 
									
										
										
										
											2025-02-21 06:12:46 +08:00
										 |  |  |   SharedDiagonal P_initial = noiseModel::Isotropic::Sigma(2, 0.1); | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   // Linearize the factor and add it to the linear factor graph
 | 
					
						
							|  |  |  |   linearizationPoints.insert(X(0), x_initial); | 
					
						
							|  |  |  |   linearFactorGraph->add(X(0),  | 
					
						
							| 
									
										
										
										
											2025-02-21 06:12:46 +08:00
										 |  |  |     P_initial->R(),  | 
					
						
							|  |  |  |     Vector::Zero(2), | 
					
						
							|  |  |  |     noiseModel::Unit::Create(2)); | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Now predict the state at t=1, i.e. argmax_{x1} P(x1) = P(x1|x0) P(x0)
 | 
					
						
							|  |  |  |   // In Kalman Filter notation, this is x_{t+1|t} and P_{t+1|t}
 | 
					
						
							|  |  |  |   // For the Kalman Filter, this requires a motion model, f(x_{t}) = x_{t+1|t)
 | 
					
						
							|  |  |  |   // Assuming the system is linear, this will be of the form f(x_{t}) = F*x_{t} + B*u_{t} + w
 | 
					
						
							|  |  |  |   // where F is the state transition model/matrix, B is the control input model,
 | 
					
						
							|  |  |  |   // and w is zero-mean, Gaussian white noise with covariance Q
 | 
					
						
							|  |  |  |   // Note, in some models, Q is actually derived as G*w*G^T where w models uncertainty of some
 | 
					
						
							|  |  |  |   // physical property, such as velocity or acceleration, and G is derived from physics
 | 
					
						
							|  |  |  |   //
 | 
					
						
							|  |  |  |   // For the purposes of this example, let us assume we are using a constant-position model and
 | 
					
						
							|  |  |  |   // the controls are driving the point to the right at 1 m/s. Then, F = [1 0 ; 0 1], B = [1 0 ; 0 1]
 | 
					
						
							|  |  |  |   // and u = [1 ; 0]. Let us also assume that the process noise Q = [0.1 0 ; 0 0.1];
 | 
					
						
							|  |  |  |   //
 | 
					
						
							|  |  |  |   // In the case of factor graphs, the factor related to the motion model would be defined as
 | 
					
						
							|  |  |  |   // f2 = (f(x_{t}) - x_{t+1}) * Q^-1 * (f(x_{t}) - x_{t+1})^T
 | 
					
						
							|  |  |  |   // Conveniently, there is a factor type, called a BetweenFactor, that can generate this factor
 | 
					
						
							|  |  |  |   // given the expected difference, f(x_{t}) - x_{t+1}, and Q.
 | 
					
						
							|  |  |  |   // so, difference = x_{t+1} - x_{t} = F*x_{t} + B*u_{t} - I*x_{t}
 | 
					
						
							|  |  |  |   //                                  = (F - I)*x_{t} + B*u_{t}
 | 
					
						
							|  |  |  |   //                                  = B*u_{t} (for our example)
 | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   ordering->push_back(X(1)); | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  |   Point2 difference(1,0); | 
					
						
							| 
									
										
										
										
											2025-02-21 06:12:46 +08:00
										 |  |  |   SharedDiagonal Q = noiseModel::Isotropic::Sigma(2, 0.1); | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   BetweenFactor<Point2> factor2(X(0), X(1), difference, Q); | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  |   // Linearize the factor and add it to the linear factor graph
 | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   linearizationPoints.insert(X(1), x_initial); | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  |   linearFactorGraph->push_back(factor2.linearize(linearizationPoints)); | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // We have now made the small factor graph f1-(x0)-f2-(x1)
 | 
					
						
							|  |  |  |   // where factor f1 is just the prior from time t0, P(x0)
 | 
					
						
							|  |  |  |   // and   factor f2 is from the motion model
 | 
					
						
							|  |  |  |   // Eliminate this in order x0, x1, to get Bayes net P(x0|x1)P(x1)
 | 
					
						
							|  |  |  |   // As this is a filter, all we need is the posterior P(x1), so we just keep the root of the Bayes net
 | 
					
						
							|  |  |  |   //
 | 
					
						
							|  |  |  |   // Because of the way GTSAM works internally, we have used nonlinear class even though this example
 | 
					
						
							|  |  |  |   // system is linear. We first convert the nonlinear factor graph into a linear one, using the specified
 | 
					
						
							|  |  |  |   // ordering. Linear factors are simply numbered, and are not accessible via named key like the nonlinear
 | 
					
						
							|  |  |  |   // variables. Also, the nonlinear factors are linearized around an initial estimate. For a true linear
 | 
					
						
							|  |  |  |   // system, the initial estimate is not important.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Solve the linear factor graph, converting it into a linear Bayes Network ( P(x0,x1) = P(x0|x1)*P(x1) )
 | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  |   GaussianBayesNet::shared_ptr bayesNet = linearFactorGraph->eliminateSequential(*ordering); | 
					
						
							|  |  |  |   const GaussianConditional::shared_ptr& x1Conditional = bayesNet->back(); // This should be P(x1)
 | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  |   // Extract the current estimate of x1,P1 from the Bayes Network
 | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  |   VectorValues result = bayesNet->optimize(); | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   Point2 x1_predict = linearizationPoints.at<Point2>(X(1)) + result[X(1)]; | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  |   traits<Point2>::Print(x1_predict, "X1 Predict"); | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  |   // Update the new linearization point to the new estimate
 | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   linearizationPoints.update(X(1), x1_predict); | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Create a new, empty graph and add the prior from the previous step
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  |   linearFactorGraph = GaussianFactorGraph::shared_ptr(new GaussianFactorGraph); | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Convert the root conditional, P(x1) in this case, into a Prior for the next step
 | 
					
						
							|  |  |  |   // Some care must be done here, as the linearization point in future steps will be different
 | 
					
						
							|  |  |  |   // than what was used when the factor was created.
 | 
					
						
							|  |  |  |   // f = || F*dx1' - (F*x0 - x1) ||^2, originally linearized at x1 = x0
 | 
					
						
							|  |  |  |   // After this step, the factor needs to be linearized around x1 = x1_predict
 | 
					
						
							|  |  |  |   // This changes the factor to f = || F*dx1'' - b'' ||^2
 | 
					
						
							|  |  |  |   //                              = || F*(dx1' - (dx1' - dx1'')) - b'' ||^2
 | 
					
						
							|  |  |  |   //                              = || F*dx1' - F*(dx1' - dx1'') - b'' ||^2
 | 
					
						
							|  |  |  |   //                              = || F*dx1' - (b'' + F(dx1' - dx1'')) ||^2
 | 
					
						
							|  |  |  |   //                              -> b' = b'' + F(dx1' - dx1'')
 | 
					
						
							|  |  |  |   //                              -> b'' = b' - F(dx1' - dx1'')
 | 
					
						
							|  |  |  |   //                              = || F*dx1'' - (b'  - F(dx1' - dx1'')) ||^2
 | 
					
						
							|  |  |  |   //                              = || F*dx1'' - (b'  - F(x_predict - x_inital)) ||^2
 | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Create a new, empty graph and add the new prior
 | 
					
						
							|  |  |  |   linearFactorGraph = GaussianFactorGraph::shared_ptr(new GaussianFactorGraph); | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   linearFactorGraph->add( | 
					
						
							|  |  |  | 	X(1),  | 
					
						
							|  |  |  |     x1Conditional->R(), | 
					
						
							|  |  |  |     x1Conditional->d() - x1Conditional->R() * result[X(1)], | 
					
						
							|  |  |  |     x1Conditional->get_model()); | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  |   | 
					
						
							|  |  |  |   // Reset ordering for the next step
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  |   ordering = Ordering::shared_ptr(new Ordering); | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   ordering->push_back(X(1)); | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Now, a measurement, z1, has been received, and the Kalman Filter should be "Updated"/"Corrected"
 | 
					
						
							|  |  |  |   // This is equivalent to saying P(x1|z1) ~ P(z1|x1)*P(x1) ~ f3(x1)*f4(x1;z1)
 | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  |   // where f3 is the prior from the previous step, and
 | 
					
						
							|  |  |  |   // where f4 is a measurement factor
 | 
					
						
							|  |  |  |   //
 | 
					
						
							|  |  |  |   // So, now we need to create the measurement factor, f4
 | 
					
						
							|  |  |  |   // For the Kalman Filter, this is the measurement function, h(x_{t}) = z_{t}
 | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  |   // Assuming the system is linear, this will be of the form h(x_{t}) = H*x_{t} + v
 | 
					
						
							|  |  |  |   // where H is the observation model/matrix, and v is zero-mean, Gaussian white noise with covariance R
 | 
					
						
							|  |  |  |   //
 | 
					
						
							|  |  |  |   // For the purposes of this example, let us assume we have something like a GPS that returns
 | 
					
						
							|  |  |  |   // the current position of the robot. For this simple example, we can use a PriorFactor to model the
 | 
					
						
							|  |  |  |   // observation as it depends on only a single state variable, x1. To model real sensor observations
 | 
					
						
							|  |  |  |   // generally requires the creation of a new factor type. For example, factors for range sensors, bearing
 | 
					
						
							|  |  |  |   // sensors, and camera projections have already been added to GTSAM.
 | 
					
						
							|  |  |  |   //
 | 
					
						
							|  |  |  |   // In the case of factor graphs, the factor related to the measurements would be defined as
 | 
					
						
							|  |  |  |   // f4 = (h(x_{t}) - z_{t}) * R^-1 * (h(x_{t}) - z_{t})^T
 | 
					
						
							|  |  |  |   //    = (x_{t} - z_{t}) * R^-1 * (x_{t} - z_{t})^T
 | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  |   // This can be modeled using the PriorFactor, where the mean is z_{t} and the covariance is R.
 | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  |   Point2 z1(1.0, 0.0); | 
					
						
							| 
									
										
										
										
											2025-02-21 06:12:46 +08:00
										 |  |  |   SharedDiagonal R1 = noiseModel::Isotropic::Sigma(2, 0.25); | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   PriorFactor<Point2> factor4(X(1), z1, R1); | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  |   // Linearize the factor and add it to the linear factor graph
 | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  |   linearFactorGraph->push_back(factor4.linearize(linearizationPoints)); | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  |   // We have now made the small factor graph f3-(x1)-f4
 | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  |   // where factor f3 is the prior from previous time ( P(x1) )
 | 
					
						
							|  |  |  |   // and   factor f4 is from the measurement, z1 ( P(x1|z1) )
 | 
					
						
							|  |  |  |   // Eliminate this in order x1, to get Bayes net P(x1)
 | 
					
						
							|  |  |  |   // As this is a filter, all we need is the posterior P(x1), so we just keep the root of the Bayes net
 | 
					
						
							|  |  |  |   // We solve as before...
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Solve the linear factor graph, converting it into a linear Bayes Network ( P(x0,x1) = P(x0|x1)*P(x1) )
 | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  |   GaussianBayesNet::shared_ptr updatedBayesNet = linearFactorGraph->eliminateSequential(*ordering); | 
					
						
							|  |  |  |   const GaussianConditional::shared_ptr& updatedConditional = updatedBayesNet->back(); | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  |   // Extract the current estimate of x1 from the Bayes Network
 | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  |   VectorValues updatedResult = updatedBayesNet->optimize(); | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   Point2 x1_update = linearizationPoints.at<Point2>(X(1)) + updatedResult[X(1)]; | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  |   traits<Point2>::Print(x1_update, "X1 Update"); | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  |   // Update the linearization point to the new estimate
 | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   linearizationPoints.update(X(1), x1_update); | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Wash, rinse, repeat for another time step
 | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  |   // Create a new, empty graph and add the prior from the previous step
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  |   linearFactorGraph = GaussianFactorGraph::shared_ptr(new GaussianFactorGraph); | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Convert the root conditional, P(x1) in this case, into a Prior for the next step
 | 
					
						
							| 
									
										
										
										
											2011-08-27 20:27:13 +08:00
										 |  |  |   // The linearization point of this prior must be moved to the new estimate of x, and the key/index needs to be reset to 0,
 | 
					
						
							|  |  |  |   // the first key in the next iteration
 | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   linearFactorGraph->add( | 
					
						
							|  |  |  | 	X(1),  | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  |     updatedConditional->R(), | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |     updatedConditional->d() - updatedConditional->R() * updatedResult[X(1)], | 
					
						
							| 
									
										
										
										
											2025-02-21 06:12:46 +08:00
										 |  |  |     updatedConditional->get_model()); | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Create the desired ordering
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  |   ordering = Ordering::shared_ptr(new Ordering); | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   ordering->push_back(X(1)); | 
					
						
							|  |  |  |   ordering->push_back(X(2)); | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  |    | 
					
						
							|  |  |  |   // Create a nonlinear factor describing the motion model (moving right again)
 | 
					
						
							|  |  |  |   Point2 difference2(1,0); | 
					
						
							| 
									
										
										
										
											2025-02-21 06:12:46 +08:00
										 |  |  |   SharedDiagonal Q2 = noiseModel::Isotropic::Sigma(2, 0.1); | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   BetweenFactor<Point2> factor6(X(1), X(2), difference2, Q2); | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Linearize the factor and add it to the linear factor graph
 | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   linearizationPoints.insert(X(2), x1_update); | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  |   linearFactorGraph->push_back(factor6.linearize(linearizationPoints)); | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  |   // Solve the linear factor graph, converting it into a linear Bayes Network ( P(x1,x2) = P(x1|x2)*P(x2) )
 | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  |   GaussianBayesNet::shared_ptr predictionBayesNet2 = linearFactorGraph->eliminateSequential(*ordering); | 
					
						
							|  |  |  |   const GaussianConditional::shared_ptr& x2Conditional = predictionBayesNet2->back(); | 
					
						
							|  |  |  |     | 
					
						
							|  |  |  |   // Extract the predicted state
 | 
					
						
							|  |  |  |   VectorValues prediction2Result = predictionBayesNet2->optimize(); | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   Point2 x2_predict = linearizationPoints.at<Point2>(X(2)) + prediction2Result[X(2)]; | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  |   traits<Point2>::Print(x2_predict, "X2 Predict"); | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  |   // Update the linearization point to the new estimate
 | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   linearizationPoints.update(X(2), x2_predict); | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Now add the next measurement
 | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  |   // Create a new, empty graph and add the prior from the previous step
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  |   linearFactorGraph = GaussianFactorGraph::shared_ptr(new GaussianFactorGraph); | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Convert the root conditional, P(x1) in this case, into a Prior for the next step
 | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   linearFactorGraph->add( | 
					
						
							|  |  |  | 	X(2),  | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  |     x2Conditional->R(), | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |     x2Conditional->d() - x2Conditional->R() * prediction2Result[X(2)], | 
					
						
							| 
									
										
										
										
											2025-02-21 06:12:46 +08:00
										 |  |  |     x2Conditional->get_model()); | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  |   // Create the desired ordering
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  |   ordering = Ordering::shared_ptr(new Ordering); | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   ordering->push_back(X(2)); | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // And update using z2 ...
 | 
					
						
							|  |  |  |   Point2 z2(2.0, 0.0); | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  |   SharedDiagonal R2 = noiseModel::Diagonal::Sigmas((gtsam::Vector2() << 0.25, 0.25).finished()); | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   PriorFactor<Point2> factor8(X(2), z2, R2); | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Linearize the factor and add it to the linear factor graph
 | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  |   linearFactorGraph->push_back(factor8.linearize(linearizationPoints)); | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // We have now made the small factor graph f7-(x2)-f8
 | 
					
						
							|  |  |  |   // where factor f7 is the prior from previous time ( P(x2) )
 | 
					
						
							|  |  |  |   // and   factor f8 is from the measurement, z2 ( P(x2|z2) )
 | 
					
						
							|  |  |  |   // Eliminate this in order x2, to get Bayes net P(x2)
 | 
					
						
							|  |  |  |   // As this is a filter, all we need is the posterior P(x2), so we just keep the root of the Bayes net
 | 
					
						
							|  |  |  |   // We solve as before...
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Solve the linear factor graph, converting it into a linear Bayes Network ( P(x0,x1) = P(x0|x1)*P(x1) )
 | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  |   GaussianBayesNet::shared_ptr updatedBayesNet2 = linearFactorGraph->eliminateSequential(*ordering); | 
					
						
							|  |  |  |   const GaussianConditional::shared_ptr& updatedConditional2 = updatedBayesNet2->back(); | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  |   // Extract the current estimate of x2 from the Bayes Network
 | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  |   VectorValues updatedResult2 = updatedBayesNet2->optimize(); | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   Point2 x2_update = linearizationPoints.at<Point2>(X(2)) + updatedResult2[X(2)]; | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  |   traits<Point2>::Print(x2_update, "X2 Update"); | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  |   // Update the linearization point to the new estimate
 | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   linearizationPoints.update(X(2), x2_update); | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Wash, rinse, repeat for a third time step
 | 
					
						
							|  |  |  |   // Create a new, empty graph and add the prior from the previous step
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  |   linearFactorGraph = GaussianFactorGraph::shared_ptr(new GaussianFactorGraph); | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Convert the root conditional, P(x1) in this case, into a Prior for the next step
 | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   linearFactorGraph->add( | 
					
						
							|  |  |  | 	X(2),  | 
					
						
							|  |  |  |     updatedConditional2->R(), | 
					
						
							|  |  |  |     updatedConditional2->d() - updatedConditional2->R() * updatedResult2[X(2)], | 
					
						
							| 
									
										
										
										
											2025-02-21 06:12:46 +08:00
										 |  |  |     updatedConditional2->get_model()); | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  |   // Create the desired ordering
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  |   ordering = Ordering::shared_ptr(new Ordering); | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   ordering->push_back(X(2)); | 
					
						
							|  |  |  |   ordering->push_back(X(3)); | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Create a nonlinear factor describing the motion model
 | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  |   Point2 difference3(1,0); | 
					
						
							| 
									
										
										
										
											2025-02-21 06:12:46 +08:00
										 |  |  |   SharedDiagonal Q3 = noiseModel::Isotropic::Sigma(2, 0.1); | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   BetweenFactor<Point2> factor10(X(2), X(3), difference3, Q3); | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Linearize the factor and add it to the linear factor graph
 | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   linearizationPoints.insert(X(3), x2_update); | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  |   linearFactorGraph->push_back(factor10.linearize(linearizationPoints)); | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Solve the linear factor graph, converting it into a linear Bayes Network ( P(x1,x2) = P(x1|x2)*P(x2) )
 | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  |   GaussianBayesNet::shared_ptr predictionBayesNet3 = linearFactorGraph->eliminateSequential(*ordering); | 
					
						
							|  |  |  |   const GaussianConditional::shared_ptr& x3Conditional = predictionBayesNet3->back(); | 
					
						
							| 
									
										
										
										
											2011-08-20 05:27:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  |   // Extract the current estimate of x3 from the Bayes Network
 | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  |   VectorValues prediction3Result = predictionBayesNet3->optimize(); | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   Point2 x3_predict = linearizationPoints.at<Point2>(X(3)) + prediction3Result[X(3)]; | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  |   traits<Point2>::Print(x3_predict, "X3 Predict"); | 
					
						
							| 
									
										
										
										
											2011-08-20 05:27:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  |   // Update the linearization point to the new estimate
 | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   linearizationPoints.update(X(3), x3_predict); | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-20 05:27:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  |   // Now add the next measurement
 | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  |   // Create a new, empty graph and add the prior from the previous step
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  |   linearFactorGraph = GaussianFactorGraph::shared_ptr(new GaussianFactorGraph); | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Convert the root conditional, P(x1) in this case, into a Prior for the next step
 | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   linearFactorGraph->add( | 
					
						
							|  |  |  | 	X(3),  | 
					
						
							| 
									
										
										
										
											2025-02-21 06:12:46 +08:00
										 |  |  |     x3Conditional->R(), | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |     x3Conditional->d() - x3Conditional->R() * prediction3Result[X(3)], | 
					
						
							| 
									
										
										
										
											2025-02-21 06:12:46 +08:00
										 |  |  |     x3Conditional->get_model()); | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  |   | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  |   // Create the desired ordering
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  |   ordering = Ordering::shared_ptr(new Ordering); | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   ordering->push_back(X(3)); | 
					
						
							| 
									
										
										
										
											2011-08-20 05:27:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  |   // And update using z3 ...
 | 
					
						
							|  |  |  |   Point2 z3(3.0, 0.0); | 
					
						
							| 
									
										
										
										
											2025-02-21 06:12:46 +08:00
										 |  |  |   SharedDiagonal R3 = noiseModel::Isotropic::Sigma(2, 0.25); | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   PriorFactor<Point2> factor12(X(3), z3, R3); | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Linearize the factor and add it to the linear factor graph
 | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  |   linearFactorGraph->push_back(factor12.linearize(linearizationPoints)); | 
					
						
							| 
									
										
										
										
											2011-08-20 05:27:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  |   // We have now made the small factor graph f11-(x3)-f12
 | 
					
						
							|  |  |  |   // where factor f11 is the prior from previous time ( P(x3) )
 | 
					
						
							|  |  |  |   // and   factor f12 is from the measurement, z3 ( P(x3|z3) )
 | 
					
						
							|  |  |  |   // Eliminate this in order x3, to get Bayes net P(x3)
 | 
					
						
							|  |  |  |   // As this is a filter, all we need is the posterior P(x3), so we just keep the root of the Bayes net
 | 
					
						
							|  |  |  |   // We solve as before...
 | 
					
						
							| 
									
										
										
										
											2011-08-20 05:27:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  |   // Solve the linear factor graph, converting it into a linear Bayes Network ( P(x0,x1) = P(x0|x1)*P(x1) )
 | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  |   GaussianBayesNet::shared_ptr updatedBayesNet3 = linearFactorGraph->eliminateSequential(*ordering); | 
					
						
							|  |  |  |   const GaussianConditional::shared_ptr& updatedConditional3 = updatedBayesNet3->back(); | 
					
						
							| 
									
										
										
										
											2011-08-20 05:27:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  |   // Extract the current estimate of x2 from the Bayes Network
 | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  |   VectorValues updatedResult3 = updatedBayesNet3->optimize(); | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   Point2 x3_update = linearizationPoints.at<Point2>(X(3)) + updatedResult3[X(3)]; | 
					
						
							| 
									
										
										
										
											2025-02-21 05:19:18 +08:00
										 |  |  |   traits<Point2>::Print(x3_update, "X3 Update"); | 
					
						
							| 
									
										
										
										
											2011-08-20 05:27:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  |   // Update the linearization point to the new estimate
 | 
					
						
							| 
									
										
										
										
											2025-02-21 07:25:00 +08:00
										 |  |  |   linearizationPoints.update(X(3), x3_update); | 
					
						
							| 
									
										
										
										
											2011-08-25 21:34:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-22 23:35:24 +08:00
										 |  |  |   return 0; | 
					
						
							| 
									
										
										
										
											2011-08-20 05:27:47 +08:00
										 |  |  | } |