Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExtendedKalman.cpp
1 #include "ExtendedKalman.hh"
2 
3 using namespace std;
4 using namespace BIAS;
5 
6 ExtendedKalman::ExtendedKalman()
7 {
8  //defaults indicating nothing set
9  systemStateDim_ = 0;
10  measurementDim_ = 0;
11 }
12 
13 int ExtendedKalman::Predict(const Vector<double>& control)
14 {
15 #ifdef BIAS_DEBUG
16  if (systemStateDim_ == 0) {
17  BIASERR("Either state- or measurement-dimension is zero!!!");
18  return -1;
19  }
20 #endif //BIAS_DEBUG
21 
22  //project the state ahead
23  StateTransition(posterioState_, control, priorState_);
24 
25  //project cov. ahead
26  covariance_ = A_ * covariance_ * AT_ + W_ * Q_ * WT_;
27 
28  return 0;
29 }
30 
31 int ExtendedKalman::Update(const Vector<double>& measurement)
32 {
33  measurementDim_ = measurement.size();
34 
35 #ifdef BIAS_DEBUG
36  if ((systemStateDim_ == 0)||(measurementDim_ == 0)) {
37  BIASERR("Either state- or measurement-dimension is zero!!!");
38  return -1;
39  }
40 #endif //BIAS_DEBUG
41 
42  Identity_.clear();
43  Identity_ = Matrix<double>(systemStateDim_, systemStateDim_);
44  Identity_.SetIdentity();
45 
46  //compute Kalman gain
47  {
48  //create temporary matrix for inversion of (H_*covariance_*HT_ + V_*R_*VT_)
49  Matrix<double> invTmp(measurementDim_, measurementDim_);//systemStateDim_, measurementDim_);
50 
51  invTmp = H_ * covariance_ * HT_ + V_ * R_ * VT_;
52  invTmp = svd_.Invert(invTmp);
53 
54  K_ = covariance_ * HT_ * invTmp;
55  }
56 
57  //update state estimate
58  {
59  Vector<double> measurePred(measurementDim_);
60  //call virtual measurement-function
61  MeasurementFunction(priorState_, measurePred);
62  //correct state with measurement
63  lastKalmanUpdate_ = K_ * (measurement - measurePred);
64  posterioState_ = priorState_ + lastKalmanUpdate_;
65  }
66 
67  //update error-covariance
68  {
69  covariance_ = (Identity_ - K_ * H_) * covariance_;
70  }
71 
72  return 0;
73 }
void SetIdentity()
Converts matrix to identity matrix.
Definition: Matrix.cpp:383
Subscript size() const
Definition: vec.h:262