btMultiBodyLink.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2013 Erwin Coumans http://bulletphysics.org
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #ifndef BT_MULTIBODY_LINK_H
  14. #define BT_MULTIBODY_LINK_H
  15. #include "LinearMath/btQuaternion.h"
  16. #include "LinearMath/btVector3.h"
  17. #include "BulletCollision/CollisionDispatch/btCollisionObject.h"
  18. enum btMultiBodyLinkFlags
  19. {
  20. BT_MULTIBODYLINKFLAGS_DISABLE_PARENT_COLLISION = 1,
  21. BT_MULTIBODYLINKFLAGS_DISABLE_ALL_PARENT_COLLISION = 2,
  22. };
  23. //both defines are now permanently enabled
  24. #define BT_MULTIBODYLINK_INCLUDE_PLANAR_JOINTS
  25. #define TEST_SPATIAL_ALGEBRA_LAYER
  26. //
  27. // Various spatial helper functions
  28. //
  29. //namespace {
  30. #include "LinearMath/btSpatialAlgebra.h"
  31. //}
  32. //
  33. // Link struct
  34. //
  35. struct btMultibodyLink
  36. {
  37. BT_DECLARE_ALIGNED_ALLOCATOR();
  38. btScalar m_mass; // mass of link
  39. btVector3 m_inertiaLocal; // inertia of link (local frame; diagonal)
  40. int m_parent; // index of the parent link (assumed to be < index of this link), or -1 if parent is the base link.
  41. btQuaternion m_zeroRotParentToThis; // rotates vectors in parent-frame to vectors in local-frame (when q=0). constant.
  42. btVector3 m_dVector; // vector from the inboard joint pos to this link's COM. (local frame.) constant.
  43. //this is set to zero for planar joint (see also m_eVector comment)
  44. // m_eVector is constant, but depends on the joint type:
  45. // revolute, fixed, prismatic, spherical: vector from parent's COM to the pivot point, in PARENT's frame.
  46. // planar: vector from COM of parent to COM of this link, WHEN Q = 0. (local frame.)
  47. // todo: fix the planar so it is consistent with the other joints
  48. btVector3 m_eVector;
  49. btSpatialMotionVector m_absFrameTotVelocity, m_absFrameLocVelocity;
  50. enum eFeatherstoneJointType
  51. {
  52. eRevolute = 0,
  53. ePrismatic = 1,
  54. eSpherical = 2,
  55. ePlanar = 3,
  56. eFixed = 4,
  57. eInvalid
  58. };
  59. // "axis" = spatial joint axis (Mirtich Defn 9 p104). (expressed in local frame.) constant.
  60. // for prismatic: m_axesTop[0] = zero;
  61. // m_axesBottom[0] = unit vector along the joint axis.
  62. // for revolute: m_axesTop[0] = unit vector along the rotation axis (u);
  63. // m_axesBottom[0] = u cross m_dVector (i.e. COM linear motion due to the rotation at the joint)
  64. //
  65. // for spherical: m_axesTop[0][1][2] (u1,u2,u3) form a 3x3 identity matrix (3 rotation axes)
  66. // m_axesBottom[0][1][2] cross u1,u2,u3 (i.e. COM linear motion due to the rotation at the joint)
  67. //
  68. // for planar: m_axesTop[0] = unit vector along the rotation axis (u); defines the plane of motion
  69. // m_axesTop[1][2] = zero
  70. // m_axesBottom[0] = zero
  71. // m_axesBottom[1][2] = unit vectors along the translational axes on that plane
  72. btSpatialMotionVector m_axes[6];
  73. void setAxisTop(int dof, const btVector3 &axis) { m_axes[dof].m_topVec = axis; }
  74. void setAxisBottom(int dof, const btVector3 &axis)
  75. {
  76. m_axes[dof].m_bottomVec = axis;
  77. }
  78. void setAxisTop(int dof, const btScalar &x, const btScalar &y, const btScalar &z)
  79. {
  80. m_axes[dof].m_topVec.setValue(x, y, z);
  81. }
  82. void setAxisBottom(int dof, const btScalar &x, const btScalar &y, const btScalar &z)
  83. {
  84. m_axes[dof].m_bottomVec.setValue(x, y, z);
  85. }
  86. const btVector3 &getAxisTop(int dof) const { return m_axes[dof].m_topVec; }
  87. const btVector3 &getAxisBottom(int dof) const { return m_axes[dof].m_bottomVec; }
  88. int m_dofOffset, m_cfgOffset;
  89. btQuaternion m_cachedRotParentToThis; // rotates vectors in parent frame to vectors in local frame
  90. btVector3 m_cachedRVector; // vector from COM of parent to COM of this link, in local frame.
  91. // predicted verstion
  92. btQuaternion m_cachedRotParentToThis_interpolate; // rotates vectors in parent frame to vectors in local frame
  93. btVector3 m_cachedRVector_interpolate; // vector from COM of parent to COM of this link, in local frame.
  94. btVector3 m_appliedForce; // In WORLD frame
  95. btVector3 m_appliedTorque; // In WORLD frame
  96. btVector3 m_appliedConstraintForce; // In WORLD frame
  97. btVector3 m_appliedConstraintTorque; // In WORLD frame
  98. btScalar m_jointPos[7];
  99. btScalar m_jointPos_interpolate[7];
  100. //m_jointTorque is the joint torque applied by the user using 'addJointTorque'.
  101. //It gets set to zero after each internal stepSimulation call
  102. btScalar m_jointTorque[6];
  103. class btMultiBodyLinkCollider *m_collider;
  104. int m_flags;
  105. int m_dofCount, m_posVarCount; //redundant but handy
  106. eFeatherstoneJointType m_jointType;
  107. struct btMultiBodyJointFeedback *m_jointFeedback;
  108. btTransform m_cachedWorldTransform; //this cache is updated when calling btMultiBody::forwardKinematics
  109. const char *m_linkName; //m_linkName memory needs to be managed by the developer/user!
  110. const char *m_jointName; //m_jointName memory needs to be managed by the developer/user!
  111. const void *m_userPtr; //m_userPtr ptr needs to be managed by the developer/user!
  112. btScalar m_jointDamping; //todo: implement this internally. It is unused for now, it is set by a URDF loader. User can apply manual damping.
  113. btScalar m_jointFriction; //todo: implement this internally. It is unused for now, it is set by a URDF loader. User can apply manual friction using a velocity motor.
  114. btScalar m_jointLowerLimit; //todo: implement this internally. It is unused for now, it is set by a URDF loader.
  115. btScalar m_jointUpperLimit; //todo: implement this internally. It is unused for now, it is set by a URDF loader.
  116. btScalar m_jointMaxForce; //todo: implement this internally. It is unused for now, it is set by a URDF loader.
  117. btScalar m_jointMaxVelocity; //todo: implement this internally. It is unused for now, it is set by a URDF loader.
  118. // ctor: set some sensible defaults
  119. btMultibodyLink()
  120. : m_mass(1),
  121. m_parent(-1),
  122. m_zeroRotParentToThis(0, 0, 0, 1),
  123. m_cachedRotParentToThis(0, 0, 0, 1),
  124. m_cachedRotParentToThis_interpolate(0, 0, 0, 1),
  125. m_collider(0),
  126. m_flags(0),
  127. m_dofCount(0),
  128. m_posVarCount(0),
  129. m_jointType(btMultibodyLink::eInvalid),
  130. m_jointFeedback(0),
  131. m_linkName(0),
  132. m_jointName(0),
  133. m_userPtr(0),
  134. m_jointDamping(0),
  135. m_jointFriction(0),
  136. m_jointLowerLimit(0),
  137. m_jointUpperLimit(0),
  138. m_jointMaxForce(0),
  139. m_jointMaxVelocity(0)
  140. {
  141. m_inertiaLocal.setValue(1, 1, 1);
  142. setAxisTop(0, 0., 0., 0.);
  143. setAxisBottom(0, 1., 0., 0.);
  144. m_dVector.setValue(0, 0, 0);
  145. m_eVector.setValue(0, 0, 0);
  146. m_cachedRVector.setValue(0, 0, 0);
  147. m_cachedRVector_interpolate.setValue(0, 0, 0);
  148. m_appliedForce.setValue(0, 0, 0);
  149. m_appliedTorque.setValue(0, 0, 0);
  150. m_appliedConstraintForce.setValue(0, 0, 0);
  151. m_appliedConstraintTorque.setValue(0, 0, 0);
  152. //
  153. m_jointPos[0] = m_jointPos[1] = m_jointPos[2] = m_jointPos[4] = m_jointPos[5] = m_jointPos[6] = 0.f;
  154. m_jointPos[3] = 1.f; //"quat.w"
  155. m_jointTorque[0] = m_jointTorque[1] = m_jointTorque[2] = m_jointTorque[3] = m_jointTorque[4] = m_jointTorque[5] = 0.f;
  156. m_cachedWorldTransform.setIdentity();
  157. }
  158. // routine to update m_cachedRotParentToThis and m_cachedRVector
  159. void updateCacheMultiDof(btScalar *pq = 0)
  160. {
  161. btScalar *pJointPos = (pq ? pq : &m_jointPos[0]);
  162. btQuaternion& cachedRot = m_cachedRotParentToThis;
  163. btVector3& cachedVector = m_cachedRVector;
  164. switch (m_jointType)
  165. {
  166. case eRevolute:
  167. {
  168. cachedRot = btQuaternion(getAxisTop(0), -pJointPos[0]) * m_zeroRotParentToThis;
  169. cachedVector = m_dVector + quatRotate(m_cachedRotParentToThis, m_eVector);
  170. break;
  171. }
  172. case ePrismatic:
  173. {
  174. // m_cachedRotParentToThis never changes, so no need to update
  175. cachedVector = m_dVector + quatRotate(m_cachedRotParentToThis, m_eVector) + pJointPos[0] * getAxisBottom(0);
  176. break;
  177. }
  178. case eSpherical:
  179. {
  180. cachedRot = btQuaternion(pJointPos[0], pJointPos[1], pJointPos[2], -pJointPos[3]) * m_zeroRotParentToThis;
  181. cachedVector = m_dVector + quatRotate(cachedRot, m_eVector);
  182. break;
  183. }
  184. case ePlanar:
  185. {
  186. cachedRot = btQuaternion(getAxisTop(0), -pJointPos[0]) * m_zeroRotParentToThis;
  187. cachedVector = quatRotate(btQuaternion(getAxisTop(0), -pJointPos[0]), pJointPos[1] * getAxisBottom(1) + pJointPos[2] * getAxisBottom(2)) + quatRotate(cachedRot, m_eVector);
  188. break;
  189. }
  190. case eFixed:
  191. {
  192. cachedRot = m_zeroRotParentToThis;
  193. cachedVector = m_dVector + quatRotate(cachedRot, m_eVector);
  194. break;
  195. }
  196. default:
  197. {
  198. //invalid type
  199. btAssert(0);
  200. }
  201. }
  202. m_cachedRotParentToThis_interpolate = m_cachedRotParentToThis;
  203. m_cachedRVector_interpolate = m_cachedRVector;
  204. }
  205. void updateInterpolationCacheMultiDof()
  206. {
  207. btScalar *pJointPos = &m_jointPos_interpolate[0];
  208. btQuaternion& cachedRot = m_cachedRotParentToThis_interpolate;
  209. btVector3& cachedVector = m_cachedRVector_interpolate;
  210. switch (m_jointType)
  211. {
  212. case eRevolute:
  213. {
  214. cachedRot = btQuaternion(getAxisTop(0), -pJointPos[0]) * m_zeroRotParentToThis;
  215. cachedVector = m_dVector + quatRotate(m_cachedRotParentToThis, m_eVector);
  216. break;
  217. }
  218. case ePrismatic:
  219. {
  220. // m_cachedRotParentToThis never changes, so no need to update
  221. cachedVector = m_dVector + quatRotate(m_cachedRotParentToThis, m_eVector) + pJointPos[0] * getAxisBottom(0);
  222. break;
  223. }
  224. case eSpherical:
  225. {
  226. cachedRot = btQuaternion(pJointPos[0], pJointPos[1], pJointPos[2], -pJointPos[3]) * m_zeroRotParentToThis;
  227. cachedVector = m_dVector + quatRotate(cachedRot, m_eVector);
  228. break;
  229. }
  230. case ePlanar:
  231. {
  232. cachedRot = btQuaternion(getAxisTop(0), -pJointPos[0]) * m_zeroRotParentToThis;
  233. cachedVector = quatRotate(btQuaternion(getAxisTop(0), -pJointPos[0]), pJointPos[1] * getAxisBottom(1) + pJointPos[2] * getAxisBottom(2)) + quatRotate(cachedRot, m_eVector);
  234. break;
  235. }
  236. case eFixed:
  237. {
  238. cachedRot = m_zeroRotParentToThis;
  239. cachedVector = m_dVector + quatRotate(cachedRot, m_eVector);
  240. break;
  241. }
  242. default:
  243. {
  244. //invalid type
  245. btAssert(0);
  246. }
  247. }
  248. }
  249. };
  250. #endif //BT_MULTIBODY_LINK_H