seqplay.cpp
Go to the documentation of this file.
1 // -*- mode: c++; indent-tabs-mode: t; tab-width: 4; c-basic-offset: 4; -*-
2 
3 #include <iostream>
4 #include <unistd.h>
5 #include "seqplay.h"
6 
7 #define deg2rad(x) ((x)*M_PI/180)
8 
9 seqplay::seqplay(unsigned int i_dof, double i_dt, unsigned int i_fnum, unsigned int optional_data_dim) : m_dof(i_dof)
10 {
11  interpolators[Q] = new interpolator(i_dof, i_dt);
12  interpolators[ZMP] = new interpolator(3, i_dt);
13  interpolators[ACC] = new interpolator(3, i_dt);
14  interpolators[P] = new interpolator(3, i_dt);
15  interpolators[RPY] = new interpolator(3, i_dt);
16  interpolators[TQ] = new interpolator(i_dof, i_dt);
17  interpolators[WRENCHES] = new interpolator(6 * i_fnum, i_dt, interpolator::HOFFARBIB, 100); // wrenches = 6 * [number of force sensors]
18  interpolators[OPTIONAL_DATA] = new interpolator(optional_data_dim, i_dt);
19  // Set interpolator name
20  interpolators[Q]->setName("Q");
21  interpolators[ZMP]->setName("ZMP");
22  interpolators[ACC]->setName("ACC");
23  interpolators[P]->setName("P");
24  interpolators[RPY]->setName("RPY");
25  interpolators[TQ]->setName("TQ");
26  interpolators[WRENCHES]->setName("WRENCHES");
27  interpolators[OPTIONAL_DATA]->setName("OPTIONAL_DATA");
28  //
29 
30 #ifdef WAIST_HEIGHT
31  double initial_zmp[3] = {0,0,-WAIST_HEIGHT};
32  double initial_waist[3] = {0,0,WAIST_HEIGHT};
33  interpolators[P]->set(initial_waist);
34 #elif defined(INITIAL_ZMP_REF_X)
35  double initial_zmp[3] = {INITIAL_ZMP_REF_X, 0, INITIAL_ZMP_REF_Z};
36 #else
37  double initial_zmp[] = {0,0,0};
38 #endif
39  interpolators[ZMP]->set(initial_zmp);
40  double initial_wrenches[6 * i_fnum];
41  for (size_t i = 0; i < 6 * i_fnum; i++) initial_wrenches[i] = 0;
42  interpolators[WRENCHES]->set(initial_wrenches);
43  double initial_optional_data[optional_data_dim];
44  for (size_t i = 0; i < optional_data_dim; i++) initial_optional_data[i] = 0;
45  interpolators[OPTIONAL_DATA]->set(initial_optional_data);
46 }
47 
49 {
50  for (unsigned int i=0; i<NINTERPOLATOR; i++){
51  delete interpolators[i];
52  }
53 }
54 
55 #if 0 // TODO
56 void seqplay::goHalfSitting(double tm)
57 {
58  if (tm == 0){
59  tm = (double)angle_interpolator->calc_interpolation_time(get_half_sitting_posture());
60  }
61  angle_interpolator->setGoal(get_half_sitting_posture(), tm);
62 #ifdef INITIAL_ZMP_REF_X
63  double zmp[]={INITIAL_ZMP_REF_X, 0, INITIAL_ZMP_REF_Z};
64 #else
65  double zmp[] = {0,0,0};
66 #endif
67  zmp_interpolator->setGoal(zmp, tm);
68 #ifdef INITIAL_ZMP_REF_Z
69  double waist_pos[]={ref_state.basePosAtt.px,
70  ref_state.basePosAtt.py,
71  -INITIAL_ZMP_REF_Z};
72 #else
73  double waist_pos[] = {0,0,0};
74 #endif
75  waist_pos_interpolator->setGoal(waist_pos, tm);
76  double p[3], rpy[3];
77  OpenHRP::convTransformToPosRpy(ref_state.basePosAtt, p, rpy);
78  double initial_rpy[3]={0,0,rpy[2]};
79  waist_rpy_interpolator->setGoal(initial_rpy, tm);
80 }
81 
82 void seqplay::goInitial(double tm)
83 {
84  if (tm == 0){
85  tm = (double)angle_interpolator->calc_interpolation_time(get_initial_posture());
86  }
87  angle_interpolator->setGoal(get_initial_posture(), tm);
88 #ifdef WAIST_HEIGHT
89  double zmp[] = {0,0,-WAIST_HEIGHT};
90  double pos[] = {ref_state.basePosAtt.px,
91  ref_state.basePosAtt.py,
92  WAIST_HEIGHT};
93  waist_pos_interpolator->setGoal(pos, tm);
94 #elif defined(INITIAL_ZMP_REF_Z)
95  double zmp[] = {0,0, INITIAL_ZMP_REF_Z};
96 #else
97  double zmp[] = {0,0,0};
98 #endif
99  zmp_interpolator->setGoal(zmp, tm);
100 }
101 #endif
102 
103 bool seqplay::isEmpty() const
104 {
105  for (unsigned int i=0; i<NINTERPOLATOR; i++){
106  if (!interpolators[i]->isEmpty()) return false;
107  }
108  std::map<std::string, groupInterpolator *>::const_iterator it;
109  for (it=groupInterpolators.begin(); it!=groupInterpolators.end(); it++){
110  groupInterpolator *gi = it->second;
111  if (gi && !gi->isEmpty()) return false;
112  }
113 
114  return true;
115 }
116 
117 bool seqplay::isEmpty(const char *gname)
118 {
119  char *s = (char *)gname; while(*s) {*s=toupper(*s);s++;}
121  if (!i) return true;
122  return i->isEmpty();
123 }
124 
125 #if 0
126 void seqplay::setReferenceState(const ::CharacterState& ref, double tm)
127 {
128  if (tm == 0){
129  tm = (double)angle_interpolator->calc_interpolation_time(ref.angle);
130  }
131  if (ref.angle.length()>0) angle_interpolator->setGoal(ref.angle, tm, false);
132  if (ref.velocity.length()>0)
133  velocity_interpolator->setGoal(ref.velocity, tm, false);
134  if (ref.zmp.length()>0) zmp_interpolator->setGoal(ref.zmp, tm, false);
135  if (ref.accel.length()>0 && ref.accel[0].length() == 3)
136  acc_interpolator->setGoal(ref.accel[0], tm, false);
137  double p[3], rpy[3];
138  OpenHRP::convTransformToPosRpy(ref.basePosAtt, p, rpy);
139  waist_pos_interpolator->setGoal(p, tm, false);
140  waist_rpy_interpolator->setGoal(rpy, tm, false);
141 
142  sync();
143 }
144 
145 void seqplay::getReferenceState(::CharacterState_out ref)
146 {
147  ref = new CharacterState;
148  *ref = ref_state;
149 }
150 #endif
151 
152 
153 void seqplay::setJointAngles(const double *jvs, double tm)
154 {
155  if (tm == 0){
156  interpolators[Q]->set(jvs);
157  }else{
158  interpolators[Q]->setGoal(jvs, tm);
159  }
160 }
161 
162 void seqplay::getJointAngles(double *jvs)
163 {
164  interpolators[Q]->get(jvs, false);
165 }
166 
167 
168 void seqplay::setZmp(const double *i_zmp, double i_tm)
169 {
170  if (i_tm == 0){
171  interpolators[ZMP]->set(i_zmp);
172  }else{
173  interpolators[ZMP]->setGoal(i_zmp, i_tm);
174  }
175 }
176 
177 void seqplay::setBasePos(const double *i_pos, double i_tm)
178 {
179  if (i_tm == 0){
180  interpolators[P]->set(i_pos);
181  }else{
182  interpolators[P]->setGoal(i_pos, i_tm);
183  }
184 }
185 
186 void seqplay::setBaseRpy(const double *i_rpy, double i_tm)
187 {
188  if (i_tm == 0){
189  interpolators[RPY]->set(i_rpy);
190  }else{
191  interpolators[RPY]->setGoal(i_rpy, i_tm);
192  }
193 }
194 
195 void seqplay::setBaseAcc(const double *i_acc, double i_tm)
196 {
197  if (i_tm == 0){
198  interpolators[ACC]->set(i_acc);
199  }else{
200  interpolators[ACC]->setGoal(i_acc, i_tm);
201  }
202 }
203 
204 void seqplay::setWrenches(const double *i_wrenches, double i_tm)
205 {
206  if (i_tm == 0){
207  interpolators[WRENCHES]->set(i_wrenches);
208  }else{
209  interpolators[WRENCHES]->setGoal(i_wrenches, i_tm);
210  }
211 }
212 
213 void seqplay::setJointAngle(unsigned int i_rank, double jv, double tm)
214 {
215  double pos[m_dof];
216  getJointAngles(pos);
217  pos[i_rank] = jv;
218  interpolators[Q]->setGoal(pos, tm);
219 }
220 
221 void seqplay::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)
222 {
223  const double *q=NULL, *z=NULL, *a=NULL, *p=NULL, *e=NULL, *tq=NULL, *wr=NULL, *od=NULL; double t=0;
224  double *v = new double[len];
225  for (unsigned int i=0; i<pos.size(); i++){
226  q = pos[i];
227  if (i < pos.size() - 1 ) {
228  double t0, t1;
229  if (tm.size() == pos.size()) {
230  t0 = tm[i]; t1 = tm[i+1];
231  } else {
232  t0 = t1 = tm[0];
233  }
234  const double *q_next = pos[i+1];
235  const double *q_prev
236  = i==0 ? qInit : pos[i-1];
237  for (unsigned int j = 0; j < len; j++) {
238  double d0, d1, v0, v1;
239  d0 = (q[j] - q_prev[j]);
240  d1 = (q_next[j] - q[j]);
241  v0 = d0/t0;
242  v1 = d1/t1;
243  if ( v0 * v1 >= 0 ) {
244  v[j] = 0.5 * (v0 + v1);
245  } else {
246  v[j] = 0;
247  }
248  }
249  } else {
250  for (unsigned int j = 0; j < len; j++) { v[j] = 0.0; }
251  }
252  if (i < zmp.size()) z = zmp[i];
253  if (i < rpy.size()) e = rpy[i];
254  if (i < tm.size()) t = tm[i];
255  go(q, z, a, p, e, tq, wr, od,
256  v, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
257  t, false);
258  }
259  sync();
260  delete [] v;
261 }
262 
263 void seqplay::clear(double i_timeLimit)
264 {
265  tick_t t1 = get_tick();
266  while (!isEmpty()){
267  if (i_timeLimit > 0
268  && tick2sec(get_tick()-t1)>=i_timeLimit) break;
269  pop_back();
270  }
271 }
272 
273 void seqplay::loadPattern(const char *basename, double tm)
274 {
275  double scale = 1.0;
276  bool found = false;
277  if (debug_level > 0) cout << "pos = ";
278  string pos = basename; pos.append(".pos");
279  if (access(pos.c_str(),0)==0){
280  found = true;
281  interpolators[Q]->load(pos, tm, scale, false);
282  if (debug_level > 0) cout << pos;
283  }
284  if (debug_level > 0) cout << endl << "zmp = ";
285  string zmp = basename; zmp.append(".zmp");
286  if (access(zmp.c_str(),0)==0){
287  found = true;
288  interpolators[ZMP]->load(zmp, tm, scale, false);
289  if (debug_level > 0) cout << zmp;
290  }
291  if (debug_level > 0) cout << endl << "gsens = ";
292  string acc = basename; acc.append(".gsens");
293  if (access(acc.c_str(),0)==0){
294  found = true;
295  interpolators[ACC]->load(acc, tm, scale, false);
296  if (debug_level > 0) cout << acc;
297  }
298  if (debug_level > 0) cout << endl << "hip = ";
299  string hip = basename; hip.append(".hip");
300  if (access(hip.c_str(),0)==0){
301  found = true;
302  interpolators[RPY]->load(hip, tm, scale, false);
303  if (debug_level > 0) cout << hip;
304  }else{
305  hip = basename; hip.append(".waist");
306  if (access(hip.c_str(),0)==0){
307  found = true;
308  interpolators[P]->load(hip, tm, scale, false, 0, 3);
309  interpolators[RPY]->load(hip, tm, scale, false, 3, 0);
310  if (debug_level > 0) cout << hip;
311  }
312  }
313  if (debug_level > 0) cout << endl << "torque = ";
314  string torque = basename; torque.append(".torque");
315  if (access(torque.c_str(),0)==0){
316  found = true;
317  interpolators[TQ]->load(torque, tm, scale, false);
318  if (debug_level > 0) cout << torque;
319  }
320  if (debug_level > 0) cout << endl << "wrenches = ";
321  string wrenches = basename; wrenches.append(".wrenches");
322  if (access(wrenches.c_str(),0)==0){
323  found = true;
324  interpolators[WRENCHES]->load(wrenches, tm, scale, false);
325  if (debug_level > 0) cout << wrenches;
326  }
327  if (debug_level > 0) cout << endl << "optional_data = ";
328  string optional_data = basename; optional_data.append(".optionaldata");
329  if (access(optional_data.c_str(),0)==0){
330  found = true;
331  interpolators[OPTIONAL_DATA]->load(optional_data, tm, scale, false);
332  if (debug_level > 0) cout << optional_data;
333  }
334  if (debug_level > 0) cout << endl;
335  if (!found) cerr << "pattern not found(" << basename << ")" << endl;
336  //
337  sync();
338 }
339 
341 {
342  for (unsigned int i=0; i<NINTERPOLATOR; i++){
343  interpolators[i]->sync();
344  }
345 }
346 
348 {
349  for (unsigned int i=0; i<NINTERPOLATOR; i++){
351  }
352 }
353 
354 void seqplay::get(double *o_q, double *o_zmp, double *o_accel,
355  double *o_basePos, double *o_baseRpy, double *o_tq, double *o_wrenches, double *o_optional_data)
356 {
357  double v[m_dof];
358  interpolators[Q]->get(o_q, v);
359  std::map<std::string, groupInterpolator *>::iterator it;
360  for (it=groupInterpolators.begin(); it!=groupInterpolators.end();){
361  groupInterpolator *gi = it->second;
362  if (gi){
363  gi->get(o_q, v);
364  if (gi->state == groupInterpolator::removed){
365  groupInterpolators.erase(it++);
366  delete gi;
367  continue;
368  }
369  }
370  ++it;
371  }
372  interpolators[ZMP]->get(o_zmp);
373  interpolators[ACC]->get(o_accel);
374  interpolators[P]->get(o_basePos);
375  interpolators[RPY]->get(o_baseRpy);
376  interpolators[TQ]->get(o_tq);
377  interpolators[WRENCHES]->get(o_wrenches);
378  interpolators[OPTIONAL_DATA]->get(o_optional_data);
379 }
380 
381 void seqplay::go(const double *i_q, const double *i_zmp, const double *i_acc,
382  const double *i_p, const double *i_rpy, const double *i_tq, const double *i_wrenches, const double *i_optional_data, double i_time,
383  bool immediate)
384 {
385  go(i_q, i_zmp, i_acc, i_p, i_rpy, i_tq, i_wrenches, i_optional_data,
386  NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
387  i_time, immediate);
388 }
389 
390 void seqplay::go(const double *i_q, const double *i_zmp, const double *i_acc,
391  const double *i_p, const double *i_rpy, const double *i_tq, const double *i_wrenches, const double *i_optional_data,
392  const double *ii_q, const double *ii_zmp, const double *ii_acc,
393  const double *ii_p, const double *ii_rpy, const double *ii_tq, const double *ii_wrenches, const double *ii_optional_data,
394  double i_time, bool immediate)
395 {
396  if (i_q) interpolators[Q]->go(i_q, ii_q, i_time, false);
397  if (i_zmp) interpolators[ZMP]->go(i_zmp, ii_zmp, i_time, false);
398  if (i_acc) interpolators[ACC]->go(i_acc, ii_acc, i_time, false);
399  if (i_p) interpolators[P]->go(i_p, ii_p, i_time, false);
400  if (i_rpy) interpolators[RPY]->go(i_rpy, ii_rpy, i_time, false);
401  if (i_tq) interpolators[TQ]->go(i_tq, ii_tq, i_time, false);
402  if (i_wrenches) interpolators[WRENCHES]->go(i_wrenches, ii_wrenches, i_time, false);
403  if (i_optional_data) interpolators[OPTIONAL_DATA]->go(i_optional_data, ii_optional_data, i_time, false);
404  if (immediate) sync();
405 }
406 
408 {
409  if (i_mode_ != interpolator::LINEAR && i_mode_ != interpolator::HOFFARBIB &&
410  i_mode_ != interpolator::QUINTICSPLINE && i_mode_ != interpolator::CUBICSPLINE) return false;
411 
412  bool ret=true;
413  for (unsigned int i=0; i<NINTERPOLATOR; i++){
414  ret &= interpolators[i]->setInterpolationMode(i_mode_);
415  }
416  std::map<std::string, groupInterpolator *>::const_iterator it;
417  for (it=groupInterpolators.begin(); it!=groupInterpolators.end(); it++){
418  groupInterpolator *gi = it->second;
419  ret &= gi->inter->setInterpolationMode(i_mode_);
420  }
421  return ret;
422 }
423 
424 bool seqplay::addJointGroup(const char *gname, const std::vector<int>& indices)
425 {
426  char *s = (char *)gname; while(*s) {*s=toupper(*s);s++;}
428  if (i) {
429  std::cerr << "[addJointGroup] group name " << gname << " is already installed" << std::endl;
430  return false;
431  }
432  i = new groupInterpolator(indices, interpolators[Q]->deltaT());
433  groupInterpolators[gname] = i;
434  return true;
435 }
436 
437 bool seqplay::getJointGroup(const char *gname, std::vector<int>& indices)
438 {
439  char *s = (char *)gname; while(*s) {*s=toupper(*s);s++;}
441  if (i) {
442  for(unsigned j = 0; j < i->indices.size(); j++) {
443  indices.push_back(i->indices[j]);
444  }
445  return true;
446  }else{
447  std::cerr << "[getJointGroup] group name " << gname << " is not installed" << std::endl;
448  return false;
449  }
450 }
451 
452 bool seqplay::removeJointGroup(const char *gname, double time)
453 {
454  char *s = (char *)gname; while(*s) {*s=toupper(*s);s++;}
456  if (i){
457  i->remove(time);
458  return true;
459  }else{
460  std::cerr << "[removeJointGroup] group name " << gname << " is not installed" << std::endl;
461  return false;
462  }
463 }
464 
465 bool seqplay::resetJointGroup(const char *gname, const double *full)
466 {
467  char *s = (char *)gname; while(*s) {*s=toupper(*s);s++;}
469  if (i){
470  i->set(full);
471  std::map<std::string, groupInterpolator *>::iterator it;
472  for (it=groupInterpolators.begin(); it!=groupInterpolators.end(); it++){
473  if ( it->first != std::string(gname) ) { // other
474  groupInterpolator *gi = it->second;
475  if (gi && (gi->state == groupInterpolator::created || gi->state == groupInterpolator::working) && gi->inter->isEmpty()) {
476  gi->set(full);
477  }
478  }
479  }
480  // update for non working interpolators
481 
482  return true;
483  }else{
484  std::cerr << "[resetJointGroup] group name " << gname << " is not installed" << std::endl;
485  return false;
486  }
487 }
488 
489 bool seqplay::setJointAnglesOfGroup(const char *gname, const double* i_qRef, const size_t i_qsize, double i_tm)
490 {
491  char *s = (char *)gname; while(*s) {*s=toupper(*s);s++;}
493  if (i){
494  if (i_qsize != i->indices.size() ) {
495  std::cerr << "[setJointAnglesOfGroup] group name " << gname << " : size of manipulater is not equal to input. " << i_qsize << " /= " << i->indices.size() << std::endl;
496  return false;
497  }
499  double q[m_dof], dq[m_dof];
500  interpolators[Q]->get(q, dq, false);
501  std::map<std::string, groupInterpolator *>::iterator it;
502  for (it=groupInterpolators.begin(); it!=groupInterpolators.end(); it++){
503  groupInterpolator *gi = it->second;
504  if (gi) gi->get(q, dq, false);
505  }
506  double x[i->indices.size()], v[i->indices.size()];
507  i->extract(x, q);
508  i->extract(v, dq);
509  i->inter->go(x,v,interpolators[Q]->deltaT());
510  }
511  double x[i->indices.size()], v[i->indices.size()];
512  i->inter->get(x, v, false);
513  i->setGoal(i_qRef, i_tm);
514  return true;
515  }else{
516  std::cerr << "[setJointAnglesOfGroup] group name " << gname << " is not installed" << std::endl;
517  return false;
518  }
519 }
520 
521 void seqplay::clearOfGroup(const char *gname, double i_timeLimit)
522 {
523  char *s = (char *)gname; while(*s) {*s=toupper(*s);s++;}
525  if (i){
526  i->clear(i_timeLimit);
527  }
528 }
529 
530 bool seqplay::playPatternOfGroup(const char *gname, std::vector<const double *> pos, std::vector<double> tm, const double *qInit, unsigned int len)
531 {
532  char *s = (char *)gname; while(*s) {*s=toupper(*s);s++;}
534  if (i){
535  if (len != i->indices.size() ) {
536  std::cerr << "[playPatternOfGroup] group name " << gname << " : size of manipulater is not equal to input. " << len << " /= " << i->indices.size() << std::endl;
537  return false;
538  }
540  double q[m_dof], dq[m_dof];
541  interpolators[Q]->get(q, dq, false);
542  std::map<std::string, groupInterpolator *>::iterator it;
543  for (it=groupInterpolators.begin(); it!=groupInterpolators.end(); it++){
544  groupInterpolator *gi = it->second;
545  if (gi) gi->get(q, dq, false);
546  }
547  double x[i->indices.size()], v[i->indices.size()];
548  i->extract(x, q);
549  i->extract(v, dq);
550  i->inter->go(x,v,interpolators[Q]->deltaT());
551  }
552  const double *q=NULL; double t=0;
553  double *v = new double[len];
554  double *qi = new double[len];
555  for (unsigned int j=0; j<len; j++){
556  qi[j] = qInit[i->indices[j]];
557  }
558  for (unsigned int l=0; l<pos.size(); l++){
559  q = pos[l];
560  if (l < pos.size() - 1 ) {
561  double t0, t1;
562  if (tm.size() == pos.size()) {
563  t0 = tm[l]; t1 = tm[l+1];
564  } else {
565  t0 = t1 = tm[0];
566  }
567  const double *q_next = pos[l+1];
568  const double *q_prev = l==0 ? qi : pos[l-1];
569  for (unsigned int j = 0; j < len; j++) {
570  double d0, d1, v0, v1;
571  d0 = (q[j] - q_prev[j]);
572  d1 = (q_next[j] - q[j]);
573  v0 = d0/t0;
574  v1 = d1/t1;
575  if ( v0 * v1 >= 0 ) {
576  v[j] = 0.5 * (v0 + v1);
577  } else {
578  v[j] = 0;
579  }
580  }
581  } else {
582  for (unsigned int j = 0; j < len; j++) { v[j] = 0.0; }
583  }
584  if (l < tm.size()) t = tm[l];
585  i->go(q, v, t);
586  }
587  sync();
588  delete [] v;
589  delete [] qi;
590 
591  return true;
592  }else{
593  std::cerr << "[playPatternOfGroup] group name " << gname << " is not installed" << std::endl;
594  return false;
595  }
596 }
597 
598 bool seqplay::setJointAnglesSequence(std::vector<const double*> pos, std::vector<double> tm)
599 {
600  // setJointAngles to override curren tgoal
601  double x[m_dof], v[m_dof], a[m_dof];
602  interpolators[Q]->get(x, v, a, false);
603  interpolators[Q]->set(x, v);
604  interpolators[Q]->clear();
605  interpolators[Q]->push(x, v, a, true);
606 
607  const double *q=NULL;
608  for (unsigned int i=0; i<pos.size(); i++){
609  q = pos[i];
610  if (i < pos.size() - 1 ) {
611  double t0, t1;
612  if (tm.size() == pos.size()) {
613  t0 = tm[i]; t1 = tm[i+1];
614  } else {
615  t0 = t1 = tm[0];
616  }
617  const double *q_next = pos[i+1];
618  const double *q_prev = i==0?x:pos[i-1];
619  for (int j = 0; j < m_dof; j++) {
620  double d0, d1, v0, v1;
621  d0 = (q[j] - q_prev[j]);
622  d1 = (q_next[j] - q[j]);
623  v0 = d0/t0;
624  v1 = d1/t1;
625  if ( v0 * v1 >= 0 ) {
626  v[j] = 0.5 * (v0 + v1);
627  } else {
628  v[j] = 0;
629  }
630  }
631  } else {
632  for (int j = 0; j < m_dof; j++) { v[j] = 0.0; }
633  }
634 
635  interpolators[Q]->setGoal(pos[i], v, tm[i], false);
636  do{
637  interpolators[Q]->interpolate(tm[i]);
638  }while(tm[i]>0);
639  sync();
640  }
641  return true;
642 }
643 
645 {
646  // setJointAngles to override curren tgoal
647  double x[m_dof], v[m_dof], a[m_dof];
648  interpolators[Q]->get(x, v, a, false);
649  interpolators[Q]->set(x, v);
650  interpolators[Q]->clear();
651  double tm = interpolators[Q]->deltaT();
652  interpolators[Q]->setGoal(x, v, tm, false);
653  do{
655  }while(tm>0);
656  sync();
657  return true;
658 }
659 
660 bool seqplay::setJointAnglesSequenceFull(std::vector<const double*> i_pos, std::vector<const double*> i_vel, std::vector<const double*> i_torques, std::vector<const double*> i_bpos, std::vector<const double*> i_brpy, std::vector<const double*> i_bacc, std::vector<const double*> i_zmps, std::vector<const double*> i_wrenches, std::vector<const double*> i_optionals, std::vector<double> i_tm)
661 {
662  // setJointAngles to override curren tgoal
663  double x[m_dof], v[m_dof], a[m_dof];
664  interpolators[Q]->get(x, v, a, false);
665  interpolators[Q]->set(x, v);
666  interpolators[Q]->clear();
667  interpolators[Q]->push(x, v, a, true);
668  double torque[m_dof], dummy_dof[m_dof];
669  for (int j = 0; j < m_dof; j++) { dummy_dof[j] = 0.0; }
670  interpolators[TQ]->get(torque, false);
671  interpolators[TQ]->set(torque);
672  interpolators[TQ]->clear();
673  interpolators[TQ]->push(torque, dummy_dof, dummy_dof, true);
674  double bpos[3], brpy[3], bacc[3], dummy_3[3]={0,0,0};
675  interpolators[P]->get(bpos, false);
676  interpolators[P]->set(bpos);
677  interpolators[P]->clear();
678  interpolators[P]->push(bpos, dummy_3, dummy_3, true);
679  interpolators[RPY]->get(brpy, false);
680  interpolators[RPY]->set(brpy);
681  interpolators[RPY]->clear();
682  interpolators[RPY]->push(brpy, dummy_3, dummy_3, true);
683  interpolators[ACC]->get(bacc, false);
684  interpolators[ACC]->set(bacc);
685  interpolators[ACC]->clear();
686  interpolators[ACC]->push(bacc, dummy_3, dummy_3, true);
687  int fnum = interpolators[WRENCHES]->dimension()/6, optional_data_dim = interpolators[OPTIONAL_DATA]->dimension();
688  double zmp[3], wrench[6*fnum], dummy_fnum[6*fnum], optional[optional_data_dim], dummy_optional[optional_data_dim];
689  for (int j = 0; j < 6*fnum; j++) { dummy_dof[j] = 0.0; }
690  for (int j = 0; j < optional_data_dim; j++) { dummy_optional[j] = 0.0; }
691  interpolators[ZMP]->get(zmp, false);
692  interpolators[ZMP]->set(zmp);
693  interpolators[ZMP]->clear();
694  interpolators[ZMP]->push(zmp, dummy_3, dummy_3, true);
695  interpolators[WRENCHES]->get(wrench, false);
696  interpolators[WRENCHES]->set(wrench);
698  interpolators[WRENCHES]->push(wrench, dummy_fnum, dummy_fnum, true);
699  interpolators[OPTIONAL_DATA]->get(optional, false);
700  interpolators[OPTIONAL_DATA]->set(optional);
702  interpolators[OPTIONAL_DATA]->push(optional, dummy_optional, dummy_optional, true);
703 
704  const double *q=NULL;
705  for (unsigned int i=0; i<i_pos.size(); i++){
706  if (i_vel.size() > 0 ) {
707  for (int j = 0; j < m_dof; j++) {
708  v[j] = i_vel[i][j];
709  }
710  }else{
711  q = i_pos[i];
712  if (i < i_pos.size() - 1 ) {
713  double t0, t1;
714  if (i_tm.size() == i_pos.size()) {
715  t0 = i_tm[i]; t1 = i_tm[i+1];
716  } else {
717  t0 = t1 = i_tm[0];
718  }
719  const double *q_next = i_pos[i+1];
720  const double *q_prev = i==0?x:i_pos[i-1];
721  for (int j = 0; j < m_dof; j++) {
722  double d0, d1, v0, v1;
723  d0 = (q[j] - q_prev[j]);
724  d1 = (q_next[j] - q[j]);
725  v0 = d0/t0;
726  v1 = d1/t1;
727  if ( v0 * v1 >= 0 ) {
728  v[j] = 0.5 * (v0 + v1);
729  } else {
730  v[j] = 0;
731  }
732  }
733  } else {
734  for (int j = 0; j < m_dof; j++) { v[j] = 0.0; }
735  }
736  }
737 
738  interpolators[Q]->setGoal(i_pos[i], v, i_tm[i], false);
739  interpolators[TQ]->setGoal(i_torques[i], i_tm[i], false);
740  interpolators[P]->setGoal(i_bpos[i], i_tm[i], false);
741  interpolators[RPY]->setGoal(i_brpy[i], i_tm[i], false);
742  interpolators[ACC]->setGoal(i_bacc[i], i_tm[i], false);
743  interpolators[ZMP]->setGoal(i_zmps[i], i_tm[i], false);
744  interpolators[WRENCHES]->setGoal(i_wrenches[i], i_tm[i], false);
745  interpolators[OPTIONAL_DATA]->setGoal(i_optionals[i], i_tm[i], false);
746  do{
747  double tm = i_tm[i], tm_tmp;
748  interpolators[Q]->interpolate(i_tm[i]);
749  tm_tmp = tm; interpolators[TQ]->interpolate(tm_tmp);
750  tm_tmp = tm; interpolators[P]->interpolate(tm_tmp);
751  tm_tmp = tm; interpolators[RPY]->interpolate(tm_tmp);
752  tm_tmp = tm; interpolators[ACC]->interpolate(tm_tmp);
753  tm_tmp = tm; interpolators[ZMP]->interpolate(tm_tmp);
754  tm_tmp = tm; interpolators[WRENCHES]->interpolate(tm_tmp);
755  tm_tmp = tm; interpolators[OPTIONAL_DATA]->interpolate(tm_tmp);
756  }while(i_tm[i]>0);
757  sync();
758  }
759  return true;
760 }
761 
762 bool seqplay::setJointAnglesSequenceOfGroup(const char *gname, std::vector<const double*> pos, std::vector<double> tm, const size_t pos_size)
763 {
764  char *s = (char *)gname; while(*s) {*s=toupper(*s);s++;}
766 
767  if (! i){
768  std::cerr << "[setJointAnglesSequenceOfGroup] group name " << gname << " is not installed" << std::endl;
769  return false;
770  }
771  if (pos_size != i->indices.size() ) {
772  std::cerr << "[setJointAnglesSequenceOfGroup] group name " << gname << " : size of manipulater is not equal to input. " << pos_size << " /= " << i->indices.size() << std::endl;
773  return false;
774  }
775  int len = i->indices.size();
776  // playPatternOfGroup
777  double q[m_dof], dq[m_dof];
778  interpolators[Q]->get(q, dq, false); // fill all q,dq data
779  std::map<std::string, groupInterpolator *>::iterator it;
780  for (it=groupInterpolators.begin(); it!=groupInterpolators.end(); it++){
781  groupInterpolator *gi = it->second;
782  if (gi) gi->get(q, dq, false);
783  }
784  // extract currnet limb data
785  double x[len], v[len];
786  i->extract(x, q);
787  i->extract(v, dq);
788  // override currnet goal
789  i->inter->clear();
790  i->inter->go(x,v,interpolators[Q]->deltaT());
791  const double *q_curr=NULL;
792  for (unsigned int j=0; j<pos.size(); j++){
793  q_curr = pos[j];
794  if ( j < pos.size() - 1 ) {
795  double t0, t1;
796  if (tm.size() == pos.size()) {
797  t0 = tm[j]; t1 = tm[j+1];
798  } else {
799  t0 = t1 = tm[0];
800  }
801  const double *q_next = pos[j+1];
802  const double *q_prev = j==0?x:pos[j-1];
803  for (int k = 0; k < len; k++) {
804  double d0, d1, v0, v1;
805  d0 = (q_curr[k] - q_prev[k]);
806  d1 = (q_next[k] - q_curr[k]);
807  v0 = d0/t0;
808  v1 = d1/t1;
809  if ( v0 * v1 >= 0 ) {
810  v[k] = 0.5 * (v0 + v1);
811  } else {
812  v[k] = 0;
813  }
814  }
815  } else {
816  for (int k = 0; k < len; k++) { v[k] = 0.0; }
817  }
819  double q[m_dof], dq[m_dof];
820  interpolators[Q]->get(q, dq, false);
821  std::map<std::string, groupInterpolator *>::iterator it;
822  for (it=groupInterpolators.begin(); it!=groupInterpolators.end(); it++){
823  groupInterpolator *gi = it->second;
824  if (gi) gi->get(q, dq, false);
825  }
826  double x[i->indices.size()], v[i->indices.size()];
827  i->extract(x, q);
828  i->extract(v, dq);
829  i->inter->go(x,v,interpolators[Q]->deltaT());
830  }
831  i->inter->setGoal(pos[j], v, tm[j], false);
832  do{
833  i->inter->interpolate(tm[j]);
834  }while(tm[j]>0);
835  i->inter->sync();
837  }
838  return true;
839 }
840 
841 bool seqplay::clearJointAnglesOfGroup(const char *gname)
842 {
843  char *s = (char *)gname; while(*s) {*s=toupper(*s);s++;}
845 
846  if (! i){
847  std::cerr << "[clearJointAnglesOfGroup] group name " << gname << " is not installed" << std::endl;
848  return false;
849  }
850 
852  std::cerr << "[clearJointAnglesOfGroup] group name " << gname << " is not created" << std::endl;
853  return false;
854  }
855 
857  std::cerr << "[clearJointAnglesOfGroup] group name " << gname << " is removing" << std::endl;
858  return false;
859  }
860 
861  int len = i->indices.size();
862  double x[len], v[len], a[len];
863  i->inter->get(x, v, a, false);
864  i->inter->set(x, v);
865  while(i->inter->remain_time() > 0){
866  i->inter->pop();
867  }
868  double tm = interpolators[Q]->deltaT();
869  i->inter->setGoal(x, v, tm, true);// true: update remian_t
870  do{
871  i->inter->interpolate(tm);
872  }while(tm>0);
873  i->inter->sync();
874 
875  return true;
876 }
std::map< std::string, groupInterpolator * > groupInterpolators
Definition: seqplay.h:147
void set(const double *full, const double *dfull=NULL)
Definition: seqplay.h:91
unsigned long long tick_t
void push(const double *x_, const double *v_, const double *a_, bool immediate=true)
bool setInterpolationMode(interpolation_mode i_mode_)
void interpolate(double &remain_t_)
bool setJointAnglesSequence(std::vector< const double * > pos, std::vector< double > tm)
Definition: seqplay.cpp:598
void load(string fname, double time_to_start=1.0, double scale=1.0, bool immediate=true, size_t offset1=0, size_t offset2=0)
bool isEmpty() const
Definition: seqplay.cpp:103
void setJointAngle(unsigned int i_rank, double jv, double tm)
Definition: seqplay.cpp:213
bool clearJointAngles()
Definition: seqplay.cpp:644
void pop_back()
Definition: seqplay.cpp:347
void getJointAngles(double *i_qRef)
Definition: seqplay.cpp:162
void sync()
Definition: seqplay.cpp:340
void setZmp(const double *i_zmp, double i_tm=0.0)
Definition: seqplay.cpp:168
~seqplay()
Definition: seqplay.cpp:48
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
void setGoal(const double *g, double tm)
Definition: seqplay.h:115
void go(const double *i_q, const double *i_zmp, const double *i_acc, const double *i_p, const double *i_rpy, const double *i_tq, const double *i_wrenches, const double *i_optional_data, double i_time, bool immediate=true)
Definition: seqplay.cpp:381
void setName(const std::string &_name)
Definition: interpolator.h:63
RTC::ReturnCode_t ret(RTC::Local::ReturnCode_t r)
int m_dof
Definition: seqplay.h:148
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
int dimension() const
Definition: interpolator.h:62
std::string basename(const std::string name)
void get(double *full, double *dfull=NULL, bool popp=true)
Definition: seqplay.h:71
png_uint_32 i
interpolator * inter
Definition: seqplay.h:138
seqplay(unsigned int i_dof, double i_dt, unsigned int i_fnum=0, unsigned int optional_data_dim=1)
Definition: seqplay.cpp:9
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
bool addJointGroup(const char *gname, const std::vector< int > &indices)
Definition: seqplay.cpp:424
bool resetJointGroup(const char *gname, const double *full)
Definition: seqplay.cpp:465
void clearOfGroup(const char *gname, double i_timeLimit)
Definition: seqplay.cpp:521
void setBasePos(const double *i_pos, double i_tm=0.0)
Definition: seqplay.cpp:177
def j(str, encoding="cp932")
void extract(double *dst, const double *src)
Definition: seqplay.h:101
void go(const double *gx, const double *gv, double time, bool immediate=true)
bool playPatternOfGroup(const char *gname, std::vector< const double * > pos, std::vector< double > tm, const double *qInit, unsigned int len)
Definition: seqplay.cpp:530
void clear(double i_timeLimit=0)
Definition: seqplay.h:129
t
double calc_interpolation_time(const double *g)
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
int debug_level
Definition: seqplay.h:148
void clear(double i_timeLimit=0)
Definition: seqplay.cpp:263
void remove(double time)
Definition: seqplay.h:125
double deltaT() const
Definition: interpolator.h:61
#define tick2sec(t)
convert time stamp counter into sec
Definition: timeUtil.h:37
void loadPattern(const char *i_basename, double i_tm)
Definition: seqplay.cpp:273
void get(double *x_, bool popp=true)
std::vector< int > indices
Definition: seqplay.h:139
double remain_time()
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
def goInitial(tm=3.0)
Definition: PA10.py:71
void go(const double *g, double tm)
Definition: seqplay.h:107
void setGoal(const double *gx, const double *gv, double time, bool online=true)
interpolator * interpolators[NINTERPOLATOR]
Definition: seqplay.h:146
void setJointAngles(const double *i_qRef, double i_tm=0.0)
Definition: seqplay.cpp:153
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
void set(const double *x, const double *v=NULL)
tick_t get_tick()
get time stamp counter
Definition: timeUtil.cpp:7


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