gtsam
Loading...
Searching...
No Matches
ExtendedKalmanFilter-inl.h
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
18
19#pragma once
20
25
26#include <cassert>
27
28namespace gtsam {
29
30 /* ************************************************************************* */
31 template<class VALUE>
32 typename ExtendedKalmanFilter<VALUE>::T ExtendedKalmanFilter<VALUE>::solve_(
33 const GaussianFactorGraph& linearFactorGraph,
34 const Values& linearizationPoint, Key lastKey,
36 {
37 // Compute the marginal on the last key
38 // Solve the linear factor graph, converting it into a linear Bayes Network
39 // P(x0,x1) = P(x0|x1)*P(x1)
40 const Ordering lastKeyAsOrdering{lastKey};
41 const GaussianConditional::shared_ptr marginal =
42 linearFactorGraph.marginalMultifrontalBayesNet(lastKeyAsOrdering)->front();
43
44 // Extract the current estimate of x1,P1
45 VectorValues result = marginal->solve(VectorValues());
46 const T& current = linearizationPoint.at<T>(lastKey);
47 T x = traits<T>::Retract(current, result[lastKey]);
48
49 // Create a Jacobian Factor from the root node of the produced Bayes Net.
50 // This will act as a prior for the next iteration.
51 // The linearization point of this prior must be moved to the new estimate of x,
52 // and the key/index needs to be reset to 0, the first key in the next iteration.
53 assert(marginal->nrFrontals() == 1);
54 assert(marginal->nrParents() == 0);
55 *newPrior = std::make_shared<JacobianFactor>(
56 marginal->keys().front(),
57 marginal->getA(marginal->begin()),
58 marginal->getb() - marginal->getA(marginal->begin()) * result[lastKey],
59 marginal->get_model());
60
61 return x;
62 }
63
64 /* ************************************************************************* */
65 template <class VALUE>
66 ExtendedKalmanFilter<VALUE>::ExtendedKalmanFilter(
67 Key key_initial, T x_initial, noiseModel::Gaussian::shared_ptr P_initial)
68 : x_(x_initial) // Set the initial linearization point
69 {
70 // Create a Jacobian Prior Factor directly P_initial.
71 // Since x0 is set to the provided mean, the b vector in the prior will be zero
72 // TODO(Frank): is there a reason why noiseModel is not simply P_initial?
73 size_t n = traits<T>::GetDimension(x_initial);
74 priorFactor_ = JacobianFactor::shared_ptr(
75 new JacobianFactor(key_initial, P_initial->R(), Vector::Zero(n),
76 noiseModel::Unit::Create(n)));
77 }
78
79 /* ************************************************************************* */
80 template<class VALUE>
81 typename ExtendedKalmanFilter<VALUE>::T ExtendedKalmanFilter<VALUE>::predict(
82 const NoiseModelFactor& motionFactor) {
83 const auto keys = motionFactor.keys();
84
85 // Create a Gaussian Factor Graph
86 GaussianFactorGraph linearFactorGraph;
87
88 // Add in previous posterior as prior on the first state
89 linearFactorGraph.push_back(priorFactor_);
90
91 // Linearize motion model and add it to the Kalman Filter graph
92 Values linearizationPoint;
93 linearizationPoint.insert(keys[0], x_);
94 linearizationPoint.insert(keys[1], x_); // TODO should this really be x_ ?
95 linearFactorGraph.push_back(motionFactor.linearize(linearizationPoint));
96
97 // Solve the factor graph and update the current state estimate
98 // and the posterior for the next iteration.
99 x_ = solve_(linearFactorGraph, linearizationPoint, keys[1], &priorFactor_);
100
101 return x_;
102 }
103
104 /* ************************************************************************* */
105 template<class VALUE>
106 typename ExtendedKalmanFilter<VALUE>::T ExtendedKalmanFilter<VALUE>::update(
107 const NoiseModelFactor& measurementFactor) {
108 const auto keys = measurementFactor.keys();
109
110 // Create a Gaussian Factor Graph
111 GaussianFactorGraph linearFactorGraph;
112
113 // Add in the prior on the first state
114 linearFactorGraph.push_back(priorFactor_);
115
116 // Linearize measurement factor and add it to the Kalman Filter graph
117 Values linearizationPoint;
118 linearizationPoint.insert(keys[0], x_);
119 linearFactorGraph.push_back(measurementFactor.linearize(linearizationPoint));
120
121 // Solve the factor graph and update the current state estimate
122 // and the prior factor for the next iteration
123 x_ = solve_(linearFactorGraph, linearizationPoint, keys[0], &priorFactor_);
124
125 return x_;
126 }
127
128} // namespace gtsam
Chordal Bayes Net, the result of eliminating a factor graph.
Linear Factor Graph where all factors are Gaussians.
Class to perform generic Kalman Filtering using nonlinear factor graphs.
Non-linear factor base classes.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:43
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
IsDerived< DERIVEDFACTOR > push_back(std::shared_ptr< DERIVEDFACTOR > factor)
Add a factor directly using a shared_ptr.
Definition FactorGraph.h:147
const KeyVector & keys() const
Access the factor's involved variable keys.
Definition Factor.h:143
Definition Ordering.h:33
std::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition GaussianConditional.h:46
A Linear Factor Graph is a factor graph where all factors are Gaussian, i.e.
Definition GaussianFactorGraph.h:77
std::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition JacobianFactor.h:97
VectorValues represents a collection of vector-valued variables associated each with a unique integer...
Definition VectorValues.h:73
T update(const NoiseModelFactor &measurementFactor)
Calculate posterior density P(x_) ~ L(z|x) P(x) The likelihood L(z|x) should be given as a unary fact...
Definition ExtendedKalmanFilter-inl.h:106
T predict(const NoiseModelFactor &motionFactor)
Calculate predictive density The motion model should be given as a factor with key1 for and key2 fo...
Definition ExtendedKalmanFilter-inl.h:81
A nonlinear sum-of-squares factor with a zero-mean noise model implementing the density Templated on...
Definition NonlinearFactor.h:208
std::shared_ptr< GaussianFactor > linearize(const Values &x) const override
Linearize a non-linearFactorN to get a GaussianFactor, Hence .
Definition NonlinearFactor.cpp:160
A non-templated config holding any types of Manifold-group elements.
Definition Values.h:65
void insert(Key j, const Value &val)
Add a variable with the given j, throws KeyAlreadyExists<J> if j is already present.
Definition Values.cpp:170
In Gaussian factors, the error function returns either the negative log-likelihood,...