Moved kalman computation into cpp to improve compile speed
This commit is contained in:
@@ -50,6 +50,7 @@ FILE(GLOB SOURCES
|
|||||||
mainTrilat.cpp
|
mainTrilat.cpp
|
||||||
mainProb.cpp
|
mainProb.cpp
|
||||||
Eval.cpp
|
Eval.cpp
|
||||||
|
FtmKalman.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
51
code/FtmKalman.cpp
Normal file
51
code/FtmKalman.cpp
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
#include "FtmKalman.h"
|
||||||
|
|
||||||
|
#include <eigen3/Eigen/Eigen>
|
||||||
|
|
||||||
|
float Kalman::predict(const Timestamp timestamp, const float measurment)
|
||||||
|
{
|
||||||
|
constexpr auto square = [](float x) { return x * x; };
|
||||||
|
const auto I = Eigen::Matrix2f::Identity();
|
||||||
|
|
||||||
|
Eigen::Map<Eigen::Matrix<float, 2, 1>> x(this->x);
|
||||||
|
Eigen::Map<Eigen::Matrix<float, 2, 2>> P(this->P);
|
||||||
|
|
||||||
|
// init kalman filter
|
||||||
|
if (isnan(lastTimestamp))
|
||||||
|
{
|
||||||
|
P << 10, 0,
|
||||||
|
0, 10; // Initial Uncertainty
|
||||||
|
|
||||||
|
x << measurment,
|
||||||
|
0;
|
||||||
|
}
|
||||||
|
|
||||||
|
const float dt = isnan(lastTimestamp) ? 1 : timestamp.sec() - lastTimestamp;
|
||||||
|
lastTimestamp = timestamp.sec();
|
||||||
|
|
||||||
|
Eigen::Matrix<float, 1, 2> H; // Measurement function
|
||||||
|
H << 1, 0;
|
||||||
|
|
||||||
|
Eigen::Matrix2f A; // Transition Matrix
|
||||||
|
A << 1, dt,
|
||||||
|
0, 1;
|
||||||
|
|
||||||
|
Eigen::Matrix2f Q; // Process Noise Covariance
|
||||||
|
Q << square(processNoiseDistance), 0,
|
||||||
|
0, square(processNoiseVelocity);
|
||||||
|
|
||||||
|
// Prediction
|
||||||
|
x = A * x; // Prädizierter Zustand aus Bisherigem und System
|
||||||
|
P = A * P*A.transpose() + Q; // Prädizieren der Kovarianz
|
||||||
|
|
||||||
|
// Correction
|
||||||
|
float Z = measurment;
|
||||||
|
auto y = Z - (H*x); // Innovation aus Messwertdifferenz
|
||||||
|
auto S = (H*P*H.transpose() + R); // Innovationskovarianz
|
||||||
|
auto K = P * H.transpose()* (1 / S); //Filter-Matrix (Kalman-Gain)
|
||||||
|
|
||||||
|
x = x + (K*y); // aktualisieren des Systemzustands
|
||||||
|
P = (I - (K*H))*P; // aktualisieren der Kovarianz
|
||||||
|
|
||||||
|
return x(0);
|
||||||
|
}
|
||||||
@@ -1,16 +1,13 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <eigen3/Eigen/Eigen>
|
|
||||||
|
|
||||||
#include <Indoor/data/Timestamp.h>
|
#include <Indoor/data/Timestamp.h>
|
||||||
|
|
||||||
|
|
||||||
struct Kalman
|
struct Kalman
|
||||||
{
|
{
|
||||||
int nucID = 0; // debug only
|
int nucID = 0; // debug only
|
||||||
|
|
||||||
Eigen::Matrix<float, 2, 1> x; // predicted state
|
float x[2]; // predicted state
|
||||||
Eigen::Matrix<float, 2, 2> P; // Covariance
|
float P[4]; // Covariance
|
||||||
|
|
||||||
float R = 30; // measurement noise covariance
|
float R = 30; // measurement noise covariance
|
||||||
float processNoiseDistance; // stdDev
|
float processNoiseDistance; // stdDev
|
||||||
@@ -28,50 +25,7 @@ struct Kalman
|
|||||||
: nucID(nucID), R(measStdDev*measStdDev), processNoiseDistance(processNoiseDistance), processNoiseVelocity(processNoiseVelocity)
|
: nucID(nucID), R(measStdDev*measStdDev), processNoiseDistance(processNoiseDistance), processNoiseVelocity(processNoiseVelocity)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
float predict(const Timestamp timestamp, const float measurment)
|
float predict(const Timestamp timestamp, const float measurment);
|
||||||
{
|
|
||||||
constexpr auto square = [](float x) { return x * x; };
|
|
||||||
const auto I = Eigen::Matrix2f::Identity();
|
|
||||||
|
|
||||||
// init kalman filter
|
|
||||||
if (isnan(lastTimestamp))
|
|
||||||
{
|
|
||||||
P << 10, 0,
|
|
||||||
0, 10; // Initial Uncertainty
|
|
||||||
|
|
||||||
x << measurment,
|
|
||||||
0;
|
|
||||||
}
|
|
||||||
|
|
||||||
const float dt = isnan(lastTimestamp) ? 1 : timestamp.sec() - lastTimestamp;
|
|
||||||
lastTimestamp = timestamp.sec();
|
|
||||||
|
|
||||||
Eigen::Matrix<float, 1, 2> H; // Measurement function
|
|
||||||
H << 1, 0;
|
|
||||||
|
|
||||||
Eigen::Matrix2f A; // Transition Matrix
|
|
||||||
A << 1, dt,
|
|
||||||
0, 1;
|
|
||||||
|
|
||||||
Eigen::Matrix2f Q; // Process Noise Covariance
|
|
||||||
Q << square(processNoiseDistance), 0,
|
|
||||||
0, square(processNoiseVelocity);
|
|
||||||
|
|
||||||
// Prediction
|
|
||||||
x = A * x; // Prädizierter Zustand aus Bisherigem und System
|
|
||||||
P = A * P*A.transpose()+Q; // Prädizieren der Kovarianz
|
|
||||||
|
|
||||||
// Correction
|
|
||||||
float Z = measurment;
|
|
||||||
auto y = Z - (H*x); // Innovation aus Messwertdifferenz
|
|
||||||
auto S = (H*P*H.transpose()+R); // Innovationskovarianz
|
|
||||||
auto K = P * H.transpose()* (1/S); //Filter-Matrix (Kalman-Gain)
|
|
||||||
|
|
||||||
x = x + (K*y); // aktualisieren des Systemzustands
|
|
||||||
P = (I - (K*H))*P; // aktualisieren der Kovarianz
|
|
||||||
|
|
||||||
return x(0);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user