testKalmanFilterEstimation.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
11 #include <stdio.h>
12 #include <fstream>
13 #include "RPYKalmanFilter.h"
14 #include "EKFilter.h"
15 
16 // Usage
17 // testKalmanFilterEstimation --acc-file /tmp/kftest/test.acc --rate-file /tmp/kftest/test.rate --pose-file /tmp/kftest/test.pose --Q-angle 1e-3 --Q-rate 1e-10 --dt 0.005 --R-angle 1 --use-gnuplot true
18 
19 int main(int argc, char *argv[])
20 {
21  // Default file names and params
22  double Q_angle = 0.001;
23  double Q_rate = 0.003;
24  double R_angle = 10;
25  double dt = 0.002;
26  std::string pose_file("test.pose"), acc_file("test.acc"), rate_file("test.rate");
27  bool use_gnuplot = false;
28 
29  // Parse argument
30  for (int i = 0; i < argc; ++ i) {
31  std::string arg(argv[i]);
32  if ( arg == "--Q-angle" ) { // KF parameters
33  if (++i < argc) Q_angle = atof(argv[i]);
34  } else if ( arg == "--Q-rate" ) {
35  if (++i < argc) Q_rate = atof(argv[i]);
36  } else if ( arg == "--R-angle" ) {
37  if (++i < argc) R_angle = atof(argv[i]);
38  } else if ( arg == "--dt" ) { // sampling time[s]
39  if (++i < argc) dt = atof(argv[i]);
40  } else if ( arg == "--rate-file" ) { // File path for rate
41  if (++i < argc) rate_file = argv[i];
42  } else if ( arg == "--acc-file" ) { // File path for acc
43  if (++i < argc) acc_file = argv[i];
44  } else if ( arg == "--pose-file" ) { // File path for actual pose
45  if (++i < argc) pose_file = argv[i];
46  } else if ( arg == "--use-gnuplot" ) { // Use gnuplot (true or false)
47  if (++i < argc) use_gnuplot = (std::string(argv[i])=="true"?true:false);
48  }
49  }
50 
51  // Setup input and output files
52  std::ifstream ratef(rate_file.c_str()), accf(acc_file.c_str()), posef(pose_file.c_str());
53  std::cerr << "File : " << rate_file << " " << acc_file << " " << pose_file << std::endl;
54  if (!ratef.is_open() || !accf.is_open() || !posef.is_open()) {
55  std::cerr << "No such " << rate_file << " " << acc_file << " " << pose_file << std::endl;
56  return -1;
57  }
58  std::string ofname("/tmp/testKalmanFilterEstimation.dat");
59  std::ofstream ofs(ofname.c_str());
60 
61  // Test kalman filter
62  RPYKalmanFilter rpy_kf;
63  rpy_kf.setParam(dt, Q_angle, Q_rate, R_angle);
64  hrp::Vector3 rate, acc, rpy, rpyRaw, baseRpyCurrent, rpyAct;
65  double time, time2=0.0;
66  while(!ratef.eof()){
67  posef >> time >> time >> time >> time >> rpyAct[0] >> rpyAct[1] >> rpyAct[2]; // Neglect translation in .pose file
68  ratef >> time >> rate[0] >> rate[1] >> rate[2];
69  accf >> time >> acc[0] >> acc[1] >> acc[2];
70  rpy_kf.main_one(rpy, rpyRaw, baseRpyCurrent, acc, rate, 0.0, hrp::Matrix33::Identity());
71  // rad->deg
72  rpy*=180/3.14159;
73  rpyAct*=180/3.14159;
74  if (use_gnuplot) {
75  ofs << time2 << " " << rpy[0] << " " << rpy[1] << " " << rpy[2] << " " << rpyAct[0] << " " << rpyAct[1] << " " << rpyAct[2] << std::endl;
76  } else {
77  std::cout << rpy[0] << " " << rpy[1] << " " << rpy[2] << " " << rpyAct[0] << " " << rpyAct[1] << " " << rpyAct[2] << std::endl;
78  }
79  time2+=dt;
80  }
81  // gnuplot
82  if (use_gnuplot) {
83  FILE* gp[3];
84  std::string titles[3] = {"Roll", "Pitch", "Yaw"};
85  for (size_t ii = 0; ii < 3; ii++) {
86  gp[ii] = popen("gnuplot", "w");
87  fprintf(gp[ii], "set title \"%s\"\n", titles[ii].c_str());
88  fprintf(gp[ii], "set xlabel \"Time [s]\"\n");
89  fprintf(gp[ii], "set ylabel \"Attitude [rad]\"\n");
90  fprintf(gp[ii], "plot \"%s\" using 1:%d with lines title \"Estimated\"\n", ofname.c_str(), (2 + ii));
91  fprintf(gp[ii], "replot \"%s\" using 1:%d with lines title \"Actual\"\n", ofname.c_str(), (2 + ii + 3));
92  fflush(gp[ii]);
93  }
94  std::cout << "Type any keys + enter to exit." << std::endl;
95  double tmp;
96  std::cin >> tmp;
97  for (size_t j = 0; j < 3; j++) pclose(gp[j]);
98  }
99  return 0;
100 }
int main(int argc, char *argv[])
png_uint_32 i
Eigen::Vector3d Vector3
void setParam(const double _dt, const double _Q_angle, const double _Q_rate, const double _R_angle, const std::string print_str="")
def j(str, encoding="cp932")
FILE * popen(const char *cmd, const char *mode)
char * arg
void pclose(FILE *fd)


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Thu May 6 2021 02:41:51