testObjectContactTurnaroundDetectorBase.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 
11 {
12 protected:
13  double dt; /* [s] */
16  std::vector<std::vector<hrp::Vector3> > forces_vec, moments_vec, hpos_vec;
18  std::vector<double> time_vec;
20  {
21  parse_params();
22  octd.printParams();
23  std::string fname("/tmp/plot-octd.dat");
24  FILE* fp = fopen(fname.c_str(), "w");
25  bool detected = false;
26  double max_f = -1e10, min_f = 1e10;
27  for (size_t i = 0; i < time_vec.size();i++) {
28  bool tmp_detected = octd.checkDetection(forces_vec[i], moments_vec[i], hpos_vec[i]);
29  if (tmp_detected && !detected) {
30  detect_time = time_vec[i];
31  detected = true;
32  }
33  hrp::dvector log_data = octd.getDataForLogger();
34  fprintf(fp, "%f %f %f %f\n", time_vec[i], log_data[1], log_data[2], log_data[3], detected);
35  max_f = std::max(max_f, log_data[1]);
36  min_f = std::min(min_f, log_data[1]);
37  }
38  fclose(fp);
39  if (use_gnuplot) {
40  // plot
41  std::string titles[2] = {"Wrench", "Dwrench"};
42  // plot pos
43  FILE* gp = popen("gnuplot", "w");
44  fprintf(gp, "set multiplot layout 2, 1 title 'Results'\n");
45  fprintf(gp, "set xlabel 'Time [s]'\n");
46  fprintf(gp, "set ylabel 'Wrench'\n");
47  fprintf(gp, "set arrow from %f,%f to %f,%f\n", detect_time, min_f, detect_time, max_f);
48  fprintf(gp, "plot '/tmp/plot-octd.dat' using 1:2 with lines title 'Wrench' lw 4, '/tmp/plot-octd.dat' using 1:3 with lines title 'FilteredWrench' lw 4\n");
49  fprintf(gp, "unset arrow\n");
50  fprintf(gp, "set xlabel 'Time [s]'\n");
51  fprintf(gp, "set ylabel 'Dwrench'\n");
52  fprintf(gp, "plot '/tmp/plot-octd.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  testObjectContactTurnaroundDetectorBase (const double _dt = 0.004) : dt(_dt), octd(_dt), use_gnuplot(true), detect_time(1e10), true_turnaround_time(1e10)
62  {
63  // Defaults
64  octd.setWrenchCutoffFreq(5.0);
65  octd.setDwrenchCutoffFreq(5.0);
66  octd.setAxis(hrp::Vector3::UnitZ());
67  //octd.setDetectorTotalWrench(ObjectContactTurnaroundDetectorBase::GENERALIZED_WRENCH);
68  //octd.setFrictionCoeffWrenchCutoffFreq(5.0);
69  };
70  void gen_forces_moments (const std::vector<double>& force_vec, const hrp::Vector3& force_dir = hrp::Vector3::UnitZ())
71  {
72  for (size_t i = 0; i < force_vec.size();i++) {
73  std::vector<hrp::Vector3> tmpv(1, hrp::Vector3::Zero());
74  moments_vec.push_back(tmpv);
75  hpos_vec.push_back(tmpv);
76  tmpv[0] = force_vec[i] * force_dir;
77  forces_vec.push_back(tmpv);
78  }
79  };
80  hrp::dvector6 get_ccm1_by_index (size_t idx, bool is_positive = true)
81  {
82  hrp::dvector6 ccm1(hrp::dvector6::Zero());
83  ccm1(idx) = (is_positive?1.0:-1.0);
84  return ccm1;
85  }
86  // convert object_resultant_wrench => constraint_generalized_force
87  double get_a_coeff_by_index (const double df, const hrp::dvector6& ccm1, const hrp::Vector3& fdir)
88  {
89  hrp::Vector3 fpos(0.2, 0.0, 0.7); // [m]
90  hrp::Vector3 tmp(fdir);
91  hrp::dvector6 resultant_wrench_direction_vector;
92  for (size_t i = 0; i < 3; i++) resultant_wrench_direction_vector(i) = tmp(i);
93  tmp = fpos.cross(fdir);
94  for (size_t i = 0; i < 3; i++) resultant_wrench_direction_vector(i+3) = tmp(i);
95  return -1*ccm1.dot(resultant_wrench_direction_vector) * df;
96  }
97  // Resultant force : robot's side resultant force
98  double gen_forces_moments_for_saturation (const double total_tm, const double start_tm, const double turnaround_tm,
99  const double start_resultant_force, const double turnaround_resultant_force,
100  const hrp::Vector3& force_dir = hrp::Vector3::UnitZ())
101  {
102  std::vector<double> phi_vec;
103  double dphi = (turnaround_resultant_force-start_resultant_force)/(turnaround_tm-start_tm);
104  true_turnaround_time = turnaround_tm;
105  for (size_t i = 0; i < static_cast<size_t>(total_tm/dt);i++) {
106  double current_tm = i*dt;
107  time_vec.push_back(current_tm);
108  if (current_tm < start_tm) {
109  phi_vec.push_back(start_resultant_force);
110  } else if (current_tm < turnaround_tm) {
111  phi_vec.push_back(start_resultant_force+dphi*(current_tm-start_tm));
112  } else {
113  phi_vec.push_back(turnaround_resultant_force);
114  }
115  }
116  gen_forces_moments(phi_vec, force_dir);
117  return dphi;
118  };
119  double gen_forces_moments_for_inverting (const double total_tm, const double start_tm, const double turnaround_tm,
120  const double start_resultant_force, const double turnaround_resultant_force,
121  const hrp::Vector3& force_dir = hrp::Vector3::UnitZ())
122  {
123  std::vector<double> phi_vec;
124  double dphi = (turnaround_resultant_force-start_resultant_force)/(turnaround_tm-start_tm);
125  true_turnaround_time = turnaround_tm;
126  for (size_t i = 0; i < static_cast<size_t>(total_tm/dt);i++) {
127  double current_tm = i*dt;
128  time_vec.push_back(current_tm);
129  if (current_tm < start_tm) {
130  phi_vec.push_back(start_resultant_force);
131  } else if (current_tm < turnaround_tm) {
132  phi_vec.push_back(start_resultant_force+dphi*(current_tm-start_tm));
133  } else {
134  phi_vec.push_back(turnaround_resultant_force+(current_tm-turnaround_tm)*-2*dphi );
135  }
136  }
137  gen_forces_moments(phi_vec, force_dir);
138  return dphi;
139  };
140  void test0 ()
141  {
142  std::cerr << "test0 : Increasing->saturation (TOTAL_FORCE)" << std::endl;
143  double total_tm = 4.0, start_tm = total_tm*0.1, turnaround_tm = total_tm*0.4, start_resultant_force = 0.0, turnaround_resultant_force = 40.0, dphi;
144  dphi = gen_forces_moments_for_saturation(total_tm, start_tm, turnaround_tm, start_resultant_force, turnaround_resultant_force);
145  octd.startDetection (dphi, total_tm);
147  };
148  void test1 ()
149  {
150  std::cerr << "test1 : Increasing->decreasing (TOTAL_FORCE)" << std::endl;
151  double total_tm = 4.0, start_tm = total_tm*0.1, turnaround_tm = total_tm*0.4, start_resultant_force = 0.0, turnaround_resultant_force = 40.0, dphi;
152  dphi = gen_forces_moments_for_inverting(total_tm, start_tm, turnaround_tm, start_resultant_force, turnaround_resultant_force);
153  octd.startDetection (dphi, total_tm);
155  };
156  void test2 ()
157  {
158  std::cerr << "test2 : Deacreasing->saturation (TOTAL_FORCE)" << std::endl;
159  double total_tm = 4.0, start_tm = total_tm*0.1, turnaround_tm = total_tm*0.4, start_resultant_force = 0.0, turnaround_resultant_force = -40.0, dphi;
160  dphi = gen_forces_moments_for_saturation(total_tm, start_tm, turnaround_tm, start_resultant_force, turnaround_resultant_force);
161  octd.startDetection (dphi, total_tm);
163  };
164  void test3 ()
165  {
166  std::cerr << "test3 : Decreasing->increasing (TOTAL_FORCE)" << std::endl;
167  double total_tm = 4.0, start_tm = total_tm*0.1, turnaround_tm = total_tm*0.4, start_resultant_force = 0.0, turnaround_resultant_force = -40.0, dphi;
168  dphi = gen_forces_moments_for_inverting(total_tm, start_tm, turnaround_tm, start_resultant_force, turnaround_resultant_force);
169  octd.startDetection (dphi, total_tm);
171  };
172  void test4 ()
173  {
174  std::cerr << "test4 : Lift up (GENERALIZED_WRENCH)" << std::endl;
175  double total_tm = 4.0, start_tm = total_tm*0.1, turnaround_tm = total_tm*0.4, start_resultant_force = 0.0, turnaround_resultant_force = -40.0, dphi;
176  dphi = std::fabs(gen_forces_moments_for_saturation(total_tm, start_tm, turnaround_tm, start_resultant_force, turnaround_resultant_force));
178  std::vector<hrp::dvector6> ccm1(1, get_ccm1_by_index(2));
179  double ref_dwrench = get_a_coeff_by_index(dphi, ccm1[0], hrp::Vector3::UnitZ());
181  std::vector<hrp::dvector6>(1, hrp::dvector6::Zero()),
182  std::vector<double>(1, ref_dwrench));
183  octd.setMaxTime(total_tm);
186  };
187  void test5 ()
188  {
189  std::cerr << "test5 : Push fwd (GENERALIZED_WRENCH)" << std::endl;
190  double total_tm = 4.0, start_tm = total_tm*0.1, turnaround_tm = total_tm*0.4, start_resultant_force = 0.0, turnaround_resultant_force = -40.0, dphi;
191  dphi = std::fabs(gen_forces_moments_for_saturation(total_tm, start_tm, turnaround_tm, start_resultant_force, turnaround_resultant_force, hrp::Vector3::UnitX()));
193  std::vector<hrp::dvector6> ccm1(1, get_ccm1_by_index(0));
194  double ref_dwrench = get_a_coeff_by_index(dphi, ccm1[0], hrp::Vector3::UnitX());
196  std::vector<hrp::dvector6>(1, hrp::dvector6::Zero()),
197  std::vector<double>(1, ref_dwrench));
198  octd.setMaxTime(total_tm);
201  };
202  void test6 ()
203  {
204  std::cerr << "test6 : Push bwd (GENERALIZED_WRENCH)" << std::endl;
205  double total_tm = 4.0, start_tm = total_tm*0.1, turnaround_tm = total_tm*0.4, start_resultant_force = 0.0, turnaround_resultant_force = 40.0, dphi;
206  dphi = std::fabs(gen_forces_moments_for_saturation(total_tm, start_tm, turnaround_tm, start_resultant_force, turnaround_resultant_force, hrp::Vector3::UnitX()));
208  std::vector<hrp::dvector6> ccm1(1, get_ccm1_by_index(0, false));
209  double ref_dwrench = get_a_coeff_by_index(dphi, ccm1[0], -1*hrp::Vector3::UnitX());
211  std::vector<hrp::dvector6>(1, hrp::dvector6::Zero()),
212  std::vector<double>(1, ref_dwrench));
213  octd.setMaxTime(total_tm);
216  };
217  void test7 ()
218  {
219  std::cerr << "test7 : Tilt upward (GENERALIZED_WRENCH)" << std::endl;
220  double total_tm = 4.0, start_tm = total_tm*0.1, turnaround_tm = total_tm*0.4, start_resultant_force = 0.0, turnaround_resultant_force = -40.0, dphi;
221  dphi = std::fabs(gen_forces_moments_for_saturation(total_tm, start_tm, turnaround_tm, start_resultant_force, turnaround_resultant_force));
223  std::vector<hrp::dvector6> ccm1(1, hrp::dvector6::Zero());
224  ccm1[0](2) = 0.9; ccm1[0](4) = 1.0;
225  double ref_dwrench = get_a_coeff_by_index(dphi, ccm1[0], hrp::Vector3::UnitZ());
227  std::vector<hrp::dvector6>(1, hrp::dvector6::Zero()),
228  std::vector<double>(1, ref_dwrench));
229  octd.setMaxTime(total_tm);
232  };
233  bool check_detection_time_validity (const double time_thre = 1.0) // [s]
234  {
235  return true_turnaround_time < detect_time && (detect_time-true_turnaround_time) < time_thre;
236  };
238  {
239  std::cerr << "Results:" << std::endl;
240  std::cerr << " Detected? : " << (octd.isDetected()?"true":"false") << std::endl;
241  std::cerr << " Detection time : " << (check_detection_time_validity()?"true":"false") << ", detect_time = " << detect_time << "[s], true_turnaround_time = " << true_turnaround_time << "[s]" << std::endl;
242  return octd.isDetected() && check_detection_time_validity();
243  };
244  void parse_params ()
245  {
246  for (unsigned int i = 0; i < arg_strs.size(); ++ i) {
247  if ( arg_strs[i]== "--use-gnuplot" ) {
248  if (++i < arg_strs.size()) use_gnuplot = (arg_strs[i]=="true");
249  }
250  }
251  };
252 };
253 
254 void print_usage ()
255 {
256  std::cerr << "Usage : testObjectContactTurnaroundDetectorBase [option]" << std::endl;
257  std::cerr << " [option] should be:" << std::endl;
258  std::cerr << " --test0 : Increasing->saturation (TOTAL_FORCE)" << std::endl;
259  std::cerr << " --test1 : Increasing->saturation (GENERALIZED_WRENCH)" << std::endl;
260  std::cerr << " --test2 : Increasing->decreasing (GENERALIZED_WRENCH)" << std::endl;
261  std::cerr << " --test3 : Decreasing->saturation (GENERALIZED_WRENCH)" << std::endl;
262  std::cerr << " --test4 : Decreasing->increasing (GENERALIZED_WRENCH)" << std::endl;
263 };
264 
265 int main(int argc, char* argv[])
266 {
267  int ret = 0;
268  if (argc >= 2) {
270  for (int i = 1; i < argc; ++ i) {
271  toctd.arg_strs.push_back(std::string(argv[i]));
272  }
273  if (std::string(argv[1]) == "--test0") {
274  toctd.test0();
275  } else if (std::string(argv[1]) == "--test1") {
276  toctd.test1();
277  } else if (std::string(argv[1]) == "--test2") {
278  toctd.test2();
279  } else if (std::string(argv[1]) == "--test3") {
280  toctd.test3();
281  } else if (std::string(argv[1]) == "--test4") {
282  toctd.test4();
283  } else if (std::string(argv[1]) == "--test5") {
284  toctd.test5();
285  } else if (std::string(argv[1]) == "--test6") {
286  toctd.test6();
287  } else if (std::string(argv[1]) == "--test7") {
288  toctd.test7();
289  } else {
290  print_usage();
291  ret = 1;
292  }
293  ret = (toctd.check_all_results()?0:2);
294  } else {
295  print_usage();
296  ret = 1;
297  }
298  return ret;
299 }
300 
#define max(a, b)
bool checkDetection(const std::vector< hrp::Vector3 > &forces, const std::vector< hrp::Vector3 > &moments, const std::vector< hrp::Vector3 > &hposv)
RTC::ReturnCode_t ret(RTC::Local::ReturnCode_t r)
int main(int argc, char *argv[])
png_uint_32 i
Eigen::VectorXd dvector
void startDetection(const double _ref_diff_wrench, const double _max_time)
Eigen::Vector3d Vector3
png_FILE_p fp
double get_a_coeff_by_index(const double df, const hrp::dvector6 &ccm1, const hrp::Vector3 &fdir)
#define min(a, b)
hrp::dvector6 get_ccm1_by_index(size_t idx, bool is_positive=true)
double gen_forces_moments_for_saturation(const double total_tm, const double start_tm, const double turnaround_tm, const double start_resultant_force, const double turnaround_resultant_force, const hrp::Vector3 &force_dir=hrp::Vector3::UnitZ())
std::vector< std::vector< hrp::Vector3 > > forces_vec
double gen_forces_moments_for_inverting(const double total_tm, const double start_tm, const double turnaround_tm, const double start_resultant_force, const double turnaround_resultant_force, const hrp::Vector3 &force_dir=hrp::Vector3::UnitZ())
void gen_forces_moments(const std::vector< double > &force_vec, const hrp::Vector3 &force_dir=hrp::Vector3::UnitZ())
FILE * popen(const char *cmd, const char *mode)
std::vector< std::vector< hrp::Vector3 > > moments_vec
Eigen::Matrix< double, 6, 1 > dvector6
void setConstraintConversionMatricesRefDwrench(const std::vector< hrp::dvector6 > &ccm1, const std::vector< hrp::dvector6 > &ccm2, const std::vector< double > &refdw)
void pclose(FILE *fd)
void setDetectorTotalWrench(const detector_total_wrench _dtw)


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