kalman_filter.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Software License Agreement (BSD License) *
3  * Copyright (C) 2015 by Horatiu George Todoran <todorangrg@gmail.com> *
4  * *
5  * Redistribution and use in source and binary forms, with or without *
6  * modification, are permitted provided that the following conditions *
7  * are met: *
8  * *
9  * 1. Redistributions of source code must retain the above copyright *
10  * notice, this list of conditions and the following disclaimer. *
11  * 2. Redistributions in binary form must reproduce the above copyright *
12  * notice, this list of conditions and the following disclaimer in *
13  * the documentation and/or other materials provided with the *
14  * distribution. *
15  * 3. Neither the name of the copyright holder nor the names of its *
16  * contributors may be used to endorse or promote products derived *
17  * from this software without specific prior written permission. *
18  * *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; *
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER *
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT *
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY *
29  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
30  * POSSIBILITY OF SUCH DAMAGE. *
31  ***************************************************************************/
32 #ifndef KALMAN_FILTER_H
33 #define KALMAN_FILTER_H
34 
35 #include <float.h>
36 #include <eigen3/Eigen/Dense>
37 #include <cmath>
38 #include <cfloat>
39 #include <tuw_control/utils.h>
40 
41 #include <iostream>
42 
43 namespace tuw
44 {
50 template <typename NumType, int XDim>
52 {
53  // special class member functions
54 public:
55  KalmanFilter() = default;
56 
57 public:
58  virtual ~KalmanFilter() = default;
59 
60 public:
61  KalmanFilter(const KalmanFilter&) = default;
62 
63 public:
64  KalmanFilter& operator=(const KalmanFilter&) = default;
65 
66 public:
67  KalmanFilter(KalmanFilter&&) = default;
68 
69 public:
70  KalmanFilter& operator=(KalmanFilter&&) = default;
71 
72 public:
73  using NumericalType = NumType;
74 public:
75  static constexpr const int xDim = XDim;
76 
81 public:
82  void init(const Eigen::Matrix<NumType, XDim, 1>& _x0, const Eigen::Matrix<NumType, XDim, XDim>& _Sigma0)
83  {
84  x_ = _x0;
85  Sigma_ = _Sigma0;
86  }
87 
93 public:
94  void predict(const Eigen::Matrix<NumType, XDim, 1>& _f, const Eigen::Matrix<NumType, XDim, XDim>& _Phi,
95  const Eigen::Matrix<NumType, XDim, XDim>& _Q)
96  {
97  x_ = _f;
98  Sigma_ = _Phi * Sigma_ * _Phi.transpose() + _Q;
99  }
100 
106 public:
107  template <int UpdateDim>
108  void update(const Eigen::Matrix<NumType, UpdateDim, 1>& _deltah, const Eigen::Matrix<NumType, UpdateDim, XDim>& _C,
109  const Eigen::Matrix<NumType, UpdateDim, UpdateDim>& _R)
110  {
111  Eigen::Matrix<NumType, UpdateDim, UpdateDim> S = _C * Sigma_ * _C.transpose() + _R;
112  Eigen::Matrix<NumType, XDim, UpdateDim> K = Sigma_ * _C.transpose() * S.inverse();
113  x_ += K * _deltah;
114  Sigma_ = (Eigen::Matrix<NumType, XDim, XDim>::Identity(Sigma_.rows(), Sigma_.cols()) - K * _C) * Sigma_;
115  }
116 
118 public:
119  const Eigen::Matrix<NumType, XDim, 1>& x() const
120  {
121  return x_;
122  }
124 public:
125  const Eigen::Matrix<NumType, XDim, XDim>& Sigma() const
126  {
127  return Sigma_;
128  }
129 
130 protected:
131  Eigen::Matrix<NumType, XDim, 1> x_;
132 
133 protected:
134  Eigen::Matrix<NumType, XDim, XDim> Sigma_;
135 };
136 template <typename NumType, int XDim>
137 constexpr const int KalmanFilter<NumType, XDim>::xDim;
138 
145 template <typename NumType, int XDim, size_t UDim, typename ParamType>
146 class KalmanFilterPredictInterface : public KalmanFilter<NumType, XDim>
147 {
148  // special class member functions
149 public:
150  KalmanFilterPredictInterface(ParamType& _params) : KalmanFilter<NumType, XDim>(), params_(_params)
151  {
152  }
153 
154 public:
155  virtual ~KalmanFilterPredictInterface() = default;
156 
157 public:
159 
160 public:
162 
163 public:
165 
166 public:
168 
169 public:
170  using ParamsType = ParamType;
171 
181 public:
182  void init(const Eigen::Matrix<NumType, XDim, 1>& _x0)
183  {
184  this->x_ = _x0;
185  this->Sigma_.resize(_x0.rows(), _x0.rows());
186  computeSigmaInit();
187  }
188 
204 public:
205  template <int i = XDim, typename std::enable_if<(i >= 0)>::type* = nullptr>
206  void predict(const Eigen::Matrix<NumType, UDim, 1>& _u, const double& _Ta)
207  {
208  precompute(_Ta);
209  computePhi();
210  computef(_u);
211  computeQ();
213  }
214 
225 public:
226  template <int i = XDim, typename std::enable_if<(i == -1)>::type* = nullptr>
227  void predict(const Eigen::Matrix<NumType, UDim, 1>& _u, const double& _Ta)
228  {
229  f_.resize(this->x_.rows(), 1);
230  Phi_.resize(this->x_.rows(), this->x_.rows());
231  Q_.resize(this->x_.rows(), this->x_.rows());
232  predict<0>(_u, _Ta);
233  }
234 
235 public:
236  template <int i = UDim, typename std::enable_if<(i == 0)>::type* = nullptr>
237  void predict(const double& _Ta)
238  {
239  static Eigen::Matrix<double, 0, 1> uEmpty_;
240  f_.resize(this->x_.rows(), 1);
241  Phi_.resize(this->x_.rows(), this->x_.rows());
242  Q_.resize(this->x_.rows(), this->x_.rows());
243  predict(uEmpty_, _Ta);
244  }
245 
247 public:
248  const ParamType& param() const
249  {
250  return params_;
251  }
252 
253 protected:
254  Eigen::Matrix<NumType, XDim, 1> f_;
255 protected:
256  Eigen::Matrix<NumType, XDim, XDim> Phi_;
257 protected:
259  Eigen::Matrix<NumType, XDim, XDim> Q_;
260 protected:
261  ParamType& params_;
262 
264 protected:
265  virtual void precompute(const double& _Ta) = 0;
267 protected:
268  virtual void computeSigmaInit() = 0;
270 protected:
271  virtual void computePhi() = 0;
273 protected:
274  virtual void computef(const Eigen::Matrix<NumType, UDim, 1>& _u) = 0;
276 protected:
277  virtual void computeQ() = 0;
278 };
279 
286 template <typename KFPredType, int HDim>
288 {
289  // special class member functions
290 public:
291  KalmanFilterUpdateInterface() = default;
292 
293 public:
294  virtual ~KalmanFilterUpdateInterface() = default;
295 
296 public:
298 
299 public:
301 
302 public:
304 
305 public:
307 
308 public:
309  static constexpr const int hDim = HDim;
310 
311 public:
312  const Eigen::Matrix<typename KFPredType::NumericalType, HDim, 1>& deltah() const
313  {
314  return deltah_;
315  }
316 public:
317  const Eigen::Matrix<typename KFPredType::NumericalType, HDim, KFPredType::xDim>& H() const
318  {
319  return H_;
320  }
321 public:
322  const Eigen::Matrix<typename KFPredType::NumericalType, HDim, HDim>& R() const
323  {
324  return R_;
325  }
326 
327 protected:
328  Eigen::Matrix<typename KFPredType::NumericalType, HDim, 1> deltah_;
329 protected:
330  Eigen::Matrix<typename KFPredType::NumericalType, HDim, KFPredType::xDim> H_;
331 protected:
334  Eigen::Matrix<typename KFPredType::NumericalType, HDim, HDim> R_;
335 
338 protected:
339  virtual void precompute(const KFPredType* _kf) = 0;
341 protected:
342  virtual void computeH(const KFPredType* _kf) = 0;
344 protected:
345  virtual void computeDeltah(const KFPredType* _kf,
346  const Eigen::Matrix<typename KFPredType::NumericalType, hDim, 1>& _zObs) = 0;
348 protected:
349  virtual void computeR(const KFPredType* _kf) = 0;
350 
351  template <typename KFPredTypeI, typename... KFUpdateType>
352  friend class KalmanFilterInterface;
353 };
354 template <typename KFPredType, int HDim>
356 
365 template <typename KFPredType, typename... KFUpdateType>
366 class KalmanFilterInterface : public KFPredType
367 {
368  // special class member functions
369 public:
370  KalmanFilterInterface(typename KFPredType::ParamsType& _params) : KFPredType(_params)
371  {
372  }
373 
374 public:
375  virtual ~KalmanFilterInterface() = default;
376 
377 public:
379 
380 public:
382 
383 public:
385 
386 public:
388 
408 public:
409  template <typename KFUpdateTypeI = typename std::tuple_element<0, std::tuple<KFUpdateType...>>::type,
410  int i = KFUpdateTypeI::hDim, typename std::enable_if<(i >= 0)>::type* = nullptr>
411  void update(const Eigen::Matrix<typename KFPredType::NumericalType, KFUpdateTypeI::hDim, 1>& _zObs)
412  {
413  auto& updI = std::get<KFUpdateTypeI>(updaters_);
414  updI.precompute(this);
415  updI.computeH(this);
416  updI.computeDeltah(this, _zObs);
417  updI.computeR(this);
418  KFPredType::update(updI.deltah(), updI.H(), updI.R());
419  }
420 
432 public:
433  template <typename KFUpdateTypeI = typename std::tuple_element<0, std::tuple<KFUpdateType...>>::type,
434  int i = KFUpdateTypeI::hDim, typename std::enable_if<(i == -1)>::type* = nullptr>
435  void update(const Eigen::Matrix<typename KFPredType::NumericalType, KFUpdateTypeI::hDim, 1>& _zObs)
436  {
437  auto& updI = std::get<KFUpdateTypeI>(updaters_);
438  updI.deltah_.resize(_zObs.rows(), 1);
439  updI.H_.resize(_zObs.rows(), this->x_.rows());
440  updI.R_.resize(_zObs.rows(), _zObs.rows());
441  update<KFUpdateTypeI, 0>(_zObs);
442  }
443 
444 private:
445  std::tuple<KFUpdateType...> updaters_;
446 };
447 }
448 
449 #endif // KALMAN_FILTER_H
static constexpr const int xDim
State vector size
Eigen::Matrix< NumType, XDim, 1 > f_
State transition function vector
Interface for simplified manipulation of specialized (Extended) Kalman Filter implementations.
NumType NumericalType
Numerical type.
void update(const Eigen::Matrix< typename KFPredType::NumericalType, KFUpdateTypeI::hDim, 1 > &_zObs)
Performs the update step defined by the template argument KFUpdateTypeI (defaults to first updater cl...
void update(const Eigen::Matrix< NumType, UpdateDim, 1 > &_deltah, const Eigen::Matrix< NumType, UpdateDim, XDim > &_C, const Eigen::Matrix< NumType, UpdateDim, UpdateDim > &_R)
Preforms Kalman update step.
Eigen::Matrix< typename KFPredType::NumericalType, HDim, KFPredType::xDim > H_
void predict(const double &_Ta)
Eigen::Matrix< typename KFPredType::NumericalType, HDim, HDim > R_
const Eigen::Matrix< typename KFPredType::NumericalType, HDim, 1 > & deltah() const
Const access of measurement function error vector h_.
Eigen::Matrix< NumType, XDim, 1 > x_
State vector
const ParamType & param() const
Parameters const acces.
void predict(const Eigen::Matrix< NumType, XDim, 1 > &_f, const Eigen::Matrix< NumType, XDim, XDim > &_Phi, const Eigen::Matrix< NumType, XDim, XDim > &_Q)
Preforms Kalman prediction step.
Eigen::Matrix< typename KFPredType::NumericalType, HDim, 1 > deltah_
Measurement function error vector.
const Eigen::Matrix< NumType, XDim, 1 > & x() const
State vector const access.
const Eigen::Matrix< typename KFPredType::NumericalType, HDim, KFPredType::xDim > & H() const
Const access of predicted measurement matrix H_.
Eigen::Matrix< NumType, XDim, XDim > Phi_
KalmanFilterPredictInterface(ParamType &_params)
void init(const Eigen::Matrix< NumType, XDim, 1 > &_x0)
Initializes the state vector and triggers initialization of Sigma_.
Interface for simplified manipulation of specialized (Extended) Kalman Filter updates. To be used with KalmanFilterPredictInterface.
Interface for simplified manipulation of specialized (Extended) Kalman Filter prediction part...
std::tuple< KFUpdateType... > updaters_
Container.
void init(const Eigen::Matrix< NumType, XDim, 1 > &_x0, const Eigen::Matrix< NumType, XDim, XDim > &_Sigma0)
Initializes state and covariance of filter.
Eigen::Matrix< NumType, XDim, XDim > Q_
to the state variables
const Eigen::Matrix< typename KFPredType::NumericalType, HDim, HDim > & R() const
Const access of measurement noise matrix R_.
const Eigen::Matrix< NumType, XDim, XDim > & Sigma() const
State covariance matrix const access.
ParamType & params_
Filter parameters.
Eigen::Matrix< NumType, XDim, XDim > Sigma_
State covariance matrix
Minimal cass implementing the Extended Kalman Filter algorithm.
KalmanFilterInterface(typename KFPredType::ParamsType &_params)
KalmanFilter()=default
void predict(const Eigen::Matrix< NumType, UDim, 1 > &_u, const double &_Ta)
Performs the prediction step.
KalmanFilter & operator=(const KalmanFilter &)=default
virtual ~KalmanFilter()=default


tuw_control
Author(s): George Todoran
autogenerated on Mon Jun 10 2019 15:27:21