Robotics Library  0.7.0
Kalman.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2009, Markus Rickert
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 //
14 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 // POSSIBILITY OF SUCH DAMAGE.
25 //
26 
27 #ifndef RL_MATH_KALMAN_H
28 #define RL_MATH_KALMAN_H
29 
30 #define EIGEN_MATRIXBASE_PLUGIN <rl/math/MatrixBaseAddons.h>
31 #define EIGEN_QUATERNIONBASE_PLUGIN <rl/math/QuaternionBaseAddons.h>
32 #define EIGEN_TRANSFORM_PLUGIN <rl/math/TransformAddons.h>
33 
34 #include <Eigen/Core>
35 #include <Eigen/LU>
36 
37 namespace rl
38 {
39  namespace math
40  {
50  template<typename Scalar>
51  class Kalman
52  {
53  public:
54  typedef Scalar ScalarType;
55 
56  typedef typename ::Eigen::Matrix<Scalar, ::Eigen::Dynamic, ::Eigen::Dynamic> MatrixType;
57 
58  typedef typename ::Eigen::Matrix<Scalar, ::Eigen::Dynamic, 1> VectorType;
59 
60  Kalman(const ::std::size_t& states, const ::std::size_t& observations, const ::std::size_t& controls = 0) :
61  A(MatrixType::Identity(states, states)),
62  B(MatrixType::Zero(states, controls)),
63  H(MatrixType::Zero(observations, states)),
64  PPosteriori(MatrixType::Zero(states, states)),
65  PPriori(MatrixType::Zero(states, states)),
66  Q(MatrixType::Identity(states, states)),
67  R(MatrixType::Identity(observations, observations)),
68  xPosteriori(VectorType::Zero(states)),
69  xPriori(VectorType::Zero(states))
70  {
71  }
72 
73  virtual ~Kalman()
74  {
75  }
76 
78  {
79  return this->B;
80  }
81 
82  const MatrixType& controlModel() const
83  {
84  return this->B;
85  }
86 
99  VectorType correct(const VectorType& z)
100  {
101  assert(z.size() == xPriori.size());
102 
103  // \f[ \matr{K}_{k} = \matr{P}^{-}_{k} \matr{H}^{\mathrm{T}} \left( \matr{H} \matr{P}^{-}_{k} \matr{H}^{\mathrm{T}} + \matr{R} \right)^{-1} \f]
104  MatrixType K = PPriori * H.transpose() * (H * PPriori * H.transpose() + R).inverse();
105  // \f[ \hat{\vec{x}}_{k} = \hat{\vec{x}}^{-}_{k} + \matr{K}_{k} \left( \vec{z}_{k} - \matr{H} \hat{\vec{x}}^{-}_{k} \right) \f]
106  xPosteriori = xPriori + K * (z - H * xPriori);
107  // \f[ \matr{P}_{k} = \left( \matr{1} - \matr{K}_{k} \matr{H} \right) \matr{P}^{-}_{k} \f]
108  PPosteriori = (MatrixType::Identity(K.rows(), H.cols()) - K * H) * PPriori;
109  return xPosteriori;
110  }
111 
113  {
114  return this->PPosteriori;
115  }
116 
117  const MatrixType& errorCovariancePosteriori() const
118  {
119  return this->PPosteriori;
120  }
121 
122  MatrixType& errorCovariancePriori()
123  {
124  return this->PPriori;
125  }
126 
127  const MatrixType& errorCovariancePriori() const
128  {
129  return this->PPriori;
130  }
131 
132  MatrixType& measurementModel()
133  {
134  return this->H;
135  }
136 
137  const MatrixType& measurementModel() const
138  {
139  return this->H;
140  }
141 
143  {
144  return this->R;
145  }
146 
147  const MatrixType& measurementNoiseCovariance() const
148  {
149  return this->R;
150  }
151 
160  VectorType predict()
161  {
162  // \f[ \hat{\vec{x}}^{-}_{k} = \matr{A} \hat{\vec{x}}_{k - 1} \f]
163  xPriori = A * xPosteriori;
164  // \f[ \matr{P}^{-}_{k} = \matr{A} \matr{P}_{k - 1} \matr{A}^{\mathrm{T}} + \matr{Q} \f]
165  PPriori = A * PPosteriori * A.transpose() + Q;
166  return xPriori;
167  }
168 
180  {
181  assert(u.size() == B.cols());
182 
183  // \f[ \hat{\vec{x}}^{-}_{k} = \matr{A} \hat{\vec{x}}_{k - 1} + \matr{B} \vec{u}_{k - 1} \f]
184  xPriori = A * xPosteriori + B * u;
185  // \matr{P}^{-}_{k} = \matr{A} \matr{P}_{k - 1} \matr{A}^{\mathrm{T}} + \matr{Q} \f]
186  PPriori = A * PPosteriori * A.transpose() + Q;
187  return xPriori;
188  }
189 
191  {
192  return this->Q;
193  }
194 
195  const MatrixType& processNoiseCovariance() const
196  {
197  return this->Q;
198  }
199 
200  VectorType& statePosteriori()
201  {
202  return this->xPosteriori;
203  }
204 
205  const VectorType& statePosteriori() const
206  {
207  return this->xPosteriori;
208  }
209 
210  VectorType& statePriori()
211  {
212  return this->xPriori;
213  }
214 
215  const VectorType& statePriori() const
216  {
217  return this->xPriori;
218  }
219 
220  MatrixType& stateTransitionModel()
221  {
222  return this->A;
223  }
224 
225  const MatrixType& stateTransitionModel() const
226  {
227  return this->A;
228  }
229 
230  protected:
231 
232  private:
234  MatrixType A;
235 
238 
241 
244 
247 
250 
253 
256 
259  };
260  }
261 }
262 
263 #endif // RL_MATH_KALMAN_H
rl::math::Kalman::measurementModel
const MatrixType & measurementModel() const
Definition: Kalman.h:137
rl::math::Kalman::xPriori
VectorType xPriori
A priori state estimate .
Definition: Kalman.h:258
rl::math::Kalman::errorCovariancePosteriori
const MatrixType & errorCovariancePosteriori() const
Definition: Kalman.h:117
rl::math::Kalman::processNoiseCovariance
const MatrixType & processNoiseCovariance() const
Definition: Kalman.h:195
rl::math::Kalman::MatrixType
::Eigen::Matrix< Scalar, ::Eigen::Dynamic, ::Eigen::Dynamic > MatrixType
Definition: Kalman.h:56
rl::math::Kalman::A
MatrixType A
relates the state at the previous time step to the state at the current step .
Definition: Kalman.h:234
rl::math::Kalman::ScalarType
Scalar ScalarType
Definition: Kalman.h:54
rl::math::Kalman::PPriori
MatrixType PPriori
A priori estimate error covariance .
Definition: Kalman.h:246
rl::math::Kalman::H
MatrixType H
relates the state to the measurement .
Definition: Kalman.h:240
rl::math::Kalman::Kalman
Kalman(const ::std::size_t &states, const ::std::size_t &observations, const ::std::size_t &controls=0)
Definition: Kalman.h:60
rl::math::Kalman::statePriori
VectorType & statePriori()
Definition: Kalman.h:210
rl::math::Kalman::measurementModel
MatrixType & measurementModel()
Definition: Kalman.h:132
rl::math::Kalman::PPosteriori
MatrixType PPosteriori
A posteriori estimate error covariance .
Definition: Kalman.h:243
rl::math::Kalman::correct
VectorType correct(const VectorType &z)
Measurement update ("correct").
Definition: Kalman.h:99
rl::math::Kalman::errorCovariancePosteriori
MatrixType & errorCovariancePosteriori()
Definition: Kalman.h:112
rl::math::Kalman::B
MatrixType B
relates the control input to the state .
Definition: Kalman.h:237
rl::math::Kalman::statePriori
const VectorType & statePriori() const
Definition: Kalman.h:215
rl::math::Kalman::statePosteriori
const VectorType & statePosteriori() const
Definition: Kalman.h:205
rl::math::Kalman::Q
MatrixType Q
Process noise covariance .
Definition: Kalman.h:249
rl::math::Kalman::errorCovariancePriori
const MatrixType & errorCovariancePriori() const
Definition: Kalman.h:127
rl::math::Kalman::errorCovariancePriori
MatrixType & errorCovariancePriori()
Definition: Kalman.h:122
rl::math::Kalman::measurementNoiseCovariance
MatrixType & measurementNoiseCovariance()
Definition: Kalman.h:142
rl::math::Kalman::controlModel
MatrixType & controlModel()
Definition: Kalman.h:77
rl::math::Kalman::controlModel
const MatrixType & controlModel() const
Definition: Kalman.h:82
rl::math::Kalman::predict
VectorType predict()
Time update ("predict") without control input.
Definition: Kalman.h:160
rl::math::Kalman::predict
VectorType predict(const VectorType &u)
Time update ("predict") with control input.
Definition: Kalman.h:179
rl::math::Kalman::statePosteriori
VectorType & statePosteriori()
Definition: Kalman.h:200
rl::math::Kalman::processNoiseCovariance
MatrixType & processNoiseCovariance()
Definition: Kalman.h:190
rl::math::Kalman::xPosteriori
VectorType xPosteriori
A posteriori state estimate .
Definition: Kalman.h:255
rl::math::Kalman::VectorType
::Eigen::Matrix< Scalar, ::Eigen::Dynamic, 1 > VectorType
Definition: Kalman.h:58
rl::math::Kalman
Kalman filter.
Definition: Kalman.h:52
rl::math::Kalman::stateTransitionModel
MatrixType & stateTransitionModel()
Definition: Kalman.h:220
rl::math::Kalman::stateTransitionModel
const MatrixType & stateTransitionModel() const
Definition: Kalman.h:225
rl::math::Kalman::~Kalman
virtual ~Kalman()
Definition: Kalman.h:73
rl::math::Kalman::R
MatrixType R
Measurement error covariance .
Definition: Kalman.h:252
rl::math::Kalman::measurementNoiseCovariance
const MatrixType & measurementNoiseCovariance() const
Definition: Kalman.h:147
rl
Robotics Library.
Definition: AnalogInput.cpp:30