testZMPDistributor.cpp
Go to the documentation of this file.
1 /* -*- coding:utf-8-unix; mode:c++; -*- */
2 
3 #include "ZMPDistributor.h"
4 /* samples */
5 #include <stdio.h>
6 #include <cstdio>
7 #include <iostream>
8 #include "hrpsys/util/Hrpsys.h" // added for QNX compile
9 
11 {
12 protected:
13  double dt; // [s]
14  double total_fz; // [N]
18  size_t sleep_msec;
19  std::vector<hrp::Vector3> leg_pos;
20  std::vector<hrp::Vector3> ee_pos;
21  std::vector<hrp::Vector3> cop_pos;
22  std::vector<hrp::Matrix33> ee_rot;
23  std::vector<std::vector<Eigen::Vector2d> > fs;
24 private:
25  void gen_and_plot ()
26  {
27  parse_params();
28  std::cerr << "distribution_algorithm = ";
29  if (distribution_algorithm==EEFM) std::cerr << "EEFM";
30  else if (distribution_algorithm==EEFMQP) std::cerr << "EEFMQP";
31  else if (distribution_algorithm==EEFMQP2) std::cerr << "EEFMQP(cop_distribution)";
32  else if (distribution_algorithm==EEFMQPCOP) std::cerr << "EEFMQPCOP";
33  else if (distribution_algorithm==EEFMQPCOP2) std::cerr << "EEFMQPCOP2";
34  else std::cerr << "None?";
35  std::cerr << ", sleep_msec = " << sleep_msec << "[msec]" << std::endl;
36  szd->print_vertices("");
37  szd->print_params(std::string(""));
38  //
39  std::vector<double> x_vec;
40  double txx = -0.25;
41  while (txx < 0.25) {
42  x_vec.push_back(txx);
43  txx += 0.05;
44  };
45  std::vector<double> y_vec;
46  double tyy = -0.24;
47  while (tyy < 0.25) {
48  y_vec.push_back(tyy);
49  tyy += 0.01;
50  };
51  std::vector<hrp::Vector3> refzmp_vec;
52  for (size_t xx = 0; xx < x_vec.size(); xx++) {
53  for (size_t yy = 0; yy < y_vec.size(); yy++) {
54  refzmp_vec.push_back(hrp::Vector3(x_vec[xx], y_vec[yy], 0.0));
55  }
56  }
57  //
58 
59  // plot
60  FILE* gp;
61  FILE* gp_m;
62  FILE* gp_f;
63  FILE* gp_a;
64  if (use_gnuplot) {
65  gp = popen("gnuplot", "w");
66  gp_m = popen("gnuplot", "w");
67  gp_f = popen("gnuplot", "w");
68  gp_a = popen("gnuplot", "w");
69  }
70  std::string fname_fm("/tmp/plot-fm.dat");
71  FILE* fp_fm = fopen(fname_fm.c_str(), "w");
72  std::vector<std::string> names;
73  names.push_back("rleg");
74  names.push_back("lleg");
75  std::vector<double> limb_gains(names.size(), 1.0);
76  std::vector<double> toeheel_ratio(names.size(), 1.0);
77  std::vector<hrp::Vector3> ref_foot_force(names.size(), hrp::Vector3::Zero()), ref_foot_moment(names.size(), hrp::Vector3::Zero());
78  std::vector<std::vector<Eigen::Vector2d> > fs;
79  szd->get_vertices(fs);
80  //
81  std::ostringstream oss_foot_params("");
82  oss_foot_params << "'/tmp/plotrleg.dat' using 1:2:3 with lines title 'rleg', "
83  << "'/tmp/plotlleg.dat' using 1:2:3 with lines title 'lleg', "
84  << "'/tmp/plotrleg-cop.dat' using 1:2:3 with points title 'rleg cop', "
85  << "'/tmp/plotlleg-cop.dat' using 1:2:3 with points title 'lleg cop', ";
86  std::string foot_params_str = oss_foot_params.str();
87  //
88  for (size_t i = 0; i < refzmp_vec.size(); i++) {
89  double alpha = szd->calcAlpha(refzmp_vec[i], ee_pos, ee_rot, names);
90  if (distribution_algorithm == EEFMQP) {
91  szd->distributeZMPToForceMoments(ref_foot_force, ref_foot_moment,
92  ee_pos, cop_pos, ee_rot, names, limb_gains, toeheel_ratio,
93  refzmp_vec[i], refzmp_vec[i],
94  total_fz, dt);
95  } else if (distribution_algorithm == EEFMQP || distribution_algorithm == EEFMQP2) {
96  szd->distributeZMPToForceMomentsQP(ref_foot_force, ref_foot_moment,
97  ee_pos, cop_pos, ee_rot, names, limb_gains, toeheel_ratio,
98  refzmp_vec[i], refzmp_vec[i],
99  total_fz, dt, true, "", (distribution_algorithm == EEFMQP2));
100  } else if (distribution_algorithm == EEFMQPCOP) {
101  szd->distributeZMPToForceMomentsPseudoInverse(ref_foot_force, ref_foot_moment,
102  ee_pos, cop_pos, ee_rot, names, limb_gains, toeheel_ratio,
103  refzmp_vec[i], refzmp_vec[i],
104  total_fz, dt, true, "");
105  } else if (distribution_algorithm == EEFMQPCOP2) {
106  // std::vector<double> ee_forcemoment_distribution_weight(names.size(), 1.0);
107  // szd->distributeZMPToForceMomentsPseudoInverse2(ref_foot_force, ref_foot_moment,
108  // ee_pos, cop_pos, ee_rot, names, limb_gains, toeheel_ratio,
109  // refzmp_vec[i], refzmp_vec[i],
110  // total_fz, dt, true, "");
111  }
112  for (size_t j = 0; j < fs.size(); j++) {
113  std::string fname("/tmp/plot"+names[j]+".dat");
114  FILE* fp = fopen(fname.c_str(), "w");
115  for (size_t i = 0; i < fs[j].size(); i++) {
116  hrp::Vector3 tmpf(hrp::Vector3(ee_rot[j] * hrp::Vector3(fs[j][i](0), fs[j][i](1), 0) + ee_pos[j]));
117  fprintf(fp, "%f %f %f\n", tmpf(0), tmpf(1), tmpf(2));
118  }
119  hrp::Vector3 tmpf(hrp::Vector3(ee_rot[j] * hrp::Vector3(fs[j][0](0), fs[j][0](1), 0) + ee_pos[j]));
120  fprintf(fp, "%f %f %f\n", tmpf(0), tmpf(1), tmpf(2));
121  fclose(fp);
122  }
123  for (size_t j = 0; j < fs.size(); j++) {
124  std::string fname("/tmp/plot"+names[j]+"-cop.dat");
125  FILE* fp = fopen(fname.c_str(), "w");
126  fprintf(fp, "%f %f %f\n", ee_pos[j](0), ee_pos[j](1), ee_pos[j](2));
127  fclose(fp);
128  }
129  for (size_t j = 0; j < fs.size(); j++) {
130  std::string fname("/tmp/plot"+names[j]+"fm.dat");
131  FILE* fp = fopen(fname.c_str(), "w");
132  hrp::Vector3 cop;
133  cop(0) = -ref_foot_moment[j](1)/ref_foot_force[j](2) + cop_pos[j](0);
134  cop(1) = ref_foot_moment[j](0)/ref_foot_force[j](2) + cop_pos[j](1);
135  fprintf(fp, "%f %f 0\n", cop(0), cop(1));
136  fprintf(fp, "%f %f %f\n", cop(0), cop(1), ref_foot_force[j](2));
137  fclose(fp);
138  }
139  fprintf(fp_fm, "%f %f %f %f %f %f %f %f %f\n",
140  refzmp_vec[i](0), refzmp_vec[i](1),
141  ref_foot_force[0](2), ref_foot_force[1](2),
142  ref_foot_moment[0](0), ref_foot_moment[1](0),
143  ref_foot_moment[0](1), ref_foot_moment[1](1),
144  alpha);
145  {
146  std::string fname("/tmp/plotzmp.dat");
147  FILE* fp = fopen(fname.c_str(), "w");
148  fprintf(fp, "%f %f %f\n", refzmp_vec[i](0), refzmp_vec[i](1), refzmp_vec[i](2));
149  fclose(fp);
150  }
151  if (use_gnuplot) {
152  std::ostringstream oss("");
153  oss << "splot [-0.5:0.5][-0.5:0.5][-1:1000] " << foot_params_str
154  << "'/tmp/plotrlegfm.dat' using 1:2:3 with lines title 'rleg fm' lw 5, "
155  << "'/tmp/plotllegfm.dat' using 1:2:3 with lines title 'lleg fm' lw 5, "
156  << "'/tmp/plotzmp.dat' using 1:2:3 with points title 'zmp' lw 10";
157  fprintf(gp, "%s\n", oss.str().c_str());
158  fflush(gp);
159  usleep(1000*sleep_msec);
160  }
161  }
162  fclose(fp_fm);
163  if (use_gnuplot) {
164  std::ostringstream oss("");
165  oss << "splot [-0.5:0.5][-0.5:0.5][-100:100] " << foot_params_str
166  << "'/tmp/plot-fm.dat' using 1:2:5 with points title 'rleg nx' lw 5, "
167  << "'/tmp/plot-fm.dat' using 1:2:6 with points title 'lleg nx' lw 5, "
168  << "'/tmp/plot-fm.dat' using 1:2:7 with points title 'rleg ny' lw 5, "
169  << "'/tmp/plot-fm.dat' using 1:2:8 with points title 'lleg ny' lw 5";
170  fprintf(gp_m, "%s\n", oss.str().c_str());
171  fflush(gp_m);
172  oss.str("");
173  oss << "splot [-0.5:0.5][-0.5:0.5][-50:" << total_fz*1.1 << "] " << foot_params_str
174  << "'/tmp/plot-fm.dat' using 1:2:3 with points title 'rleg fz' lw 5, "
175  << "'/tmp/plot-fm.dat' using 1:2:4 with points title 'lleg fz' lw 5";
176  fprintf(gp_f, "%s\n", oss.str().c_str());
177  fflush(gp_f);
178  oss.str("");
179  oss << "splot [-0.5:0.5][-0.5:0.5][-0.1:1.1] " << foot_params_str
180  << "'/tmp/plot-fm.dat' using 1:2:9 with points title 'alpha' lw 5";
181  fprintf(gp_a, "%s\n", oss.str().c_str());
182  fflush(gp_a);
183  double tmp;
184  std::cin >> tmp;
185  pclose(gp);
186  pclose(gp_m);
187  pclose(gp_f);
188  pclose(gp_a);
189  }
190  };
191 public:
192  std::vector<std::string> arg_strs;
193  testZMPDistributor(const double _dt) : dt(_dt), distribution_algorithm(EEFMQP), use_gnuplot(true), sleep_msec(100)
194  {
195  szd = new SimpleZMPDistributor(_dt);
196  };
198  {
199  if (szd != NULL) {
200  delete szd;
201  szd = NULL;
202  }
203  };
204 
205  void parse_params ()
206  {
207  for (int i = 0; i < arg_strs.size(); ++ i) {
208  if ( arg_strs[i]== "--distribution-algorithm" ) {
209  if (++i < arg_strs.size()) {
210  if (arg_strs[i]=="EEFM") {
211  distribution_algorithm = EEFM;
212  } else if (arg_strs[i]=="EEFMQP") {
213  distribution_algorithm = EEFMQP;
214  } else if (arg_strs[i]=="EEFMQP2") {
215  distribution_algorithm = EEFMQP2;
216  } else if (arg_strs[i]=="EEFMQPCOP") {
217  distribution_algorithm = EEFMQPCOP;
218  } else if (arg_strs[i]=="EEFMQPCOP") {
219  distribution_algorithm = EEFMQPCOP;
220  } else if (arg_strs[i]=="EEFMQPCOP2") {
221  distribution_algorithm = EEFMQPCOP2;
222  } else {
223  distribution_algorithm = EEFM;
224  }
225  }
226  } else if ( arg_strs[i]== "--sleep-msec" ) {
227  if (++i < arg_strs.size()) sleep_msec = atoi(arg_strs[i].c_str());
228  } else if ( arg_strs[i]== "--use-gnuplot" ) {
229  if (++i < arg_strs.size()) use_gnuplot = (arg_strs[i]=="true");
230  }
231  }
232  };
233 
234  void test0 ()
235  {
236  std::cerr << "test0 : Default foot pos" << std::endl;
237  ee_pos = leg_pos;
238  cop_pos = leg_pos;
239  ee_rot.push_back(hrp::Matrix33::Identity());
240  ee_rot.push_back(hrp::Matrix33::Identity());
241  gen_and_plot();
242  };
243 
244  void test1 ()
245  {
246  std::cerr << "test1 : Fwd foot pos" << std::endl;
247  ee_pos = leg_pos;
248  ee_pos[0] = ee_pos[0] + hrp::Vector3(0.05,0,0);
249  ee_pos[1] = ee_pos[1] + hrp::Vector3(-0.05,0,0);
250  cop_pos = ee_pos;
251  ee_rot.push_back(hrp::Matrix33::Identity());
252  ee_rot.push_back(hrp::Matrix33::Identity());
253  gen_and_plot();
254  };
255 
256  void test2 ()
257  {
258  std::cerr << "test2 : Rot foot pos" << std::endl;
259  ee_pos = leg_pos;
260  ee_pos[0] = ee_pos[0] + hrp::Vector3(0.02,0,0);
261  ee_pos[1] = ee_pos[1] + hrp::Vector3(-0.02,0,0);
262  cop_pos = ee_pos;
263  hrp::Matrix33 tmpr;
264  tmpr = hrp::rotFromRpy(hrp::Vector3(0,0,-15*M_PI/180.0));
265  ee_rot.push_back(tmpr);
266  tmpr = hrp::rotFromRpy(hrp::Vector3(0,0,15*M_PI/180.0));
267  ee_rot.push_back(tmpr);
268  gen_and_plot();
269  };
270 };
271 
273 {
274  public:
276  {
277  total_fz = 56*9.8066;
278  szd->set_leg_inside_margin(0.070104);
279  szd->set_leg_outside_margin(0.070104);
280  szd->set_leg_front_margin(0.137525);
281  szd->set_leg_rear_margin(0.106925);
283  leg_pos.push_back(hrp::Vector3(0,-0.105,0));
284  leg_pos.push_back(hrp::Vector3(0,0.105,0));
285  };
286 };
287 
289 {
290  public:
292  {
293  total_fz = 130.442*9.8066;
294  szd->set_leg_inside_margin(0.055992);
295  szd->set_leg_outside_margin(0.075992);
296  szd->set_leg_front_margin(0.133242);
297  szd->set_leg_rear_margin(0.100445);
299  leg_pos.push_back(hrp::Vector3(0,-0.100,0));
300  leg_pos.push_back(hrp::Vector3(0,0.100,0));
301  };
302 };
303 
304 void print_usage ()
305 {
306  std::cerr << "Usage : testZMPDistributor [robot-name] [test-type] [option]" << std::endl;
307  std::cerr << " [robot-name] should be: --hrp2jsk, --jaxon_red" << std::endl;
308  std::cerr << " [test-type] should be:" << std::endl;
309  std::cerr << " --test0 : Default foot pos" << std::endl;
310  std::cerr << " --test1 : Fwd foot pos" << std::endl;
311  std::cerr << " --test2 : Rot foot pos" << std::endl;
312 };
313 
314 int main(int argc, char* argv[])
315 {
316  int ret = 0;
317  if (argc >= 3) {
318  testZMPDistributor* tzd = NULL;
319  if (std::string(argv[1]) == "--hrp2jsk") {
320  tzd = new testZMPDistributorHRP2JSK();
321  } else if (std::string(argv[1]) == "--jaxon_red") {
322  tzd = new testZMPDistributorJAXON_RED();
323  } else {
324  print_usage();
325  ret = 1;
326  }
327  if (tzd != NULL) {
328  for (int i = 2; i < argc; ++ i) {
329  tzd->arg_strs.push_back(std::string(argv[i]));
330  }
331  if (std::string(argv[2]) == "--test0") {
332  tzd->test0();
333  } else if (std::string(argv[2]) == "--test1") {
334  tzd->test1();
335  } else if (std::string(argv[2]) == "--test2") {
336  tzd->test2();
337  } else {
338  print_usage();
339  ret = 1;
340  }
341  delete tzd;
342  }
343  } else {
344  print_usage();
345  ret = 1;
346  }
347  return ret;
348 }
349 
std::vector< std::vector< Eigen::Vector2d > > fs
testZMPDistributor(const double _dt)
RTC::ReturnCode_t ret(RTC::Local::ReturnCode_t r)
std::vector< hrp::Vector3 > ee_pos
ZMP distribution.
png_uint_32 i
void distributeZMPToForceMomentsQP(std::vector< hrp::Vector3 > &ref_foot_force, std::vector< hrp::Vector3 > &ref_foot_moment, const std::vector< hrp::Vector3 > &ee_pos, const std::vector< hrp::Vector3 > &cop_pos, const std::vector< hrp::Matrix33 > &ee_rot, const std::vector< std::string > &ee_name, const std::vector< double > &limb_gains, const std::vector< double > &toeheel_ratio, const hrp::Vector3 &new_refzmp, const hrp::Vector3 &ref_zmp, const double total_fz, const double dt, const bool printp=true, const std::string &print_str="", const bool use_cop_distribution=false)
Eigen::Vector3d Vector3
SimpleZMPDistributor * szd
double calcAlpha(const hrp::Vector3 &tmprefzmp, const std::vector< hrp::Vector3 > &ee_pos, const std::vector< hrp::Matrix33 > &ee_rot, const std::vector< std::string > &ee_name)
enum testZMPDistributor::@13 distribution_algorithm
png_FILE_p fp
Matrix33 rotFromRpy(const Vector3 &rpy)
Eigen::Matrix3d Matrix33
void set_leg_inside_margin(const double a)
void set_leg_front_margin(const double a)
std::vector< std::string > arg_strs
def j(str, encoding="cp932")
int main(int argc, char *argv[])
void set_vertices_from_margin_params()
FILE * popen(const char *cmd, const char *mode)
#define M_PI
void distributeZMPToForceMoments(std::vector< hrp::Vector3 > &ref_foot_force, std::vector< hrp::Vector3 > &ref_foot_moment, const std::vector< hrp::Vector3 > &ee_pos, const std::vector< hrp::Vector3 > &cop_pos, const std::vector< hrp::Matrix33 > &ee_rot, const std::vector< std::string > &ee_name, const std::vector< double > &limb_gains, const std::vector< double > &toeheel_ratio, const hrp::Vector3 &new_refzmp, const hrp::Vector3 &ref_zmp, const double total_fz, const double dt, const bool printp=true, const std::string &print_str="")
void print_params(const std::string &str)
void set_leg_outside_margin(const double a)
void print_usage()
void get_vertices(std::vector< std::vector< Eigen::Vector2d > > &vs)
void set_leg_rear_margin(const double a)
void print_vertices(const std::string &str)
void distributeZMPToForceMomentsPseudoInverse(std::vector< hrp::Vector3 > &ref_foot_force, std::vector< hrp::Vector3 > &ref_foot_moment, const std::vector< hrp::Vector3 > &ee_pos, const std::vector< hrp::Vector3 > &cop_pos, const std::vector< hrp::Matrix33 > &ee_rot, const std::vector< std::string > &ee_name, const std::vector< double > &limb_gains, const std::vector< double > &toeheel_ratio, const hrp::Vector3 &new_refzmp, const hrp::Vector3 &ref_zmp, const double total_fz, const double dt, const bool printp=true, const std::string &print_str="", const bool use_cop_distribution=true, const std::vector< bool > is_contact_list=std::vector< bool >())
void pclose(FILE *fd)
std::vector< hrp::Vector3 > cop_pos
std::vector< hrp::Matrix33 > ee_rot
std::vector< hrp::Vector3 > leg_pos
int usleep(useconds_t usec)


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