2016-05-10 00:25:07 +00:00
|
|
|
|
|
|
|
#include "ImportSDFSetup.h"
|
|
|
|
#include "BulletDynamics/ConstraintSolver/btGeneric6DofSpring2Constraint.h"
|
|
|
|
|
|
|
|
#include "BulletDynamics/Featherstone/btMultiBodyLinkCollider.h"
|
|
|
|
#include "Bullet3Common/b3FileUtils.h"
|
|
|
|
|
|
|
|
#include "BulletDynamics/Featherstone/btMultiBodyJointMotor.h"
|
|
|
|
#include "BulletDynamics/Featherstone/btMultiBodyDynamicsWorld.h"
|
|
|
|
#include "../CommonInterfaces/CommonParameterInterface.h"
|
|
|
|
#include "../../Utils/b3ResourcePath.h"
|
|
|
|
|
|
|
|
#include "../ImportURDFDemo/BulletUrdfImporter.h"
|
2016-06-01 05:55:13 +00:00
|
|
|
|
2016-05-10 00:25:07 +00:00
|
|
|
#include "../ImportURDFDemo/URDF2Bullet.h"
|
|
|
|
|
|
|
|
//#include "urdf_samples.h"
|
|
|
|
|
|
|
|
#include "../CommonInterfaces/CommonMultiBodyBase.h"
|
|
|
|
|
|
|
|
#include "../ImportURDFDemo/MyMultiBodyCreator.h"
|
|
|
|
|
|
|
|
class ImportSDFSetup : public CommonMultiBodyBase
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
char m_fileName[1024];
|
2016-05-10 00:25:07 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
struct ImportSDFInternalData* m_data;
|
2016-05-10 00:25:07 +00:00
|
|
|
bool m_useMultiBody;
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
//todo(erwincoumans) we need a name memory for each model
|
|
|
|
btAlignedObjectArray<std::string*> m_nameMemory;
|
2016-05-10 00:25:07 +00:00
|
|
|
|
|
|
|
public:
|
2018-09-23 21:17:31 +00:00
|
|
|
ImportSDFSetup(struct GUIHelperInterface* helper, int option, const char* fileName);
|
|
|
|
virtual ~ImportSDFSetup();
|
2016-05-10 00:25:07 +00:00
|
|
|
|
|
|
|
virtual void initPhysics();
|
|
|
|
virtual void stepSimulation(float deltaTime);
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
void setFileName(const char* urdfFileName);
|
2016-05-10 00:25:07 +00:00
|
|
|
|
|
|
|
virtual void resetCamera()
|
|
|
|
{
|
|
|
|
float dist = 3.5;
|
2017-06-01 22:30:37 +00:00
|
|
|
float pitch = -28;
|
|
|
|
float yaw = -136;
|
2018-09-23 21:17:31 +00:00
|
|
|
float targetPos[3] = {0.47, 0, -0.64};
|
|
|
|
m_guiHelper->resetCamera(dist, yaw, pitch, targetPos[0], targetPos[1], targetPos[2]);
|
2016-05-10 00:25:07 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static btAlignedObjectArray<std::string> gFileNameArray;
|
|
|
|
|
|
|
|
#define MAX_NUM_MOTORS 1024
|
|
|
|
|
|
|
|
struct ImportSDFInternalData
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
ImportSDFInternalData()
|
|
|
|
: m_numMotors(0)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < MAX_NUM_MOTORS; i++)
|
2016-05-10 00:25:07 +00:00
|
|
|
{
|
|
|
|
m_jointMotors[i] = 0;
|
|
|
|
m_generic6DofJointMotors[i] = 0;
|
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
}
|
2016-05-10 00:25:07 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
btScalar m_motorTargetVelocities[MAX_NUM_MOTORS];
|
|
|
|
btMultiBodyJointMotor* m_jointMotors[MAX_NUM_MOTORS];
|
|
|
|
btGeneric6DofSpring2Constraint* m_generic6DofJointMotors[MAX_NUM_MOTORS];
|
|
|
|
int m_numMotors;
|
2016-05-10 00:25:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ImportSDFSetup::ImportSDFSetup(struct GUIHelperInterface* helper, int option, const char* fileName)
|
2018-09-23 21:17:31 +00:00
|
|
|
: CommonMultiBodyBase(helper)
|
2016-05-10 00:25:07 +00:00
|
|
|
{
|
|
|
|
m_data = new ImportSDFInternalData;
|
|
|
|
(void)option;
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
// if (option==1)
|
|
|
|
// {
|
|
|
|
m_useMultiBody = true;
|
|
|
|
//
|
|
|
|
// } else
|
|
|
|
// {
|
|
|
|
// m_useMultiBody = false;
|
|
|
|
// }
|
2016-05-10 00:25:07 +00:00
|
|
|
|
|
|
|
static int count = 0;
|
|
|
|
if (fileName)
|
|
|
|
{
|
|
|
|
setFileName(fileName);
|
2018-09-23 21:17:31 +00:00
|
|
|
}
|
|
|
|
else
|
2016-05-10 00:25:07 +00:00
|
|
|
{
|
|
|
|
gFileNameArray.clear();
|
|
|
|
|
|
|
|
//load additional urdf file names from file
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
FILE* f = fopen("sdf_files.txt", "r");
|
2016-05-10 00:25:07 +00:00
|
|
|
if (f)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
//warning: we don't avoid string buffer overflow in this basic example in fscanf
|
|
|
|
char fileName[1024];
|
|
|
|
do
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
result = fscanf(f, "%s", fileName);
|
|
|
|
b3Printf("sdf_files.txt entry %s", fileName);
|
|
|
|
if (result == 1)
|
2016-05-10 00:25:07 +00:00
|
|
|
{
|
|
|
|
gFileNameArray.push_back(fileName);
|
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
} while (result == 1);
|
2016-05-10 00:25:07 +00:00
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
|
|
|
|
if (gFileNameArray.size() == 0)
|
2016-05-10 00:25:07 +00:00
|
|
|
{
|
2016-05-11 22:52:50 +00:00
|
|
|
gFileNameArray.push_back("two_cubes.sdf");
|
2016-05-10 00:25:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int numFileNames = gFileNameArray.size();
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (count >= numFileNames)
|
2016-05-10 00:25:07 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
count = 0;
|
2016-05-10 00:25:07 +00:00
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
sprintf(m_fileName, "%s", gFileNameArray[count++].c_str());
|
2016-05-10 00:25:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ImportSDFSetup::~ImportSDFSetup()
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
for (int i = 0; i < m_nameMemory.size(); i++)
|
2016-05-10 00:25:07 +00:00
|
|
|
{
|
|
|
|
delete m_nameMemory[i];
|
|
|
|
}
|
|
|
|
m_nameMemory.clear();
|
2018-09-23 21:17:31 +00:00
|
|
|
delete m_data;
|
2016-05-10 00:25:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ImportSDFSetup::setFileName(const char* urdfFileName)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
memcpy(m_fileName, urdfFileName, strlen(urdfFileName) + 1);
|
2016-05-10 00:25:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ImportSDFSetup::initPhysics()
|
|
|
|
{
|
|
|
|
int upAxis = 2;
|
|
|
|
m_guiHelper->setUpAxis(upAxis);
|
|
|
|
|
|
|
|
this->createEmptyDynamicsWorld();
|
|
|
|
//m_dynamicsWorld->getSolverInfo().m_numIterations = 100;
|
2018-09-23 21:17:31 +00:00
|
|
|
m_guiHelper->createPhysicsDebugDrawer(m_dynamicsWorld);
|
|
|
|
m_dynamicsWorld->getDebugDrawer()->setDebugMode(
|
|
|
|
btIDebugDraw::DBG_DrawConstraints + btIDebugDraw::DBG_DrawContactPoints + btIDebugDraw::DBG_DrawAabb); //+btIDebugDraw::DBG_DrawConstraintLimits);
|
2016-05-10 00:25:07 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
btVector3 gravity(0, 0, 0);
|
|
|
|
gravity[upAxis] = -9.8;
|
2016-05-10 00:25:07 +00:00
|
|
|
|
|
|
|
m_dynamicsWorld->setGravity(gravity);
|
|
|
|
|
2018-10-09 04:27:08 +00:00
|
|
|
BulletURDFImporter u2b(m_guiHelper, 0, 0, 1, 0);
|
2016-05-10 00:25:07 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
bool loadOk = u2b.loadSDF(m_fileName);
|
2016-05-10 00:25:07 +00:00
|
|
|
|
|
|
|
if (loadOk)
|
|
|
|
{
|
|
|
|
//printTree(u2b,u2b.getRootLinkIndex());
|
|
|
|
|
|
|
|
//u2b.printTree();
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
btTransform rootTrans;
|
|
|
|
rootTrans.setIdentity();
|
|
|
|
|
|
|
|
for (int m = 0; m < u2b.getNumModels(); m++)
|
2016-05-10 00:25:07 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
u2b.activateModel(m);
|
2016-05-10 00:25:07 +00:00
|
|
|
|
|
|
|
btMultiBody* mb = 0;
|
|
|
|
|
|
|
|
//todo: move these internal API called inside the 'ConvertURDF2Bullet' call, hidden from the user
|
2017-01-16 06:26:11 +00:00
|
|
|
//int rootLinkIndex = u2b.getRootLinkIndex();
|
2016-10-19 05:05:28 +00:00
|
|
|
//b3Printf("urdf root link index = %d\n",rootLinkIndex);
|
2016-05-10 00:25:07 +00:00
|
|
|
MyMultiBodyCreator creation(m_guiHelper);
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
u2b.getRootTransformInWorld(rootTrans);
|
|
|
|
ConvertURDF2Bullet(u2b, creation, rootTrans, m_dynamicsWorld, m_useMultiBody, u2b.getPathPrefix(), CUF_USE_SDF);
|
2016-05-10 00:25:07 +00:00
|
|
|
mb = creation.getBulletMultiBody();
|
2018-09-23 21:17:31 +00:00
|
|
|
|
|
|
|
if (m_useMultiBody && mb)
|
2016-06-03 01:04:22 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
std::string* name = new std::string(u2b.getLinkName(u2b.getRootLinkIndex()));
|
2016-06-03 01:04:22 +00:00
|
|
|
m_nameMemory.push_back(name);
|
|
|
|
#ifdef TEST_MULTIBODY_SERIALIZATION
|
2018-09-23 21:17:31 +00:00
|
|
|
s->registerNameForPointer(name->c_str(), name->c_str());
|
|
|
|
#endif //TEST_MULTIBODY_SERIALIZATION
|
2016-06-03 01:04:22 +00:00
|
|
|
mb->setBaseName(name->c_str());
|
|
|
|
//create motors for each btMultiBody joint
|
|
|
|
int numLinks = mb->getNumLinks();
|
2018-09-23 21:17:31 +00:00
|
|
|
for (int i = 0; i < numLinks; i++)
|
2016-06-03 01:04:22 +00:00
|
|
|
{
|
|
|
|
int mbLinkIndex = i;
|
|
|
|
int urdfLinkIndex = creation.m_mb2urdfLink[mbLinkIndex];
|
|
|
|
|
|
|
|
std::string* jointName = new std::string(u2b.getJointName(urdfLinkIndex));
|
|
|
|
std::string* linkName = new std::string(u2b.getLinkName(urdfLinkIndex).c_str());
|
2018-09-23 21:17:31 +00:00
|
|
|
#ifdef TEST_MULTIBODY_SERIALIZATION
|
|
|
|
s->registerNameForPointer(jointName->c_str(), jointName->c_str());
|
|
|
|
s->registerNameForPointer(linkName->c_str(), linkName->c_str());
|
|
|
|
#endif //TEST_MULTIBODY_SERIALIZATION
|
2016-06-03 01:04:22 +00:00
|
|
|
m_nameMemory.push_back(jointName);
|
|
|
|
m_nameMemory.push_back(linkName);
|
|
|
|
|
|
|
|
mb->getLink(i).m_linkName = linkName->c_str();
|
|
|
|
mb->getLink(i).m_jointName = jointName->c_str();
|
2018-09-23 21:17:31 +00:00
|
|
|
|
|
|
|
if (mb->getLink(mbLinkIndex).m_jointType == btMultibodyLink::eRevolute || mb->getLink(mbLinkIndex).m_jointType == btMultibodyLink::ePrismatic)
|
2016-06-03 01:04:22 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
if (m_data->m_numMotors < MAX_NUM_MOTORS)
|
2016-06-03 01:04:22 +00:00
|
|
|
{
|
|
|
|
char motorName[1024];
|
2018-09-23 21:17:31 +00:00
|
|
|
sprintf(motorName, "%s q'", jointName->c_str());
|
2016-06-03 01:04:22 +00:00
|
|
|
btScalar* motorVel = &m_data->m_motorTargetVelocities[m_data->m_numMotors];
|
|
|
|
*motorVel = 0.f;
|
2018-09-23 21:17:31 +00:00
|
|
|
SliderParams slider(motorName, motorVel);
|
|
|
|
slider.m_minVal = -4;
|
|
|
|
slider.m_maxVal = 4;
|
2016-06-03 01:04:22 +00:00
|
|
|
m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
|
|
|
|
float maxMotorImpulse = 10.1f;
|
2018-09-23 21:17:31 +00:00
|
|
|
btMultiBodyJointMotor* motor = new btMultiBodyJointMotor(mb, mbLinkIndex, 0, 0, maxMotorImpulse);
|
2016-06-03 01:04:22 +00:00
|
|
|
//motor->setMaxAppliedImpulse(0);
|
2018-09-23 21:17:31 +00:00
|
|
|
m_data->m_jointMotors[m_data->m_numMotors] = motor;
|
2016-06-03 01:04:22 +00:00
|
|
|
m_dynamicsWorld->addMultiBodyConstraint(motor);
|
|
|
|
m_data->m_numMotors++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
}
|
2016-05-10 00:25:07 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
for (int i = 0; i < m_dynamicsWorld->getNumMultiBodyConstraints(); i++)
|
|
|
|
{
|
|
|
|
m_dynamicsWorld->getMultiBodyConstraint(i)->finalizeMultiDof();
|
|
|
|
}
|
2016-05-10 00:25:07 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
bool createGround = true;
|
2016-05-10 00:25:07 +00:00
|
|
|
if (createGround)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
btVector3 groundHalfExtents(20, 20, 20);
|
|
|
|
groundHalfExtents[upAxis] = 1.f;
|
2016-05-10 00:25:07 +00:00
|
|
|
btBoxShape* box = new btBoxShape(groundHalfExtents);
|
|
|
|
box->initializePolyhedralFeatures();
|
|
|
|
|
|
|
|
m_guiHelper->createCollisionShapeGraphicsObject(box);
|
2018-09-23 21:17:31 +00:00
|
|
|
btTransform start;
|
|
|
|
start.setIdentity();
|
|
|
|
btVector3 groundOrigin(0, 0, 0);
|
|
|
|
groundOrigin[upAxis] = -2.5;
|
2016-05-10 00:25:07 +00:00
|
|
|
start.setOrigin(groundOrigin);
|
2018-09-23 21:17:31 +00:00
|
|
|
btRigidBody* body = createRigidBody(0, start, box);
|
2016-05-10 00:25:07 +00:00
|
|
|
//m_dynamicsWorld->removeRigidBody(body);
|
2018-09-23 21:17:31 +00:00
|
|
|
// m_dynamicsWorld->addRigidBody(body,2,1);
|
|
|
|
btVector3 color(0.5, 0.5, 0.5);
|
|
|
|
m_guiHelper->createRigidBodyGraphicsObject(body, color);
|
2016-05-10 00:25:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
///this extra stepSimulation call makes sure that all the btMultibody transforms are properly propagates.
|
2018-09-23 21:17:31 +00:00
|
|
|
m_dynamicsWorld->stepSimulation(1. / 240., 0); // 1., 10, 1. / 240.);
|
2016-05-10 00:25:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImportSDFSetup::stepSimulation(float deltaTime)
|
|
|
|
{
|
|
|
|
if (m_dynamicsWorld)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
for (int i = 0; i < m_data->m_numMotors; i++)
|
|
|
|
{
|
2016-05-10 00:25:07 +00:00
|
|
|
if (m_data->m_jointMotors[i])
|
|
|
|
{
|
|
|
|
m_data->m_jointMotors[i]->setVelocityTarget(m_data->m_motorTargetVelocities[i]);
|
|
|
|
}
|
|
|
|
if (m_data->m_generic6DofJointMotors[i])
|
|
|
|
{
|
|
|
|
GenericConstraintUserInfo* jointInfo = (GenericConstraintUserInfo*)m_data->m_generic6DofJointMotors[i]->getUserConstraintPtr();
|
2018-09-23 21:17:31 +00:00
|
|
|
m_data->m_generic6DofJointMotors[i]->setTargetVelocity(jointInfo->m_jointAxisIndex, m_data->m_motorTargetVelocities[i]);
|
2016-05-10 00:25:07 +00:00
|
|
|
//jointInfo->
|
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
}
|
2016-05-10 00:25:07 +00:00
|
|
|
|
|
|
|
//the maximal coordinates/iterative MLCP solver requires a smallish timestep to converge
|
2018-09-23 21:17:31 +00:00
|
|
|
m_dynamicsWorld->stepSimulation(deltaTime, 10, 1. / 240.);
|
2016-05-10 00:25:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
class CommonExampleInterface* ImportSDFCreateFunc(struct CommonExampleOptions& options)
|
2016-05-10 00:25:07 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
return new ImportSDFSetup(options.m_guiHelper, options.m_option, options.m_fileName);
|
2016-05-10 00:25:07 +00:00
|
|
|
}
|