#include <mrs_lib/dkf.h>
#include <random>
#include <ros/ros.h>
{
const int n_states = 6;
const int n_inputs = 0;
using dkf_t = DKF<n_states, n_inputs>;
}
using pt3_t = dkf_t::pt3_t;
using vec3_t = dkf_t::vec3_t;
using mat3_t = Eigen::Matrix3d;
enum x_pos
{
x_x = 0,
x_y = 1,
x_z = 2,
x_dx = 3,
x_dy = 4,
x_dz = 5,
};
template <int rows>
Eigen::Matrix<double, rows, 1> normal_randmat(const Eigen::Matrix<double, rows, rows>& cov)
{
static std::random_device rd{};
static std::mt19937 gen{rd()};
static std::normal_distribution<> d{0,1};
Eigen::Matrix<double, rows, 1> ret;
for (int row = 0; row < rows; row++)
ret(row, 0) = d(gen);
return cov*ret;
}
A_t A;
x_t tra_model_f(const x_t& x)
{
return A*x;
}
{
pt3_t line_origin;
vec3_t line_direction;
double line_variance;
};
obs_t observation(
const x_t& x,
const double meas_std)
{
const pt3_t target = x.head<3>();
const vec3_t meas_noise = normal_randmat<3>(meas_std*meas_std*mat3_t::Identity());
const pt3_t observer = target + 100.0*pt3_t::Random();
ret.
line_direction = (target - observer).normalized();
ret.
line_origin = observer + meas_noise;
ret.
line_variance = meas_std*meas_std;
return ret;
}
int main()
{
const double dt = 1.0;
const double sigma_a = 0.03;
const double sigma_R = 0.1;
A = A_t::Identity();
A.block<3, 3>(0, 3) = dt*mat3_t::Identity();
const mat3_t Q_a = sigma_a*sigma_a*mat3_t::Identity();
Eigen::Matrix<double, 6, 3> B_Q;
B_Q.block<3, 3>(0, 0) = 0.5*dt*dt*mat3_t::Identity();
B_Q.block<3, 3>(3, 0) = dt*mat3_t::Identity();
const Q_t Q = B_Q * Q_a * B_Q.transpose();
x_t x_gt = 100.0*x_t::Random();
const P_t P_tmp = P_t::Random();
const P_t P0 = 10.0*P_tmp*P_tmp.transpose();
const x_t x0 = x_gt + normal_randmat(P0);
const statecov_t sc0({x0, P0});
dkf_t kf(A, B_t::Zero());
const int n_its = 1e4;
statecov_t sc_est = sc0;
std::cout << "A: " << A << "\n";
std::cout << "Q: " << Q << "\n";
std::cout << "x0_gt: " << x_gt << "\n";
std::cout << "x0_est: " << sc_est.x << "\n";
for (int it = 0; it < n_its; it++)
{
const u_t u = u_t::Random();
x_gt = tra_model_f(x_gt) + normal_randmat(Q);
const obs_t obs = observation(x_gt, sigma_R);
try
{
sc_est = kf.predict(sc_est, u, Q, dt);
sc_est = kf.correctLine(sc_est, obs.line_origin, obs.line_direction, obs.line_variance);
}
catch (const std::exception& e)
{
ROS_ERROR("KF failed: %s", e.what());
}
}
const x_t error = x_gt - sc_est.x;
const double RMSE = (error).norm();
const double maha = std::sqrt( error.transpose() * sc_est.P.inverse() * error );
std::cout << "Current KF estimation error is: " << RMSE << std::endl;
std::cout << "Current KF mahalanobis error is: " << maha << std::endl;
std::cout << "Current ground-truth state is: " << x_gt.transpose() << std::endl;
std::cout << "Current KF estimated state is: " << sc_est.x.transpose() << std::endl;
std::cout << "Current KF state covariance is:" << std::endl << sc_est.P << std::endl;
return 0;
}