testImpedanceOutputGenerator.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<hrp::Vector3>& force_diff_vec,
24  const std::vector<hrp::Vector3>& moment_diff_vec,
25  const std::vector<hrp::Vector3>& target_p0_vec,
26  const std::vector<hrp::Matrix33>& target_r0_vec,
27  const std::vector<double>& time_vec)
28  {
29  parse_params();
30  std::string fname("/tmp/plot-imp.dat");
31  FILE* fp = fopen(fname.c_str(), "w");
32  for (size_t i = 0; i < time_vec.size();i++) {
33  imp.target_p0 = target_p0_vec[i];
34  imp.target_r0 = target_r0_vec[i];
35  imp.current_p1 = imp.output_p1;
36  imp.current_r1 = imp.output_r1;
37  hrp::Vector3 vel_p, vel_r;
38  hrp::Matrix33 eeR = hrp::Matrix33::Identity();
39  imp.calcTargetVelocity(vel_p, vel_r,
40  eeR, force_diff_vec[i], moment_diff_vec[i], dt);
41  hrp::Vector3 output_rot, target_rot;
42  rats::difference_rotation(output_rot, hrp::Matrix33::Identity(), imp.output_r1);
43  rats::difference_rotation(target_rot, hrp::Matrix33::Identity(), imp.target_r1);
44  fprintf(fp, "%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f\n",
45  time_vec[i],
46  imp.output_p1(0), imp.output_p1(1), imp.output_p1(2),
47  imp.target_p1(0), imp.target_p1(1), imp.target_p1(2),
48  force_diff_vec[i](0)/imp.K_p, force_diff_vec[i](1)/imp.K_p, force_diff_vec[i](2)/imp.K_p,
49  rad2deg(output_rot(0)), rad2deg(output_rot(1)), rad2deg(output_rot(2)),
50  rad2deg(target_rot(0)), rad2deg(target_rot(1)), rad2deg(target_rot(2)),
51  rad2deg(moment_diff_vec[i](0)/imp.K_r), rad2deg(moment_diff_vec[i](1)/imp.K_r), rad2deg(moment_diff_vec[i](2)/imp.K_r)
52  );
53  }
54  fclose(fp);
55  if (use_gnuplot) {
56  // plot
57  std::string titles[3] = {"X", "Y", "Z"};
58  // plot pos
59  FILE* gp_pos = popen("gnuplot", "w");
60  fprintf(gp_pos, "set multiplot layout 3, 1 title 'Pos results'\n");
61  for (size_t ii = 0; ii < 3; ii++) {
62  fprintf(gp_pos, "set xlabel 'Time [s]'\n");
63  fprintf(gp_pos, "set ylabel 'pos %s [m]'\n", titles[ii].c_str());
64  fprintf(gp_pos, "plot '/tmp/plot-imp.dat' using 1:%d with lines title 'cur pos(%s)' lw 4, '/tmp/plot-imp.dat' using 1:%d with lines title 'tgt pos(%s)' lw 3, '/tmp/plot-imp.dat' using 1:%d with lines title 'force_diff/K(%s)' lw 2\n",
65  ii+2, titles[ii].c_str(), ii+2+3, titles[ii].c_str(), ii+2+3*2, titles[ii].c_str());
66  }
67  fflush(gp_pos);
68  // plot rot
69  FILE* gp_rot = popen("gnuplot", "w");
70  fprintf(gp_rot, "set multiplot layout 3, 1 title 'Rot results'\n");
71  for (size_t ii = 0; ii < 3; ii++) {
72  fprintf(gp_rot, "set xlabel 'Time [s]'\n");
73  fprintf(gp_rot, "set ylabel 'rot %s [deg]'\n", titles[ii].c_str());
74  fprintf(gp_rot, "plot '/tmp/plot-imp.dat' using 1:%d with lines title 'cur rot(%s)' lw 4, '/tmp/plot-imp.dat' using 1:%d with lines title 'tgt rot(%s)' lw 3, '/tmp/plot-imp.dat' using 1:%d with lines title 'moment_diff/K(%s)' lw 2\n",
75  ii+2+9, titles[ii].c_str(), ii+2+3+9, titles[ii].c_str(), ii+2+3*2+9, titles[ii].c_str());
76  }
77  fflush(gp_rot);
78  double tmp;
79  std::cin >> tmp;
80  pclose(gp_pos);
81  pclose(gp_rot);
82  }
83  };
84 public:
85  std::vector<std::string> arg_strs;
86  testImpedanceOutputGenerator (const double _dt = 0.004) : dt(_dt), imp(), use_gnuplot(true) {};
87  void test0 ()
88  {
89  std::cerr << "test0 : Set ref force" << std::endl;
90  double tm = 0.0, total_tm = 4.0;
91  std::vector<double> time_vec;
92  std::vector<hrp::Vector3> force_diff_vec, moment_diff_vec, target_p0_vec;
93  std::vector<hrp::Matrix33> target_r0_vec;
94  for (size_t i = 0; i < static_cast<size_t>(total_tm/dt);i++) {
95  time_vec.push_back(tm);
96  force_diff_vec.push_back((i*dt < total_tm * 0.2 ? hrp::Vector3::Zero() : hrp::Vector3(10,-20,30)));
97  moment_diff_vec.push_back((i*dt < total_tm * 0.3 ? hrp::Vector3::Zero() : hrp::Vector3(5,-10,15)));
98  target_p0_vec.push_back(hrp::Vector3::Zero());
99  target_r0_vec.push_back(hrp::Matrix33::Identity());
100  tm += dt;
101  }
102  gen_pattern_and_plot (force_diff_vec, moment_diff_vec, target_p0_vec, target_r0_vec, time_vec);
103  };
104  void test1 ()
105  {
106  std::cerr << "test1 : Move pos and rot" << std::endl;
107  double tm = 0.0, total_tm = 1.0;
108  std::vector<double> time_vec;
109  std::vector<hrp::Vector3> force_diff_vec, moment_diff_vec, target_p0_vec;
110  std::vector<hrp::Matrix33> target_r0_vec;
111  //imp.M_p = 0.0; imp.M_r = 0.0;
112  for (size_t i = 0; i < static_cast<size_t>(total_tm/dt);i++) {
113  time_vec.push_back(tm);
114  force_diff_vec.push_back(hrp::Vector3::Zero());
115  moment_diff_vec.push_back(hrp::Vector3::Zero());
116  double ratio = (i*dt < total_tm * 0.3 ? i*dt/(total_tm * 0.3) : 1.0);
117  target_p0_vec.push_back(hrp::Vector3((1-ratio)*hrp::Vector3::Zero()+ratio*hrp::Vector3(0.01,-0.02,0.03)));
118  hrp::Vector3 tmpv(hrp::Vector3((1-ratio)*hrp::Vector3::Zero() + ratio*hrp::Vector3(0.1,-0.2,0.3)));
119  Eigen::AngleAxis<double> tmpr;
120  if (tmpv.norm() != 0.0) {
121  tmpr = Eigen::AngleAxis<double>(tmpv.norm(), tmpv.normalized());
122  } else {
123  tmpr = hrp::Matrix33::Identity();
124  }
125  target_r0_vec.push_back(tmpr.toRotationMatrix());
126  tm += dt;
127  }
128  gen_pattern_and_plot (force_diff_vec, moment_diff_vec, target_p0_vec, target_r0_vec, time_vec);
129  };
130  void parse_params ()
131  {
132  for (unsigned int i = 0; i < arg_strs.size(); ++ i) {
133  if ( arg_strs[i]== "--use-gnuplot" ) {
134  if (++i < arg_strs.size()) use_gnuplot = (arg_strs[i]=="true");
135  }
136  }
137  };
138 };
139 
140 void print_usage ()
141 {
142  std::cerr << "Usage : testImpedanceOutputGenerator [option]" << std::endl;
143  std::cerr << " [option] should be:" << std::endl;
144  std::cerr << " --test0 : Set ref force" << std::endl;
145  std::cerr << " --test1 : Move pos and rot" << std::endl;
146 };
147 
148 int main(int argc, char* argv[])
149 {
150  int ret = 0;
151  if (argc >= 2) {
153  for (int i = 1; i < argc; ++ i) {
154  tiog.arg_strs.push_back(std::string(argv[i]));
155  }
156  if (std::string(argv[1]) == "--test0") {
157  tiog.test0();
158  } else if (std::string(argv[1]) == "--test1") {
159  tiog.test1();
160  } else {
161  print_usage();
162  ret = 1;
163  }
164  } else {
165  print_usage();
166  ret = 1;
167  }
168  return ret;
169 }
170 
testImpedanceOutputGenerator(const double _dt=0.004)
#define rad2deg(rad)
void difference_rotation(hrp::Vector3 &ret_dif_rot, const hrp::Matrix33 &self_rot, const hrp::Matrix33 &target_rot)
Definition: RatsMatrix.cpp:40
RTC::ReturnCode_t ret(RTC::Local::ReturnCode_t r)
int main(int argc, char *argv[])
png_uint_32 i
Eigen::Vector3d Vector3
png_FILE_p fp
Eigen::Matrix3d Matrix33
FILE * popen(const char *cmd, const char *mode)
void gen_pattern_and_plot(const std::vector< hrp::Vector3 > &force_diff_vec, const std::vector< hrp::Vector3 > &moment_diff_vec, const std::vector< hrp::Vector3 > &target_p0_vec, const std::vector< hrp::Matrix33 > &target_r0_vec, const std::vector< double > &time_vec)
void pclose(FILE *fd)
void calcTargetVelocity(hrp::Vector3 &vel_p, hrp::Vector3 &vel_r, const hrp::Matrix33 &eeR, const hrp::Vector3 &force_diff, const hrp::Vector3 &moment_diff, const double _dt, const bool printp=false, const std::string &print_str="", const std::string &ee_name="")


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