UnscentedKalmanFilter.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package ROBDEKON
17  * @author Christoph Pohl ( christoph dot pohl at kit dot edu )
18  * @date 16.03.21
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  *
22  * Code adapted to C++ from: https://github.com/CAOR-MINES-ParisTech/ukfm
23  * See also:
24  * - https://arxiv.org/pdf/2002.00878.pdf
25  * - https://ieeexplore.ieee.org/document/8206066/
26  */
27 
28 #ifndef ROBDEKON_UNSCENTEDKALMANFILTER_H
29 #define ROBDEKON_UNSCENTEDKALMANFILTER_H
30 
31 #include <Eigen/Dense>
32 
33 #include "SystemModel.h"
34 
35 
36 template <typename SystemModelT>
38 {
39 public:
40  using FloatT = typename SystemModelT::FloatT;
41  static constexpr float eps = 10 * std::numeric_limits<float>::epsilon();
42  // variable dimensions:
43  using dim = typename SystemModelT::dim;
44  using StateT = typename SystemModelT::StateT;
45  using ControlT = typename SystemModelT::ControlT;
46  using ControlNoiseT =
47  typename SystemModelT::ControlNoiseT; // todo: correct name? Is it the same as ControlT?
48  using SigmaPointsT = typename SystemModelT::SigmaPointsT; // todo: rename
49 
55 
56 
58  ObsCovT R,
59  const AlphaT& alpha,
60  StateT state0,
61  StateCovT P0);
62  UnscentedKalmanFilter() = delete;
63 
64 private:
65  PropCovT Q_;
66  PropCovT chol_Q_;
67  ObsCovT R_;
68  AlphaT alpha_;
69  StateT state_;
70  StateCovT P_;
71 
72  struct Weights
73  {
74  struct W
75  {
76  W(size_t l, FloatT alpha);
77  float sqrt_d_lambda, wj, wm, w0;
78  };
79 
80  explicit Weights(AlphaT alpha);
81  W state, control, update;
82  };
83 
84  Weights weights_;
85  StateCovT calculateClosestPosSemidefMatrix(const StateCovT& cov);
86 
87 public:
88  void propagation(const ControlT& omega, FloatT dt);
89  void update(const ObsT& y);
90  const StateT& getCurrentState() const;
91  const StateCovT& getCurrentStateCovariance() const;
92 };
93 
94 extern template class UnscentedKalmanFilter<SystemModelSE3<float>>;
98 
99 
100 
101 template <typename SystemModelT>
103 {
104 public:
105  using FloatT = typename SystemModelT::FloatT;
106  static constexpr float eps = 10 * std::numeric_limits<float>::epsilon();
107  // variable dimensions:
108  using dim = typename SystemModelT::dim;
109  using StateT = typename SystemModelT::StateT;
110  using SigmaPointsT = typename SystemModelT::SigmaPointsT; // todo: rename
111 
116 
117 
119  ObsCovT R,
120  const AlphaT& alpha,
121  StateT state0,
122  StateCovT P0);
124 
125 private:
126  ObsCovT R_;
127  AlphaT alpha_;
128  StateT state_;
129  StateCovT P_;
130 
131  struct Weights
132  {
133  struct W
134  {
135  W(size_t l, FloatT alpha);
136  float sqrt_d_lambda, wj, wm, w0;
137  };
138 
139  explicit Weights(AlphaT alpha);
140  W state, update;
141  };
142 
143  Weights weights_;
144  StateCovT calculateClosestPosSemidefMatrix(const StateCovT& cov);
145 
146 public:
147  void propagation(FloatT dt);
148  void update(const ObsT& y);
149  const StateT& getCurrentState() const;
150  const StateCovT& getCurrentStateCovariance() const;
151 };
152 
157 
158 #endif //ROBDEKON_UNSCENTEDKALMANFILTER_H
UnscentedKalmanFilter::dim
typename SystemModelT::dim dim
Definition: UnscentedKalmanFilter.h:43
UnscentedKalmanFilter
Definition: UnscentedKalmanFilter.h:37
UnscentedKalmanFilter::UnscentedKalmanFilter
UnscentedKalmanFilter()=delete
UnscentedKalmanFilterWithoutControl::Weights::W::sqrt_d_lambda
float sqrt_d_lambda
Definition: UnscentedKalmanFilter.h:136
UnscentedKalmanFilterWithoutControl::getCurrentStateCovariance
const StateCovT & getCurrentStateCovariance() const
Definition: UnscentedKalmanFilter.cpp:369
UnscentedKalmanFilter::FloatT
typename SystemModelT::FloatT FloatT
Definition: UnscentedKalmanFilter.h:40
UnscentedKalmanFilterWithoutControl::propagation
void propagation(FloatT dt)
Definition: UnscentedKalmanFilter.cpp:242
UnscentedKalmanFilterWithoutControl::eps
static constexpr float eps
Definition: UnscentedKalmanFilter.h:106
UnscentedKalmanFilter::Weights::W::sqrt_d_lambda
float sqrt_d_lambda
Definition: UnscentedKalmanFilter.h:77
UnscentedKalmanFilterWithoutControl::dim
typename SystemModelT::dim dim
Definition: UnscentedKalmanFilter.h:108
SystemModelSE3::StateT
StateSE3< FloatT > StateT
Definition: SystemModel.h:82
UnscentedKalmanFilter::propagation
void propagation(const ControlT &omega, FloatT dt)
Definition: UnscentedKalmanFilter.cpp:53
UnscentedKalmanFilter::StateT
typename SystemModelT::StateT StateT
Definition: UnscentedKalmanFilter.h:44
SystemModelSE3::ControlT
ControlSE3< FloatT > ControlT
Definition: SystemModel.h:83
UnscentedKalmanFilter::Weights::W::wm
float wm
Definition: UnscentedKalmanFilter.h:77
UnscentedKalmanFilter::Weights::W::wj
float wj
Definition: UnscentedKalmanFilter.h:77
UnscentedKalmanFilterWithoutControl::FloatT
typename SystemModelT::FloatT FloatT
Definition: UnscentedKalmanFilter.h:105
UnscentedKalmanFilter::ControlNoiseT
typename SystemModelT::ControlNoiseT ControlNoiseT
Definition: UnscentedKalmanFilter.h:47
UnscentedKalmanFilter::Weights::W
Definition: UnscentedKalmanFilter.h:74
UnscentedKalmanFilterWithoutControl::getCurrentState
const StateT & getCurrentState() const
Definition: UnscentedKalmanFilter.cpp:362
UnscentedKalmanFilter::eps
static constexpr float eps
Definition: UnscentedKalmanFilter.h:41
UnscentedKalmanFilter::update
void update(const ObsT &y)
Definition: UnscentedKalmanFilter.cpp:121
UnscentedKalmanFilterWithoutControl::StateT
typename SystemModelT::StateT StateT
Definition: UnscentedKalmanFilter.h:109
UnscentedKalmanFilterWithoutControl::SigmaPointsT
typename SystemModelT::SigmaPointsT SigmaPointsT
Definition: UnscentedKalmanFilter.h:110
SystemModelSE3::FloatT
floatT FloatT
Definition: SystemModel.h:81
UnscentedKalmanFilterWithoutControl::Weights::W::W
W(size_t l, FloatT alpha)
Definition: UnscentedKalmanFilter.cpp:395
UnscentedKalmanFilter::getCurrentState
const StateT & getCurrentState() const
Definition: UnscentedKalmanFilter.cpp:170
UnscentedKalmanFilterWithoutControl
Definition: UnscentedKalmanFilter.h:102
SystemModelSE3::dim
Definition: SystemModel.h:77
UnscentedKalmanFilterWithoutControl::UnscentedKalmanFilterWithoutControl
UnscentedKalmanFilterWithoutControl()=delete
UnscentedKalmanFilterWithoutControl::Weights::W
Definition: UnscentedKalmanFilter.h:133
UnscentedKalmanFilter::getCurrentStateCovariance
const StateCovT & getCurrentStateCovariance() const
Definition: UnscentedKalmanFilter.cpp:177
UnscentedKalmanFilterWithoutControl::Weights::W::wm
float wm
Definition: UnscentedKalmanFilter.h:136
SystemModelSE3::SigmaPointsT
Eigen::Matrix< FloatT, dim::state, 1 > SigmaPointsT
Definition: SystemModel.h:86
UnscentedKalmanFilterWithoutControl::Weights::W::wj
float wj
Definition: UnscentedKalmanFilter.h:136
Eigen::Matrix< FloatT, dim::state, dim::state >
UnscentedKalmanFilter::Weights::W::w0
float w0
Definition: UnscentedKalmanFilter.h:77
UnscentedKalmanFilter::SigmaPointsT
typename SystemModelT::SigmaPointsT SigmaPointsT
Definition: UnscentedKalmanFilter.h:48
UnscentedKalmanFilterWithoutControl::update
void update(const ObsT &y)
Definition: UnscentedKalmanFilter.cpp:310
SystemModel.h
SystemModelSE3::ControlNoiseT
Eigen::Matrix< FloatT, dim::control, 1 > ControlNoiseT
Definition: SystemModel.h:85
UnscentedKalmanFilter::Weights::W::W
W(size_t l, FloatT alpha)
Definition: UnscentedKalmanFilter.cpp:203
control
This file is part of ArmarX.
dt
constexpr T dt
Definition: UnscentedKalmanFilterTest.cpp:42
UnscentedKalmanFilter::ControlT
typename SystemModelT::ControlT ControlT
Definition: UnscentedKalmanFilter.h:45
UnscentedKalmanFilterWithoutControl::Weights::W::w0
float w0
Definition: UnscentedKalmanFilter.h:136