Choreonoid  1.8
Jacobian.h
Go to the documentation of this file.
1 
2 #ifndef CNOID_BODY_JACOBIAN_H
3 #define CNOID_BODY_JACOBIAN_H
4 
5 #include "Body.h"
6 #include "Link.h"
7 #include "JointPath.h"
8 #include "exportdecl.h"
9 
10 namespace cnoid {
11 
12 CNOID_EXPORT void calcCMJacobian(Body* body, Link* base, Eigen::MatrixXd& J);
13 
14 CNOID_EXPORT void calcAngularMomentumJacobian(Body* body, Link* base, Eigen::MatrixXd& H);
15 
16 template<int elementMask, int rowOffset, int colOffset, bool useTargetLinkLocalPos>
17 void setJacobian(const JointPath& path, Link* targetLink, const Vector3& targetLinkLocalPos,
18  MatrixXd& out_J) {
19 
20  const bool isTranslationValid = (elementMask & 0x7);
21 
22  int n = path.numJoints();
23  int i = 0;
24  while(i < n){
25 
26  Link* link = path.joint(i);
27  int row = rowOffset;
28 
29  MatrixXd::ColXpr Ji = out_J.col(i + colOffset);
30 
31  switch(link->jointType()){
32 
34  {
35  Vector3 omega = link->R() * link->a();
36  if(!path.isJointDownward(i)){
37  omega = -omega;
38  }
39  if(isTranslationValid){
40  Vector3 arm;
41  if(useTargetLinkLocalPos){
42  arm = targetLink->p() + targetLink->R() * targetLinkLocalPos - link->p();
43  } else {
44  arm = targetLink->p() - link->p();
45  }
46  const Vector3 dp = omega.cross(arm);
47  if(elementMask & 0x1) Ji(row++) = dp.x();
48  if(elementMask & 0x2) Ji(row++) = dp.y();
49  if(elementMask & 0x4) Ji(row++) = dp.z();
50  }
51  if(elementMask & 0x8) Ji(row++) = omega.x();
52  if(elementMask & 0x10) Ji(row++) = omega.y();
53  if(elementMask & 0x20) Ji(row ) = omega.z();
54  }
55  break;
56 
57  case Link::SLIDE_JOINT:
58  {
59  if(isTranslationValid){
60  Vector3 dp = link->R() * link->d();
61  if(!path.isJointDownward(i)){
62  dp = -dp;
63  }
64  if(elementMask & 0x1) Ji(row++) = dp.x();
65  if(elementMask & 0x2) Ji(row++) = dp.y();
66  if(elementMask & 0x4) Ji(row++) = dp.z();
67  }
68  if(elementMask & 0x8) Ji(row++) = 0.0;
69  if(elementMask & 0x10) Ji(row++) = 0.0;
70  if(elementMask & 0x20) Ji(row ) = 0.0;
71  }
72  break;
73 
74  default:
75  if(elementMask & 0x1) Ji(row++) = 0.0;
76  if(elementMask & 0x2) Ji(row++) = 0.0;
77  if(elementMask & 0x4) Ji(row++) = 0.0;
78  if(elementMask & 0x8) Ji(row++) = 0.0;
79  if(elementMask & 0x10) Ji(row++) = 0.0;
80  if(elementMask & 0x20) Ji(row ) = 0.0;
81  }
82 
83  ++i;
84 
85  if(link == targetLink){
86  break;
87  }
88  }
89 
90  while(i < n){
91  MatrixXd::ColXpr Ji = out_J.col(i + colOffset);
92  int row = rowOffset;
93  if(elementMask & 0x1) Ji(row++) = 0.0;
94  if(elementMask & 0x2) Ji(row++) = 0.0;
95  if(elementMask & 0x4) Ji(row++) = 0.0;
96  if(elementMask & 0x8) Ji(row++) = 0.0;
97  if(elementMask & 0x10) Ji(row++) = 0.0;
98  if(elementMask & 0x20) Ji(row ) = 0.0;
99  ++i;
100  }
101 }
102 
103 template<int elementMask, int rowOffset, int colOffset>
104 void setJacobian(const JointPath& path, Link* targetLink, MatrixXd& out_J) {
105  static const Vector3 targetLinkLocalPos(Vector3::Zero());
106  setJacobian<elementMask, rowOffset, colOffset, false>(
107  path, targetLink, targetLinkLocalPos, out_J);
108 }
109 
110 }
111 
112 #endif
cnoid::JointPath::isJointDownward
bool isJointDownward(int index) const
Definition: JointPath.h:66
cnoid::Vector3
Eigen::Vector3d Vector3
Definition: EigenTypes.h:57
cnoid::JointPath
Definition: JointPath.h:23
cnoid::calcCMJacobian
void calcCMJacobian(Body *body, Link *base, Eigen::MatrixXd &J)
compute CoM Jacobian
Definition: Jacobian.cpp:68
cnoid::calcAngularMomentumJacobian
void calcAngularMomentumJacobian(Body *body, Link *base, Eigen::MatrixXd &H)
compute Angular Momentum Jacobian
Definition: Jacobian.cpp:150
JointPath.h
The header file of the JointPath class.
cnoid::setJacobian
void setJacobian(const JointPath &path, Link *targetLink, const Vector3 &targetLinkLocalPos, MatrixXd &out_J)
Definition: Jacobian.h:17
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::JointPath::joint
Link * joint(int index) const
Definition: JointPath.h:50
Body.h
cnoid::JointPath::numJoints
int numJoints() const
Definition: JointPath.h:46