testObjectTurnaroundDetector.cpp
Go to the documentation of this file.
1 /* -*- coding:utf-8-unix; mode:c++; -*- */
2 
4 /* samples */
5 #include <stdio.h>
6 #include <cstdio>
7 #include <iostream>
8 #include <vector>
9 
10 #ifndef rad2deg
11 #define rad2deg(rad) (rad * 180 / M_PI)
12 #endif
13 #ifndef deg2rad
14 #define deg2rad(deg) (deg * M_PI / 180)
15 #endif
16 
18 {
19 protected:
20  double dt; /* [s] */
23  void gen_pattern_and_plot (const std::vector<double>& force_vec,
24  const std::vector<double>& time_vec)
25  {
26  parse_params();
27  std::string fname("/tmp/plot-otd.dat");
28  FILE* fp = fopen(fname.c_str(), "w");
29  double detect_time;
30  bool detected = false;
31  for (size_t i = 0; i < time_vec.size();i++) {
32  bool tmp_detected = otd.checkDetection(force_vec[i]);
33  if (tmp_detected && !detected) {
34  detect_time = time_vec[i];
35  detected = true;
36  }
37  fprintf(fp, "%f %f %f %f\n", time_vec[i], force_vec[i], otd.getFilteredWrench(), otd.getFilteredDwrench(), detected);
38  }
39  fclose(fp);
40  if (use_gnuplot) {
41  // plot
42  std::string titles[2] = {"Wrench", "Dwrench"};
43  // plot pos
44  FILE* gp = popen("gnuplot", "w");
45  fprintf(gp, "set multiplot layout 2, 1 title 'Results'\n");
46  fprintf(gp, "set xlabel 'Time [s]'\n");
47  fprintf(gp, "set ylabel 'Wrench'\n");
48  fprintf(gp, "set arrow from %f,%f to %f,%f\n", detect_time, force_vec.front(), detect_time, force_vec.back());
49  fprintf(gp, "plot '/tmp/plot-otd.dat' using 1:2 with lines title 'Wrench' lw 4, '/tmp/plot-otd.dat' using 1:3 with lines title 'FilteredWrench' lw 4\n");
50  fprintf(gp, "set xlabel 'Time [s]'\n");
51  fprintf(gp, "set ylabel 'Dwrench'\n");
52  fprintf(gp, "plot '/tmp/plot-otd.dat' using 1:4 with lines title 'Dwrench' lw 4\n");
53  fflush(gp);
54  double tmp;
55  std::cin >> tmp;
56  pclose(gp);
57  }
58  };
59 public:
60  std::vector<std::string> arg_strs;
61  testObjectTurnaroundDetector (const double _dt = 0.004) : dt(_dt), otd(_dt), use_gnuplot(true) {};
62  void test0 ()
63  {
64  std::cerr << "test0 : Set" << std::endl;
65  double tm = 0.0, total_tm = 2.0, df = 10;
66  std::vector<double> time_vec, force_vec;
67  for (size_t i = 0; i < static_cast<size_t>(total_tm/dt);i++) {
68  time_vec.push_back(tm);
69  if (i*dt < total_tm*0.2) {
70  force_vec.push_back(0);
71  } else if (i*dt > total_tm*0.8) {
72  force_vec.push_back(df*total_tm*(0.8-0.2));
73  } else {
74  force_vec.push_back(df*(i*dt-total_tm*0.2));
75  }
76  tm += dt;
77  }
78  otd.startDetection(df, total_tm);
79  gen_pattern_and_plot (force_vec, time_vec);
80  };
81  void parse_params ()
82  {
83  for (unsigned int i = 0; i < arg_strs.size(); ++ i) {
84  if ( arg_strs[i]== "--use-gnuplot" ) {
85  if (++i < arg_strs.size()) use_gnuplot = (arg_strs[i]=="true");
86  }
87  }
88  };
89 };
90 
91 void print_usage ()
92 {
93  std::cerr << "Usage : testObjectTurnaroundDetector [option]" << std::endl;
94  std::cerr << " [option] should be:" << std::endl;
95  std::cerr << " --test0 : Set ref force" << std::endl;
96  // std::cerr << " --test1 : Move pos and rot" << std::endl;
97 };
98 
99 int main(int argc, char* argv[])
100 {
101  int ret = 0;
102  if (argc >= 2) {
104  for (int i = 1; i < argc; ++ i) {
105  totd.arg_strs.push_back(std::string(argv[i]));
106  }
107  if (std::string(argv[1]) == "--test0") {
108  totd.test0();
109  // } else if (std::string(argv[1]) == "--test1") {
110  // tiog.test1();
111  } else {
112  print_usage();
113  ret = 1;
114  }
115  } else {
116  print_usage();
117  ret = 1;
118  }
119  return ret;
120 }
121 
RTC::ReturnCode_t ret(RTC::Local::ReturnCode_t r)
png_uint_32 i
png_FILE_p fp
bool checkDetection(const std::vector< hrp::Vector3 > &fmv, const std::vector< hrp::Vector3 > &hposv)
int main(int argc, char *argv[])
FILE * popen(const char *cmd, const char *mode)
testObjectTurnaroundDetector(const double _dt=0.004)
void gen_pattern_and_plot(const std::vector< double > &force_vec, const std::vector< double > &time_vec)
void pclose(FILE *fd)
void startDetection(const double _ref_diff_wrench, const double _max_time)


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