Bullet Collision Detection & Physics Library
btMultiBodyJointMotor.cpp
Go to the documentation of this file.
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2013 Erwin Coumans http://bulletphysics.org
4 
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
10 
11 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.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15 
17 
18 #include "btMultiBodyJointMotor.h"
19 #include "btMultiBody.h"
22 
23 
24 btMultiBodyJointMotor::btMultiBodyJointMotor(btMultiBody* body, int link, btScalar desiredVelocity, btScalar maxMotorImpulse)
25  :btMultiBodyConstraint(body,body,link,body->getLink(link).m_parent,1,true),
26  m_desiredVelocity(desiredVelocity),
27  m_desiredPosition(0),
28  m_kd(1.),
29  m_kp(0),
30  m_erp(1),
31  m_rhsClamp(SIMD_INFINITY)
32 {
33 
34  m_maxAppliedImpulse = maxMotorImpulse;
35  // the data.m_jacobians never change, so may as well
36  // initialize them here
37 
38 
39 }
40 
42 {
44  // note: we rely on the fact that data.m_jacobians are
45  // always initialized to zero by the Constraint ctor
46  int linkDoF = 0;
47  unsigned int offset = 6 + (m_bodyA->getLink(m_linkA).m_dofOffset + linkDoF);
48 
49  // row 0: the lower bound
50  // row 0: the lower bound
51  jacobianA(0)[offset] = 1;
52 
54 }
55 
56 btMultiBodyJointMotor::btMultiBodyJointMotor(btMultiBody* body, int link, int linkDoF, btScalar desiredVelocity, btScalar maxMotorImpulse)
57  //:btMultiBodyConstraint(body,0,link,-1,1,true),
58  :btMultiBodyConstraint(body,body,link,body->getLink(link).m_parent,1,true),
59  m_desiredVelocity(desiredVelocity),
61  m_kd(1.),
62  m_kp(0),
63  m_erp(1),
65 {
66  btAssert(linkDoF < body->getLink(link).m_dofCount);
67 
68  m_maxAppliedImpulse = maxMotorImpulse;
69 
70 }
72 {
73 }
74 
76 {
77  if (this->m_linkA < 0)
78  {
80  if (col)
81  return col->getIslandTag();
82  }
83  else
84  {
86  {
88  }
89  }
90  return -1;
91 }
92 
94 {
95  if (m_linkB < 0)
96  {
98  if (col)
99  return col->getIslandTag();
100  }
101  else
102  {
104  {
106  }
107  }
108  return -1;
109 }
110 
111 
114  const btContactSolverInfo& infoGlobal)
115 {
116  // only positions need to be updated -- data.m_jacobians and force
117  // directions were set in the ctor and never change.
118 
120  {
122  }
123 
124  //don't crash
126  return;
127 
128  if (m_maxAppliedImpulse==0.f)
129  return;
130 
131  const btScalar posError = 0;
132  const btVector3 dummy(0, 0, 0);
133 
134  for (int row=0;row<getNumRows();row++)
135  {
136  btMultiBodySolverConstraint& constraintRow = constraintRows.expandNonInitializing();
137 
138  int dof = 0;
139  btScalar currentPosition = m_bodyA->getJointPosMultiDof(m_linkA)[dof];
140  btScalar currentVelocity = m_bodyA->getJointVelMultiDof(m_linkA)[dof];
141  btScalar positionStabiliationTerm = m_erp*(m_desiredPosition-currentPosition)/infoGlobal.m_timeStep;
142 
143  btScalar velocityError = (m_desiredVelocity - currentVelocity);
144  btScalar rhs = m_kp * positionStabiliationTerm + currentVelocity+m_kd * velocityError;
145  if (rhs>m_rhsClamp)
146  {
147  rhs=m_rhsClamp;
148  }
149  if (rhs<-m_rhsClamp)
150  {
151  rhs=-m_rhsClamp;
152  }
153 
154 
155  fillMultiBodyConstraint(constraintRow,data,jacobianA(row),jacobianB(row),dummy,dummy,dummy,dummy,posError,infoGlobal,-m_maxAppliedImpulse,m_maxAppliedImpulse,false,1,false,rhs);
156  constraintRow.m_orgConstraint = this;
157  constraintRow.m_orgDofIndex = row;
158  {
159  //expect either prismatic or revolute joint type for now
162  {
164  {
165  constraintRow.m_contactNormal1.setZero();
166  constraintRow.m_contactNormal2.setZero();
168  constraintRow.m_relpos1CrossNormal=revoluteAxisInWorld;
169  constraintRow.m_relpos2CrossNormal=-revoluteAxisInWorld;
170 
171  break;
172  }
174  {
176  constraintRow.m_contactNormal1=prismaticAxisInWorld;
177  constraintRow.m_contactNormal2=-prismaticAxisInWorld;
178  constraintRow.m_relpos1CrossNormal.setZero();
179  constraintRow.m_relpos2CrossNormal.setZero();
180 
181  break;
182  }
183  default:
184  {
185  btAssert(0);
186  }
187  };
188 
189  }
190 
191  }
192 
193 }
194 
virtual int getIslandIdB() const
btScalar * jacobianB(int row)
const btMultibodyLink & getLink(int index) const
Definition: btMultiBody.h:119
1D constraint along a normal axis between bodyA and bodyB. It can be combined to solve contact and fr...
btQuaternion getRotation() const
Return a quaternion representing the rotation.
Definition: btTransform.h:122
btMultiBodyConstraint * m_orgConstraint
#define btAssert(x)
Definition: btScalar.h:131
btScalar * jacobianA(int row)
btScalar * getJointVelMultiDof(int i)
btMultiBodyJointMotor(btMultiBody *body, int link, btScalar desiredVelocity, btScalar maxMotorImpulse)
This file was written by Erwin Coumans.
btVector3 quatRotate(const btQuaternion &rotation, const btVector3 &v)
Definition: btQuaternion.h:937
#define SIMD_INFINITY
Definition: btScalar.h:522
virtual int getIslandIdA() const
void setZero()
Definition: btVector3.h:683
virtual void createConstraintRows(btMultiBodyConstraintArray &constraintRows, btMultiBodyJacobianData &data, const btContactSolverInfo &infoGlobal)
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
int getIslandTag() const
const btMultiBodyLinkCollider * getBaseCollider() const
Definition: btMultiBody.h:134
btScalar fillMultiBodyConstraint(btMultiBodySolverConstraint &solverConstraint, btMultiBodyJacobianData &data, btScalar *jacOrgA, btScalar *jacOrgB, const btVector3 &constraintNormalAng, const btVector3 &constraintNormalLin, const btVector3 &posAworld, const btVector3 &posBworld, btScalar posError, const btContactSolverInfo &infoGlobal, btScalar lowerLimit, btScalar upperLimit, bool angConstraint=false, btScalar relaxation=1.f, bool isFriction=false, btScalar desiredVelocity=0, btScalar cfmSlip=0)
btScalar * getJointPosMultiDof(int i)
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:292