TestJointTorqueSetup.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 = 270;
  22. float yaw = 21;
  23. float targetPos[3]={-1.34,3.4,-0.44};
  24. m_guiHelper->resetCamera(dist,pitch,yaw,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. ///this is a temporary global, until we determine if we need the option or not
  36. extern bool gJointFeedbackInWorldSpace;
  37. extern bool gJointFeedbackInJointFrame;
  38. void TestJointTorqueSetup::initPhysics()
  39. {
  40. int upAxis = 1;
  41. gJointFeedbackInWorldSpace = true;
  42. gJointFeedbackInJointFrame = true;
  43. m_guiHelper->setUpAxis(upAxis);
  44. btVector4 colors[4] =
  45. {
  46. btVector4(1,0,0,1),
  47. btVector4(0,1,0,1),
  48. btVector4(0,1,1,1),
  49. btVector4(1,1,0,1),
  50. };
  51. int curColor = 0;
  52. this->createEmptyDynamicsWorld();
  53. m_guiHelper->createPhysicsDebugDrawer(m_dynamicsWorld);
  54. m_dynamicsWorld->getDebugDrawer()->setDebugMode(
  55. //btIDebugDraw::DBG_DrawConstraints
  56. +btIDebugDraw::DBG_DrawWireframe
  57. +btIDebugDraw::DBG_DrawContactPoints
  58. +btIDebugDraw::DBG_DrawAabb
  59. );//+btIDebugDraw::DBG_DrawConstraintLimits);
  60. //create a static ground object
  61. if (1)
  62. {
  63. btVector3 groundHalfExtents(1,1,0.2);
  64. groundHalfExtents[upAxis]=1.f;
  65. btBoxShape* box = new btBoxShape(groundHalfExtents);
  66. box->initializePolyhedralFeatures();
  67. m_guiHelper->createCollisionShapeGraphicsObject(box);
  68. btTransform start; start.setIdentity();
  69. btVector3 groundOrigin(-0.4f, 3.f, 0.f);
  70. groundOrigin[upAxis] -=.5;
  71. groundOrigin[2]-=0.6;
  72. start.setOrigin(groundOrigin);
  73. btQuaternion groundOrn(btVector3(0,1,0),0.25*SIMD_PI);
  74. // start.setRotation(groundOrn);
  75. btRigidBody* body = createRigidBody(0,start,box);
  76. body->setFriction(0);
  77. btVector4 color = colors[curColor];
  78. curColor++;
  79. curColor&=3;
  80. m_guiHelper->createRigidBodyGraphicsObject(body,color);
  81. }
  82. {
  83. bool floating = false;
  84. bool damping = false;
  85. bool gyro = false;
  86. int numLinks = 2;
  87. bool spherical = false; //set it ot false -to use 1DoF hinges instead of 3DoF sphericals
  88. bool canSleep = false;
  89. bool selfCollide = false;
  90. btVector3 linkHalfExtents(0.05, 0.37, 0.1);
  91. btVector3 baseHalfExtents(0.05, 0.37, 0.1);
  92. btVector3 basePosition = btVector3(-0.4f, 3.f, 0.f);
  93. //mbC->forceMultiDof(); //if !spherical, you can comment this line to check the 1DoF algorithm
  94. //init the base
  95. btVector3 baseInertiaDiag(0.f, 0.f, 0.f);
  96. float baseMass = 1.f;
  97. if(baseMass)
  98. {
  99. //btCollisionShape *shape = new btSphereShape(baseHalfExtents[0]);// btBoxShape(btVector3(baseHalfExtents[0], baseHalfExtents[1], baseHalfExtents[2]));
  100. btCollisionShape *shape = new btBoxShape(btVector3(baseHalfExtents[0], baseHalfExtents[1], baseHalfExtents[2]));
  101. shape->calculateLocalInertia(baseMass, baseInertiaDiag);
  102. delete shape;
  103. }
  104. btMultiBody *pMultiBody = new btMultiBody(numLinks, baseMass, baseInertiaDiag, !floating, canSleep);
  105. m_multiBody = pMultiBody;
  106. btQuaternion baseOriQuat(0.f, 0.f, 0.f, 1.f);
  107. // baseOriQuat.setEulerZYX(-.25*SIMD_PI,0,-1.75*SIMD_PI);
  108. pMultiBody->setBasePos(basePosition);
  109. pMultiBody->setWorldToBaseRot(baseOriQuat);
  110. btVector3 vel(0, 0, 0);
  111. // pMultiBody->setBaseVel(vel);
  112. //init the links
  113. btVector3 hingeJointAxis(1, 0, 0);
  114. //y-axis assumed up
  115. btVector3 parentComToCurrentCom(0, -linkHalfExtents[1] * 2.f, 0); //par body's COM to cur body's COM offset
  116. btVector3 currentPivotToCurrentCom(0, -linkHalfExtents[1], 0); //cur body's COM to cur body's PIV offset
  117. btVector3 parentComToCurrentPivot = parentComToCurrentCom - currentPivotToCurrentCom; //par body's COM to cur body's PIV offset
  118. //////
  119. btScalar q0 = 0.f * SIMD_PI/ 180.f;
  120. btQuaternion quat0(btVector3(0, 1, 0).normalized(), q0);
  121. quat0.normalize();
  122. /////
  123. for(int i = 0; i < numLinks; ++i)
  124. {
  125. float linkMass = 1.f;
  126. //if (i==3 || i==2)
  127. // linkMass= 1000;
  128. btVector3 linkInertiaDiag(0.f, 0.f, 0.f);
  129. btCollisionShape* shape = 0;
  130. if (i==0)
  131. {
  132. shape = new btBoxShape(btVector3(linkHalfExtents[0], linkHalfExtents[1], linkHalfExtents[2]));//
  133. } else
  134. {
  135. shape = new btSphereShape(radius);
  136. }
  137. shape->calculateLocalInertia(linkMass, linkInertiaDiag);
  138. delete shape;
  139. if(!spherical)
  140. {
  141. //pMultiBody->setupRevolute(i, linkMass, linkInertiaDiag, i - 1, btQuaternion(0.f, 0.f, 0.f, 1.f), hingeJointAxis, parentComToCurrentPivot, currentPivotToCurrentCom, false);
  142. if (i==0)
  143. {
  144. pMultiBody->setupRevolute(i, linkMass, linkInertiaDiag, i - 1,
  145. btQuaternion(0.f, 0.f, 0.f, 1.f),
  146. hingeJointAxis,
  147. parentComToCurrentPivot,
  148. currentPivotToCurrentCom, false);
  149. } else
  150. {
  151. btVector3 parentComToCurrentCom(0, -radius * 2.f, 0); //par body's COM to cur body's COM offset
  152. btVector3 currentPivotToCurrentCom(0, -radius, 0); //cur body's COM to cur body's PIV offset
  153. btVector3 parentComToCurrentPivot = parentComToCurrentCom - currentPivotToCurrentCom; //par body's COM to cur body's PIV offset
  154. pMultiBody->setupFixed(i, linkMass, linkInertiaDiag, i - 1,
  155. btQuaternion(0.f, 0.f, 0.f, 1.f),
  156. parentComToCurrentPivot,
  157. currentPivotToCurrentCom);
  158. }
  159. //pMultiBody->setupFixed(i,linkMass,linkInertiaDiag,i-1,btQuaternion(0,0,0,1),parentComToCurrentPivot,currentPivotToCurrentCom,false);
  160. }
  161. else
  162. {
  163. //pMultiBody->setupPlanar(i, linkMass, linkInertiaDiag, i - 1, btQuaternion(0.f, 0.f, 0.f, 1.f)/*quat0*/, btVector3(1, 0, 0), parentComToCurrentPivot*2, false);
  164. pMultiBody->setupSpherical(i, linkMass, linkInertiaDiag, i - 1, btQuaternion(0.f, 0.f, 0.f, 1.f), parentComToCurrentPivot, currentPivotToCurrentCom, false);
  165. }
  166. }
  167. pMultiBody->finalizeMultiDof();
  168. //for (int i=pMultiBody->getNumLinks()-1;i>=0;i--)//
  169. for (int i=0;i<pMultiBody->getNumLinks();i++)
  170. {
  171. btMultiBodyJointFeedback* fb = new btMultiBodyJointFeedback();
  172. pMultiBody->getLink(i).m_jointFeedback = fb;
  173. m_jointFeedbacks.push_back(fb);
  174. //break;
  175. }
  176. btMultiBodyDynamicsWorld* world = m_dynamicsWorld;
  177. ///
  178. world->addMultiBody(pMultiBody);
  179. btMultiBody* mbC = pMultiBody;
  180. mbC->setCanSleep(canSleep);
  181. mbC->setHasSelfCollision(selfCollide);
  182. mbC->setUseGyroTerm(gyro);
  183. //
  184. if(!damping)
  185. {
  186. mbC->setLinearDamping(0.f);
  187. mbC->setAngularDamping(0.f);
  188. }else
  189. { mbC->setLinearDamping(0.1f);
  190. mbC->setAngularDamping(0.9f);
  191. }
  192. //
  193. m_dynamicsWorld->setGravity(btVector3(0,0,-10));
  194. //////////////////////////////////////////////
  195. if(0)//numLinks > 0)
  196. {
  197. btScalar q0 = 45.f * SIMD_PI/ 180.f;
  198. if(!spherical)
  199. {
  200. mbC->setJointPosMultiDof(0, &q0);
  201. }
  202. else
  203. {
  204. btQuaternion quat0(btVector3(1, 1, 0).normalized(), q0);
  205. quat0.normalize();
  206. mbC->setJointPosMultiDof(0, quat0);
  207. }
  208. }
  209. ///
  210. btAlignedObjectArray<btQuaternion> world_to_local;
  211. world_to_local.resize(pMultiBody->getNumLinks() + 1);
  212. btAlignedObjectArray<btVector3> local_origin;
  213. local_origin.resize(pMultiBody->getNumLinks() + 1);
  214. world_to_local[0] = pMultiBody->getWorldToBaseRot();
  215. local_origin[0] = pMultiBody->getBasePos();
  216. // double friction = 1;
  217. {
  218. // float pos[4]={local_origin[0].x(),local_origin[0].y(),local_origin[0].z(),1};
  219. // btScalar quat[4]={-world_to_local[0].x(),-world_to_local[0].y(),-world_to_local[0].z(),world_to_local[0].w()};
  220. if (1)
  221. {
  222. btCollisionShape* shape = new btBoxShape(btVector3(baseHalfExtents[0],baseHalfExtents[1],baseHalfExtents[2]));//new btSphereShape(baseHalfExtents[0]);
  223. m_guiHelper->createCollisionShapeGraphicsObject(shape);
  224. btMultiBodyLinkCollider* col= new btMultiBodyLinkCollider(pMultiBody, -1);
  225. col->setCollisionShape(shape);
  226. btTransform tr;
  227. tr.setIdentity();
  228. //if we don't set the initial pose of the btCollisionObject, the simulator will do this
  229. //when syncing the btMultiBody link transforms to the btMultiBodyLinkCollider
  230. tr.setOrigin(local_origin[0]);
  231. btQuaternion orn(btVector3(0,0,1),0.25*3.1415926538);
  232. tr.setRotation(orn);
  233. col->setWorldTransform(tr);
  234. bool isDynamic = (baseMass > 0 && floating);
  235. short collisionFilterGroup = isDynamic? short(btBroadphaseProxy::DefaultFilter) : short(btBroadphaseProxy::StaticFilter);
  236. short collisionFilterMask = isDynamic? short(btBroadphaseProxy::AllFilter) : short(btBroadphaseProxy::AllFilter ^ btBroadphaseProxy::StaticFilter);
  237. world->addCollisionObject(col,collisionFilterGroup,collisionFilterMask);//, 2,1+2);
  238. btVector3 color(0.0,0.0,0.5);
  239. m_guiHelper->createCollisionObjectGraphicsObject(col,color);
  240. // col->setFriction(friction);
  241. pMultiBody->setBaseCollider(col);
  242. }
  243. }
  244. for (int i=0; i < pMultiBody->getNumLinks(); ++i)
  245. {
  246. const int parent = pMultiBody->getParent(i);
  247. world_to_local[i+1] = pMultiBody->getParentToLocalRot(i) * world_to_local[parent+1];
  248. local_origin[i+1] = local_origin[parent+1] + (quatRotate(world_to_local[i+1].inverse() , pMultiBody->getRVector(i)));
  249. }
  250. for (int i=0; i < pMultiBody->getNumLinks(); ++i)
  251. {
  252. btVector3 posr = local_origin[i+1];
  253. // float pos[4]={posr.x(),posr.y(),posr.z(),1};
  254. 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()};
  255. btCollisionShape* shape =0;
  256. if (i==0)
  257. {
  258. shape = new btBoxShape(btVector3(linkHalfExtents[0],linkHalfExtents[1],linkHalfExtents[2]));//btSphereShape(linkHalfExtents[0]);
  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. short collisionFilterGroup = isDynamic? short(btBroadphaseProxy::DefaultFilter) : short(btBroadphaseProxy::StaticFilter);
  274. short collisionFilterMask = isDynamic? short(btBroadphaseProxy::AllFilter) : short(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))
  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 (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. }