psim.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
3  * All rights reserved. This program is made available under the terms of the
4  * Eclipse Public License v1.0 which accompanies this distribution, and is
5  * available at http://www.eclipse.org/legal/epl-v10.html
6  * Contributors:
7  * The University of Tokyo
8  */
16 #ifndef __PSIM_H__
17 #define __PSIM_H__
18 
19 #include <chain.h>
20 #include <fMatrix3.h>
21 #include <list>
22 #include <vector>
23 
24 class pLink;
25 class pSim;
26 
27 struct JointInfo;
28 typedef std::list<class pSubChain*> subchain_list;
29 typedef std::vector<Joint*> joint_list;
30 typedef std::vector<class pJoint*> p_joint_list;
31 
33 #define N_FRIC_CONE_DIV 8
34 
35 //#define VERBOSE
36 
37 // Some definitions for test and logging.
38 // Divide-and-Conquer Algorithm (Featherstone 1999) test
39 //#define USE_DCA
40 // use normal Lemke method
41 //#define USE_NORMAL_LEMKE
42 // measure CPU time for collision simulation
43 // check timing for parallel processing
44 //#define TIMING_CHECK
45 
46 #ifdef USE_MPI
47 #include <mpi.h>
48 #endif
49 
58 {
66 };
67 
68 #ifdef USE_MPI
69 enum {
71  PSIM_TAG_LAMBDA = 100,
72  PSIM_TAG_ACC,
73  PSIM_TAG_FORCE,
74 };
75 #endif
76 
81 class pJoint
82 {
83  friend class pSim;
84  friend class pLink;
85  friend class pSubChain;
86 public:
87  pJoint(Joint* _joint, Joint* _link_side) {
88  joint = _joint;
89  link_side = _link_side;
90  if(!link_side || joint == link_side) parent_side = false;
91  else parent_side = true;
92  J.resize(6, 6);
93  Jdot.resize(6);
94  J.identity();
95  Jdot.zero();
96  f_final.resize(6);
97  f_final.zero();
98  acc_final.resize(6);
99  acc_final.zero();
100  dvel.resize(6);
101  dvel.zero();
102  colf_final.resize(6);
103  colf_final.zero();
104  vel_final.resize(6);
105  vel_final.zero();
106  plink = 0;
107  subchain = 0;
108  }
109 
111  }
112 
114  return pair;
115  }
117  return joint;
118  }
119  int ParentSide() {
120  return parent_side;
121  }
122 
123 private:
124  void calc_jacobian();
125  void calc_jdot();
126  void dump(ostream& ost);
127 
134 
137  fVec f_final; // 6
139 
140  void calc_dvel();
144 };
145 
150 class pLink
151 {
152  friend class pSim;
153  friend class pJoint;
154  friend class pSubChain;
155 public:
156  pLink(Joint* _joint) {
157  joint = _joint;
158  M.resize(6, 6);
159  Minv.resize(6, 6);
160  c.resize(6);
161  acc.resize(6);
162  M.zero();
163  Minv.zero();
164  c.zero();
165  acc.zero();
166  subchain = 0;
167  }
168 
169  ~pLink() {
170  }
171 
172 private:
173  void calc_inertia();
174  void calc_acc(const fVec3& g0);
175  void dump(ostream& ost);
176 
179 
180  fMat M; // 6x6
181  fMat Minv; // 6x6
182  fVec c; // 6
183  fVec acc; // 6
184 };
185 
191 {
192  friend class pSim;
193  friend class pJoint;
194  friend class pLink;
195 public:
196  pSubChain(pSim* _sim, pSubChain* _p, pJoint* _p0, pJoint* _p1) {
197  sim = _sim;
198  parent = _p;
199  children[0] = 0;
200  children[1] = 0;
201  last_pjoints[0] = _p0;
202  last_pjoints[1] = _p1;
203  last_joint = last_pjoints[0]->joint;
204  last_index[0] = last_index[1] = -1;
205  axis = PSIM_AXIS_NULL;
206  if(last_joint->j_type == JROTATE)
207  {
208  if(last_joint->axis(0) > 0.95) axis = PSIM_AXIS_RX;
209  else if(last_joint->axis(1) > 0.95) axis = PSIM_AXIS_RY;
210  else if(last_joint->axis(2) > 0.95) axis = PSIM_AXIS_RZ;
211  }
212  else if(last_joint->j_type == JSLIDE)
213  {
214  if(last_joint->axis(0) > 0.95) axis = PSIM_AXIS_X;
215  else if(last_joint->axis(1) > 0.95) axis = PSIM_AXIS_Y;
216  else if(last_joint->axis(2) > 0.95) axis = PSIM_AXIS_Z;
217  }
218  outer_joints = 0;
219  outer_joints_origin = 0;
220  outer_joints_index = 0;
221  n_outer_joints = 0;
222  links = 0;
223  n_links = 0;
224  Lambda = 0;
225  acc_temp = 0;
226  vel_temp = 0;
227  n_dof = last_joint->n_dof;
228  n_const = 6 - n_dof;
229  joint_index = 0;
230  const_index = 0;
231  if(n_dof > 0) joint_index = new int [n_dof];
232  if(n_const > 0) const_index = new int [n_const];
233  int count, i;
234  if(last_joint->t_given)
235  {
236  switch(last_joint->j_type)
237  {
238  case JROTATE:
239  case JSLIDE:
240  count = 0;
241  for(i=0; i<6; i++)
242  {
243  if(i == (int)axis) joint_index[0] = i;
244  else
245  {
246  const_index[count] = i;
247  count++;
248  }
249  }
250  break;
251  case JSPHERE:
252  const_index[0] = 0;
253  const_index[1] = 1;
254  const_index[2] = 2;
255  joint_index[0] = 3;
256  joint_index[1] = 4;
257  joint_index[2] = 5;
258  break;
259  case JFREE:
260  for(i=0; i<6; i++) joint_index[i] = i;
261  break;
262  case JFIXED:
263  for(i=0; i<6; i++) const_index[i] = i;
264  break;
265  default:
266  break;
267  }
268  }
269  else
270  {
271  for(i=0; i<6; i++) const_index[i] = i;
272  }
273 #ifdef USE_MPI
274  rank = 0;
275 #endif
276  }
277  pSubChain(pSim* _sim, pSubChain* _p, pLink* _pl) {
278  sim = _sim;
279  parent = _p;
280  children[0] = 0;
281  children[1] = 0;
282  last_pjoints[0] = 0;
283  last_pjoints[1] = 0;
284  last_joint = 0;
285  last_index[0] = last_index[1] = -1;
286  axis = PSIM_AXIS_NULL;
287  outer_joints = 0;
288  outer_joints_origin = 0;
289  outer_joints_index = 0;
290  n_outer_joints = 0;
291  links = new pLink* [1];
292  links[0] = _pl;
293  n_links = 1;
294  Lambda = 0;
295  acc_temp = 0;
296  vel_temp = 0;
297  n_dof = 6;
298  n_const = 0;
299  joint_index = 0;
300  const_index = 0;
301 #ifdef USE_MPI
302  rank = 0;
303 #endif
304  }
305 
307  if(outer_joints)
308  {
309  int i;
310  for(i=0; i<n_outer_joints; i++)
311  delete[] Lambda[i];
312  delete[] Lambda;
313  delete[] acc_temp;
314  delete[] vel_temp;
315  delete[] outer_joints;
316  delete[] outer_joints_origin;
317  delete[] outer_joints_index;
318  }
319  if(joint_index) delete[] joint_index;
320  if(const_index) delete[] const_index;
321  if(links) delete[] links;
322  if(children[0]) delete children[0];
323  if(children[1] && children[1] != children[0]) delete children[1];
324  }
325 
326 private:
328  int i;
329  for(i=0; i<n_outer_joints; i++)
330  if(pj == outer_joints[i]) return i;
331  return -1;
332  }
333 
334  void init();
335 
336  int total_cost();
337  int num_leaves();
338  int schedule_depth();
339 
342  pSubChain* children[2];
343  pJoint* last_pjoints[2]; // [0] is child side, [1] is parent side
346  int last_index[2]; // index of last_pjoints in outer_joints of children
347 
353  int n_links;
354 
355  int n_dof;
356  int n_const;
359 
360  void calc_inertia();
361  void calc_inertia_leaf();
362  void calc_inertia_body();
363 #ifdef USE_MPI
364  void recv_inertia();
365  void send_inertia(int dest);
366 #endif
367 
368  void calc_acc();
369  void calc_acc_leaf();
370  void calc_acc_body();
371 #ifdef USE_MPI
372  void recv_acc();
373  void send_acc(int dest);
374 #endif
375 
376  void disassembly();
377  void disassembly_body();
378  void disassembly_leaf();
379 #ifdef USE_MPI
380  void recv_force();
381  void send_force(int dest);
382 #endif
383 
384  fMat P; // 6x6
385 #ifndef USE_DCA
386  fMat Gamma; // n_const x n_const
387  fMat Gamma_inv; // n_const x n_const
388 #endif
389  fMat** Lambda; // matrix of (n_outer_joints x n_outer_joints) matrices with size 6x6
390 
392  fMat W, IW;
393  fVec* acc_temp; // vector of (n_outer_joints) vectors with size 6x1
394  fVec tau; // n_dof x 1; joint torque
395  fVec f_temp; // n_const x 1
396  fVec acc_final; // n_dof x 1
397 #ifdef USE_DCA
398  fMat Vhat;
399  fMat SVS;
400 #endif
401 
402  void calc_dvel();
403  void calc_dvel_leaf();
404  void calc_dvel_body();
405  void col_disassembly();
406  void col_disassembly_body();
409 
410  void dump(ostream& ost);
411 
412  // compute contact force based on LCP
413  // only at the root of the schedule
414  int calc_contact_force(double timestep);
415 
416  void clear_f_final();
417 
418 #ifdef USE_MPI
419  int assign_processes(int start_rank, int end_rank);
420  int create_types(int* n_proc_joints, int** lengths, MPI_Aint** disps, MPI_Datatype** oldtypes);
421  void scatter_acc();
422  int rank;
423  MPI_Datatype parent_lambda_type;
424  MPI_Datatype parent_acc_type;
425  MPI_Datatype parent_force_type;
426 #endif
427 };
428 
429 struct JointInfo
430 {
432  pjoints[0] = pjoints[1] = 0;
433  plink = 0;
434  }
436  }
437 
438  pJoint* pjoints[2];
440 };
441 
447  : virtual public Chain
448 {
449  friend class pJoint;
450  friend class pLink;
451  friend class pSubChain;
452 public:
454 
458 #ifdef USE_MPI
459  pSim(int _rank = 0): Chain() {
460 #else
461  pSim(): Chain() {
462 #endif
463  joint_info = 0;
464  subchains = 0;
465  for(int i=0; i<N_FRIC_CONE_DIV; i++)
466  {
467  double ang = 2.0*i*PI/N_FRIC_CONE_DIV;
468  cone_dir[i](0) = cos(ang);
469  cone_dir[i](1) = sin(ang);
470  cone_dir[i](2) = 0.0;
471  }
472 #ifdef USE_MPI
473 #ifdef TIMING_CHECK
474  inertia_wait_time = 0.0;
475  acc_wait_time = 0.0;
476  force_wait_time = 0.0;
477 #endif
478  rank = _rank;
479  all_acc_types = 0;
480 #endif
481  }
482  ~pSim() {
483  if(joint_info)
484  {
485  for(int i=0; i<n_joint; i++)
486  {
487  delete joint_info[i].pjoints[0];
488  delete joint_info[i].pjoints[1];
489  if(joint_info[i].plink) delete joint_info[i].plink;
490  }
491  delete[] joint_info;
492  }
493  if(subchains) delete subchains;
494 #ifdef USE_MPI
495  if(all_acc_types) delete[] all_acc_types;
496 #ifdef TIMING_CHECK
497  cerr << "[" << rank << "] inertia_wait_time = " << inertia_wait_time << endl;
498  cerr << "[" << rank << "] acc_wait_time = " << acc_wait_time << endl;
499  cerr << "[" << rank << "] force_wait_time = " << force_wait_time << endl;
500 #endif
501 #endif
502  }
503 
504  virtual void Clear();
505 
507  int Update();
508 
510 
517  int Update(double timestep, std::vector<class SDContactPair*>& sdContactPairs);
518 
520  int Schedule();
521 
523  int Schedule(Joint** joints);
524 
526  int AutoSchedule(int max_procs);
527 
528 #ifdef USE_MPI
529  int AssignProcesses(int max_procs = 1);
531 #endif
532 
534  void DumpSchedule(ostream& ost);
535 
537  int TotalCost();
538  int NumLeaves();
539  int ScheduleDepth();
540 
542  int ConstraintForces(fVec& cf);
543 
544  void GetPJoint(Joint* _joint, pJoint* _pjoints[2]) {
545  _pjoints[0] = joint_info[_joint->i_joint].pjoints[0];
546  _pjoints[1] = joint_info[_joint->i_joint].pjoints[1];
547  }
548 
549 protected:
550 #ifdef SEGA
551  virtual int init();
552 #else
553  virtual int init(SceneGraph* sg);
554 #endif
555  int myinit();
556  int init_contact();
557  virtual int clear_data();
558  virtual int clear_contact();
559 
560 private:
561  pSubChain* default_schedule(pSubChain* p, Joint* j);
562  void default_schedule_virtual(Joint* j);
563  void setup_pjoint(Joint* j);
564  void setup_pjoint_virtual(Joint* j);
565  void calc_consts();
566 
567  void update_position();
568  void update_velocity();
569  void disassembly();
570 
571  // collision
572  void update_collision();
573  void calc_dvel();
574  void col_disassembly();
575 
576  // contact force computation based on LCP
578  std::vector<fVec3> contact_relvels;
579  std::vector<double> fric_coefs;
580 
582  std::vector<fMat> all_Jv;
583  std::vector<fMat> all_Jr;
584  std::vector<fVec3> all_jdot_v;
585  std::vector<fVec3> all_jdot_r;
586 
588 
590  int count = 0;
591  for(joint_list::iterator j=contact_vjoints.begin(); j!=contact_vjoints.end(); count++, j++)
592  {
593  if(_jnt == *j) return count;
594  }
595  return -1;
596  }
597 
598  int build_subchain_tree(int _n_joints, Joint** joints, subchain_list& buf);
599  void build_subchain_tree(Joint* cur_joint, subchain_list& buf);
600  int in_subchain(pSubChain* sc, pLink* pl);
601 
604 
605 #ifdef USE_MPI
606 #ifdef TIMING_CHECK
607  double inertia_wait_time;
608  double acc_wait_time;
609  double force_wait_time;
610 #endif
611 
612  void scatter_acc();
613  MPI_Datatype* all_acc_types;
614  int size;
615  int rank;
616 #endif
617 
618 };
619 
620 #endif
621 
void calc_jacobian()
Definition: update.cpp:164
JointInfo()
Definition: psim.h:431
fMat J
Definition: psim.h:135
pLink * plink
Definition: psim.h:130
int c
Definition: autoplay.py:16
fVec tau
Definition: psim.h:394
fMat Gamma
Definition: psim.h:386
pSim()
Default constructor.
Definition: psim.h:461
int n_links
Definition: psim.h:353
joint_list contact_vjoints
Definition: psim.h:577
pJoint(Joint *_joint, Joint *_link_side)
Definition: psim.h:87
fMat ** Lambda
Definition: psim.h:389
void calc_dvel()
Definition: update.cpp:585
std::list< class pSubChain * > subchain_list
Definition: psim.h:27
int resize(int i, int j)
Changes the matrix size.
Definition: fMatrix.h:133
void col_disassembly()
Definition: update.cpp:681
Joint * last_joint
Definition: psim.h:344
#define HRPBASE_EXPORT
int contact_vjoint_index(Joint *_jnt)
Definition: psim.h:589
fVec vel_final
Definition: psim.h:143
std::vector< Joint * > joint_list
Definition: psim.h:29
pJoint ** outer_joints
Definition: psim.h:348
void calc_inertia()
Definition: update.cpp:199
int * outer_joints_index
Definition: psim.h:350
static int max_procs
Definition: schedule.cpp:24
void dump(ostream &ost)
Definition: psim.cpp:363
png_uint_32 size
Definition: png.h:1521
~pSubChain()
Definition: psim.h:306
pJoint * pair
Definition: psim.h:131
int n_dof
Definition: psim.h:355
fMat P
Definition: psim.h:384
int ParentSide()
Definition: psim.h:119
fVec acc_final
Definition: psim.h:396
png_uint_32 i
Definition: png.h:2735
pSubChain(pSim *_sim, pSubChain *_p, pJoint *_p0, pJoint *_p1)
Definition: psim.h:196
pSubChain(pSim *_sim, pSubChain *_p, pLink *_pl)
Definition: psim.h:277
fMat W
Definition: psim.h:392
fVec colf_final
Definition: psim.h:142
Joint * GetJoint()
Definition: psim.h:116
spherical (3DOF)
Definition: chain.h:43
fVec * acc_temp
Definition: psim.h:393
Joint * joint
Definition: psim.h:128
pJoint * Pair()
Definition: psim.h:113
~pJoint()
Definition: psim.h:110
fVec f_final
Definition: psim.h:137
fVec dvel
Definition: psim.h:141
int n_const
Definition: psim.h:356
std::vector< fVec3 > all_jdot_r
Definition: psim.h:585
def j(str, encoding="cp932")
pSubChain * subchains
Definition: psim.h:603
std::vector< fVec3 > contact_relvels
Definition: psim.h:578
pLink * plink
Definition: psim.h:439
fVec da6
Definition: psim.h:391
std::vector< fVec3 > all_jdot_v
Definition: psim.h:584
fMat Gamma_inv
Definition: psim.h:387
#define PI
PI.
Definition: IceTypes.h:32
void calc_jdot()
Definition: update.cpp:802
void resize(int i)
Change the size.
Definition: fMatrix.h:511
int parent_side
link is in parent side?
Definition: psim.h:132
Vector of generic size.
Definition: fMatrix.h:491
png_bytep buf
Definition: png.h:2729
3x3 matrix and 3-element vector classes.
fixed (0DOF)
Definition: chain.h:40
Classes for defining open/closed kinematic chains.
std::vector< class pJoint * > p_joint_list
Definition: psim.h:30
pSubChain * parent
Definition: psim.h:341
~JointInfo()
Definition: psim.h:435
Class for representing "handle"; two pJoint instances are attached to both sides of each joint...
Definition: psim.h:81
int * joint_index
Definition: psim.h:358
fVec Jdot
Definition: psim.h:136
prismatic (1DOF)
Definition: chain.h:42
Joint * link_side
null in parent side of space joint
Definition: psim.h:129
The class representing the whole mechanism. May contain multiple characters.
Definition: chain.h:144
void zero()
Creates a zero vector.
Definition: fMatrix.h:575
static pSim * sim
Definition: schedule.cpp:23
void init()
Definition: schedule.cpp:1140
std::vector< double > fric_coefs
Definition: psim.h:579
int * const_index
Definition: psim.h:357
friend class pSim
Definition: psim.h:83
3-element vector class.
Definition: fMatrix3.h:206
fVec * vel_temp
Definition: psim.h:407
joint_list all_vjoints
Definition: psim.h:581
pLink ** links
Definition: psim.h:352
pSubChain * subchain
the subchain which only contains the link associated with this pjoint
Definition: psim.h:133
rotational (1DOF)
Definition: chain.h:41
fVec acc_final
Definition: psim.h:138
~pSim()
Definition: psim.h:482
Main class for forward dynamics computation.
Definition: psim.h:446
#define N_FRIC_CONE_DIV
Number of vertices of the friction cone approximation.
Definition: psim.h:33
The class for representing a joint.
Definition: chain.h:538
int i_joint
index of the joint
Definition: chain.h:722
fVec f_temp
Definition: psim.h:395
int get_outer_index(pJoint *pj)
Definition: psim.h:327
int * outer_joints_origin
Definition: psim.h:349
JointInfo * joint_info
Definition: psim.h:602
PSIM_AXIS axis
Definition: psim.h:345
std::vector< fMat > all_Jr
Definition: psim.h:583
void identity()
Creates an identity matrix (only for square matrices).
Definition: fMatrix.cpp:1058
PSIM_AXIS
Definition: psim.h:57
std::vector< fMat > all_Jv
Definition: psim.h:582
void calc_acc()
Definition: update.cpp:887
void GetPJoint(Joint *_joint, pJoint *_pjoints[2])
Definition: psim.h:544
free (6DOF)
Definition: chain.h:44
int n_outer_joints
Definition: psim.h:351
Matrix of generic size. The elements are stored in a one-dimensional array in row-major order...
Definition: fMatrix.h:46
Node for schedule tree; represents a partial chain.
Definition: psim.h:190
fVec colf_temp
Definition: psim.h:408
pSim * sim
Definition: psim.h:340
void disassembly()
Definition: update.cpp:1160


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sat Apr 13 2019 02:14:25