TestJointTorqueSetup.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. //test addJointTorque
  2. #include "TestJointTorqueSetup.h"
  3. #include "BulletDynamics/Featherstone/btMultiBodyLinkCollider.h"
  4. #include "BulletDynamics/Featherstone/btMultiBodyJointFeedback.h"
  5. #include "../CommonInterfaces/CommonMultiBodyBase.h"
  6. #include "../Utils/b3ResourcePath.h"
  7. static btScalar radius(0.2);
  8. struct TestJointTorqueSetup : public CommonMultiBodyBase
  9. {
  10. btMultiBody* m_multiBody;
  11. btAlignedObjectArray<btMultiBodyJointFeedback*> m_jointFeedbacks;
  12. bool m_once;
  13. public:
  14. TestJointTorqueSetup(struct GUIHelperInterface* helper);
  15. virtual ~TestJointTorqueSetup();
  16. virtual void initPhysics();
  17. virtual void stepSimulation(float deltaTime);
  18. virtual void resetCamera()
  19. {
  20. float dist = 5;
  21. float pitch = -21;
  22. float yaw = 270;
  23. float targetPos[3] = {-1.34, 3.4, -0.44};
  24. m_guiHelper->resetCamera(dist, yaw, pitch, targetPos[0], targetPos[1], targetPos[2]);
  25. }
  26. };
  27. TestJointTorqueSetup::TestJointTorqueSetup(struct GUIHelperInterface* helper)
  28. : CommonMultiBodyBase(helper),
  29. m_once(true)
  30. {
  31. }
  32. TestJointTorqueSetup::~TestJointTorqueSetup()
  33. {
  34. }
  35. void TestJointTorqueSetup::initPhysics()
  36. {
  37. int upAxis = 1;
  38. m_guiHelper->setUpAxis(upAxis);
  39. btVector4 colors[4] =
  40. {
  41. btVector4(1, 0, 0, 1),
  42. btVector4(0, 1, 0, 1),
  43. btVector4(0, 1, 1, 1),
  44. btVector4(1, 1, 0, 1),
  45. };
  46. int curColor = 0;
  47. this->createEmptyDynamicsWorld();
  48. m_guiHelper->createPhysicsDebugDrawer(m_dynamicsWorld);
  49. m_dynamicsWorld->getDebugDrawer()->setDebugMode(
  50. //btIDebugDraw::DBG_DrawConstraints
  51. +btIDebugDraw::DBG_DrawWireframe + btIDebugDraw::DBG_DrawContactPoints + btIDebugDraw::DBG_DrawAabb); //+btIDebugDraw::DBG_DrawConstraintLimits);
  52. m_dynamicsWorld->getSolverInfo().m_jointFeedbackInWorldSpace = true;
  53. m_dynamicsWorld->getSolverInfo().m_jointFeedbackInJointFrame = true;
  54. //create a static ground object
  55. if (1)
  56. {
  57. btVector3 groundHalfExtents(1, 1, 0.2);
  58. groundHalfExtents[upAxis] = 1.f;
  59. btBoxShape* box = new btBoxShape(groundHalfExtents);
  60. box->initializePolyhedralFeatures();
  61. m_guiHelper->createCollisionShapeGraphicsObject(box);
  62. btTransform start;
  63. start.setIdentity();
  64. btVector3 groundOrigin(-0.4f, 3.f, 0.f);
  65. groundOrigin[upAxis] -= .5;
  66. groundOrigin[2] -= 0.6;
  67. start.setOrigin(groundOrigin);
  68. btQuaternion groundOrn(btVector3(0, 1, 0), 0.25 * SIMD_PI);
  69. // start.setRotation(groundOrn);
  70. btRigidBody* body = createRigidBody(0, start, box);
  71. body->setFriction(0);
  72. btVector4 color = colors[curColor];
  73. curColor++;
  74. curColor &= 3;
  75. m_guiHelper->createRigidBodyGraphicsObject(body, color);
  76. }
  77. {
  78. bool floating = false;
  79. bool damping = false;
  80. bool gyro = false;
  81. int numLinks = 2;
  82. bool spherical = false; //set it ot false -to use 1DoF hinges instead of 3DoF sphericals
  83. bool canSleep = false;
  84. bool selfCollide = false;
  85. btVector3 linkHalfExtents(0.05, 0.37, 0.1);
  86. btVector3 baseHalfExtents(0.05, 0.37, 0.1);
  87. btVector3 basePosition = btVector3(-0.4f, 3.f, 0.f);
  88. //mbC->forceMultiDof(); //if !spherical, you can comment this line to check the 1DoF algorithm
  89. //init the base
  90. btVector3 baseInertiaDiag(0.f, 0.f, 0.f);
  91. float baseMass = 1.f;
  92. if (baseMass)
  93. {
  94. //btCollisionShape *shape = new btSphereShape(baseHalfExtents[0]);// btBoxShape(btVector3(baseHalfExtents[0], baseHalfExtents[1], baseHalfExtents[2]));
  95. btCollisionShape* shape = new btBoxShape(btVector3(baseHalfExtents[0], baseHalfExtents[1], baseHalfExtents[2]));
  96. shape->calculateLocalInertia(baseMass, baseInertiaDiag);
  97. delete shape;
  98. }
  99. btMultiBody* pMultiBody = new btMultiBody(numLinks, baseMass, baseInertiaDiag, !floating, canSleep);
  100. m_multiBody = pMultiBody;
  101. btQuaternion baseOriQuat(0.f, 0.f, 0.f, 1.f);
  102. // baseOriQuat.setEulerZYX(-.25*SIMD_PI,0,-1.75*SIMD_PI);
  103. pMultiBody->setBasePos(basePosition);
  104. pMultiBody->setWorldToBaseRot(baseOriQuat);
  105. btVector3 vel(0, 0, 0);
  106. // pMultiBody->setBaseVel(vel);
  107. //init the links
  108. btVector3 hingeJointAxis(1, 0, 0);
  109. //y-axis assumed up
  110. btVector3 parentComToCurrentCom(0, -linkHalfExtents[1] * 2.f, 0); //par body's COM to cur body's COM offset
  111. btVector3 currentPivotToCurrentCom(0, -linkHalfExtents[1], 0); //cur body's COM to cur body's PIV offset
  112. btVector3 parentComToCurrentPivot = parentComToCurrentCom - currentPivotToCurrentCom; //par body's COM to cur body's PIV offset
  113. //////
  114. btScalar q0 = 0.f * SIMD_PI / 180.f;
  115. btQuaternion quat0(btVector3(0, 1, 0).normalized(), q0);
  116. quat0.normalize();
  117. /////
  118. for (int i = 0; i < numLinks; ++i)
  119. {
  120. float linkMass = 1.f;
  121. //if (i==3 || i==2)
  122. // linkMass= 1000;
  123. btVector3 linkInertiaDiag(0.f, 0.f, 0.f);
  124. btCollisionShape* shape = 0;
  125. if (i == 0)
  126. {
  127. shape = new btBoxShape(btVector3(linkHalfExtents[0], linkHalfExtents[1], linkHalfExtents[2])); //
  128. }
  129. else
  130. {
  131. shape = new btSphereShape(radius);
  132. }
  133. shape->calculateLocalInertia(linkMass, linkInertiaDiag);
  134. delete shape;
  135. if (!spherical)
  136. {
  137. //pMultiBody->setupRevolute(i, linkMass, linkInertiaDiag, i - 1, btQuaternion(0.f, 0.f, 0.f, 1.f), hingeJointAxis, parentComToCurrentPivot, currentPivotToCurrentCom, false);
  138. if (i == 0)
  139. {
  140. pMultiBody->setupRevolute(i, linkMass, linkInertiaDiag, i - 1,
  141. btQuaternion(0.f, 0.f, 0.f, 1.f),
  142. hingeJointAxis,
  143. parentComToCurrentPivot,
  144. currentPivotToCurrentCom, false);
  145. }
  146. else
  147. {
  148. btVector3 parentComToCurrentCom(0, -radius * 2.f, 0); //par body's COM to cur body's COM offset
  149. btVector3 currentPivotToCurrentCom(0, -radius, 0); //cur body's COM to cur body's PIV offset
  150. btVector3 parentComToCurrentPivot = parentComToCurrentCom - currentPivotToCurrentCom; //par body's COM to cur body's PIV offset
  151. pMultiBody->setupFixed(i, linkMass, linkInertiaDiag, i - 1,
  152. btQuaternion(0.f, 0.f, 0.f, 1.f),
  153. parentComToCurrentPivot,
  154. currentPivotToCurrentCom);
  155. }
  156. //pMultiBody->setupFixed(i,linkMass,linkInertiaDiag,i-1,btQuaternion(0,0,0,1),parentComToCurrentPivot,currentPivotToCurrentCom,false);
  157. }
  158. else
  159. {
  160. //pMultiBody->setupPlanar(i, linkMass, linkInertiaDiag, i - 1, btQuaternion(0.f, 0.f, 0.f, 1.f)/*quat0*/, btVector3(1, 0, 0), parentComToCurrentPivot*2, false);
  161. pMultiBody->setupSpherical(i, linkMass, linkInertiaDiag, i - 1, btQuaternion(0.f, 0.f, 0.f, 1.f), parentComToCurrentPivot, currentPivotToCurrentCom, false);
  162. }
  163. }
  164. pMultiBody->finalizeMultiDof();
  165. //for (int i=pMultiBody->getNumLinks()-1;i>=0;i--)//
  166. for (int i = 0; i < pMultiBody->getNumLinks(); i++)
  167. {
  168. btMultiBodyJointFeedback* fb = new btMultiBodyJointFeedback();
  169. pMultiBody->getLink(i).m_jointFeedback = fb;
  170. m_jointFeedbacks.push_back(fb);
  171. //break;
  172. }
  173. btMultiBodyDynamicsWorld* world = m_dynamicsWorld;
  174. ///
  175. world->addMultiBody(pMultiBody);
  176. btMultiBody* mbC = pMultiBody;
  177. mbC->setCanSleep(canSleep);
  178. mbC->setHasSelfCollision(selfCollide);
  179. mbC->setUseGyroTerm(gyro);
  180. //
  181. if (!damping)
  182. {
  183. mbC->setLinearDamping(0.f);
  184. mbC->setAngularDamping(0.f);
  185. }
  186. else
  187. {
  188. mbC->setLinearDamping(0.1f);
  189. mbC->setAngularDamping(0.9f);
  190. }
  191. //
  192. m_dynamicsWorld->setGravity(btVector3(0, 0, -10));
  193. //////////////////////////////////////////////
  194. if (/* DISABLES CODE */ (0)) //numLinks > 0)
  195. {
  196. btScalar q0 = 45.f * SIMD_PI / 180.f;
  197. if (!spherical)
  198. {
  199. mbC->setJointPosMultiDof(0, &q0);
  200. }
  201. else
  202. {
  203. btQuaternion quat0(btVector3(1, 1, 0).normalized(), q0);
  204. quat0.normalize();
  205. mbC->setJointPosMultiDof(0, quat0);
  206. }
  207. }
  208. ///
  209. btAlignedObjectArray<btQuaternion> world_to_local;
  210. world_to_local.resize(pMultiBody->getNumLinks() + 1);
  211. btAlignedObjectArray<btVector3> local_origin;
  212. local_origin.resize(pMultiBody->getNumLinks() + 1);
  213. world_to_local[0] = pMultiBody->getWorldToBaseRot();
  214. local_origin[0] = pMultiBody->getBasePos();
  215. // double friction = 1;
  216. {
  217. // float pos[4]={local_origin[0].x(),local_origin[0].y(),local_origin[0].z(),1};
  218. // btScalar quat[4]={-world_to_local[0].x(),-world_to_local[0].y(),-world_to_local[0].z(),world_to_local[0].w()};
  219. if (1)
  220. {
  221. btCollisionShape* shape = new btBoxShape(btVector3(baseHalfExtents[0], baseHalfExtents[1], baseHalfExtents[2])); //new btSphereShape(baseHalfExtents[0]);
  222. m_guiHelper->createCollisionShapeGraphicsObject(shape);
  223. btMultiBodyLinkCollider* col = new btMultiBodyLinkCollider(pMultiBody, -1);
  224. col->setCollisionShape(shape);
  225. btTransform tr;
  226. tr.setIdentity();
  227. //if we don't set the initial pose of the btCollisionObject, the simulator will do this
  228. //when syncing the btMultiBody link transforms to the btMultiBodyLinkCollider
  229. tr.setOrigin(local_origin[0]);
  230. btQuaternion orn(btVector3(0, 0, 1), 0.25 * 3.1415926538);
  231. tr.setRotation(orn);
  232. col->setWorldTransform(tr);
  233. bool isDynamic = (baseMass > 0 && floating);
  234. int collisionFilterGroup = isDynamic ? int(btBroadphaseProxy::DefaultFilter) : int(btBroadphaseProxy::StaticFilter);
  235. int collisionFilterMask = isDynamic ? int(btBroadphaseProxy::AllFilter) : int(btBroadphaseProxy::AllFilter ^ btBroadphaseProxy::StaticFilter);
  236. world->addCollisionObject(col, collisionFilterGroup, collisionFilterMask); //, 2,1+2);
  237. btVector3 color(0.0, 0.0, 0.5);
  238. m_guiHelper->createCollisionObjectGraphicsObject(col, color);
  239. // col->setFriction(friction);
  240. pMultiBody->setBaseCollider(col);
  241. }
  242. }
  243. for (int i = 0; i < pMultiBody->getNumLinks(); ++i)
  244. {
  245. const int parent = pMultiBody->getParent(i);
  246. world_to_local[i + 1] = pMultiBody->getParentToLocalRot(i) * world_to_local[parent + 1];
  247. local_origin[i + 1] = local_origin[parent + 1] + (quatRotate(world_to_local[i + 1].inverse(), pMultiBody->getRVector(i)));
  248. }
  249. for (int i = 0; i < pMultiBody->getNumLinks(); ++i)
  250. {
  251. btVector3 posr = local_origin[i + 1];
  252. // float pos[4]={posr.x(),posr.y(),posr.z(),1};
  253. btScalar quat[4] = {-world_to_local[i + 1].x(), -world_to_local[i + 1].y(), -world_to_local[i + 1].z(), world_to_local[i + 1].w()};
  254. btCollisionShape* shape = 0;
  255. if (i == 0)
  256. {
  257. shape = new btBoxShape(btVector3(linkHalfExtents[0], linkHalfExtents[1], linkHalfExtents[2])); //btSphereShape(linkHalfExtents[0]);
  258. }
  259. else
  260. {
  261. shape = new btSphereShape(radius);
  262. }
  263. m_guiHelper->createCollisionShapeGraphicsObject(shape);
  264. btMultiBodyLinkCollider* col = new btMultiBodyLinkCollider(pMultiBody, i);
  265. col->setCollisionShape(shape);
  266. btTransform tr;
  267. tr.setIdentity();
  268. tr.setOrigin(posr);
  269. tr.setRotation(btQuaternion(quat[0], quat[1], quat[2], quat[3]));
  270. col->setWorldTransform(tr);
  271. // col->setFriction(friction);
  272. bool isDynamic = 1; //(linkMass > 0);
  273. int collisionFilterGroup = isDynamic ? int(btBroadphaseProxy::DefaultFilter) : int(btBroadphaseProxy::StaticFilter);
  274. int collisionFilterMask = isDynamic ? int(btBroadphaseProxy::AllFilter) : int(btBroadphaseProxy::AllFilter ^ btBroadphaseProxy::StaticFilter);
  275. //if (i==0||i>numLinks-2)
  276. {
  277. world->addCollisionObject(col, collisionFilterGroup, collisionFilterMask); //,2,1+2);
  278. btVector4 color = colors[curColor];
  279. curColor++;
  280. curColor &= 3;
  281. m_guiHelper->createCollisionObjectGraphicsObject(col, color);
  282. pMultiBody->getLink(i).m_collider = col;
  283. }
  284. }
  285. }
  286. btSerializer* s = new btDefaultSerializer;
  287. m_dynamicsWorld->serialize(s);
  288. char resourcePath[1024];
  289. if (b3ResourcePath::findResourcePath("multibody.bullet", resourcePath, 1024,0))
  290. {
  291. FILE* f = fopen(resourcePath, "wb");
  292. fwrite(s->getBufferPointer(), s->getCurrentBufferSize(), 1, f);
  293. fclose(f);
  294. }
  295. }
  296. void TestJointTorqueSetup::stepSimulation(float deltaTime)
  297. {
  298. //m_multiBody->addLinkForce(0,btVector3(100,100,100));
  299. if (/* DISABLES CODE */ (0)) //m_once)
  300. {
  301. m_once = false;
  302. m_multiBody->addJointTorque(0, 10.0);
  303. btScalar torque = m_multiBody->getJointTorque(0);
  304. b3Printf("t = %f,%f,%f\n", torque, torque, torque); //[0],torque[1],torque[2]);
  305. }
  306. m_dynamicsWorld->stepSimulation(1. / 240, 0);
  307. static int count = 0;
  308. if ((count & 0x0f) == 0)
  309. {
  310. for (int i = 0; i < m_jointFeedbacks.size(); i++)
  311. {
  312. b3Printf("F_reaction[%i] linear:%f,%f,%f, angular:%f,%f,%f",
  313. i,
  314. m_jointFeedbacks[i]->m_reactionForces.m_topVec[0],
  315. m_jointFeedbacks[i]->m_reactionForces.m_topVec[1],
  316. m_jointFeedbacks[i]->m_reactionForces.m_topVec[2],
  317. m_jointFeedbacks[i]->m_reactionForces.m_bottomVec[0],
  318. m_jointFeedbacks[i]->m_reactionForces.m_bottomVec[1],
  319. m_jointFeedbacks[i]->m_reactionForces.m_bottomVec[2]
  320. );
  321. }
  322. }
  323. count++;
  324. /*
  325. b3Printf("base angvel = %f,%f,%f",m_multiBody->getBaseOmega()[0],
  326. m_multiBody->getBaseOmega()[1],
  327. m_multiBody->getBaseOmega()[2]
  328. );
  329. */
  330. // btScalar jointVel =m_multiBody->getJointVel(0);
  331. // b3Printf("child angvel = %f",jointVel);
  332. }
  333. class CommonExampleInterface* TestJointTorqueCreateFunc(struct CommonExampleOptions& options)
  334. {
  335. return new TestJointTorqueSetup(options.m_guiHelper);
  336. }