PhysicsClientC_API.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #ifndef PHYSICS_CLIENT_C_API_H
  2. #define PHYSICS_CLIENT_C_API_H
  3. //#include "SharedMemoryBlock.h"
  4. #include "SharedMemoryPublic.h"
  5. #define B3_DECLARE_HANDLE(name) typedef struct name##__ { int unused; } *name
  6. B3_DECLARE_HANDLE(b3PhysicsClientHandle);
  7. B3_DECLARE_HANDLE(b3SharedMemoryCommandHandle);
  8. B3_DECLARE_HANDLE(b3SharedMemoryStatusHandle);
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. ///b3ConnectSharedMemory will connect to a physics server over shared memory, so
  13. ///make sure to start the server first.
  14. ///and a way to spawn an OpenGL 3D GUI physics server and connect (b3CreateInProcessPhysicsServerAndConnect)
  15. b3PhysicsClientHandle b3ConnectSharedMemory(int key);
  16. ///b3DisconnectSharedMemory will disconnect the client from the server and cleanup memory.
  17. void b3DisconnectSharedMemory(b3PhysicsClientHandle physClient);
  18. ///There can only be 1 outstanding command. Check if a command can be send.
  19. int b3CanSubmitCommand(b3PhysicsClientHandle physClient);
  20. ///blocking submit command and wait for status
  21. b3SharedMemoryStatusHandle b3SubmitClientCommandAndWaitStatus(b3PhysicsClientHandle physClient, b3SharedMemoryCommandHandle commandHandle);
  22. ///In general it is better to use b3SubmitClientCommandAndWaitStatus. b3SubmitClientCommand is a non-blocking submit
  23. ///command, which requires checking for the status manually, using b3ProcessServerStatus. Also, before sending the
  24. ///next command, make sure to check if you can send a command using 'b3CanSubmitCommand'.
  25. int b3SubmitClientCommand(b3PhysicsClientHandle physClient, b3SharedMemoryCommandHandle commandHandle);
  26. ///non-blocking check status
  27. b3SharedMemoryStatusHandle b3ProcessServerStatus(b3PhysicsClientHandle physClient);
  28. /// Get the physics server return status type. See EnumSharedMemoryServerStatus in SharedMemoryPublic.h for error codes.
  29. int b3GetStatusType(b3SharedMemoryStatusHandle statusHandle);
  30. int b3GetStatusBodyIndices(b3SharedMemoryStatusHandle statusHandle, int* bodyIndicesOut, int bodyIndicesCapacity);
  31. int b3GetStatusBodyIndex(b3SharedMemoryStatusHandle statusHandle);
  32. int b3GetStatusActualState(b3SharedMemoryStatusHandle statusHandle,
  33. int* bodyUniqueId,
  34. int* numDegreeOfFreedomQ,
  35. int* numDegreeOfFreedomU,
  36. const double* rootLocalInertialFrame[],
  37. const double* actualStateQ[],
  38. const double* actualStateQdot[],
  39. const double* jointReactionForces[]);
  40. ///return the total number of bodies in the simulation
  41. int b3GetNumBodies(b3PhysicsClientHandle physClient);
  42. /// return the body unique id, given the index in range [0 , b3GetNumBodies() )
  43. int b3GetBodyUniqueId(b3PhysicsClientHandle physClient, int serialIndex);
  44. ///given a body unique id, return the body information. See b3BodyInfo in SharedMemoryPublic.h
  45. int b3GetBodyInfo(b3PhysicsClientHandle physClient, int bodyUniqueId, struct b3BodyInfo* info);
  46. ///give a unique body index (after loading the body) return the number of joints.
  47. int b3GetNumJoints(b3PhysicsClientHandle physClient, int bodyIndex);
  48. ///given a body and joint index, return the joint information. See b3JointInfo in SharedMemoryPublic.h
  49. int b3GetJointInfo(b3PhysicsClientHandle physClient, int bodyIndex, int jointIndex, struct b3JointInfo* info);
  50. b3SharedMemoryCommandHandle b3CreateJoint(b3PhysicsClientHandle physClient, int parentBodyIndex, int parentJointIndex, int childBodyIndex, int childJointIndex, struct b3JointInfo* info);
  51. ///Request debug lines for debug visualization. The flags in debugMode are the same as used in Bullet
  52. ///See btIDebugDraw::DebugDrawModes in Bullet/src/LinearMath/btIDebugDraw.h
  53. b3SharedMemoryCommandHandle b3InitRequestDebugLinesCommand(b3PhysicsClientHandle physClient, int debugMode);
  54. ///Get the pointers to the debug line information, after b3InitRequestDebugLinesCommand returns
  55. ///status CMD_DEBUG_LINES_COMPLETED
  56. void b3GetDebugLines(b3PhysicsClientHandle physClient, struct b3DebugLines* lines);
  57. ///request an image from a simulated camera, using a software renderer.
  58. b3SharedMemoryCommandHandle b3InitRequestCameraImage(b3PhysicsClientHandle physClient);
  59. void b3RequestCameraImageSetCameraMatrices(b3SharedMemoryCommandHandle command, float viewMatrix[16], float projectionMatrix[16]);
  60. void b3RequestCameraImageSetViewMatrix(b3SharedMemoryCommandHandle command, const float cameraPosition[3], const float cameraTargetPosition[3], const float cameraUp[3]);
  61. void b3RequestCameraImageSetViewMatrix2(b3SharedMemoryCommandHandle commandHandle, const float cameraTargetPosition[3], float distance, float yaw, float pitch, float roll, int upAxis);
  62. void b3RequestCameraImageSetProjectionMatrix(b3SharedMemoryCommandHandle command, float left, float right, float bottom, float top, float nearVal, float farVal);
  63. void b3RequestCameraImageSetFOVProjectionMatrix(b3SharedMemoryCommandHandle command, float fov, float aspect, float nearVal, float farVal);
  64. void b3RequestCameraImageSetPixelResolution(b3SharedMemoryCommandHandle command, int width, int height );
  65. void b3RequestCameraImageSelectRenderer(b3SharedMemoryCommandHandle commandHandle, int renderer);
  66. void b3GetCameraImageData(b3PhysicsClientHandle physClient, struct b3CameraImageData* imageData);
  67. ///request an contact point information
  68. b3SharedMemoryCommandHandle b3InitRequestContactPointInformation(b3PhysicsClientHandle physClient);
  69. void b3SetContactFilterBodyA(b3SharedMemoryCommandHandle commandHandle, int bodyUniqueIdA);
  70. void b3SetContactFilterBodyB(b3SharedMemoryCommandHandle commandHandle, int bodyUniqueIdB);
  71. void b3GetContactPointInformation(b3PhysicsClientHandle physClient, struct b3ContactInformation* contactPointData);
  72. b3SharedMemoryCommandHandle b3InitPhysicsParamCommand(b3PhysicsClientHandle physClient);
  73. int b3PhysicsParamSetGravity(b3SharedMemoryCommandHandle commandHandle, double gravx,double gravy, double gravz);
  74. int b3PhysicsParamSetTimeStep(b3SharedMemoryCommandHandle commandHandle, double timeStep);
  75. int b3PhysicsParamSetDefaultContactERP(b3SharedMemoryCommandHandle commandHandle, double defaultContactERP);
  76. int b3PhysicsParamSetNumSubSteps(b3SharedMemoryCommandHandle commandHandle, int numSubSteps);
  77. int b3PhysicsParamSetRealTimeSimulation(b3SharedMemoryCommandHandle commandHandle, int enableRealTimeSimulation);
  78. int b3PhysicsParamSetNumSolverIterations(b3SharedMemoryCommandHandle commandHandle, int numSolverIterations);
  79. b3SharedMemoryCommandHandle b3InitStepSimulationCommand(b3PhysicsClientHandle physClient);
  80. b3SharedMemoryCommandHandle b3InitResetSimulationCommand(b3PhysicsClientHandle physClient);
  81. ///Load a robot from a URDF file. Status type will CMD_URDF_LOADING_COMPLETED.
  82. ///Access the robot from the unique body index, through b3GetStatusBodyIndex(statusHandle);
  83. b3SharedMemoryCommandHandle b3LoadUrdfCommandInit(b3PhysicsClientHandle physClient, const char* urdfFileName);
  84. int b3LoadUrdfCommandSetStartPosition(b3SharedMemoryCommandHandle commandHandle, double startPosX,double startPosY,double startPosZ);
  85. int b3LoadUrdfCommandSetStartOrientation(b3SharedMemoryCommandHandle commandHandle, double startOrnX,double startOrnY,double startOrnZ, double startOrnW);
  86. int b3LoadUrdfCommandSetUseMultiBody(b3SharedMemoryCommandHandle commandHandle, int useMultiBody);
  87. int b3LoadUrdfCommandSetUseFixedBase(b3SharedMemoryCommandHandle commandHandle, int useFixedBase);
  88. ///compute the forces to achieve an acceleration, given a state q and qdot using inverse dynamics
  89. b3SharedMemoryCommandHandle b3CalculateInverseDynamicsCommandInit(b3PhysicsClientHandle physClient, int bodyIndex,
  90. const double* jointPositionsQ, const double* jointVelocitiesQdot, const double* jointAccelerations);
  91. int b3GetStatusInverseDynamicsJointForces(b3SharedMemoryStatusHandle statusHandle,
  92. int* bodyUniqueId,
  93. int* dofCount,
  94. double* jointForces);
  95. b3SharedMemoryCommandHandle b3CalculateJacobianCommandInit(b3PhysicsClientHandle physClient, int bodyIndex, int linkIndex, const double* localPosition, const double* jointPositionsQ, const double* jointVelocitiesQdot, const double* jointAccelerations);
  96. int b3GetStatusJacobian(b3SharedMemoryStatusHandle statusHandle, double* linearJacobian, double* angularJacobian);
  97. ///compute the joint positions to move the end effector to a desired target using inverse kinematics
  98. b3SharedMemoryCommandHandle b3CalculateInverseKinematicsCommandInit(b3PhysicsClientHandle physClient, int bodyIndex);
  99. void b3CalculateInverseKinematicsAddTargetPurePosition(b3SharedMemoryCommandHandle commandHandle, int endEffectorLinkIndex, const double targetPosition[3]);
  100. void b3CalculateInverseKinematicsAddTargetPositionWithOrientation(b3SharedMemoryCommandHandle commandHandle, int endEffectorLinkIndex, const double targetPosition[3], const double targetOrientation[4]);
  101. void b3CalculateInverseKinematicsPosWithNullSpaceVel(b3SharedMemoryCommandHandle commandHandle, int numDof, int endEffectorLinkIndex, const double targetPosition[3], const double* lowerLimit, const double* upperLimit, const double* jointRange, const double* restPose);
  102. void b3CalculateInverseKinematicsPosOrnWithNullSpaceVel(b3SharedMemoryCommandHandle commandHandle, int numDof, int endEffectorLinkIndex, const double targetPosition[3], const double targetOrientation[4], const double* lowerLimit, const double* upperLimit, const double* jointRange, const double* restPose);
  103. int b3GetStatusInverseKinematicsJointPositions(b3SharedMemoryStatusHandle statusHandle,
  104. int* bodyUniqueId,
  105. int* dofCount,
  106. double* jointPositions);
  107. b3SharedMemoryCommandHandle b3LoadSdfCommandInit(b3PhysicsClientHandle physClient, const char* sdfFileName);
  108. int b3LoadSdfCommandSetUseMultiBody(b3SharedMemoryCommandHandle commandHandle, int useMultiBody);
  109. b3SharedMemoryCommandHandle b3SaveWorldCommandInit(b3PhysicsClientHandle physClient, const char* sdfFileName);
  110. ///The b3JointControlCommandInit method is obsolete, use b3JointControlCommandInit2 instead
  111. b3SharedMemoryCommandHandle b3JointControlCommandInit(b3PhysicsClientHandle physClient, int controlMode);
  112. ///Set joint motor control variables such as desired position/angle, desired velocity,
  113. ///applied joint forces, dependent on the control mode (CONTROL_MODE_VELOCITY or CONTROL_MODE_TORQUE)
  114. b3SharedMemoryCommandHandle b3JointControlCommandInit2(b3PhysicsClientHandle physClient, int bodyUniqueId, int controlMode);
  115. ///Only use when controlMode is CONTROL_MODE_POSITION_VELOCITY_PD
  116. int b3JointControlSetDesiredPosition(b3SharedMemoryCommandHandle commandHandle, int qIndex, double value);
  117. int b3JointControlSetKp(b3SharedMemoryCommandHandle commandHandle, int dofIndex, double value);
  118. int b3JointControlSetKd(b3SharedMemoryCommandHandle commandHandle, int dofIndex, double value);
  119. ///Only use when controlMode is CONTROL_MODE_VELOCITY
  120. int b3JointControlSetDesiredVelocity(b3SharedMemoryCommandHandle commandHandle, int dofIndex, double value); /* find a better name for dof/q/u indices, point to b3JointInfo */
  121. int b3JointControlSetMaximumForce(b3SharedMemoryCommandHandle commandHandle, int dofIndex, double value);
  122. ///Only use if when controlMode is CONTROL_MODE_TORQUE,
  123. int b3JointControlSetDesiredForceTorque(b3SharedMemoryCommandHandle commandHandle, int dofIndex, double value);
  124. ///the creation of collision shapes and rigid bodies etc is likely going to change,
  125. ///but good to have a b3CreateBoxShapeCommandInit for now
  126. ///create a box of size (1,1,1) at world origin (0,0,0) at orientation quat (0,0,0,1)
  127. ///after that, you can optionally adjust the initial position, orientation and size
  128. b3SharedMemoryCommandHandle b3CreateBoxShapeCommandInit(b3PhysicsClientHandle physClient);
  129. int b3CreateBoxCommandSetStartPosition(b3SharedMemoryCommandHandle commandHandle, double startPosX,double startPosY,double startPosZ);
  130. int b3CreateBoxCommandSetStartOrientation(b3SharedMemoryCommandHandle commandHandle, double startOrnX,double startOrnY,double startOrnZ, double startOrnW);
  131. int b3CreateBoxCommandSetHalfExtents(b3SharedMemoryCommandHandle commandHandle, double halfExtentsX,double halfExtentsY,double halfExtentsZ);
  132. int b3CreateBoxCommandSetMass(b3SharedMemoryCommandHandle commandHandle, double mass);
  133. int b3CreateBoxCommandSetCollisionShapeType(b3SharedMemoryCommandHandle commandHandle, int collisionShapeType);
  134. int b3CreateBoxCommandSetColorRGBA(b3SharedMemoryCommandHandle commandHandle, double red,double green,double blue, double alpha);
  135. ///b3CreatePoseCommandInit will initialize (teleport) the pose of a body/robot. You can individually set the base position,
  136. ///base orientation and joint angles. This will set all velocities of base and joints to zero.
  137. ///This is not a robot control command using actuators/joint motors, but manual repositioning the robot.
  138. b3SharedMemoryCommandHandle b3CreatePoseCommandInit(b3PhysicsClientHandle physClient, int bodyIndex);
  139. int b3CreatePoseCommandSetBasePosition(b3SharedMemoryCommandHandle commandHandle, double startPosX,double startPosY,double startPosZ);
  140. int b3CreatePoseCommandSetBaseOrientation(b3SharedMemoryCommandHandle commandHandle, double startOrnX,double startOrnY,double startOrnZ, double startOrnW);
  141. int b3CreatePoseCommandSetJointPositions(b3SharedMemoryCommandHandle commandHandle, int numJointPositions, const double* jointPositions);
  142. int b3CreatePoseCommandSetJointPosition(b3PhysicsClientHandle physClient, b3SharedMemoryCommandHandle commandHandle, int jointIndex, double jointPosition);
  143. ///We are currently not reading the sensor information from the URDF file, and programmatically assign sensors.
  144. ///This is rather inconsistent, to mix programmatical creation with loading from file.
  145. b3SharedMemoryCommandHandle b3CreateSensorCommandInit(b3PhysicsClientHandle physClient, int bodyUniqueId);
  146. int b3CreateSensorEnable6DofJointForceTorqueSensor(b3SharedMemoryCommandHandle commandHandle, int jointIndex, int enable);
  147. ///b3CreateSensorEnableIMUForLink is not implemented yet.
  148. ///For now, if the IMU is located in the root link, use the root world transform to mimic an IMU.
  149. int b3CreateSensorEnableIMUForLink(b3SharedMemoryCommandHandle commandHandle, int linkIndex, int enable);
  150. b3SharedMemoryCommandHandle b3RequestActualStateCommandInit(b3PhysicsClientHandle physClient,int bodyUniqueId);
  151. void b3GetJointState(b3PhysicsClientHandle physClient, b3SharedMemoryStatusHandle statusHandle, int jointIndex, struct b3JointSensorState *state);
  152. void b3GetLinkState(b3PhysicsClientHandle physClient, b3SharedMemoryStatusHandle statusHandle, int linkIndex, struct b3LinkState *state);
  153. b3SharedMemoryCommandHandle b3PickBody(b3PhysicsClientHandle physClient, double rayFromWorldX,
  154. double rayFromWorldY, double rayFromWorldZ,
  155. double rayToWorldX, double rayToWorldY, double rayToWorldZ);
  156. b3SharedMemoryCommandHandle b3MovePickedBody(b3PhysicsClientHandle physClient, double rayFromWorldX,
  157. double rayFromWorldY, double rayFromWorldZ,
  158. double rayToWorldX, double rayToWorldY,
  159. double rayToWorldZ);
  160. b3SharedMemoryCommandHandle b3RemovePickingConstraint(b3PhysicsClientHandle physClient);
  161. /// Apply external force at the body (or link) center of mass, in world space/Cartesian coordinates.
  162. b3SharedMemoryCommandHandle b3ApplyExternalForceCommandInit(b3PhysicsClientHandle physClient);
  163. void b3ApplyExternalForce(b3SharedMemoryCommandHandle commandHandle, int bodyUniqueId, int linkId, const double force[3], const double position[3], int flags);
  164. void b3ApplyExternalTorque(b3SharedMemoryCommandHandle commandHandle, int bodyUniqueId, int linkId, const double torque[3], int flags);
  165. #ifdef __cplusplus
  166. }
  167. #endif
  168. #endif //PHYSICS_CLIENT_C_API_H