btMultiBodyJointMotor.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. ///This file was written by Erwin Coumans
  14. #include "btMultiBodyJointMotor.h"
  15. #include "btMultiBody.h"
  16. #include "btMultiBodyLinkCollider.h"
  17. #include "BulletCollision/CollisionDispatch/btCollisionObject.h"
  18. btMultiBodyJointMotor::btMultiBodyJointMotor(btMultiBody* body, int link, btScalar desiredVelocity, btScalar maxMotorImpulse)
  19. : btMultiBodyConstraint(body, body, link, body->getLink(link).m_parent, 1, true, MULTIBODY_CONSTRAINT_1DOF_JOINT_MOTOR),
  20. m_desiredVelocity(desiredVelocity),
  21. m_desiredPosition(0),
  22. m_kd(1.),
  23. m_kp(0),
  24. m_erp(1),
  25. m_rhsClamp(SIMD_INFINITY)
  26. {
  27. m_maxAppliedImpulse = maxMotorImpulse;
  28. // the data.m_jacobians never change, so may as well
  29. // initialize them here
  30. }
  31. void btMultiBodyJointMotor::finalizeMultiDof()
  32. {
  33. allocateJacobiansMultiDof();
  34. // note: we rely on the fact that data.m_jacobians are
  35. // always initialized to zero by the Constraint ctor
  36. int linkDoF = 0;
  37. unsigned int offset = 6 + (m_bodyA->getLink(m_linkA).m_dofOffset + linkDoF);
  38. // row 0: the lower bound
  39. // row 0: the lower bound
  40. jacobianA(0)[offset] = 1;
  41. m_numDofsFinalized = m_jacSizeBoth;
  42. }
  43. btMultiBodyJointMotor::btMultiBodyJointMotor(btMultiBody* body, int link, int linkDoF, btScalar desiredVelocity, btScalar maxMotorImpulse)
  44. //:btMultiBodyConstraint(body,0,link,-1,1,true),
  45. : btMultiBodyConstraint(body, body, link, body->getLink(link).m_parent, 1, true, MULTIBODY_CONSTRAINT_1DOF_JOINT_MOTOR),
  46. m_desiredVelocity(desiredVelocity),
  47. m_desiredPosition(0),
  48. m_kd(1.),
  49. m_kp(0),
  50. m_erp(1),
  51. m_rhsClamp(SIMD_INFINITY)
  52. {
  53. btAssert(linkDoF < body->getLink(link).m_dofCount);
  54. m_maxAppliedImpulse = maxMotorImpulse;
  55. }
  56. btMultiBodyJointMotor::~btMultiBodyJointMotor()
  57. {
  58. }
  59. int btMultiBodyJointMotor::getIslandIdA() const
  60. {
  61. if (this->m_linkA < 0)
  62. {
  63. btMultiBodyLinkCollider* col = m_bodyA->getBaseCollider();
  64. if (col)
  65. return col->getIslandTag();
  66. }
  67. else
  68. {
  69. if (m_bodyA->getLink(m_linkA).m_collider)
  70. {
  71. return m_bodyA->getLink(m_linkA).m_collider->getIslandTag();
  72. }
  73. }
  74. return -1;
  75. }
  76. int btMultiBodyJointMotor::getIslandIdB() const
  77. {
  78. if (m_linkB < 0)
  79. {
  80. btMultiBodyLinkCollider* col = m_bodyB->getBaseCollider();
  81. if (col)
  82. return col->getIslandTag();
  83. }
  84. else
  85. {
  86. if (m_bodyB->getLink(m_linkB).m_collider)
  87. {
  88. return m_bodyB->getLink(m_linkB).m_collider->getIslandTag();
  89. }
  90. }
  91. return -1;
  92. }
  93. void btMultiBodyJointMotor::createConstraintRows(btMultiBodyConstraintArray& constraintRows,
  94. btMultiBodyJacobianData& data,
  95. const btContactSolverInfo& infoGlobal)
  96. {
  97. // only positions need to be updated -- data.m_jacobians and force
  98. // directions were set in the ctor and never change.
  99. if (m_numDofsFinalized != m_jacSizeBoth)
  100. {
  101. finalizeMultiDof();
  102. }
  103. //don't crash
  104. if (m_numDofsFinalized != m_jacSizeBoth)
  105. return;
  106. if (m_maxAppliedImpulse == 0.f)
  107. return;
  108. const btScalar posError = 0;
  109. const btVector3 dummy(0, 0, 0);
  110. for (int row = 0; row < getNumRows(); row++)
  111. {
  112. btMultiBodySolverConstraint& constraintRow = constraintRows.expandNonInitializing();
  113. int dof = 0;
  114. btScalar currentPosition = m_bodyA->getJointPosMultiDof(m_linkA)[dof];
  115. btScalar currentVelocity = m_bodyA->getJointVelMultiDof(m_linkA)[dof];
  116. btScalar positionStabiliationTerm = m_erp * (m_desiredPosition - currentPosition) / infoGlobal.m_timeStep;
  117. btScalar velocityError = (m_desiredVelocity - currentVelocity);
  118. btScalar rhs = m_kp * positionStabiliationTerm + currentVelocity + m_kd * velocityError;
  119. if (rhs > m_rhsClamp)
  120. {
  121. rhs = m_rhsClamp;
  122. }
  123. if (rhs < -m_rhsClamp)
  124. {
  125. rhs = -m_rhsClamp;
  126. }
  127. fillMultiBodyConstraint(constraintRow, data, jacobianA(row), jacobianB(row), dummy, dummy, dummy, dummy, posError, infoGlobal, -m_maxAppliedImpulse, m_maxAppliedImpulse, false, 1, false, rhs);
  128. constraintRow.m_orgConstraint = this;
  129. constraintRow.m_orgDofIndex = row;
  130. {
  131. //expect either prismatic or revolute joint type for now
  132. btAssert((m_bodyA->getLink(m_linkA).m_jointType == btMultibodyLink::eRevolute) || (m_bodyA->getLink(m_linkA).m_jointType == btMultibodyLink::ePrismatic));
  133. switch (m_bodyA->getLink(m_linkA).m_jointType)
  134. {
  135. case btMultibodyLink::eRevolute:
  136. {
  137. constraintRow.m_contactNormal1.setZero();
  138. constraintRow.m_contactNormal2.setZero();
  139. btVector3 revoluteAxisInWorld = quatRotate(m_bodyA->getLink(m_linkA).m_cachedWorldTransform.getRotation(), m_bodyA->getLink(m_linkA).m_axes[0].m_topVec);
  140. constraintRow.m_relpos1CrossNormal = revoluteAxisInWorld;
  141. constraintRow.m_relpos2CrossNormal = -revoluteAxisInWorld;
  142. break;
  143. }
  144. case btMultibodyLink::ePrismatic:
  145. {
  146. btVector3 prismaticAxisInWorld = quatRotate(m_bodyA->getLink(m_linkA).m_cachedWorldTransform.getRotation(), m_bodyA->getLink(m_linkA).m_axes[0].m_bottomVec);
  147. constraintRow.m_contactNormal1 = prismaticAxisInWorld;
  148. constraintRow.m_contactNormal2 = -prismaticAxisInWorld;
  149. constraintRow.m_relpos1CrossNormal.setZero();
  150. constraintRow.m_relpos2CrossNormal.setZero();
  151. break;
  152. }
  153. default:
  154. {
  155. btAssert(0);
  156. }
  157. };
  158. }
  159. }
  160. }