re-enable auto-joint velocity target motors in URDF multibody import
This commit is contained in:
@@ -8,6 +8,7 @@
|
|||||||
#include "BulletDynamics/Featherstone/btMultiBodyLinkCollider.h"
|
#include "BulletDynamics/Featherstone/btMultiBodyLinkCollider.h"
|
||||||
#include "Bullet3Common/b3FileUtils.h"
|
#include "Bullet3Common/b3FileUtils.h"
|
||||||
#include "BulletCollision/CollisionShapes/btShapeHull.h"//to create a tesselation of a generic btConvexShape
|
#include "BulletCollision/CollisionShapes/btShapeHull.h"//to create a tesselation of a generic btConvexShape
|
||||||
|
#include "BulletDynamics/Featherstone/btMultiBodyJointMotor.h"
|
||||||
|
|
||||||
static int bodyCollisionFilterGroup=btBroadphaseProxy::CharacterFilter;
|
static int bodyCollisionFilterGroup=btBroadphaseProxy::CharacterFilter;
|
||||||
static int bodyCollisionFilterMask=btBroadphaseProxy::AllFilter&(~btBroadphaseProxy::CharacterFilter);
|
static int bodyCollisionFilterMask=btBroadphaseProxy::AllFilter&(~btBroadphaseProxy::CharacterFilter);
|
||||||
@@ -37,6 +38,11 @@ class MyURDF2Bullet : public URDF2Bullet
|
|||||||
mutable btMultiBody* m_bulletMultiBody;
|
mutable btMultiBody* m_bulletMultiBody;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
mutable btAlignedObjectArray<int> m_urdf2mbLink;
|
||||||
|
mutable btAlignedObjectArray<int> m_mb2urdfLink;
|
||||||
|
|
||||||
|
|
||||||
MyURDF2Bullet(my_shared_ptr<ModelInterface> robot,GraphicsPhysicsBridge& gfxBridge)
|
MyURDF2Bullet(my_shared_ptr<ModelInterface> robot,GraphicsPhysicsBridge& gfxBridge)
|
||||||
:m_robot(robot),
|
:m_robot(robot),
|
||||||
m_gfxBridge(gfxBridge),
|
m_gfxBridge(gfxBridge),
|
||||||
@@ -49,6 +55,9 @@ public:
|
|||||||
{
|
{
|
||||||
m_links[i]->m_link_index = i;
|
m_links[i]->m_link_index = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m_urdf2mbLink.resize(m_links.size(),-2);
|
||||||
|
m_mb2urdfLink.resize(m_links.size(),-2);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual int getRootLinkIndex() const
|
virtual int getRootLinkIndex() const
|
||||||
@@ -79,6 +88,11 @@ public:
|
|||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
virtual std::string getJointName(int linkIndex) const
|
||||||
|
{
|
||||||
|
return m_links[linkIndex]->parent_joint->name;
|
||||||
|
}
|
||||||
|
|
||||||
virtual void getMassAndInertia(int linkIndex, btScalar& mass,btVector3& localInertiaDiagonal, btTransform& inertialFrame) const
|
virtual void getMassAndInertia(int linkIndex, btScalar& mass,btVector3& localInertiaDiagonal, btTransform& inertialFrame) const
|
||||||
{
|
{
|
||||||
if ((*m_links[linkIndex]).inertial)
|
if ((*m_links[linkIndex]).inertial)
|
||||||
@@ -231,6 +245,11 @@ public:
|
|||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
virtual void addLinkMapping(int urdfLinkIndex, int mbLinkIndex) const
|
||||||
|
{
|
||||||
|
m_urdf2mbLink[urdfLinkIndex] = mbLinkIndex;
|
||||||
|
m_mb2urdfLink[mbLinkIndex] = urdfLinkIndex;
|
||||||
|
}
|
||||||
|
|
||||||
virtual void createRigidBodyGraphicsInstance(int linkIndex, btRigidBody* body, const btVector3& colorRgba, int graphicsIndex) const
|
virtual void createRigidBodyGraphicsInstance(int linkIndex, btRigidBody* body, const btVector3& colorRgba, int graphicsIndex) const
|
||||||
{
|
{
|
||||||
@@ -254,12 +273,30 @@ public:
|
|||||||
|
|
||||||
btAlignedObjectArray<std::string> gFileNameArray;
|
btAlignedObjectArray<std::string> gFileNameArray;
|
||||||
|
|
||||||
|
|
||||||
|
#define MAX_NUM_MOTORS 1024
|
||||||
|
|
||||||
|
struct ImportUrdfInternalData
|
||||||
|
{
|
||||||
|
ImportUrdfInternalData()
|
||||||
|
:m_numMotors(0)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
float m_motorTargetVelocities[MAX_NUM_MOTORS];
|
||||||
|
btMultiBodyJointMotor* m_jointMotors [MAX_NUM_MOTORS];
|
||||||
|
int m_numMotors;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
ImportUrdfSetup::ImportUrdfSetup()
|
ImportUrdfSetup::ImportUrdfSetup()
|
||||||
{
|
{
|
||||||
static int count = 0;
|
static int count = 0;
|
||||||
gFileNameArray.clear();
|
gFileNameArray.clear();
|
||||||
gFileNameArray.push_back("r2d2.urdf");
|
gFileNameArray.push_back("r2d2.urdf");
|
||||||
|
|
||||||
|
m_data = new ImportUrdfInternalData;
|
||||||
|
|
||||||
//load additional urdf file names from file
|
//load additional urdf file names from file
|
||||||
|
|
||||||
FILE* f = fopen("urdf_files.txt","r");
|
FILE* f = fopen("urdf_files.txt","r");
|
||||||
@@ -291,7 +328,7 @@ ImportUrdfSetup::ImportUrdfSetup()
|
|||||||
|
|
||||||
ImportUrdfSetup::~ImportUrdfSetup()
|
ImportUrdfSetup::~ImportUrdfSetup()
|
||||||
{
|
{
|
||||||
|
delete m_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
static btVector4 colors[4] =
|
static btVector4 colors[4] =
|
||||||
@@ -1523,6 +1560,12 @@ void ImportUrdfSetup::initPhysics(GraphicsPhysicsBridge& gfxBridge)
|
|||||||
mappings.m_urdfLinkIndices2BulletLinkIndices.resize(numJoints+1,-2);//root and child links (=1+numJoints)
|
mappings.m_urdfLinkIndices2BulletLinkIndices.resize(numJoints+1,-2);//root and child links (=1+numJoints)
|
||||||
URDFvisual2BulletCollisionShape(root_link, gfxBridge, identityTrans,m_dynamicsWorld,mappings,pathPrefix);
|
URDFvisual2BulletCollisionShape(root_link, gfxBridge, identityTrans,m_dynamicsWorld,mappings,pathPrefix);
|
||||||
mb = mappings.m_bulletMultiBody;
|
mb = mappings.m_bulletMultiBody;
|
||||||
|
if (useFeatherstone)
|
||||||
|
{
|
||||||
|
mb->setHasSelfCollision(false);
|
||||||
|
mb->finalizeMultiDof();
|
||||||
|
m_dynamicsWorld->addMultiBody(mb);
|
||||||
|
}
|
||||||
} else
|
} else
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -1532,19 +1575,51 @@ void ImportUrdfSetup::initPhysics(GraphicsPhysicsBridge& gfxBridge)
|
|||||||
printf("urdf root link index = %d\n",rootLinkIndex);
|
printf("urdf root link index = %d\n",rootLinkIndex);
|
||||||
ConvertURDF2Bullet(u2b,identityTrans,m_dynamicsWorld,useFeatherstone,pathPrefix);
|
ConvertURDF2Bullet(u2b,identityTrans,m_dynamicsWorld,useFeatherstone,pathPrefix);
|
||||||
mb = u2b.getBulletMultiBody();
|
mb = u2b.getBulletMultiBody();
|
||||||
|
|
||||||
|
if (useFeatherstone)
|
||||||
|
{
|
||||||
|
mb->setHasSelfCollision(false);
|
||||||
|
mb->finalizeMultiDof();
|
||||||
|
m_dynamicsWorld->addMultiBody(mb);
|
||||||
|
|
||||||
|
|
||||||
|
//create motors for each joint
|
||||||
|
|
||||||
|
for (int i=0;i<mb->getNumLinks();i++)
|
||||||
|
{
|
||||||
|
int mbLinkIndex = i;
|
||||||
|
if (mb->getLink(mbLinkIndex).m_jointType==btMultibodyLink::eRevolute)
|
||||||
|
{
|
||||||
|
if (m_data->m_numMotors<MAX_NUM_MOTORS)
|
||||||
|
{
|
||||||
|
int urdfLinkIndex = u2b.m_mb2urdfLink[mbLinkIndex];
|
||||||
|
|
||||||
|
std::string jointName = u2b.getJointName(urdfLinkIndex);
|
||||||
|
char motorName[1024];
|
||||||
|
sprintf(motorName,"%s q'", jointName.c_str());
|
||||||
|
float* motorVel = &m_data->m_motorTargetVelocities[m_data->m_numMotors];
|
||||||
|
*motorVel = 0.f;
|
||||||
|
SliderParams slider(motorName,motorVel);
|
||||||
|
slider.m_minVal=-4;
|
||||||
|
slider.m_maxVal=4;
|
||||||
|
gfxBridge.getParameterInterface()->registerSliderFloatParameter(slider);
|
||||||
|
float maxMotorImpulse = 0.1f;
|
||||||
|
btMultiBodyJointMotor* motor = new btMultiBodyJointMotor(mb,mbLinkIndex,0,0,maxMotorImpulse);
|
||||||
|
m_data->m_jointMotors[m_data->m_numMotors]=motor;
|
||||||
|
m_dynamicsWorld->addMultiBodyConstraint(motor);
|
||||||
|
m_data->m_numMotors++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (useFeatherstone)
|
|
||||||
{
|
|
||||||
mb->setHasSelfCollision(false);
|
|
||||||
mb->finalizeMultiDof();
|
|
||||||
m_dynamicsWorld->addMultiBody(mb);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//the btMultiBody support is work-in-progress :-)
|
//the btMultiBody support is work-in-progress :-)
|
||||||
|
|
||||||
useFeatherstone = !useFeatherstone;
|
//useFeatherstone = !useFeatherstone;
|
||||||
printf("numJoints/DOFS = %d\n", numJoints);
|
printf("numJoints/DOFS = %d\n", numJoints);
|
||||||
|
|
||||||
bool createGround=true;
|
bool createGround=true;
|
||||||
@@ -1575,6 +1650,11 @@ void ImportUrdfSetup::stepSimulation(float deltaTime)
|
|||||||
{
|
{
|
||||||
if (m_dynamicsWorld)
|
if (m_dynamicsWorld)
|
||||||
{
|
{
|
||||||
|
for (int i=0;i<m_data->m_numMotors;i++)
|
||||||
|
{
|
||||||
|
m_data->m_jointMotors[i]->setVelocityTarget(m_data->m_motorTargetVelocities[i]);
|
||||||
|
}
|
||||||
|
|
||||||
//the maximal coordinates/iterative MLCP solver requires a smallish timestep to converge
|
//the maximal coordinates/iterative MLCP solver requires a smallish timestep to converge
|
||||||
m_dynamicsWorld->stepSimulation(deltaTime,10,1./240.);
|
m_dynamicsWorld->stepSimulation(deltaTime,10,1./240.);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ class ImportUrdfSetup : public CommonMultiBodySetup
|
|||||||
{
|
{
|
||||||
char m_fileName[1024];
|
char m_fileName[1024];
|
||||||
|
|
||||||
|
struct ImportUrdfInternalData* m_data;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ImportUrdfSetup();
|
ImportUrdfSetup();
|
||||||
virtual ~ImportUrdfSetup();
|
virtual ~ImportUrdfSetup();
|
||||||
|
|||||||
@@ -285,6 +285,7 @@ void ConvertURDF2BulletInternal(const URDF2Bullet& u2b, URDF2BulletCachedData& c
|
|||||||
btQuaternion rot = offsetInA.inverse().getRotation();//parent2joint.inverse().getRotation();
|
btQuaternion rot = offsetInA.inverse().getRotation();//parent2joint.inverse().getRotation();
|
||||||
cache.m_bulletMultiBody->setupFixed(mbLinkIndex, mass, localInertiaDiagonal, mbParentIndex,
|
cache.m_bulletMultiBody->setupFixed(mbLinkIndex, mass, localInertiaDiagonal, mbParentIndex,
|
||||||
rot*offsetInB.getRotation(), offsetInA.getOrigin(),-offsetInB.getOrigin(),disableParentCollision);
|
rot*offsetInB.getRotation(), offsetInA.getOrigin(),-offsetInB.getOrigin(),disableParentCollision);
|
||||||
|
u2b.addLinkMapping(urdfLinkIndex,mbLinkIndex);
|
||||||
|
|
||||||
btMatrix3x3 rm(rot);
|
btMatrix3x3 rm(rot);
|
||||||
btScalar y,p,r;
|
btScalar y,p,r;
|
||||||
@@ -325,6 +326,7 @@ void ConvertURDF2BulletInternal(const URDF2Bullet& u2b, URDF2BulletCachedData& c
|
|||||||
offsetInA.inverse().getRotation()*offsetInB.getRotation(), quatRotate(offsetInB.inverse().getRotation(),jointAxisInJointSpace), offsetInA.getOrigin(),//parent2joint.getOrigin(),
|
offsetInA.inverse().getRotation()*offsetInB.getRotation(), quatRotate(offsetInB.inverse().getRotation(),jointAxisInJointSpace), offsetInA.getOrigin(),//parent2joint.getOrigin(),
|
||||||
-offsetInB.getOrigin(),
|
-offsetInB.getOrigin(),
|
||||||
disableParentCollision);
|
disableParentCollision);
|
||||||
|
u2b.addLinkMapping(urdfLinkIndex,mbLinkIndex);
|
||||||
|
|
||||||
} else
|
} else
|
||||||
{
|
{
|
||||||
@@ -387,7 +389,7 @@ void ConvertURDF2BulletInternal(const URDF2Bullet& u2b, URDF2BulletCachedData& c
|
|||||||
-offsetInB.getOrigin(),
|
-offsetInB.getOrigin(),
|
||||||
disableParentCollision);
|
disableParentCollision);
|
||||||
|
|
||||||
|
u2b.addLinkMapping(urdfLinkIndex,mbLinkIndex);
|
||||||
|
|
||||||
} else
|
} else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -27,6 +27,8 @@ public:
|
|||||||
|
|
||||||
///pure virtual interfaces, precondition is a valid linkIndex (you can assert/terminate if the linkIndex is out of range)
|
///pure virtual interfaces, precondition is a valid linkIndex (you can assert/terminate if the linkIndex is out of range)
|
||||||
virtual std::string getLinkName(int linkIndex) const =0;
|
virtual std::string getLinkName(int linkIndex) const =0;
|
||||||
|
|
||||||
|
virtual std::string getJointName(int linkIndex) const = 0;
|
||||||
|
|
||||||
//fill mass and inertial data. If inertial data is missing, please initialize mass, inertia to sensitive values, and inertialFrame to identity.
|
//fill mass and inertial data. If inertial data is missing, please initialize mass, inertia to sensitive values, and inertialFrame to identity.
|
||||||
virtual void getMassAndInertia(int urdfLinkIndex, btScalar& mass,btVector3& localInertiaDiagonal, btTransform& inertialFrame) const =0;
|
virtual void getMassAndInertia(int urdfLinkIndex, btScalar& mass,btVector3& localInertiaDiagonal, btTransform& inertialFrame) const =0;
|
||||||
@@ -54,6 +56,7 @@ public:
|
|||||||
|
|
||||||
virtual class btMultiBodyLinkCollider* allocateMultiBodyLinkCollider(int urdfLinkIndex, int mbLinkIndex, btMultiBody* body) const = 0;
|
virtual class btMultiBodyLinkCollider* allocateMultiBodyLinkCollider(int urdfLinkIndex, int mbLinkIndex, btMultiBody* body) const = 0;
|
||||||
|
|
||||||
|
virtual void addLinkMapping(int urdfLinkIndex, int mbLinkIndex) const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user