41 #ifndef IIROB_FILTERS_KALMAN_FILTER_H 42 #define IIROB_FILTERS_KALMAN_FILTER_H 45 #include <Eigen/Dense> 46 #include <iirob_filters/KalmanFilterParameters.h> 59 bool configure(
const std::vector<T>& init_state_vector,
const std::string param_namespace =
"");
60 bool configure(
const std::string& param_namespace);
61 virtual bool update(
const std::vector<T>& data_in, std::vector<T>& data_out);
62 bool update(
const std::vector<T>& data_in, std::vector<T>& data_out,
const double& delta_t,
bool update_Q_matrix =
false,
const Eigen::MatrixXd& R_loc = Eigen::MatrixXd::Zero(1,1));
63 bool predict(std::vector<T>& data_out);
71 bool likelihood(
const std::vector<T>& data_in,
double& data_out);
75 Eigen::MatrixXd
A,
At,
C,
Q,
Q_coeff,
Q_exponent,
R,
P,
K,
P0,
gatingMatrix;
99 bool fromStdVectorToEigenMatrix(std::vector<double>& in, Eigen::MatrixXd& out,
int rows,
int columns, std::string matrix_name);
102 double fac(
double x);
104 bool getParams(iirob_filters::KalmanFilterParameters&,
const std::string&);
109 template <
typename T>
121 template <
typename T>
127 template <
typename T>
129 int columns, std::string matrix_name) {
130 if (in.size() != rows * columns || in.size() == 0) {
ROS_ERROR(
"%s is not valid!", matrix_name.c_str());
return false; }
131 out.resize(rows, columns);
132 std::vector<double>::iterator it = in.begin();
133 for (
int i = 0; i < rows; ++i)
135 for (
int j = 0; j < columns; ++j)
144 template <
typename T>
150 template <
typename T>
152 if (in.size() != rows) {
ROS_ERROR(
"%s vector is not valid!", vector_name.c_str());
return false; }
154 for (
int i = 0; i < rows; ++i)
161 template <
typename T>
164 if (!
configure(param_namespace)) {
return false; }
166 if (init_state_vector.size() !=
n) {
ROS_ERROR(
"Kalman: Not valid init state vector!");
return false; }
168 for (
int i = 0; i < init_state_vector.size(); i++)
170 x_hat(i) = init_state_vector[i];
175 template <
typename T>
180 template <
typename T>
182 std::vector<double> temp;
184 if (param_namespace !=
"")
186 full_namespace += param_namespace;
187 iirob_filters::KalmanFilterParameters dynamic_params{full_namespace};
188 dynamic_params.fromParamServer();
189 if (!
getParams(dynamic_params, full_namespace)) {
return false; }
193 full_namespace +=
"KalmanFilter";
194 iirob_filters::KalmanFilterParameters default_namespace_params{full_namespace};
195 default_namespace_params.fromParamServer();
196 if (!
getParams(default_namespace_params, full_namespace)) {
return false; }
199 I = Eigen::MatrixXd::Zero(
n,
n);
211 template <
typename T>
220 ROS_ERROR(
"Kalman: dt is not available!");
230 ROS_ERROR(
"Kalman: n is not available!");
240 ROS_ERROR(
"Kalman: m is not available!");
244 if (
dt <= 0 ||
n <= 0 ||
m <= 0)
246 ROS_ERROR(
"Kalman: dt, n or m is not valid! (dt <= 0 or n <= 0 or m <= 0)");
250 std::vector<double> temp;
265 temp = parameters.At;
266 if (temp.size() == 0)
302 bool skipDynamicPartQ =
false;
305 temp = parameters.Q_coeff;
307 "Process noise covariance (coefficients of dynamic part of Q)"))
309 skipDynamicPartQ =
true;
315 skipDynamicPartQ =
true;
321 temp = parameters.Q_exponent;
323 "Process noise covariance (exponents of the time difference)"))
325 skipDynamicPartQ =
true;
330 ROS_DEBUG(
"Q_exponent is not available!");
331 skipDynamicPartQ =
true;
342 ROS_DEBUG(
"Q_variance is not available!");
369 temp = parameters.x0;
376 x_hat = Eigen::VectorXd::Zero(
n);
382 template <
typename T>
386 template <
typename T>
391 Eigen::VectorXd v = Eigen::VectorXd::Zero(
n);
395 for (
int i = 0; i < data_out.size(); ++i) {
402 template <
typename T>
407 Eigen::MatrixXd A_t =
At;
409 for(
int i = 0; i <
n; i++){
410 for(
int j = 0; j <
n; j++){
412 A_t(i,j) =
pow( delta_t , A_t(i,j) )/
fac( A_t(i,j) );
417 Eigen::VectorXd v = Eigen::VectorXd::Zero(n);
421 for (
int i = 0; i < data_out.size(); ++i) {
428 template <
typename T>
434 P =
A*
P*
A.transpose() +
Q;
437 for (
int i = 0; i < data_out.size(); ++i) {
438 data_out[i] =
x_hat(i);
444 template <
typename T>
448 if(data_in.size() !=
m) {
ROS_ERROR(
"Kalman: Not valid measurement vector!");
return false; }
450 Eigen::VectorXd y(
m);
451 for (
int i = 0; i <
m; ++i) {
456 P =
A*
P*
A.transpose() +
Q;
464 for (
int i = 0; i < data_out.size(); ++i) {
465 data_out[i] =
x_hat(i);
471 template <
typename T>
479 template <
typename T>
487 template <
typename T>
489 const double& delta_t,
bool update_Q_matrix,
const Eigen::MatrixXd& R_loc)
493 if(data_in.size() !=
m) {
ROS_ERROR(
"Kalman: Not valid measurement vector!");
return false; }
495 Eigen::VectorXd y(
m);
496 for (
int i = 0; i <
m; ++i) {
500 Eigen::MatrixXd A_t =
At;
502 for(
int i = 0; i <
n; i++){
503 for(
int j = 0; j <
n; j++){
505 A_t(i,j) =
pow( delta_t , A_t(i,j) )/
fac( A_t(i,j) );
511 Eigen::MatrixXd Q_updated =
Q;
514 for(
int i = 0; i <
n; i++){
515 for(
int j = 0; j <
n; j++){
522 Eigen::MatrixXd R_used =
R;
523 if(!R_loc.isZero() && R_loc.cols() ==
R.cols() && R_loc.rows() ==
R.rows())
528 P = (A_t+
A)*
P*((A_t+
A).transpose()) + Q_updated;
536 for (
int i = 0; i <
n; ++i) {
537 data_out[i] =
x_hat(i);
542 template <
typename T>
547 for (
int i = 0; i <
n; ++i) {
548 data_out[i] =
x_hat(i);
553 template <
typename T>
558 if(data_in.size() !=
m) {
ROS_ERROR(
"Kalman: Not valid measurement vector!");
return false; }
560 Eigen::VectorXd measurement(
m);
561 for (
int i = 0; i <
m; ++i) {
562 measurement(i) = data_in[i];
566 Eigen::VectorXd prediction =
C *
A *
x_hat;
569 Eigen::VectorXd continuousPrediction = prediction -
C *
x_hat;
572 Eigen::VectorXd timeShiftedPrediction = (
C *
x_hat ) + (
dt * continuousPrediction );
574 Eigen::VectorXd
d = timeShiftedPrediction - measurement;
577 const double e = -0.5 * d.transpose() *
gatingMatrix.inverse() * d;
580 data_out = (1. / (std::pow(2. * M_PI, (
double) m * .5)
581 * std::sqrt(
gatingMatrix.determinant()))) * std::exp(e);
586 template <
typename T>
591 for (
int i =
m; i <
n; i++)
Eigen::VectorXd x_hat_new
bool getErrorCovarianceMatrix(Eigen::MatrixXd &data_out)
bool resetErrorCovAndState()
bool predict(std::vector< T > &data_out)
bool getGatingMatrix(Eigen::MatrixXd &data_out)
bool fromStdVectorToEigenVector(std::vector< double > &in, Eigen::VectorXd &out, int rows, std::string vector_name)
Eigen::MatrixXd gatingMatrix
bool getCurrentState(std::vector< T > &data_out)
bool fromStdVectorToEigenMatrix(std::vector< double > &in, Eigen::MatrixXd &out, int rows, int columns, std::string matrix_name)
const std::string & getNamespace() const
ROSCPP_DECL bool has(const std::string &key)
bool computePrediction(std::vector< T > &data_out)
bool getParams(iirob_filters::KalmanFilterParameters &, const std::string &)
MultiChannelKalmanFilter()
INLINE Rall1d< T, V, S > pow(const Rall1d< T, V, S > &arg, double m)
Eigen::MatrixXd Q_exponent
virtual bool update(const std::vector< T > &data_in, std::vector< T > &data_out)
~MultiChannelKalmanFilter()
bool likelihood(const std::vector< T > &data_in, double &data_out)