Bullet Collision Detection & Physics Library
btMultiBodyGearConstraint.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 
19 #include "btMultiBody.h"
22 
23 btMultiBodyGearConstraint::btMultiBodyGearConstraint(btMultiBody* bodyA, int linkA, btMultiBody* bodyB, int linkB, const btVector3& pivotInA, const btVector3& pivotInB, const btMatrix3x3& frameInA, const btMatrix3x3& frameInB)
24  :btMultiBodyConstraint(bodyA,bodyB,linkA,linkB,1,false),
25  m_gearRatio(1),
26  m_gearAuxLink(-1),
27  m_erp(0),
28  m_relativePositionTarget(0)
29 {
30 
31 }
32 
34 {
35 
37 
39 }
40 
42 {
43 }
44 
45 
47 {
48  if (m_bodyA)
49  {
50  if (m_linkA < 0)
51  {
53  if (col)
54  return col->getIslandTag();
55  }
56  else
57  {
60  }
61  }
62  return -1;
63 }
64 
66 {
67  if (m_bodyB)
68  {
69  if (m_linkB < 0)
70  {
72  if (col)
73  return col->getIslandTag();
74  }
75  else
76  {
79  }
80  }
81  return -1;
82 }
83 
84 
87  const btContactSolverInfo& infoGlobal)
88 {
89  // only positions need to be updated -- data.m_jacobians and force
90  // directions were set in the ctor and never change.
91 
93  {
95  }
96 
97  //don't crash
99  return;
100 
101 
102  if (m_maxAppliedImpulse==0.f)
103  return;
104 
105  // note: we rely on the fact that data.m_jacobians are
106  // always initialized to zero by the Constraint ctor
107  int linkDoF = 0;
108  unsigned int offsetA = 6 + (m_bodyA->getLink(m_linkA).m_dofOffset + linkDoF);
109  unsigned int offsetB = 6 + (m_bodyB->getLink(m_linkB).m_dofOffset + linkDoF);
110 
111  // row 0: the lower bound
112  jacobianA(0)[offsetA] = 1;
113  jacobianB(0)[offsetB] = m_gearRatio;
114 
115  btScalar posError = 0;
116  const btVector3 dummy(0, 0, 0);
117 
118  btScalar kp = 1;
119  btScalar kd = 1;
120  int numRows = getNumRows();
121 
122  for (int row=0;row<numRows;row++)
123  {
124  btMultiBodySolverConstraint& constraintRow = constraintRows.expandNonInitializing();
125 
126 
127  int dof = 0;
128  btScalar currentPosition = m_bodyA->getJointPosMultiDof(m_linkA)[dof];
129  btScalar currentVelocity = m_bodyA->getJointVelMultiDof(m_linkA)[dof];
130  btScalar auxVel = 0;
131 
132  if (m_gearAuxLink>=0)
133  {
134  auxVel = m_bodyA->getJointVelMultiDof(m_gearAuxLink)[dof];
135  }
136  currentVelocity += auxVel;
137  if (m_erp!=0)
138  {
139  btScalar currentPositionA = m_bodyA->getJointPosMultiDof(m_linkA)[dof];
140  if (m_gearAuxLink >= 0)
141  {
142  currentPositionA -= m_bodyA->getJointPosMultiDof(m_gearAuxLink)[dof];
143  }
144  btScalar currentPositionB = m_gearRatio*m_bodyA->getJointPosMultiDof(m_linkB)[dof];
145  btScalar diff = currentPositionB+currentPositionA;
146  btScalar desiredPositionDiff = this->m_relativePositionTarget;
147  posError = -m_erp*(desiredPositionDiff - diff);
148  }
149 
150  btScalar desiredRelativeVelocity = auxVel;
151 
152  fillMultiBodyConstraint(constraintRow,data,jacobianA(row),jacobianB(row),dummy,dummy,dummy,dummy,posError,infoGlobal,-m_maxAppliedImpulse,m_maxAppliedImpulse,false,1,false,desiredRelativeVelocity);
153 
154  constraintRow.m_orgConstraint = this;
155  constraintRow.m_orgDofIndex = row;
156  {
157  //expect either prismatic or revolute joint type for now
160  {
162  {
163  constraintRow.m_contactNormal1.setZero();
164  constraintRow.m_contactNormal2.setZero();
166  constraintRow.m_relpos1CrossNormal=revoluteAxisInWorld;
167  constraintRow.m_relpos2CrossNormal=-revoluteAxisInWorld;
168 
169  break;
170  }
172  {
174  constraintRow.m_contactNormal1=prismaticAxisInWorld;
175  constraintRow.m_contactNormal2=-prismaticAxisInWorld;
176  constraintRow.m_relpos1CrossNormal.setZero();
177  constraintRow.m_relpos2CrossNormal.setZero();
178  break;
179  }
180  default:
181  {
182  btAssert(0);
183  }
184  };
185 
186  }
187 
188  }
189 
190 }
191 
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)
btVector3 quatRotate(const btQuaternion &rotation, const btVector3 &v)
Definition: btQuaternion.h:937
void setZero()
Definition: btVector3.h:683
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
virtual void createConstraintRows(btMultiBodyConstraintArray &constraintRows, btMultiBodyJacobianData &data, const btContactSolverInfo &infoGlobal)
int getIslandTag() const
The btMatrix3x3 class implements a 3x3 rotation matrix, to perform linear algebra in combination with...
Definition: btMatrix3x3.h:48
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)
btMultiBodyGearConstraint(btMultiBody *bodyA, int linkA, btMultiBody *bodyB, int linkB, const btVector3 &pivotInA, const btVector3 &pivotInB, const btMatrix3x3 &frameInA, const btMatrix3x3 &frameInB)
This file was written by Erwin Coumans.
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