btMultiBodyFromURDF.hpp 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #ifndef BTMULTIBODYFROMURDF_HPP
  2. #define BTMULTIBODYFROMURDF_HPP
  3. #include "btBulletDynamicsCommon.h"
  4. #include "BulletDynamics/Featherstone/btMultiBodyConstraintSolver.h"
  5. #include "BulletDynamics/Featherstone/btMultiBodyDynamicsWorld.h"
  6. #include "BulletDynamics/Featherstone/btMultiBodyLinkCollider.h"
  7. #include "BulletDynamics/Featherstone/btMultiBodyPoint2Point.h"
  8. #include "../../examples/CommonInterfaces/CommonGUIHelperInterface.h"
  9. #include "../../examples/Importers/ImportURDFDemo/BulletUrdfImporter.h"
  10. #include "../../examples/Importers/ImportURDFDemo/URDF2Bullet.h"
  11. #include "../../examples/Importers/ImportURDFDemo/MyMultiBodyCreator.h"
  12. #include "../../examples/Importers/ImportURDFDemo/URDF2Bullet.h"
  13. /// Create a btMultiBody model from URDF.
  14. /// This is adapted from Bullet URDF loader example
  15. class MyBtMultiBodyFromURDF {
  16. public:
  17. /// ctor
  18. /// @param gravity gravitational acceleration (in world frame)
  19. /// @param base_fixed if true, the root body is treated as fixed,
  20. /// if false, it is treated as floating
  21. MyBtMultiBodyFromURDF(const btVector3 &gravity, const bool base_fixed)
  22. : m_gravity(gravity), m_base_fixed(base_fixed) {
  23. m_broadphase = 0x0;
  24. m_dispatcher = 0x0;
  25. m_solver = 0x0;
  26. m_collisionConfiguration = 0x0;
  27. m_dynamicsWorld = 0x0;
  28. m_multibody = 0x0;
  29. }
  30. /// dtor
  31. ~MyBtMultiBodyFromURDF() {
  32. delete m_dynamicsWorld;
  33. delete m_solver;
  34. delete m_broadphase;
  35. delete m_dispatcher;
  36. delete m_collisionConfiguration;
  37. delete m_multibody;
  38. }
  39. /// @param name path to urdf file
  40. void setFileName(const std::string name) { m_filename = name; }
  41. /// load urdf file and build btMultiBody model
  42. void init() {
  43. this->createEmptyDynamicsWorld();
  44. m_dynamicsWorld->setGravity(m_gravity);
  45. BulletURDFImporter urdf_importer(&m_nogfx,0);
  46. URDFImporterInterface &u2b(urdf_importer);
  47. bool loadOk = u2b.loadURDF(m_filename.c_str(), m_base_fixed);
  48. if (loadOk) {
  49. btTransform identityTrans;
  50. identityTrans.setIdentity();
  51. MyMultiBodyCreator creation(&m_nogfx);
  52. const bool use_multibody = true;
  53. ConvertURDF2Bullet(u2b, creation, identityTrans, m_dynamicsWorld, use_multibody,
  54. u2b.getPathPrefix());
  55. m_multibody = creation.getBulletMultiBody();
  56. m_dynamicsWorld->stepSimulation(1. / 240., 0);
  57. }
  58. }
  59. /// @return pointer to the btMultiBody model
  60. btMultiBody *getBtMultiBody() { return m_multibody; }
  61. private:
  62. // internal utility function
  63. void createEmptyDynamicsWorld() {
  64. m_collisionConfiguration = new btDefaultCollisionConfiguration();
  65. /// use the default collision dispatcher. For parallel processing you can use a diffent
  66. /// dispatcher (see Extras/BulletMultiThreaded)
  67. m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration);
  68. m_broadphase = new btDbvtBroadphase();
  69. m_solver = new btMultiBodyConstraintSolver;
  70. m_dynamicsWorld = new btMultiBodyDynamicsWorld(m_dispatcher, m_broadphase, m_solver,
  71. m_collisionConfiguration);
  72. m_dynamicsWorld->setGravity(m_gravity);
  73. }
  74. btBroadphaseInterface *m_broadphase;
  75. btCollisionDispatcher *m_dispatcher;
  76. btMultiBodyConstraintSolver *m_solver;
  77. btDefaultCollisionConfiguration *m_collisionConfiguration;
  78. btMultiBodyDynamicsWorld *m_dynamicsWorld;
  79. std::string m_filename;
  80. DummyGUIHelper m_nogfx;
  81. btMultiBody *m_multibody;
  82. const btVector3 m_gravity;
  83. const bool m_base_fixed;
  84. };
  85. #endif // BTMULTIBODYFROMURDF_HPP