kalman-cpp
Implementation of Kalman Filter in C++
ekf2.h
Go to the documentation of this file.
1 
21 #ifndef EKF2_H
22 #define EKF2_H
23 
24 #define _USE_MATH_DEFINES
25 #include <math.h>
26 
27 #include <assert.h>
28 #include <armadillo>
29 
30 using namespace std;
31 using namespace arma;
32 
37 class EKF2 {
38 public:
42  EKF2();
43 
47  ~EKF2();
48 
56  void InitSystem(int n_states, int n_outputs, const mat& Q, const mat& R);
62  virtual colvec f(const colvec &x, const colvec &u);
63 
68  virtual colvec h(const colvec &x);
69 
76  void InitSystemState(const colvec& x0);
77 
84  void InitSystemStateCovariance(const mat& P0);
85 
91  void EKalmanf(const colvec& u);
92 
101  void EKalmanf(const colvec& z, const colvec& u);
102 
107  colvec* GetCurrentState();
108 
114  colvec* GetCurrentOutput();
115 
121  colvec* GetCurrentEstimatedState();
122 
127  colvec* GetCurrentEstimatedOutput();
128 
129 private:
136  mat CalcFx(const colvec &x, const colvec &u);
137 
143  mat CalcHx(const colvec &x);
144 
152  mat CalcFxx(const colvec &x, const colvec &u, const int i);
153 
161  mat CalcHxx(const colvec &x, const int i);
162 
169  colvec e(const int i, const int max_len);
170 
171  mat Q_;
172  mat R_;
173  colvec v_;
174  colvec w_;
175 
176  mat sqrt_Q_;
177  mat sqrt_R_;
178 
179  colvec x_m_;
180  colvec x_p_;
181 
182  mat P_p_;
183  mat P_m_;
184 
185  colvec z_m_;
186 
187  double epsilon_;
188 
189 protected:
190 
191  int nStates_;
192  int nOutputs_;
193 
194  colvec x_;
195  colvec z_;
196 };
197 
198 
199 #endif
Implemetation of the extended Kalman filter. This class needs to be derived.
Definition: ekf2.h:37
~EKF2()
Destructur, nothing happens here.
Definition: ekf2.cpp:15
int nStates_
Number of the states.
Definition: ekf2.h:191
colvec x_
State vector.
Definition: ekf2.h:194
void InitSystemState(const colvec &x0)
Initialize the system states. Must be called after InitSystem. If not called, system state is initial...
Definition: ekf2.cpp:169
colvec * GetCurrentOutput()
Get current simulated true output. This is analogous to the measurements.
Definition: ekf2.cpp:268
void EKalmanf(const colvec &u)
Do the extended Kalman iteration step-by-step while simulating the system. Simulating the system is d...
Definition: ekf2.cpp:182
void InitSystemStateCovariance(const mat &P0)
Initialize the system state covariance. Must be called after InitSystem. If not called,...
Definition: ekf2.cpp:176
EKF2()
Constructor, nothing happens here.
Definition: ekf2.cpp:10
virtual colvec h(const colvec &x)
Define the output model of your system.
Definition: ekf2.cpp:65
colvec * GetCurrentEstimatedOutput()
Get current estimated output.
Definition: ekf2.cpp:279
colvec * GetCurrentEstimatedState()
Get current estimated state. This is analogous to the filtered measurements.
Definition: ekf2.cpp:273
colvec z_
Output matrix.
Definition: ekf2.h:195
void InitSystem(int n_states, int n_outputs, const mat &Q, const mat &R)
Tell me how many states and outputs you have!
Definition: ekf2.cpp:20
int nOutputs_
Number of outputs.
Definition: ekf2.h:192
colvec * GetCurrentState()
Get current simulated true state.
Definition: ekf2.cpp:263
virtual colvec f(const colvec &x, const colvec &u)
Define model of your system.
Definition: ekf2.cpp:58