KalmanFilter.cpp
Go to the documentation of this file.
1 /************************************************************************
2  * Copyright (C) 2012 Eindhoven University of Technology (TU/e). *
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 *
7  * are met: *
8  * *
9  * 1. Redistributions of source code must retain the above *
10  * copyright notice, this list of conditions and the following *
11  * disclaimer. *
12  * *
13  * 2. Redistributions in binary form must reproduce the above *
14  * copyright notice, this list of conditions and the following *
15  * disclaimer in the documentation and/or other materials *
16  * provided with the distribution. *
17  * *
18  * THIS SOFTWARE IS PROVIDED BY TU/e "AS IS" AND ANY EXPRESS OR *
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED *
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE *
21  * ARE DISCLAIMED. IN NO EVENT SHALL TU/e OR CONTRIBUTORS BE LIABLE *
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR *
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT *
24  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; *
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
26  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE *
28  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
29  * DAMAGE. *
30  * *
31  * The views and conclusions contained in the software and *
32  * documentation are those of the authors and should not be *
33  * interpreted as representing official policies, either expressed or *
34  * implied, of TU/e. *
35  ************************************************************************/
36 
37 #include "KalmanFilter.h"
38 
40 : meas_dim_(dim), state_dim_(dim * 2), G_(dim * 2), G_small_(dim), a_max_(0) {
41 }
42 
44 : meas_dim_(orig.meas_dim_), state_dim_(orig.state_dim_), G_(orig.G_), G_small_(orig.G_small_), H_(orig.H_), a_max_(orig.a_max_) {
45 }
46 
48 }
49 
51  return new KalmanFilter(*this);
52 }
53 
56 
57  G_.setMean(H_.t() * z.getMean());
58  G_.setCovariance(H_.t() * z.getCovariance() * H_);
59 
60  G_small_.setMean(z.getMean());;
62 }
63 
64 void KalmanFilter::propagate(const double& dt) {
65  if (a_max_ > 0) {
66  // const pbl::Vector& x = G_.getMean();
67  // const pbl::Matrix& P = G_.getCovariance();
68 
69  // set state transition matrix
71  for(int i = 0; i < meas_dim_; ++i) {
72  F(i, i + meas_dim_) = dt;
73  }
74 
75  pbl::Vector x = G_.getMean();
76 
77  for(int i = 0; i < meas_dim_; ++i) {
78  x(i) += x(i + meas_dim_) * dt;
79  }
80 
81  G_.setMean(x);
82 
83  // // set system noise
84  double q = a_max_ * a_max_ / 4;
85  double dt2 = dt * dt;
86  double dt4 = dt2 * dt2;
87 
88  pbl::Matrix P = F * G_.getCovariance() * F.t();
89  for(int i = 0; i < meas_dim_; ++i) {
90  P(i, i) += dt4 / 4 * q; // cov pos
91  P(i, i + meas_dim_) += dt4 / 4 * q; // cov pos~vel
92  P(i + meas_dim_, i + meas_dim_) += dt2 * q; // cov vel
93  }
94 
95  G_.setCovariance(P);
96 
99  }
100 }
101 
103  const pbl::Vector& x = G_.getMean();
104  const pbl::Matrix& P = G_.getCovariance();
105 
106  // determine innovation
107  pbl::Vector y = z.getMean() - H_ * x;
108 
109  // determine innovation covariance
110  pbl::Matrix S = H_ * P * H_.t() + z.getCovariance();
111 
112  // calculate optimal Kalman gain
113  pbl::Matrix K = P * H_.t() * inv(S);
114 
115  // update state
116  G_.setMean(x + K * y);
117 
118  // update state covariance
120 
121  G_small_.setMean(H_ * G_.getMean());
122  G_small_.setCovariance(H_ * G_.getCovariance() * H_.t());
123 }
124 
126  return z.getDensity(G_small_);
127 }
128 
130  return G_small_;
131 }
132 
134  return G_.getMean();
135 }
136 
138  return G_.getCovariance();
139 }
140 
142  a_max_ = a_max;
143 }
const pbl::Matrix & getStateCovariance() const
Returns the covariance of the Kalman state.
double getLikelihood(const pbl::Gaussian &z) const
Calculates the likelihood that the given measurement originates from the estimated Kalman state...
void setMaxAcceleration(double a_max)
Set the maximum expected acceleration. This parameter is used to determine the system noise in the pr...
KalmanFilter(int dim)
Constructs a Kalman filter with specified state dimensionality.
const pbl::Vector & getState() const
Returns the Kalman state (mean of the estimated Gaussian)
arma::vec Vector
Kalman filter with constant-velocity system model. The system noise is automatically calculated from ...
Definition: KalmanFilter.h:46
TFSIMD_FORCE_INLINE const tfScalar & y() const
virtual void init(const pbl::Gaussian &G)
Initializes the Kalman filter according to the given Gaussian, i.e., the initial state and state cova...
virtual ~KalmanFilter()
Destructor.
pbl::Matrix H_
Definition: KalmanFilter.h:141
const arma::mat & getCovariance() const
void setMean(const arma::vec &mu)
const arma::vec & getMean() const
pbl::Gaussian G_
Definition: KalmanFilter.h:135
static bool inv(Mat< eT > &out, const Base< eT, T1 > &X, const bool slow=false)
TFSIMD_FORCE_INLINE const tfScalar & x() const
virtual void propagate(const double &dt)
Propagates the state of the Kalman filter according to the (constant- velocity) system model...
double getDensity(const arma::vec &v, double max_mah_dist=0) const
const pbl::Gaussian & getGaussian() const
Returns the Kalman state and covariance as Gaussian.
void update(const pbl::Gaussian &z)
Updates the Kalman filter with the given (Gaussian) measurement.
arma_inline const Gen< mat::elem_type, gen_ones_diag > eye(const uword n_rows, const uword n_cols)
arma::mat Matrix
void setCovariance(const arma::mat &cov)
virtual KalmanFilter * clone() const
Clone operator.
pbl::Gaussian G_small_
Definition: KalmanFilter.h:138


wire_state_estimators
Author(s): Sjoerd van den Dries, Jos Elfring
autogenerated on Fri Apr 16 2021 02:32:34