R2D2GraspExample.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include "R2D2GraspExample.h"
  2. #include "../CommonInterfaces/CommonGraphicsAppInterface.h"
  3. #include "Bullet3Common/b3Quaternion.h"
  4. #include "Bullet3Common/b3AlignedObjectArray.h"
  5. #include "../CommonInterfaces/CommonRenderInterface.h"
  6. #include "../CommonInterfaces/CommonExampleInterface.h"
  7. #include "../CommonInterfaces/CommonGUIHelperInterface.h"
  8. #include "../SharedMemory/PhysicsServerSharedMemory.h"
  9. #include "../SharedMemory/PhysicsClientC_API.h"
  10. #include <string>
  11. #include "../RobotSimulator/b3RobotSimulatorClientAPI.h"
  12. #include "../Utils/b3Clock.h"
  13. ///quick demo showing the right-handed coordinate system and positive rotations around each axis
  14. class R2D2GraspExample : public CommonExampleInterface
  15. {
  16. CommonGraphicsApp* m_app;
  17. GUIHelperInterface* m_guiHelper;
  18. b3RobotSimulatorClientAPI m_robotSim;
  19. int m_options;
  20. int m_r2d2Index;
  21. b3AlignedObjectArray<int> m_movingInstances;
  22. enum
  23. {
  24. numCubesX = 20,
  25. numCubesY = 20
  26. };
  27. public:
  28. R2D2GraspExample(GUIHelperInterface* helper, int options)
  29. : m_app(helper->getAppInterface()),
  30. m_guiHelper(helper),
  31. m_options(options),
  32. m_r2d2Index(-1)
  33. {
  34. m_app->setUpAxis(2);
  35. }
  36. virtual ~R2D2GraspExample()
  37. {
  38. }
  39. virtual void physicsDebugDraw(int debugDrawMode)
  40. {
  41. }
  42. virtual void initPhysics()
  43. {
  44. int mode = eCONNECT_EXISTING_EXAMPLE_BROWSER;
  45. m_robotSim.setGuiHelper(m_guiHelper);
  46. bool connected = m_robotSim.connect(mode);
  47. m_robotSim.configureDebugVisualizer(COV_ENABLE_RGB_BUFFER_PREVIEW, 0);
  48. m_robotSim.configureDebugVisualizer(COV_ENABLE_DEPTH_BUFFER_PREVIEW, 0);
  49. m_robotSim.configureDebugVisualizer(COV_ENABLE_SEGMENTATION_MARK_PREVIEW, 0);
  50. b3Printf("robotSim connected = %d", connected);
  51. if ((m_options & eROBOTIC_LEARN_GRASP) != 0)
  52. {
  53. {
  54. b3RobotSimulatorLoadUrdfFileArgs args;
  55. args.m_startPosition.setValue(0, 0, .5);
  56. m_r2d2Index = m_robotSim.loadURDF("r2d2.urdf", args);
  57. if (m_r2d2Index >= 0)
  58. {
  59. int numJoints = m_robotSim.getNumJoints(m_r2d2Index);
  60. b3Printf("numJoints = %d", numJoints);
  61. for (int i = 0; i < numJoints; i++)
  62. {
  63. b3JointInfo jointInfo;
  64. m_robotSim.getJointInfo(m_r2d2Index, i, &jointInfo);
  65. b3Printf("joint[%d].m_jointName=%s", i, jointInfo.m_jointName);
  66. }
  67. int wheelJointIndices[4] = {2, 3, 6, 7};
  68. int wheelTargetVelocities[4] = {-10, -10, -10, -10};
  69. for (int i = 0; i < 4; i++)
  70. {
  71. b3RobotSimulatorJointMotorArgs controlArgs(CONTROL_MODE_VELOCITY);
  72. controlArgs.m_targetVelocity = wheelTargetVelocities[i];
  73. controlArgs.m_maxTorqueValue = 1e30;
  74. m_robotSim.setJointMotorControl(m_r2d2Index, wheelJointIndices[i], controlArgs);
  75. }
  76. }
  77. }
  78. {
  79. b3RobotSimulatorLoadFileResults results;
  80. m_robotSim.loadSDF("kiva_shelf/model.sdf", results);
  81. }
  82. {
  83. m_robotSim.loadURDF("plane.urdf");
  84. }
  85. m_robotSim.setGravity(btVector3(0, 0, -10));
  86. }
  87. if ((m_options & eROBOTIC_LEARN_COMPLIANT_CONTACT) != 0)
  88. {
  89. b3RobotSimulatorLoadUrdfFileArgs args;
  90. b3RobotSimulatorLoadFileResults results;
  91. {
  92. args.m_startPosition.setValue(0, 0, 2.5);
  93. args.m_startOrientation.setEulerZYX(0, 0.2, 0);
  94. m_r2d2Index = m_robotSim.loadURDF("cube_soft.urdf", args);
  95. }
  96. {
  97. args.m_startPosition.setValue(0, 2, 2.5);
  98. args.m_startOrientation.setEulerZYX(0, 0.2, 0);
  99. m_robotSim.loadURDF("cube_no_friction.urdf", args);
  100. }
  101. {
  102. args.m_startPosition.setValue(0, 0, 0);
  103. args.m_startOrientation.setEulerZYX(0, 0.2, 0);
  104. args.m_forceOverrideFixedBase = true;
  105. m_robotSim.loadURDF("plane.urdf", args);
  106. }
  107. m_robotSim.setGravity(btVector3(0, 0, -10));
  108. }
  109. if ((m_options & eROBOTIC_LEARN_ROLLING_FRICTION) != 0)
  110. {
  111. b3RobotSimulatorLoadUrdfFileArgs args;
  112. b3RobotSimulatorLoadFileResults results;
  113. {
  114. args.m_startPosition.setValue(0, 0, 2.5);
  115. args.m_startOrientation.setEulerZYX(0, 0, 0);
  116. args.m_useMultiBody = true;
  117. m_robotSim.loadURDF("sphere2_rolling_friction.urdf", args);
  118. }
  119. {
  120. args.m_startPosition.setValue(0, 2, 2.5);
  121. args.m_startOrientation.setEulerZYX(0, 0, 0);
  122. args.m_useMultiBody = true;
  123. m_robotSim.loadURDF("sphere2.urdf", args);
  124. }
  125. {
  126. args.m_startPosition.setValue(0, 0, 0);
  127. args.m_startOrientation.setEulerZYX(0, 0.2, 0);
  128. args.m_useMultiBody = true;
  129. args.m_forceOverrideFixedBase = true;
  130. m_robotSim.loadURDF("plane.urdf", args);
  131. }
  132. m_robotSim.setGravity(btVector3(0, 0, -10));
  133. }
  134. }
  135. virtual void exitPhysics()
  136. {
  137. m_robotSim.disconnect();
  138. }
  139. virtual void stepSimulation(float deltaTime)
  140. {
  141. m_robotSim.stepSimulation();
  142. }
  143. virtual void renderScene()
  144. {
  145. m_robotSim.renderScene();
  146. //m_app->m_renderer->renderScene();
  147. }
  148. virtual void physicsDebugDraw()
  149. {
  150. }
  151. virtual bool mouseMoveCallback(float x, float y)
  152. {
  153. return false;
  154. }
  155. virtual bool mouseButtonCallback(int button, int state, float x, float y)
  156. {
  157. return false;
  158. }
  159. virtual bool keyboardCallback(int key, int state)
  160. {
  161. return false;
  162. }
  163. virtual void resetCamera()
  164. {
  165. float dist = 3;
  166. float pitch = -30;
  167. float yaw = -75;
  168. float targetPos[3] = {-0.2, 0.8, 0.3};
  169. if (m_app->m_renderer && m_app->m_renderer->getActiveCamera())
  170. {
  171. m_app->m_renderer->getActiveCamera()->setCameraDistance(dist);
  172. m_app->m_renderer->getActiveCamera()->setCameraPitch(pitch);
  173. m_app->m_renderer->getActiveCamera()->setCameraYaw(yaw);
  174. m_app->m_renderer->getActiveCamera()->setCameraTargetPosition(targetPos[0], targetPos[1], targetPos[2]);
  175. }
  176. }
  177. };
  178. class CommonExampleInterface* R2D2GraspExampleCreateFunc(struct CommonExampleOptions& options)
  179. {
  180. return new R2D2GraspExample(options.m_guiHelper, options.m_option);
  181. }