UWB-Tracking
Loading...
Searching...
No Matches
extended-kalman.h
Go to the documentation of this file.
1#pragma once
2
11#include <ArduinoEigen.h>
12#include <EEPROM.h>
13#include <datatypes.h>
14
15using namespace Eigen;
16
17
18namespace ekf{
19
20#define DIM_X 3
21#define DIM_Z NUM_LANDMARKS
22
28{
29public:
34 EKF_Filter();
35
40
45 Matrix<double, DIM_X, 1>& vecX() { return m_vecX; }
46
51 const Matrix<double, DIM_X, 1>& vecX() const { return m_vecX; }
52
57 Matrix<double, DIM_X, DIM_X>& matP() { return m_matP; }
58
63 const Matrix<double, DIM_X, DIM_X>& matP() const { return m_matP; }
64
70 void predict(const Matrix<double, DIM_X, DIM_X>& matF, const Matrix<double, DIM_X, DIM_X>& matQ)
71 {
72 m_vecX = matF * m_vecX;
73 m_matP = matF * m_matP * matF.transpose() + matQ;
74 }
75
82 void correct(const Matrix<double, DIM_Z, 1>& vecZ, const Matrix<double, DIM_Z, DIM_Z>& matR, const Matrix<double, DIM_Z, DIM_X>& matH)
83 {
84 const Matrix<double, DIM_X, DIM_X> matI = Matrix<double, DIM_X, DIM_X>::Identity(); // Identity matrix
85 const Matrix<double, DIM_Z, DIM_Z> matSk = matH * m_matP * matH.transpose() + matR; // Innovation covariance
86 const Matrix<double, DIM_X, DIM_Z> matKk = m_matP * matH.transpose() * matSk.inverse(); // Kalman Gain
87
88 m_vecX = m_vecX + matKk * (vecZ - (matH * m_vecX));
89 m_matP = (matI - matKk * matH) * m_matP;
90 }
91
99 template<typename PredictionModelCallback>
100 void predictEkf(PredictionModelCallback predictionModel, const Matrix<double, DIM_X, DIM_X>& matJacobF, const Matrix<double, DIM_X, DIM_X>& matQ)
101 {
103 m_matP = matJacobF * m_matP * matJacobF.transpose() + matQ;
104 }
105
114 template<typename MeasurementModelCallback>
115 void correctEkf(MeasurementModelCallback measurementModel, const Matrix<double, DIM_Z, 1>& vecZ, const Matrix<double, DIM_Z, DIM_Z>& matR, const Matrix<double, DIM_Z, DIM_X>& matJcobH)
116 {
117 const Matrix<double, DIM_X, DIM_X> matI = Matrix<double, DIM_X, DIM_X>::Identity(); // Identity matrix
118 const Matrix<double, DIM_Z, DIM_Z> matSk = matJcobH * m_matP * matJcobH.transpose() + matR; // Innovation covariance
119 const Matrix<double, DIM_X, DIM_Z> matKk = m_matP * matJcobH.transpose() * matSk.inverse(); // Kalman Gain
120
121 m_vecX = m_vecX + matKk * (vecZ - measurementModel(m_vecX));
122 m_matP = (matI - matKk * matJcobH) * m_matP;
123 }
124
125private:
126 Matrix<double, DIM_X, 1> m_vecX{ Matrix<double, DIM_X, 1>::Zero() };
127 Matrix<double, DIM_X, DIM_X> m_matP{ Matrix<double, DIM_X, DIM_X>::Zero() };
128
133};
134
135static uint32_t lastTime = millis();
136static Matrix<double, DIM_X, 1> x_kminus1 = (Eigen::Matrix<double, DIM_X, 1>() << 0.0, 0.0, 0.0).finished();
137
138static Matrix<double, NUM_LANDMARKS, 3> landmarkPositions { Matrix<double, NUM_LANDMARKS, 3>::Zero() };
139
145Matrix<double, DIM_X, 1> predictionModel(const Matrix<double, DIM_X, 1>& currentState);
146
152Matrix<double, DIM_Z, DIM_X> calculateJacobianMatrix(const Matrix<double, DIM_X, 1>& vecX);
153
159Matrix<double, DIM_Z, 1> calculateMeasurement(const Matrix<double, DIM_X, 1>& currentPosition);
160}; //namespace ekf
Extended Kalman Filter (EKF) class for state estimation.
Definition: extended-kalman.h:28
void predictEkf(PredictionModelCallback predictionModel, const Matrix< double, DIM_X, DIM_X > &matJacobF, const Matrix< double, DIM_X, DIM_X > &matQ)
Perform the prediction step of the EKF using a user-defined prediction model.
Definition: extended-kalman.h:100
void correctEkf(MeasurementModelCallback measurementModel, const Matrix< double, DIM_Z, 1 > &vecZ, const Matrix< double, DIM_Z, DIM_Z > &matR, const Matrix< double, DIM_Z, DIM_X > &matJcobH)
Perform the correction step of the EKF using a user-defined measurement model.
Definition: extended-kalman.h:115
Matrix< double, DIM_X, DIM_X > m_matP
estimated state vector
Definition: extended-kalman.h:127
void predict(const Matrix< double, DIM_X, DIM_X > &matF, const Matrix< double, DIM_X, DIM_X > &matQ)
Perform the prediction step of the EKF.
Definition: extended-kalman.h:70
Matrix< double, DIM_X, 1 > m_vecX
Definition: extended-kalman.h:126
~EKF_Filter()
Destructor of Kalman-Filter Object, cleanup recources.
Definition: extended-kalman.h:39
const Matrix< double, DIM_X, 1 > & vecX() const
Get the estimated state vector (const version).
Definition: extended-kalman.h:51
void correct(const Matrix< double, DIM_Z, 1 > &vecZ, const Matrix< double, DIM_Z, DIM_Z > &matR, const Matrix< double, DIM_Z, DIM_X > &matH)
Perform the correction step of the EKF.
Definition: extended-kalman.h:82
void read_landmarks_from_eeprom(void)
state covariance matrix
Definition: extended-kalman.cpp:11
Matrix< double, DIM_X, 1 > & vecX()
Get the estimated state vector.
Definition: extended-kalman.h:45
const Matrix< double, DIM_X, DIM_X > & matP() const
Get the state covariance matrix (const version).
Definition: extended-kalman.h:63
EKF_Filter()
Default constructor for the EKF_Filter class. Initializes the EKF and reads the Anchor positions from...
Definition: extended-kalman.cpp:6
Matrix< double, DIM_X, DIM_X > & matP()
Get the state covariance matrix.
Definition: extended-kalman.h:57
Defines custom data types used in the UWB device code.
Definition: extended-kalman.h:18
static Matrix< double, NUM_LANDMARKS, 3 > landmarkPositions
Definition: extended-kalman.h:138
Matrix< double, DIM_X, 1 > predictionModel(const Matrix< double, DIM_X, 1 > &currentState)
Prediction model for the Extended Kalman Filter (EKF).
Definition: extended-kalman.cpp:22
Matrix< double, DIM_Z, DIM_X > calculateJacobianMatrix(const Matrix< double, DIM_X, 1 > &vecX)
Calculate the Jacobian matrix for the measurement model.
Definition: extended-kalman.cpp:39
static uint32_t lastTime
Definition: extended-kalman.h:135
static Matrix< double, DIM_X, 1 > x_kminus1
Definition: extended-kalman.h:136
Matrix< double, DIM_Z, 1 > calculateMeasurement(const Matrix< double, DIM_X, 1 > &currentPosition)
Calculate the measurement vector.
Definition: extended-kalman.cpp:58