SequencePlayer.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
10 #include <rtm/CorbaNaming.h>
11 #include <hrpModel/Link.h>
12 #include <hrpModel/ModelLoaderUtil.h>
13 #include "SequencePlayer.h"
14 #include "hrpsys/util/VectorConvert.h"
15 #include <hrpModel/JointPath.h>
16 #include <hrpUtil/MatrixSolvers.h>
17 #include "../ImpedanceController/JointPathEx.h"
18 
20 
21 // Module specification
22 // <rtc-template block="module_spec">
23 static const char* sequenceplayer_spec[] =
24  {
25  "implementation_id", "SequencePlayer",
26  "type_name", "SequencePlayer",
27  "description", "sequence player component",
28  "version", HRPSYS_PACKAGE_VERSION,
29  "vendor", "AIST",
30  "category", "example",
31  "activity_type", "DataFlowComponent",
32  "max_instance", "10",
33  "language", "C++",
34  "lang_type", "compile",
35  // Configuration variables
36  "conf.default.debugLevel", "0",
37  "conf.default.fixedLink", "",
38 
39  ""
40  };
41 // </rtc-template>
42 
44  : RTC::DataFlowComponentBase(manager),
45  // <rtc-template block="initializer">
46  m_qInitIn("qInit", m_qInit),
47  m_basePosInitIn("basePosInit", m_basePosInit),
48  m_baseRpyInitIn("baseRpyInit", m_baseRpyInit),
49  m_zmpRefInitIn("zmpRefInit", m_zmpRefInit),
50  m_qRefOut("qRef", m_qRef),
51  m_tqRefOut("tqRef", m_tqRef),
52  m_zmpRefOut("zmpRef", m_zmpRef),
53  m_accRefOut("accRef", m_accRef),
54  m_basePosOut("basePos", m_basePos),
55  m_baseRpyOut("baseRpy", m_baseRpy),
56  m_optionalDataOut("optionalData", m_optionalData),
57  m_SequencePlayerServicePort("SequencePlayerService"),
58  // </rtc-template>
59  m_robot(hrp::BodyPtr()),
60  m_debugLevel(0),
61  m_error_pos(0.0001),
62  m_error_rot(0.001),
63  m_iteration(50),
64  dummy(0)
65 {
66  sem_init(&m_waitSem, 0, 0);
67  m_service0.player(this);
68  m_clearFlag = false;
69  m_waitFlag = false;
70 }
71 
73 {
74 }
75 
76 
77 RTC::ReturnCode_t SequencePlayer::onInitialize()
78 {
79  std::cout << "SequencePlayer::onInitialize()" << std::endl;
80  // Registration: InPort/OutPort/Service
81  // <rtc-template block="registration">
82  // Set InPort buffers
83  addInPort("qInit", m_qInitIn);
84  addInPort("basePosInit", m_basePosInitIn);
85  addInPort("baseRpyInit", m_baseRpyInitIn);
86  addInPort("zmpRefInit", m_zmpRefInitIn);
87 
88  // Set OutPort buffer
89  addOutPort("qRef", m_qRefOut);
90  addOutPort("tqRef", m_tqRefOut);
91  addOutPort("zmpRef", m_zmpRefOut);
92  addOutPort("accRef", m_accRefOut);
93  addOutPort("basePos", m_basePosOut);
94  addOutPort("baseRpy", m_baseRpyOut);
95  addOutPort("optionalData", m_optionalDataOut);
96 
97  // Set service provider to Ports
98  m_SequencePlayerServicePort.registerProvider("service0", "SequencePlayerService", m_service0);
99 
100  // Set service consumers to Ports
101 
102  // Set CORBA Service Ports
104 
105  // </rtc-template>
106  // <rtc-template block="bind_config">
107  // Bind variables and configuration variable
108 
109  bindParameter("debugLevel", m_debugLevel, "0");
110  bindParameter("fixedLink", m_fixedLink, "");
111  // </rtc-template>
112 
114  coil::stringTo(dt, prop["dt"].c_str());
115 
116  m_robot = hrp::BodyPtr(new Body());
117 
118  RTC::Manager& rtcManager = RTC::Manager::instance();
119  std::string nameServer = rtcManager.getConfig()["corba.nameservers"];
120  int comPos = nameServer.find(",");
121  if (comPos < 0){
122  comPos = nameServer.length();
123  }
124  nameServer = nameServer.substr(0, comPos);
125  RTC::CorbaNaming naming(rtcManager.getORB(), nameServer.c_str());
126  if (!loadBodyFromModelLoader(m_robot, prop["model"].c_str(),
127  CosNaming::NamingContext::_duplicate(naming.getRootContext())
128  )){
129  std::cerr << "failed to load model[" << prop["model"] << "]"
130  << std::endl;
131  }
132 
133  unsigned int dof = m_robot->numJoints();
134 
135 
136  // Setting for wrench data ports (real + virtual)
137  std::vector<std::string> fsensor_names;
138  // find names for real force sensors
139  unsigned int npforce = m_robot->numSensors(hrp::Sensor::FORCE);
140  for (unsigned int i=0; i<npforce; i++){
141  fsensor_names.push_back(m_robot->sensor(hrp::Sensor::FORCE, i)->name);
142  }
143  // find names for virtual force sensors
144  coil::vstring virtual_force_sensor = coil::split(prop["virtual_force_sensor"], ",");
145  unsigned int nvforce = virtual_force_sensor.size()/10;
146  for (unsigned int i=0; i<nvforce; i++){
147  fsensor_names.push_back(virtual_force_sensor[i*10+0]);
148  }
149  // add ports for all force sensors
150  unsigned int nforce = npforce + nvforce;
151  m_wrenches.resize(nforce);
152  m_wrenchesOut.resize(nforce);
153  for (unsigned int i=0; i<nforce; i++){
154  m_wrenchesOut[i] = new OutPort<TimedDoubleSeq>(std::string(fsensor_names[i]+"Ref").c_str(), m_wrenches[i]);
155  m_wrenches[i].data.length(6);
156  registerOutPort(std::string(fsensor_names[i]+"Ref").c_str(), *m_wrenchesOut[i]);
157  }
158 
159  if (prop.hasKey("seq_optional_data_dim")) {
160  coil::stringTo(optional_data_dim, prop["seq_optional_data_dim"].c_str());
161  } else {
162  optional_data_dim = 1;
163  }
164 
165  m_seq = new seqplay(dof, dt, nforce, optional_data_dim);
166 
167  m_qInit.data.length(dof);
168  for (unsigned int i=0; i<dof; i++) m_qInit.data[i] = 0.0;
169  Link *root = m_robot->rootLink();
170  m_basePosInit.data.x = root->p[0]; m_basePosInit.data.y = root->p[1]; m_basePosInit.data.z = root->p[2];
172  m_baseRpyInit.data.r = rpy[0]; m_baseRpyInit.data.p = rpy[1]; m_baseRpyInit.data.y = rpy[2];
173  m_zmpRefInit.data.x = 0; m_zmpRefInit.data.y = 0; m_zmpRefInit.data.z = 0;
174 
175  // allocate memory for outPorts
176  m_qRef.data.length(dof);
177  m_tqRef.data.length(dof);
178  m_optionalData.data.length(optional_data_dim);
179 
180  return RTC::RTC_OK;
181 }
182 
183 
184 
185 RTC::ReturnCode_t SequencePlayer::onFinalize()
186 {
187  if ( m_debugLevel > 0 ) {
188  std::cerr << __PRETTY_FUNCTION__ << std::endl;
189  }
190  return RTC::RTC_OK;
191 }
192 
193 /*
194  RTC::ReturnCode_t SequencePlayer::onStartup(RTC::UniqueId ec_id)
195  {
196  return RTC::RTC_OK;
197  }
198 */
199 
200 /*
201  RTC::ReturnCode_t SequencePlayer::onShutdown(RTC::UniqueId ec_id)
202  {
203  return RTC::RTC_OK;
204  }
205 */
206 
208 {
209  std::cout << "SequencePlayer::onActivated(" << ec_id << ")" << std::endl;
210 
211  return RTC::RTC_OK;
212 }
213 
214 /*
215  RTC::ReturnCode_t SequencePlayer::onDeactivated(RTC::UniqueId ec_id)
216  {
217  return RTC::RTC_OK;
218  }
219 */
220 
221 RTC::ReturnCode_t SequencePlayer::onExecute(RTC::UniqueId ec_id)
222 {
223  static int loop = 0;
224  loop++;
225  if ( m_debugLevel > 0 && loop % 1000 == 0) {
226  std::cerr << __PRETTY_FUNCTION__ << "(" << ec_id << ")" << std::endl;
227  }
228  if (m_qInitIn.isNew()) m_qInitIn.read();
232 
233  if (m_gname != "" && m_seq->isEmpty(m_gname.c_str())){
234  if (m_waitFlag){
235  m_gname = "";
236  m_waitFlag = false;
237  sem_post(&m_waitSem);
238  }
239  }
240  if (m_seq->isEmpty()){
241  m_clearFlag = false;
242  if (m_waitFlag){
243  m_waitFlag = false;
244  sem_post(&m_waitSem);
245  }
246  }else{
247  Guard guard(m_mutex);
248 
249  double zmp[3], acc[3], pos[3], rpy[3], wrenches[6*m_wrenches.size()];
250  m_seq->get(m_qRef.data.get_buffer(), zmp, acc, pos, rpy, m_tqRef.data.get_buffer(), wrenches, m_optionalData.data.get_buffer());
251  m_zmpRef.data.x = zmp[0];
252  m_zmpRef.data.y = zmp[1];
253  m_zmpRef.data.z = zmp[2];
254  m_accRef.data.ax = acc[0];
255  m_accRef.data.ay = acc[1];
256  m_accRef.data.az = acc[2];
257 
258  if (m_fixedLink != ""){
259  for (int i=0; i<m_robot->numJoints(); i++){
260  m_robot->joint(i)->q = m_qRef.data[i];
261  }
262  for (int i=0; i<3; i++){
263  m_robot->rootLink()->p[i] = pos[i];
264  }
265  m_robot->rootLink()->R = hrp::rotFromRpy(rpy[0], rpy[1], rpy[2]);
266  m_robot->calcForwardKinematics();
267  hrp::Link *root = m_robot->rootLink();
268  hrp::Vector3 rootP;
269  hrp::Matrix33 rootR;
270  if (m_timeToStartPlaying > 0){
272  hrp::Link *fixed = m_robot->link(m_fixedLink);
273  hrp::Matrix33 fixed2rootR = fixed->R.transpose()*root->R;
274  hrp::Vector3 fixed2rootP = fixed->R.transpose()*(root->p - fixed->p);
275  rootR = m_fixedR*fixed2rootR;
276  rootP = m_fixedR*fixed2rootP + m_fixedP;
277  }else{
278  rootR = m_offsetR*m_robot->rootLink()->R;
279  rootP = m_offsetR*m_robot->rootLink()->p + m_offsetP;
280  }
281  hrp::Vector3 rootRpy = hrp::rpyFromRot(rootR);
282  pos[0] = rootP[0];
283  pos[1] = rootP[1];
284  pos[2] = rootP[2];
285  rpy[0] = rootRpy[0];
286  rpy[1] = rootRpy[1];
287  rpy[2] = rootRpy[2];
288  }
289  m_basePos.data.x = pos[0];
290  m_basePos.data.y = pos[1];
291  m_basePos.data.z = pos[2];
292  m_baseRpy.data.r = rpy[0];
293  m_baseRpy.data.p = rpy[1];
294  m_baseRpy.data.y = rpy[2];
295  size_t force_i = 0;
296  for (size_t i = 0; i < m_wrenches.size(); i++) {
297  m_wrenches[i].data[0] = wrenches[force_i++];
298  m_wrenches[i].data[1] = wrenches[force_i++];
299  m_wrenches[i].data[2] = wrenches[force_i++];
300  m_wrenches[i].data[3] = wrenches[force_i++];
301  m_wrenches[i].data[4] = wrenches[force_i++];
302  m_wrenches[i].data[5] = wrenches[force_i++];
303  }
304  m_qRef.tm = m_qInit.tm;
305  m_qRefOut.write();
306  m_tqRefOut.write();
307  m_zmpRefOut.write();
308  m_accRefOut.write();
312  for (size_t i = 0; i < m_wrenchesOut.size(); i++) {
313  m_wrenchesOut[i]->write();
314  }
315 
316  if (m_clearFlag){
317  m_seq->clear(0.001);
318  }
319  }
320  return RTC::RTC_OK;
321 }
322 
323 /*
324  RTC::ReturnCode_t SequencePlayer::onAborting(RTC::UniqueId ec_id)
325  {
326  return RTC::RTC_OK;
327  }
328 */
329 
330 /*
331  RTC::ReturnCode_t SequencePlayer::onError(RTC::UniqueId ec_id)
332  {
333  return RTC::RTC_OK;
334  }
335 */
336 
337 /*
338  RTC::ReturnCode_t SequencePlayer::onReset(RTC::UniqueId ec_id)
339  {
340  return RTC::RTC_OK;
341  }
342 */
343 
344 /*
345  RTC::ReturnCode_t SequencePlayer::onStateUpdate(RTC::UniqueId ec_id)
346  {
347  return RTC::RTC_OK;
348  }
349 */
350 
351 /*
352  RTC::ReturnCode_t SequencePlayer::onRateChanged(RTC::UniqueId ec_id)
353  {
354  return RTC::RTC_OK;
355  }
356 */
357 
359 {
360  if ( m_debugLevel > 0 ) {
361  std::cerr << __PRETTY_FUNCTION__ << std::endl;
362  }
363  m_clearFlag = true;
364 }
365 
367 {
368  if ( m_debugLevel > 0 ) {
369  std::cerr << __PRETTY_FUNCTION__ << std::endl;
370  }
371  m_waitFlag = true;
372  sem_wait(&m_waitSem);
373 }
374 
376 {
377  if ( m_debugLevel > 0 ) {
378  std::cerr << __PRETTY_FUNCTION__ << std::endl;
379  }
380  m_gname = gname;
381  m_waitFlag = true;
382  sem_wait(&m_waitSem);
383  return true;
384 }
385 
386 
387 bool SequencePlayer::setJointAngle(short id, double angle, double tm)
388 {
389  if ( m_debugLevel > 0 ) {
390  std::cerr << __PRETTY_FUNCTION__ << std::endl;
391  }
392  Guard guard(m_mutex);
393  if (!setInitialState()) return false;
394  dvector q(m_robot->numJoints());
395  m_seq->getJointAngles(q.data());
396  q[id] = angle;
397  for (unsigned int i=0; i<m_robot->numJoints(); i++){
398  hrp::Link *j = m_robot->joint(i);
399  if (j) j->q = q[i];
400  }
401  m_robot->calcForwardKinematics();
402  hrp::Vector3 absZmp = m_robot->calcCM();
403  absZmp[2] = 0;
404  hrp::Link *root = m_robot->rootLink();
405  hrp::Vector3 relZmp = root->R.transpose()*(absZmp - root->p);
406  m_seq->setJointAngles(q.data(), tm);
407  m_seq->setZmp(relZmp.data(), tm);
408  return true;
409 }
410 
411 bool SequencePlayer::setJointAngles(const double *angles, double tm)
412 {
413  if ( m_debugLevel > 0 ) {
414  std::cerr << __PRETTY_FUNCTION__ << std::endl;
415  }
416  Guard guard(m_mutex);
417  if (!setInitialState()) return false;
418  for (unsigned int i=0; i<m_robot->numJoints(); i++){
419  hrp::Link *j = m_robot->joint(i);
420  if (j) j->q = angles[i];
421  }
422  m_robot->calcForwardKinematics();
423  hrp::Vector3 absZmp = m_robot->calcCM();
424  absZmp[2] = 0;
425  hrp::Link *root = m_robot->rootLink();
426  hrp::Vector3 relZmp = root->R.transpose()*(absZmp - root->p);
427  std::vector<const double*> v_poss;
428  std::vector<double> v_tms;
429  v_poss.push_back(angles);
430  v_tms.push_back(tm);
431  m_seq->setJointAnglesSequence(v_poss, v_tms);
432  m_seq->setZmp(relZmp.data(), tm);
433  return true;
434 }
435 
436 bool SequencePlayer::setJointAngles(const double *angles, const bool *mask,
437  double tm)
438 {
439  if ( m_debugLevel > 0 ) {
440  std::cerr << __PRETTY_FUNCTION__ << std::endl;
441  }
442  Guard guard(m_mutex);
443 
444  if (!setInitialState()) return false;
445 
446  double pose[m_robot->numJoints()];
447  for (unsigned int i=0; i<m_robot->numJoints(); i++){
448  pose[i] = mask[i] ? angles[i] : m_qInit.data[i];
449  }
450  m_seq->setJointAngles(pose, tm);
451  return true;
452 }
453 
454 bool SequencePlayer::setJointAnglesSequence(const OpenHRP::dSequenceSequence angless, const OpenHRP::bSequence& mask, const OpenHRP::dSequence& times)
455 {
456  if ( m_debugLevel > 0 ) {
457  std::cerr << __PRETTY_FUNCTION__ << std::endl;
458  }
459  Guard guard(m_mutex);
460 
461  if (!setInitialState()) return false;
462 
463  bool tmp_mask[robot()->numJoints()];
464  if (mask.length() != robot()->numJoints()) {
465  for (unsigned int i=0; i < robot()->numJoints(); i++) tmp_mask[i] = true;
466  }else{
467  for (unsigned int i=0; i < robot()->numJoints(); i++) tmp_mask[i] = mask.get_buffer()[i];
468  }
469  int len = angless.length();
470  std::vector<const double*> v_poss;
471  std::vector<double> v_tms;
472  for ( unsigned int i = 0; i < angless.length(); i++ ) v_poss.push_back(angless[i].get_buffer());
473  for ( unsigned int i = 0; i < times.length(); i++ ) v_tms.push_back(times[i]);
474  return m_seq->setJointAnglesSequence(v_poss, v_tms);
475 }
476 
478 {
479  if ( m_debugLevel > 0 ) {
480  std::cerr << __PRETTY_FUNCTION__ << std::endl;
481  }
482  Guard guard(m_mutex);
483 
484  if (!setInitialState()) return false;
485 
486  return m_seq->clearJointAngles();
487 }
488 
489 bool SequencePlayer::setJointAnglesSequenceOfGroup(const char *gname, const OpenHRP::dSequenceSequence angless, const OpenHRP::dSequence& times)
490 {
491  if ( m_debugLevel > 0 ) {
492  std::cerr << __PRETTY_FUNCTION__ << std::endl;
493  }
494  Guard guard(m_mutex);
495  if (!setInitialState()) return false;
496 
497  if (!m_seq->resetJointGroup(gname, m_qInit.data.get_buffer())) return false;
498 
499  std::vector<const double*> v_poss;
500  std::vector<double> v_tms;
501  for ( unsigned int i = 0; i < angless.length(); i++ ) v_poss.push_back(angless[i].get_buffer());
502  for ( unsigned int i = 0; i < times.length(); i++ ) v_tms.push_back(times[i]);
503  return m_seq->setJointAnglesSequenceOfGroup(gname, v_poss, v_tms, angless.length()>0?angless[0].length():0);
504 }
505 
507 {
508  if ( m_debugLevel > 0 ) {
509  std::cerr << __PRETTY_FUNCTION__ << std::endl;
510  }
511  Guard guard(m_mutex);
512  if (!setInitialState()) return false;
513 
514  if (!m_seq->resetJointGroup(gname, m_qInit.data.get_buffer())) return false;
515 
516  return m_seq->clearJointAnglesOfGroup(gname);
517 }
518 
519 bool SequencePlayer::setJointAnglesSequenceFull(const OpenHRP::dSequenceSequence i_jvss, const OpenHRP::dSequenceSequence i_vels, const OpenHRP::dSequenceSequence i_torques, const OpenHRP::dSequenceSequence i_poss, const OpenHRP::dSequenceSequence i_rpys, const OpenHRP::dSequenceSequence i_accs, const OpenHRP::dSequenceSequence i_zmps, const OpenHRP::dSequenceSequence i_wrenches, const OpenHRP::dSequenceSequence i_optionals, const dSequence i_tms)
520 {
521  if ( m_debugLevel > 0 ) {
522  std::cerr << __PRETTY_FUNCTION__ << std::endl;
523  }
524  Guard guard(m_mutex);
525 
526  if (!setInitialState()) return false;
527 
528  int len = i_jvss.length();
529  std::vector<const double*> v_jvss, v_vels, v_torques, v_poss, v_rpys, v_accs, v_zmps, v_wrenches, v_optionals;
530  std::vector<double> v_tms;
531  for ( unsigned int i = 0; i < i_jvss.length(); i++ ) v_jvss.push_back(i_jvss[i].get_buffer());
532  for ( unsigned int i = 0; i < i_vels.length(); i++ ) v_vels.push_back(i_vels[i].get_buffer());
533  for ( unsigned int i = 0; i < i_torques.length(); i++ ) v_torques.push_back(i_torques[i].get_buffer());
534  for ( unsigned int i = 0; i < i_poss.length(); i++ ) v_poss.push_back(i_poss[i].get_buffer());
535  for ( unsigned int i = 0; i < i_rpys.length(); i++ ) v_rpys.push_back(i_rpys[i].get_buffer());
536  for ( unsigned int i = 0; i < i_accs.length(); i++ ) v_accs.push_back(i_accs[i].get_buffer());
537  for ( unsigned int i = 0; i < i_zmps.length(); i++ ) v_zmps.push_back(i_zmps[i].get_buffer());
538  for ( unsigned int i = 0; i < i_wrenches.length(); i++ ) v_wrenches.push_back(i_wrenches[i].get_buffer());
539  for ( unsigned int i = 0; i < i_optionals.length(); i++ ) v_optionals.push_back(i_optionals[i].get_buffer());
540  for ( unsigned int i = 0; i < i_tms.length(); i++ ) v_tms.push_back(i_tms[i]);
541  return m_seq->setJointAnglesSequenceFull(v_jvss, v_vels, v_torques, v_poss, v_rpys, v_accs, v_zmps, v_wrenches, v_optionals, v_tms);
542 }
543 
544 bool SequencePlayer::setBasePos(const double *pos, double tm)
545 {
546  if ( m_debugLevel > 0 ) {
547  std::cerr << __PRETTY_FUNCTION__ << std::endl;
548  }
549  Guard guard(m_mutex);
550  m_seq->setBasePos(pos, tm);
551  return true;
552 }
553 
554 bool SequencePlayer::setBaseRpy(const double *rpy, double tm)
555 {
556  if ( m_debugLevel > 0 ) {
557  std::cerr << __PRETTY_FUNCTION__ << std::endl;
558  }
559  Guard guard(m_mutex);
560  m_seq->setBaseRpy(rpy, tm);
561  return true;
562 }
563 
564 bool SequencePlayer::setZmp(const double *zmp, double tm)
565 {
566  if ( m_debugLevel > 0 ) {
567  std::cerr << __PRETTY_FUNCTION__ << std::endl;
568  }
569  Guard guard(m_mutex);
570  m_seq->setZmp(zmp, tm);
571  return true;
572 }
573 
574 bool SequencePlayer::setWrenches(const double *wrenches, double tm)
575 {
576  Guard guard(m_mutex);
577  m_seq->setWrenches(wrenches, tm);
578  return true;
579 }
580 
581 bool SequencePlayer::setTargetPose(const char* gname, const double *xyz, const double *rpy, double tm, const char* frame_name)
582 {
583  if ( m_debugLevel > 0 ) {
584  std::cerr << __PRETTY_FUNCTION__ << std::endl;
585  }
586  Guard guard(m_mutex);
587  if (!setInitialState()) return false;
588  // setup
589  std::vector<int> indices;
590  hrp::dvector start_av, end_av;
591  std::vector<hrp::dvector> avs;
592  if (! m_seq->getJointGroup(gname, indices) ) {
593  std::cerr << "[setTargetPose] Could not find joint group " << gname << std::endl;
594  return false;
595  }
596  start_av.resize(indices.size());
597  end_av.resize(indices.size());
598 
599  //std::cerr << std::endl;
600  if ( ! m_robot->joint(indices[0])->parent ) {
601  std::cerr << "[setTargetPose] " << m_robot->joint(indices[0])->name << " does not have parent" << std::endl;
602  return false;
603  }
604  string base_parent_name = m_robot->joint(indices[0])->parent->name;
605  string target_name = m_robot->joint(indices[indices.size()-1])->name;
606  // prepare joint path
607  hrp::JointPathExPtr manip = hrp::JointPathExPtr(new hrp::JointPathEx(m_robot, m_robot->link(base_parent_name), m_robot->link(target_name), dt, true, std::string(m_profile.instance_name)));
608 
609  // calc fk
610  for (unsigned int i=0; i<m_robot->numJoints(); i++){
611  hrp::Link *j = m_robot->joint(i);
612  if (j) j->q = m_qRef.data.get_buffer()[i];
613  }
614  m_robot->calcForwardKinematics();
615  for ( unsigned int i = 0; i < manip->numJoints(); i++ ){
616  start_av[i] = manip->joint(i)->q;
617  }
618 
619  // xyz and rpy are relateive to root link, where as pos and rotatoin of manip->calcInverseKinematics are relative to base link
620 
621  // ik params
622  hrp::Vector3 start_p(m_robot->link(target_name)->p);
623  hrp::Matrix33 start_R(m_robot->link(target_name)->R);
624  hrp::Vector3 end_p(xyz[0], xyz[1], xyz[2]);
625  hrp::Matrix33 end_R = m_robot->link(target_name)->calcRfromAttitude(hrp::rotFromRpy(rpy[0], rpy[1], rpy[2]));
626 
627  // change start and end must be relative to the frame_name
628  if ( (frame_name != NULL) && (! m_robot->link(frame_name) ) ) {
629  std::cerr << "[setTargetPose] Could not find frame_name " << frame_name << std::endl;
630  return false;
631  } else if ( frame_name != NULL ) {
632  hrp::Vector3 frame_p(m_robot->link(frame_name)->p);
633  hrp::Matrix33 frame_R(m_robot->link(frame_name)->attitude());
634  // fix start/end references from root to frame;
635  end_p = frame_R * end_p + frame_p;
636  end_R = frame_R * end_R;
637  }
638  manip->setMaxIKError(m_error_pos,m_error_rot);
639  manip->setMaxIKIteration(m_iteration);
640  std::cerr << "[setTargetPose] Solveing IK with frame " << (frame_name? frame_name:"world_frame") << ", Error " << m_error_pos << m_error_rot << ", Iteration " << m_iteration << std::endl;
641  std::cerr << " Start\n" << start_p << "\n" << start_R<< std::endl;
642  std::cerr << " End\n" << end_p << "\n" << end_R<< std::endl;
643 
644  // interpolate & calc ik
645  int len = max(((start_p - end_p).norm() / 0.02 ), // 2cm
646  ((hrp::omegaFromRot(start_R.transpose() * end_R).norm()) / 0.025)); // 2 deg
647  len = max(len, 1);
648 
649  std::vector<const double *> v_pos;
650  std::vector<double> v_tm;
651  v_pos.resize(len);
652  v_tm.resize(len);
653 
654  // do loop
655  for (int i = 0; i < len; i++ ) {
656  double a = (1+i)/(double)len;
657  hrp::Vector3 p = (1-a)*start_p + a*end_p;
658  hrp::Vector3 omega = hrp::omegaFromRot(start_R.transpose() * end_R);
659  hrp::Matrix33 R = start_R * rodrigues(omega.isZero()?omega:omega.normalized(), a*omega.norm());
660  bool ret = manip->calcInverseKinematics2(p, R);
661 
662  if ( m_debugLevel > 0 ) {
663  // for debug
664  std::cerr << "target pos/rot : " << i << "/" << a << " : "
665  << p[0] << " " << p[1] << " " << p[2] << ","
666  << omega[0] << " " << omega[1] << " " << omega[2] << std::endl;
667  }
668  if ( ! ret ) {
669  std::cerr << "[setTargetPose] IK failed" << std::endl;
670  return false;
671  }
672  v_pos[i] = (const double *)malloc(sizeof(double)*manip->numJoints());
673  for ( unsigned int j = 0; j < manip->numJoints(); j++ ){
674  ((double *)v_pos[i])[j] = manip->joint(j)->q;
675  }
676  v_tm[i] = tm/len;
677  }
678 
679  if ( m_debugLevel > 0 ) {
680  // for debug
681  for(int i = 0; i < len; i++ ) {
682  std::cerr << v_tm[i] << ":";
683  for(int j = 0; j < start_av.size(); j++ ) {
684  std::cerr << v_pos[i][j] << " ";
685  }
686  std::cerr << std::endl;
687  }
688  }
689 
690  if (!m_seq->resetJointGroup(gname, m_qInit.data.get_buffer())) return false; // reset sequencer
691  bool ret = m_seq->playPatternOfGroup(gname, v_pos, v_tm, m_qInit.data.get_buffer(), v_pos.size()>0?indices.size():0);
692 
693  // clean up memory, need to improve
694  for (int i = 0; i < len; i++ ) {
695  free((double *)v_pos[i]);
696  }
697 
698  return ret;
699 }
700 
701 void SequencePlayer::loadPattern(const char *basename, double tm)
702 {
703  if ( m_debugLevel > 0 ) {
704  std::cerr << __PRETTY_FUNCTION__ << std::endl;
705  }
706  Guard guard(m_mutex);
707  if (setInitialState()){
708  if (m_fixedLink != ""){
709  hrp::Link *l = m_robot->link(m_fixedLink);
710  if (!l) {
711  std::cerr << __PRETTY_FUNCTION__ << "can't find a fixed link("
712  << m_fixedLink << ")" << std::endl;
713  m_fixedLink = "";
714  return;
715  }
716  m_robot->calcForwardKinematics(); // this is not called by setinitialstate()
717  m_fixedP = l->p;
718  m_fixedR = l->R;
719 
720  std::string pos = std::string(basename)+".pos";
721  std::string wst = std::string(basename)+".waist";
722  std::ifstream ifspos(pos.c_str());
723  std::ifstream ifswst(wst.c_str());
724  if (!ifspos.is_open() || !ifswst.is_open()){
725  std::cerr << __PRETTY_FUNCTION__ << "can't open " << pos << " or "
726  << wst << ")" << std::endl;
727  m_fixedLink = "";
728  return;
729  }
730  double time;
731  ifspos >> time;
732  for (int i=0; i<m_robot->numJoints(); i++){
733  ifspos >> m_robot->joint(i)->q;
734  }
735  ifswst >> time;
736  for (int i=0; i<3; i++) ifswst >> m_robot->rootLink()->p[i];
737  hrp::Vector3 rpy;
738  for (int i=0; i<3; i++) ifswst >> rpy[i];
739  m_robot->rootLink()->R = hrp::rotFromRpy(rpy);
740  m_robot->calcForwardKinematics();
741 
742  m_offsetR = m_fixedR*l->R.transpose();
743  m_offsetP = m_fixedP - m_offsetR*l->p;
745  }
746  m_seq->loadPattern(basename, tm);
747  }
748 }
749 
751 {
752  if ( m_debugLevel > 0 ) {
753  std::cerr << __PRETTY_FUNCTION__ << "m_seq-isEmpty() " << m_seq->isEmpty() << ", m_Init.data.length() " << m_qInit.data.length() << std::endl;
754  }
755  if (!m_seq->isEmpty()) return true;
756 
757  if (m_qInit.data.length() == 0){
758  std::cerr << "can't determine initial posture" << std::endl;
759  return false;
760  }else{
761  m_seq->setJointAngles(m_qInit.data.get_buffer(), tm);
762  for (unsigned int i=0; i<m_robot->numJoints(); i++){
763  Link *l = m_robot->joint(i);
764  l->q = m_qInit.data[i];
765  m_qRef.data[i] = m_qInit.data[i]; // update m_qRef for setTargetPose()
766  }
767 
768  Link *root = m_robot->rootLink();
769 
770  root->p << m_basePosInit.data.x,
771  m_basePosInit.data.y,
772  m_basePosInit.data.z;
773  m_seq->setBasePos(root->p.data(), tm);
774 
775  double rpy[] = {m_baseRpyInit.data.r,
776  m_baseRpyInit.data.p,
777  m_baseRpyInit.data.y};
778  m_seq->setBaseRpy(rpy, tm);
779  calcRotFromRpy(root->R, rpy[0], rpy[1], rpy[2]);
780 
781  double zmp[] = {m_zmpRefInit.data.x, m_zmpRefInit.data.y, m_zmpRefInit.data.z};
782  m_seq->setZmp(zmp, tm);
783  double zero[] = {0,0,0};
784  m_seq->setBaseAcc(zero, tm);
785  return true;
786  }
787 }
788 
789 void SequencePlayer::playPattern(const dSequenceSequence& pos, const dSequenceSequence& rpy, const dSequenceSequence& zmp, const dSequence& tm)
790 {
791  if ( m_debugLevel > 0 ) {
792  std::cerr << __PRETTY_FUNCTION__ << std::endl;
793  }
794  Guard guard(m_mutex);
795  if (!setInitialState()) return;
796 
797  std::vector<const double *> v_pos, v_rpy, v_zmp;
798  std::vector<double> v_tm;
799  for ( unsigned int i = 0; i < pos.length(); i++ ) v_pos.push_back(pos[i].get_buffer());
800  for ( unsigned int i = 0; i < rpy.length(); i++ ) v_rpy.push_back(rpy[i].get_buffer());
801  for ( unsigned int i = 0; i < zmp.length(); i++ ) v_zmp.push_back(zmp[i].get_buffer());
802  for ( unsigned int i = 0; i < tm.length() ; i++ ) v_tm.push_back(tm[i]);
803  return m_seq->playPattern(v_pos, v_rpy, v_zmp, v_tm, m_qInit.data.get_buffer(), pos.length()>0?pos[0].length():0);
804 }
805 
806 bool SequencePlayer::setInterpolationMode(OpenHRP::SequencePlayerService::interpolationMode i_mode_)
807 {
808  if ( m_debugLevel > 0 ) {
809  std::cerr << __PRETTY_FUNCTION__ << std::endl;
810  }
811  Guard guard(m_mutex);
813  if (i_mode_ == OpenHRP::SequencePlayerService::LINEAR){
814  new_mode = interpolator::LINEAR;
815  }else if (i_mode_ == OpenHRP::SequencePlayerService::HOFFARBIB){
816  new_mode = interpolator::HOFFARBIB;
817  }else{
818  return false;
819  }
820  return m_seq->setInterpolationMode(new_mode);
821 }
822 
823 bool SequencePlayer::addJointGroup(const char *gname, const OpenHRP::SequencePlayerService::StrSequence& jnames)
824 {
825  std::cerr << "[addJointGroup] group name = " << gname << std::endl;
826  if ( m_debugLevel > 0 ) {
827  std::cerr << __PRETTY_FUNCTION__ << std::endl;
828  }
829  if (!waitInterpolationOfGroup(gname)) return false;
830 
831  Guard guard(m_mutex);
832  std::vector<int> indices;
833  for (size_t i=0; i<jnames.length(); i++){
834  hrp::Link *l = m_robot->link(std::string(jnames[i]));
835  if (l){
836  indices.push_back(l->jointId);
837  }else{
838  std::cerr << "[addJointGroup] link name " << jnames[i] << "is not found" << std::endl;
839  return false;
840  }
841  }
842  return m_seq->addJointGroup(gname, indices);
843 }
844 
845 bool SequencePlayer::removeJointGroup(const char *gname)
846 {
847  std::cerr << "[removeJointGroup] group name = " << gname << std::endl;
848  if (!waitInterpolationOfGroup(gname)) return false;
849  bool ret;
850  {
851  Guard guard(m_mutex);
852  if (!setInitialState()) return false;
853  ret = m_seq->removeJointGroup(gname);
854  }
855  return ret;
856 }
857 
858 bool SequencePlayer::setJointAnglesOfGroup(const char *gname, const dSequence& jvs, double tm)
859 {
860  if ( m_debugLevel > 0 ) {
861  std::cerr << __PRETTY_FUNCTION__ << std::endl;
862  }
863  Guard guard(m_mutex);
864  if (!setInitialState()) return false;
865 
866  if (!m_seq->resetJointGroup(gname, m_qInit.data.get_buffer())) return false;
867  return m_seq->setJointAnglesOfGroup(gname, jvs.get_buffer(), jvs.length(), tm);
868 }
869 
870 bool SequencePlayer::playPatternOfGroup(const char *gname, const dSequenceSequence& pos, const dSequence& tm)
871 {
872  if ( m_debugLevel > 0 ) {
873  std::cerr << __PRETTY_FUNCTION__ << std::endl;
874  }
875  Guard guard(m_mutex);
876  if (!setInitialState()) return false;
877 
878  std::vector<const double *> v_pos;
879  std::vector<double> v_tm;
880  for ( unsigned int i = 0; i < pos.length(); i++ ) v_pos.push_back(pos[i].get_buffer());
881  for ( unsigned int i = 0; i < tm.length() ; i++ ) v_tm.push_back(tm[i]);
882  return m_seq->playPatternOfGroup(gname, v_pos, v_tm, m_qInit.data.get_buffer(), pos.length()>0?pos[0].length():0);
883 }
884 
885 void SequencePlayer::setMaxIKError(double pos, double rot){
886  m_error_pos = pos;
887  m_error_rot = rot;
888 }
889 
891  m_iteration= iter;
892 }
893 
894 
895 extern "C"
896 {
897 
899  {
901  manager->registerFactory(profile,
902  RTC::Create<SequencePlayer>,
903  RTC::Delete<SequencePlayer>);
904  }
905 
906 };
907 
908 
ComponentProfile m_profile
png_infop png_charpp int png_charpp profile
hrp::Matrix33 m_offsetR
#define max(a, b)
static const char * sequenceplayer_spec[]
bool setTargetPose(const char *gname, const double *xyz, const double *rpy, double tm, const char *frame_name)
unsigned int m_debugLevel
bool setJointAnglesSequence(std::vector< const double * > pos, std::vector< double > tm)
Definition: seqplay.cpp:598
virtual RTC::ReturnCode_t onFinalize()
bool isEmpty() const
Definition: seqplay.cpp:103
bool setJointAnglesSequenceFull(const OpenHRP::dSequenceSequence i_jvss, const OpenHRP::dSequenceSequence i_vels, const OpenHRP::dSequenceSequence i_torques, const OpenHRP::dSequenceSequence i_poss, const OpenHRP::dSequenceSequence i_rpys, const OpenHRP::dSequenceSequence i_accs, const OpenHRP::dSequenceSequence i_zmps, const OpenHRP::dSequenceSequence i_wrenches, const OpenHRP::dSequenceSequence i_optionals, const dSequence i_tms)
void SequencePlayerInit(RTC::Manager *manager)
hrp::BodyPtr robot()
bool stringTo(To &val, const char *str)
bool clearJointAngles()
Definition: seqplay.cpp:644
void getJointAngles(double *i_qRef)
Definition: seqplay.cpp:162
bool setWrenches(const double *wrenches, double tm)
void setZmp(const double *i_zmp, double i_tm=0.0)
Definition: seqplay.cpp:168
void setBaseAcc(const double *i_acc, double i_tm=0.0)
Definition: seqplay.cpp:195
bool setJointAnglesOfGroup(const char *gname, const double *i_qRef, const size_t i_qsize, double i_tm=0.0)
Definition: seqplay.cpp:489
bool setZmp(const double *zmp, double tm)
sequence player component
char * malloc()
png_infop png_charpp name
virtual RTC::ReturnCode_t onActivated(RTC::UniqueId ec_id)
RTC::ReturnCode_t ret(RTC::Local::ReturnCode_t r)
std::vector< TimedDoubleSeq > m_wrenches
coil::Guard< coil::Mutex > Guard
bool setJointAngles(const double *angles, double tm)
TimedDoubleSeq m_tqRef
vstring split(const std::string &input, const std::string &delimiter, bool ignore_empty)
OutPort< TimedOrientation3D > m_baseRpyOut
HRP_UTIL_EXPORT void calcRotFromRpy(Matrix33 &out_R, double r, double p, double y)
void player(SequencePlayer *i_player)
bool getJointGroup(const char *gname, std::vector< int > &indices)
Definition: seqplay.cpp:437
void setBaseRpy(const double *i_rpy, double i_tm=0.0)
Definition: seqplay.cpp:186
CORBA::ORB_ptr getORB()
boost::shared_ptr< JointPathEx > JointPathExPtr
Definition: JointPathEx.h:67
std::string basename(const std::string name)
png_uint_32 i
Properties * hasKey(const char *key) const
Eigen::VectorXd dvector
coil::Properties & getProperties()
std::string m_fixedLink
bool setJointAnglesSequence(const OpenHRP::dSequenceSequence angless, const OpenHRP::bSequence &mask, const OpenHRP::dSequence &times)
static Manager & instance()
InPort< TimedPoint3D > m_basePosInitIn
InPort< TimedDoubleSeq > m_qInitIn
bool addOutPort(const char *name, OutPortBase &outport)
bool removeJointGroup(const char *gname)
boost::shared_ptr< Body > BodyPtr
TimedDoubleSeq m_qInit
void loadPattern(const char *basename, double time)
hrp::BodyPtr m_robot
SequencePlayerService_impl m_service0
bool addJointGroup(const char *gname, const OpenHRP::SequencePlayerService::StrSequence &jnames)
Eigen::Vector3d Vector3
bool removeJointGroup(const char *gname, double time=2.5)
Definition: seqplay.cpp:452
bool setJointAnglesSequenceOfGroup(const char *gname, std::vector< const double * > pos, std::vector< double > tm, const size_t pos_size)
Definition: seqplay.cpp:762
Matrix33 rotFromRpy(const Vector3 &rpy)
bool setJointAnglesSequenceOfGroup(const char *gname, const OpenHRP::dSequenceSequence angless, const OpenHRP::dSequence &times)
bool addJointGroup(const char *gname, const std::vector< int > &indices)
Definition: seqplay.cpp:424
bool setBaseRpy(const double *rpy, double tm)
Eigen::Matrix3d Matrix33
coil::Mutex m_mutex
bool resetJointGroup(const char *gname, const double *full)
Definition: seqplay.cpp:465
std::vector< std::string > vstring
void setBasePos(const double *i_pos, double i_tm=0.0)
Definition: seqplay.cpp:177
bool setBasePos(const double *pos, double tm)
coil::Properties & getConfig()
bool waitInterpolationOfGroup(const char *gname)
bool clearJointAnglesOfGroup(const char *gname)
OutPort< TimedPoint3D > m_zmpRefOut
ExecutionContextHandle_t UniqueId
bool bindParameter(const char *param_name, VarType &var, const char *def_val, bool(*trans)(VarType &, const char *)=coil::stringTo)
OutPort< TimedDoubleSeq > m_tqRefOut
def j(str, encoding="cp932")
void playPattern(const OpenHRP::dSequenceSequence &pos, const OpenHRP::dSequenceSequence &rpy, const OpenHRP::dSequenceSequence &zmp, const OpenHRP::dSequence &tm)
Matrix33 rodrigues(const Vector3 &axis, double q)
TimedPoint3D m_zmpRefInit
bool playPatternOfGroup(const char *gname, std::vector< const double * > pos, std::vector< double > tm, const double *qInit, unsigned int len)
Definition: seqplay.cpp:530
int loadBodyFromModelLoader(::World *world, const char *name, const char *url, CosNaming::NamingContext_var cxt)
void setMaxIKIteration(short iter)
int free()
bool setInterpolationMode(interpolator::interpolation_mode i_mode_)
Definition: seqplay.cpp:407
void get(double *o_q, double *o_zmp, double *o_accel, double *o_basePos, double *o_baseRpy, double *o_tq, double *o_wrenches, double *o_optional_data)
Definition: seqplay.cpp:354
TimedAcceleration3D m_accRef
std::string m_gname
OutPort< TimedAcceleration3D > m_accRefOut
hrp::Vector3 m_fixedP
virtual ~SequencePlayer()
void clear(double i_timeLimit=0)
Definition: seqplay.cpp:263
#define __PRETTY_FUNCTION__
void setMaxIKError(double pos, double rot)
bool setJointAngle(short id, double angle, double tm)
prop
TimedDoubleSeq m_optionalData
bool setJointAnglesOfGroup(const char *gname, const OpenHRP::dSequence &jvs, double tm)
TimedDoubleSeq m_qRef
def norm(a)
HRP_UTIL_EXPORT Vector3 rpyFromRot(const Matrix33 &m)
naming
TimedOrientation3D m_baseRpy
bool setInitialState(double tm=0.0)
InPort< TimedPoint3D > m_zmpRefInitIn
TimedPoint3D m_basePos
TimedPoint3D m_zmpRef
OutPort< TimedDoubleSeq > m_qRefOut
virtual bool isNew()
void loadPattern(const char *i_basename, double i_tm)
Definition: seqplay.cpp:273
bool setInterpolationMode(OpenHRP::SequencePlayerService::interpolationMode i_mode_)
static int id
bool addPort(PortBase &port)
OutPort< TimedDoubleSeq > m_optionalDataOut
virtual bool write(DataType &value)
hrp::Matrix33 m_fixedR
HRP_UTIL_EXPORT Vector3 omegaFromRot(const Matrix33 &r)
virtual RTC::ReturnCode_t onInitialize()
hrp::BodyPtr m_robot
RTC::CorbaPort m_SequencePlayerServicePort
bool setJointAnglesSequenceFull(std::vector< const double * > pos, std::vector< const double * > vel, std::vector< const double * > torques, std::vector< const double * > bpos, std::vector< const double * > brpy, std::vector< const double * > bacc, std::vector< const double * > zmps, std::vector< const double * > wrenches, std::vector< const double * > optionals, std::vector< double > tm)
Definition: seqplay.cpp:660
bool clearJointAnglesOfGroup(const char *gname)
Definition: seqplay.cpp:841
OutPort< TimedPoint3D > m_basePosOut
SequencePlayer(RTC::Manager *manager)
bool playPatternOfGroup(const char *gname, const OpenHRP::dSequenceSequence &pos, const OpenHRP::dSequence &tm)
bool addInPort(const char *name, InPortBase &inport)
void registerOutPort(const char *name, OutPortBase &outport)
InPort< TimedOrientation3D > m_baseRpyInitIn
TimedPoint3D m_basePosInit
bool registerFactory(coil::Properties &profile, RtcNewFunc new_func, RtcDeleteFunc delete_func)
double m_timeToStartPlaying
void setJointAngles(const double *i_qRef, double i_tm=0.0)
Definition: seqplay.cpp:153
hrp::Vector3 m_offsetP
void playPattern(std::vector< const double * > pos, std::vector< const double * > zmp, std::vector< const double * > rpy, std::vector< double > tm, const double *qInit, unsigned int len)
Definition: seqplay.cpp:221
void setWrenches(const double *i_wrenches, double i_tm=0.0)
Definition: seqplay.cpp:204
size_t optional_data_dim
bool registerProvider(const char *instance_name, const char *type_name, PortableServer::RefCountServantBase &provider)
virtual RTC::ReturnCode_t onExecute(RTC::UniqueId ec_id)
std::vector< OutPort< TimedDoubleSeq > * > m_wrenchesOut
TimedOrientation3D m_baseRpyInit


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