2015-07-14 22:30:17 +00:00
|
|
|
|
|
|
|
#include "PhysicsClientExample.h"
|
|
|
|
|
|
|
|
#include "../CommonInterfaces/CommonMultiBodyBase.h"
|
2016-05-31 17:23:04 +00:00
|
|
|
#include "../CommonInterfaces/Common2dCanvasInterface.h"
|
2015-07-14 22:30:17 +00:00
|
|
|
#include "SharedMemoryCommon.h"
|
|
|
|
#include "../CommonInterfaces/CommonParameterInterface.h"
|
2015-09-25 05:42:22 +00:00
|
|
|
#include "PhysicsClientC_API.h"
|
2015-07-14 22:30:17 +00:00
|
|
|
#include "PhysicsClient.h"
|
2015-09-17 06:09:10 +00:00
|
|
|
//#include "SharedMemoryCommands.h"
|
2015-11-23 04:50:32 +00:00
|
|
|
#include "PhysicsLoopBackC_API.h"
|
|
|
|
#include "PhysicsDirectC_API.h"
|
2015-09-17 06:09:10 +00:00
|
|
|
#include "PhysicsClientC_API.h"
|
2016-05-18 16:34:42 +00:00
|
|
|
#include "PhysicsServerSharedMemory.h"
|
2015-08-06 18:59:31 +00:00
|
|
|
struct MyMotorInfo2
|
|
|
|
{
|
|
|
|
btScalar m_velTarget;
|
|
|
|
btScalar m_maxForce;
|
2016-04-19 21:19:18 +00:00
|
|
|
btScalar m_posTarget;
|
2015-08-06 18:59:31 +00:00
|
|
|
int m_uIndex;
|
2016-04-19 21:19:18 +00:00
|
|
|
int m_qIndex;
|
2015-08-06 18:59:31 +00:00
|
|
|
};
|
|
|
|
|
2016-06-02 00:47:41 +00:00
|
|
|
int camVisualizerWidth = 640;//1024/3;
|
|
|
|
int camVisualizerHeight = 480;//768/3;
|
2016-05-31 17:23:04 +00:00
|
|
|
|
2015-09-17 06:09:10 +00:00
|
|
|
|
2015-08-06 18:59:31 +00:00
|
|
|
#define MAX_NUM_MOTORS 128
|
|
|
|
|
2015-07-14 22:30:17 +00:00
|
|
|
class PhysicsClientExample : public SharedMemoryCommon
|
|
|
|
{
|
|
|
|
protected:
|
2015-09-17 06:09:10 +00:00
|
|
|
b3PhysicsClientHandle m_physicsClientHandle;
|
2016-05-18 16:34:42 +00:00
|
|
|
|
|
|
|
//this m_physicsServer is only used when option eCLIENTEXAMPLE_SERVER is enabled
|
|
|
|
PhysicsServerSharedMemory m_physicsServer;
|
2015-07-14 22:30:17 +00:00
|
|
|
|
|
|
|
bool m_wantsTermination;
|
2015-09-17 06:09:10 +00:00
|
|
|
btAlignedObjectArray<int> m_userCommandRequests;
|
|
|
|
int m_sharedMemoryKey;
|
2015-10-13 18:32:25 +00:00
|
|
|
int m_selectedBody;
|
|
|
|
int m_prevSelectedBody;
|
2016-05-31 17:23:04 +00:00
|
|
|
struct Common2dCanvasInterface* m_canvas;
|
|
|
|
int m_canvasIndex;
|
|
|
|
|
2015-07-14 22:30:17 +00:00
|
|
|
void createButton(const char* name, int id, bool isTrigger );
|
|
|
|
|
2015-08-07 07:13:26 +00:00
|
|
|
void createButtons();
|
|
|
|
|
2016-05-18 16:34:42 +00:00
|
|
|
|
2015-07-14 22:30:17 +00:00
|
|
|
|
2015-08-06 18:59:31 +00:00
|
|
|
//@todo, add accessor methods
|
2016-04-19 23:52:47 +00:00
|
|
|
// MyMotorInfo2 m_motorTargetVelocities[MAX_NUM_MOTORS];
|
2016-04-19 21:19:18 +00:00
|
|
|
MyMotorInfo2 m_motorTargetPositions[MAX_NUM_MOTORS];
|
2015-08-06 18:59:31 +00:00
|
|
|
int m_numMotors;
|
2016-05-18 16:34:42 +00:00
|
|
|
int m_options;
|
|
|
|
bool m_isOptionalServerConnected;
|
2015-08-06 18:59:31 +00:00
|
|
|
|
2016-05-18 16:34:42 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
PhysicsClientExample(GUIHelperInterface* helper, int options);
|
2015-07-14 22:30:17 +00:00
|
|
|
virtual ~PhysicsClientExample();
|
|
|
|
|
|
|
|
virtual void initPhysics();
|
2015-10-13 18:32:25 +00:00
|
|
|
void selectComboBox(int comboIndex, const char* name)
|
|
|
|
{
|
|
|
|
if (m_guiHelper && m_guiHelper->getParameterInterface())
|
|
|
|
{
|
|
|
|
int bodyIndex = comboIndex;
|
|
|
|
if (m_selectedBody != bodyIndex)
|
|
|
|
{
|
|
|
|
m_selectedBody = bodyIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-14 22:30:17 +00:00
|
|
|
virtual void stepSimulation(float deltaTime);
|
|
|
|
|
|
|
|
virtual void resetCamera()
|
|
|
|
{
|
2015-10-13 18:32:25 +00:00
|
|
|
float dist = 5;
|
2015-07-14 22:30:17 +00:00
|
|
|
float pitch = 50;
|
|
|
|
float yaw = 35;
|
2015-08-20 05:51:16 +00:00
|
|
|
float targetPos[3]={0,0,0};//-3,2.8,-2.5};
|
2015-07-14 22:30:17 +00:00
|
|
|
m_guiHelper->resetCamera(dist,pitch,yaw,targetPos[0],targetPos[1],targetPos[2]);
|
2015-08-20 05:51:16 +00:00
|
|
|
|
2015-07-14 22:30:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool wantsTermination()
|
|
|
|
{
|
|
|
|
return m_wantsTermination;
|
|
|
|
}
|
2015-08-06 18:59:31 +00:00
|
|
|
|
|
|
|
virtual bool isConnected()
|
|
|
|
{
|
2015-09-17 06:09:10 +00:00
|
|
|
return (m_physicsClientHandle!=0);
|
2015-08-06 18:59:31 +00:00
|
|
|
}
|
|
|
|
|
2015-09-17 06:09:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
void enqueueCommand(int commandId);
|
|
|
|
|
|
|
|
void prepareAndSubmitCommand(int commandId);
|
|
|
|
|
|
|
|
|
2015-08-07 07:13:26 +00:00
|
|
|
virtual void exitPhysics(){};
|
2015-08-20 05:51:16 +00:00
|
|
|
virtual void renderScene()
|
|
|
|
{
|
2016-05-18 16:34:42 +00:00
|
|
|
if (m_options == eCLIENTEXAMPLE_SERVER)
|
|
|
|
{
|
|
|
|
m_physicsServer.renderScene();
|
|
|
|
}
|
|
|
|
|
2015-09-17 06:09:10 +00:00
|
|
|
b3DebugLines debugLines;
|
|
|
|
b3GetDebugLines(m_physicsClientHandle,&debugLines);
|
|
|
|
int numLines = debugLines.m_numDebugLines;
|
2015-09-03 21:18:22 +00:00
|
|
|
|
|
|
|
int lineWidth = 1;
|
|
|
|
|
|
|
|
if (1)
|
|
|
|
{
|
|
|
|
btAlignedObjectArray<btVector3FloatData> points;
|
|
|
|
points.resize(numLines*2);
|
|
|
|
btAlignedObjectArray<unsigned int> indices;
|
|
|
|
indices.resize(numLines*2);
|
|
|
|
|
|
|
|
for (int i=0;i<numLines;i++)
|
|
|
|
{
|
2015-09-17 06:09:10 +00:00
|
|
|
points[i*2].m_floats[0] = debugLines.m_linesFrom[i*3+0];
|
|
|
|
points[i*2].m_floats[1] = debugLines.m_linesFrom[i*3+1];
|
|
|
|
points[i*2].m_floats[2] = debugLines.m_linesFrom[i*3+2];
|
|
|
|
points[i*2+1].m_floats[0] = debugLines.m_linesTo[i*3+0];
|
|
|
|
points[i*2+1].m_floats[1] = debugLines.m_linesTo[i*3+1];
|
|
|
|
points[i*2+1].m_floats[2] = debugLines.m_linesTo[i*3+2];
|
2015-09-03 21:18:22 +00:00
|
|
|
indices[i*2] = i*2;
|
|
|
|
indices[i*2+1] = i*2+1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-10-14 05:23:28 +00:00
|
|
|
float color[4] = {0.2,0.2,1,1};
|
2015-09-03 21:18:22 +00:00
|
|
|
|
|
|
|
if (points.size() && indices.size())
|
|
|
|
{
|
|
|
|
m_guiHelper->getRenderInterface()->drawLines(&points[0].m_floats[0],color,points.size(),sizeof(btVector3FloatData),&indices[0],indices.size(),lineWidth);
|
|
|
|
}
|
|
|
|
} else
|
2015-08-20 05:51:16 +00:00
|
|
|
{
|
2015-09-03 21:18:22 +00:00
|
|
|
for (int i=0;i<numLines;i++)
|
|
|
|
{
|
2015-09-17 06:09:10 +00:00
|
|
|
m_guiHelper->getRenderInterface()->drawLine(debugLines.m_linesFrom,debugLines.m_linesTo,debugLines.m_linesColor,lineWidth);
|
2015-09-03 21:18:22 +00:00
|
|
|
}
|
2015-08-20 05:51:16 +00:00
|
|
|
}
|
2015-09-04 18:28:08 +00:00
|
|
|
}
|
|
|
|
|
2015-09-17 06:09:10 +00:00
|
|
|
void prepareControlCommand(b3SharedMemoryCommandHandle commandHandle)
|
2015-09-04 18:28:08 +00:00
|
|
|
{
|
|
|
|
for (int i=0;i<m_numMotors;i++)
|
|
|
|
{
|
2016-04-19 21:19:18 +00:00
|
|
|
|
|
|
|
btScalar targetPos = m_motorTargetPositions[i].m_posTarget;
|
|
|
|
int qIndex = m_motorTargetPositions[i].m_qIndex;
|
2016-04-19 23:52:47 +00:00
|
|
|
int uIndex = m_motorTargetPositions[i].m_uIndex;
|
2016-04-19 21:19:18 +00:00
|
|
|
b3JointControlSetDesiredPosition(commandHandle, qIndex, targetPos);
|
2016-04-19 23:52:47 +00:00
|
|
|
b3JointControlSetKp(commandHandle, uIndex, 0.1);
|
2016-04-19 21:19:18 +00:00
|
|
|
|
2015-09-17 06:09:10 +00:00
|
|
|
b3JointControlSetMaximumForce(commandHandle,uIndex,1000);
|
2015-09-04 18:28:08 +00:00
|
|
|
}
|
2015-08-20 05:51:16 +00:00
|
|
|
}
|
2016-05-18 16:34:42 +00:00
|
|
|
virtual void physicsDebugDraw(int debugFlags)
|
|
|
|
{
|
|
|
|
if (m_options==eCLIENTEXAMPLE_SERVER)
|
|
|
|
{
|
|
|
|
m_physicsServer.physicsDebugDraw(debugFlags);
|
|
|
|
}
|
|
|
|
}
|
2015-08-07 07:13:26 +00:00
|
|
|
virtual bool mouseMoveCallback(float x,float y){return false;};
|
|
|
|
virtual bool mouseButtonCallback(int button, int state, float x, float y){return false;}
|
|
|
|
virtual bool keyboardCallback(int key, int state){return false;}
|
|
|
|
|
|
|
|
|
|
|
|
virtual void setSharedMemoryKey(int key)
|
|
|
|
{
|
2015-09-17 06:09:10 +00:00
|
|
|
m_sharedMemoryKey = key;
|
2015-08-07 07:13:26 +00:00
|
|
|
}
|
2015-07-14 22:30:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-10-13 18:32:25 +00:00
|
|
|
void MyComboBoxCallback (int combobox, const char* item, void* userPointer)
|
|
|
|
{
|
|
|
|
b3Printf("Item selected %s", item);
|
|
|
|
|
|
|
|
PhysicsClientExample* cl = (PhysicsClientExample*) userPointer;
|
|
|
|
b3Assert(cl);
|
|
|
|
if (cl)
|
|
|
|
{
|
|
|
|
cl->selectComboBox(combobox,item);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-08-06 18:59:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-07-14 22:30:17 +00:00
|
|
|
void MyCallback(int buttonId, bool buttonState, void* userPtr)
|
|
|
|
{
|
|
|
|
PhysicsClientExample* cl = (PhysicsClientExample*) userPtr;
|
2015-10-13 18:32:25 +00:00
|
|
|
b3Assert(cl);
|
|
|
|
|
|
|
|
if (cl && buttonState)
|
2015-09-17 06:09:10 +00:00
|
|
|
{
|
|
|
|
cl->enqueueCommand(buttonId);
|
|
|
|
}
|
|
|
|
}
|
2015-07-14 22:30:17 +00:00
|
|
|
|
2015-09-17 06:09:10 +00:00
|
|
|
void PhysicsClientExample::enqueueCommand(int commandId)
|
|
|
|
{
|
|
|
|
m_userCommandRequests.push_back(commandId);
|
2015-07-14 22:30:17 +00:00
|
|
|
}
|
|
|
|
|
2015-09-17 06:09:10 +00:00
|
|
|
void PhysicsClientExample::prepareAndSubmitCommand(int commandId)
|
|
|
|
{
|
|
|
|
|
|
|
|
switch (commandId)
|
|
|
|
{
|
|
|
|
case CMD_LOAD_URDF:
|
|
|
|
{
|
|
|
|
|
2016-04-19 23:52:47 +00:00
|
|
|
b3SharedMemoryCommandHandle commandHandle = b3LoadUrdfCommandInit(m_physicsClientHandle, "kuka_iiwa/model.urdf");
|
2015-09-17 06:09:10 +00:00
|
|
|
|
|
|
|
//setting the initial position, orientation and other arguments are optional
|
|
|
|
double startPosX = 0;
|
2015-10-13 18:32:25 +00:00
|
|
|
static double startPosY = 0;
|
2015-09-17 06:09:10 +00:00
|
|
|
double startPosZ = 0;
|
2015-09-25 05:42:22 +00:00
|
|
|
b3LoadUrdfCommandSetStartPosition(commandHandle, startPosX,startPosY,startPosZ);
|
2015-10-13 18:32:25 +00:00
|
|
|
startPosY += 2.f;
|
2015-09-17 06:09:10 +00:00
|
|
|
// ret = b3LoadUrdfCommandSetUseFixedBase(commandHandle, 1);
|
2015-09-25 05:42:22 +00:00
|
|
|
b3SubmitClientCommand(m_physicsClientHandle, commandHandle);
|
2015-09-17 06:09:10 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2016-05-20 01:37:15 +00:00
|
|
|
case CMD_REQUEST_CAMERA_IMAGE_DATA:
|
|
|
|
{
|
|
|
|
///request an image from a simulated camera, using a software renderer.
|
|
|
|
|
|
|
|
b3SharedMemoryCommandHandle commandHandle = b3InitRequestCameraImage(m_physicsClientHandle);
|
2016-06-01 18:04:10 +00:00
|
|
|
|
|
|
|
float viewMatrix[16];
|
|
|
|
float projectionMatrix[16];
|
|
|
|
this->m_guiHelper->getRenderInterface()->getActiveCamera()->getCameraProjectionMatrix(projectionMatrix);
|
|
|
|
this->m_guiHelper->getRenderInterface()->getActiveCamera()->getCameraViewMatrix(viewMatrix);
|
|
|
|
b3RequestCameraImageSetCameraMatrices(commandHandle, viewMatrix,projectionMatrix);
|
2016-05-20 01:37:15 +00:00
|
|
|
b3SubmitClientCommand(m_physicsClientHandle, commandHandle);
|
|
|
|
break;
|
|
|
|
}
|
2015-09-17 06:09:10 +00:00
|
|
|
case CMD_CREATE_BOX_COLLISION_SHAPE:
|
|
|
|
{
|
|
|
|
b3SharedMemoryCommandHandle commandHandle = b3CreateBoxShapeCommandInit(m_physicsClientHandle);
|
|
|
|
b3CreateBoxCommandSetStartPosition(commandHandle,0,0,-3);
|
2015-11-07 01:11:15 +00:00
|
|
|
b3CreateBoxCommandSetColorRGBA(commandHandle,0,0,1,1);
|
2015-09-17 06:09:10 +00:00
|
|
|
b3SubmitClientCommand(m_physicsClientHandle, commandHandle);
|
|
|
|
break;
|
|
|
|
}
|
2015-10-27 21:55:46 +00:00
|
|
|
case CMD_CREATE_RIGID_BODY:
|
|
|
|
{
|
|
|
|
b3SharedMemoryCommandHandle commandHandle = b3CreateBoxShapeCommandInit(m_physicsClientHandle);
|
|
|
|
b3CreateBoxCommandSetStartPosition(commandHandle,0,0,0);
|
|
|
|
b3CreateBoxCommandSetMass(commandHandle,1);
|
|
|
|
b3CreateBoxCommandSetCollisionShapeType(commandHandle,COLLISION_SHAPE_TYPE_CYLINDER_Y);
|
2015-11-07 01:11:15 +00:00
|
|
|
b3CreateBoxCommandSetColorRGBA(commandHandle,1,1,0,1);
|
2015-10-27 21:55:46 +00:00
|
|
|
double radius = 0.2;
|
|
|
|
double halfHeight = 0.5;
|
|
|
|
b3CreateBoxCommandSetHalfExtents(commandHandle,radius,halfHeight,radius);
|
|
|
|
b3SubmitClientCommand(m_physicsClientHandle, commandHandle);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-09-17 06:09:10 +00:00
|
|
|
case CMD_REQUEST_ACTUAL_STATE:
|
|
|
|
{
|
2015-10-27 22:46:13 +00:00
|
|
|
if (m_selectedBody>=0)
|
|
|
|
{
|
|
|
|
b3SharedMemoryCommandHandle commandHandle = b3RequestActualStateCommandInit(m_physicsClientHandle,m_selectedBody);
|
2016-04-19 23:52:47 +00:00
|
|
|
b3SharedMemoryStatusHandle statusHandle = b3SubmitClientCommandAndWaitStatus(m_physicsClientHandle, commandHandle);
|
2015-10-27 22:46:13 +00:00
|
|
|
b3SubmitClientCommand(m_physicsClientHandle, commandHandle);
|
2016-04-19 23:52:47 +00:00
|
|
|
|
|
|
|
int numJoints = b3GetNumJoints(m_physicsClientHandle, m_selectedBody);
|
|
|
|
for (int i = 0; i < numJoints; ++i) {
|
|
|
|
struct b3JointSensorState sensorState;
|
|
|
|
b3GetJointState(m_physicsClientHandle, statusHandle, i, &sensorState);
|
|
|
|
b3Printf("Joint %d: %f", i, sensorState.m_jointMotorTorque);
|
|
|
|
}
|
2015-10-27 22:46:13 +00:00
|
|
|
}
|
2015-09-17 06:09:10 +00:00
|
|
|
break;
|
|
|
|
};
|
2015-07-14 22:30:17 +00:00
|
|
|
|
2015-10-14 05:23:28 +00:00
|
|
|
case CMD_INIT_POSE:
|
|
|
|
{
|
|
|
|
|
|
|
|
if (m_selectedBody>=0)
|
|
|
|
{
|
|
|
|
b3SharedMemoryCommandHandle commandHandle = b3CreatePoseCommandInit(m_physicsClientHandle,m_selectedBody);
|
|
|
|
static int toggle = 0;
|
|
|
|
double pos[3] = {0,0,0};
|
|
|
|
pos[toggle] = 2;
|
|
|
|
toggle++;
|
|
|
|
if (toggle>2)
|
|
|
|
toggle=0;
|
|
|
|
|
|
|
|
btQuaternion orn;
|
|
|
|
orn.setValue(0,0,0,1);
|
|
|
|
|
|
|
|
switch (toggle)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
orn = btQuaternion(btVector3(1,0,0),SIMD_HALF_PI);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
orn = btQuaternion(btVector3(0,1,0),SIMD_HALF_PI);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
orn = btQuaternion(btVector3(0,0,1),SIMD_HALF_PI);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
orn.setValue(0,0,0,1);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
b3CreatePoseCommandSetBaseOrientation(commandHandle,orn[0],orn[1],orn[2],orn[3]);
|
|
|
|
b3CreatePoseCommandSetBasePosition(commandHandle, pos[0],pos[1],pos[2]);
|
|
|
|
int numJoints = b3GetNumJoints(m_physicsClientHandle,m_selectedBody);
|
|
|
|
static double jointPos = SIMD_PI/2.f;
|
|
|
|
|
|
|
|
for (int i=0;i<numJoints;i++)
|
|
|
|
{
|
|
|
|
b3JointInfo info;
|
|
|
|
b3GetJointInfo(m_physicsClientHandle, m_selectedBody,i, &info);
|
|
|
|
if ((info.m_jointType == 0) || (info.m_jointType == 1)) //revolute or prismatic
|
|
|
|
{
|
|
|
|
b3CreatePoseCommandSetJointPosition(m_physicsClientHandle,commandHandle,i,jointPos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
jointPos += SIMD_PI/8.0;
|
|
|
|
b3SubmitClientCommand(m_physicsClientHandle, commandHandle);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2015-09-17 06:09:10 +00:00
|
|
|
case CMD_STEP_FORWARD_SIMULATION:
|
|
|
|
{
|
|
|
|
|
|
|
|
b3SharedMemoryCommandHandle commandHandle = b3InitStepSimulationCommand(m_physicsClientHandle);
|
|
|
|
b3SubmitClientCommand(m_physicsClientHandle, commandHandle);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case CMD_REQUEST_DEBUG_LINES:
|
|
|
|
{
|
|
|
|
b3SharedMemoryCommandHandle commandHandle = b3InitRequestDebugLinesCommand(m_physicsClientHandle, btIDebugDraw::DBG_DrawWireframe);
|
|
|
|
b3SubmitClientCommand(m_physicsClientHandle, commandHandle);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CMD_SEND_DESIRED_STATE:
|
|
|
|
{
|
2016-04-19 23:52:47 +00:00
|
|
|
// b3SharedMemoryCommandHandle command = b3JointControlCommandInit( m_physicsClientHandle, CONTROL_MODE_VELOCITY);
|
|
|
|
b3SharedMemoryCommandHandle command = b3JointControlCommandInit( m_physicsClientHandle, CONTROL_MODE_POSITION_VELOCITY_PD);
|
2015-09-17 06:09:10 +00:00
|
|
|
prepareControlCommand(command);
|
|
|
|
b3SubmitClientCommand(m_physicsClientHandle, command);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CMD_RESET_SIMULATION:
|
|
|
|
{
|
|
|
|
b3SharedMemoryCommandHandle commandHandle = b3InitResetSimulationCommand(m_physicsClientHandle);
|
|
|
|
b3SubmitClientCommand(m_physicsClientHandle, commandHandle);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CMD_SEND_BULLET_DATA_STREAM:
|
|
|
|
{
|
|
|
|
#if 0
|
2015-07-14 22:30:17 +00:00
|
|
|
|
2015-09-17 06:09:10 +00:00
|
|
|
//this worked, but needs C-API and a streaming options, similar to debug lines
|
|
|
|
command.m_type = buttonId;
|
|
|
|
cl->enqueueCommand(command);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2016-04-19 23:52:47 +00:00
|
|
|
case CMD_SEND_PHYSICS_SIMULATION_PARAMETERS: {
|
|
|
|
b3SharedMemoryCommandHandle commandHandle = b3InitPhysicsParamCommand(m_physicsClientHandle);
|
|
|
|
b3PhysicsParamSetGravity(commandHandle, 0.0, 0.0, -9.8);
|
2016-05-18 16:34:42 +00:00
|
|
|
b3SubmitClientCommand(m_physicsClientHandle, commandHandle);
|
2016-04-19 23:52:47 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-09-17 06:09:10 +00:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
b3Error("Unknown buttonId");
|
|
|
|
btAssert(0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2015-07-14 22:30:17 +00:00
|
|
|
|
2015-11-11 20:44:26 +00:00
|
|
|
|
|
|
|
|
2016-05-18 16:34:42 +00:00
|
|
|
PhysicsClientExample::PhysicsClientExample(GUIHelperInterface* helper, int options)
|
2015-07-14 22:30:17 +00:00
|
|
|
:SharedMemoryCommon(helper),
|
2015-10-30 20:24:02 +00:00
|
|
|
m_physicsClientHandle(0),
|
2015-08-07 07:13:26 +00:00
|
|
|
m_wantsTermination(false),
|
2015-09-17 06:09:10 +00:00
|
|
|
m_sharedMemoryKey(SHARED_MEMORY_KEY),
|
2015-11-11 20:44:26 +00:00
|
|
|
m_selectedBody(-1),
|
|
|
|
m_prevSelectedBody(-1),
|
2016-05-18 16:34:42 +00:00
|
|
|
m_numMotors(0),
|
|
|
|
m_options(options),
|
2016-05-31 17:23:04 +00:00
|
|
|
m_isOptionalServerConnected(false),
|
|
|
|
m_canvas(0)
|
2015-07-14 22:30:17 +00:00
|
|
|
{
|
|
|
|
b3Printf("Started PhysicsClientExample\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsClientExample::~PhysicsClientExample()
|
|
|
|
{
|
2015-10-13 18:32:25 +00:00
|
|
|
if (m_physicsClientHandle)
|
|
|
|
{
|
|
|
|
b3ProcessServerStatus(m_physicsClientHandle);
|
|
|
|
b3DisconnectSharedMemory(m_physicsClientHandle);
|
|
|
|
}
|
2016-05-18 16:34:42 +00:00
|
|
|
|
|
|
|
if (m_options == eCLIENTEXAMPLE_SERVER)
|
|
|
|
{
|
|
|
|
bool deInitializeSharedMemory = true;
|
|
|
|
m_physicsServer.disconnectSharedMemory(deInitializeSharedMemory);
|
|
|
|
}
|
2016-06-04 02:03:56 +00:00
|
|
|
|
|
|
|
if (m_canvas && (m_canvasIndex>=0))
|
|
|
|
{
|
|
|
|
m_canvas->destroyCanvas(m_canvasIndex);
|
|
|
|
}
|
2015-07-14 22:30:17 +00:00
|
|
|
b3Printf("~PhysicsClientExample\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsClientExample::createButton(const char* name, int buttonId, bool isTrigger )
|
|
|
|
{
|
|
|
|
ButtonParams button(name,buttonId, isTrigger);
|
|
|
|
button.m_callback = MyCallback;
|
|
|
|
button.m_userPointer = this;
|
|
|
|
m_guiHelper->getParameterInterface()->registerButtonParameter(button);
|
|
|
|
}
|
2015-08-07 07:13:26 +00:00
|
|
|
|
|
|
|
void PhysicsClientExample::createButtons()
|
|
|
|
{
|
|
|
|
bool isTrigger = false;
|
|
|
|
|
2015-09-09 22:14:47 +00:00
|
|
|
if (m_guiHelper && m_guiHelper->getParameterInterface())
|
|
|
|
{
|
2015-10-13 18:32:25 +00:00
|
|
|
m_guiHelper->getParameterInterface()->removeAllParameters();
|
|
|
|
|
2015-09-09 22:14:47 +00:00
|
|
|
createButton("Load URDF",CMD_LOAD_URDF, isTrigger);
|
2016-05-20 01:37:15 +00:00
|
|
|
createButton("Get Camera Image",CMD_REQUEST_CAMERA_IMAGE_DATA,isTrigger);
|
2015-09-09 22:14:47 +00:00
|
|
|
createButton("Step Sim",CMD_STEP_FORWARD_SIMULATION, isTrigger);
|
|
|
|
createButton("Send Bullet Stream",CMD_SEND_BULLET_DATA_STREAM, isTrigger);
|
2016-05-18 16:34:42 +00:00
|
|
|
if (m_options!=eCLIENTEXAMPLE_SERVER)
|
|
|
|
{
|
|
|
|
createButton("Get State",CMD_REQUEST_ACTUAL_STATE, isTrigger);
|
|
|
|
}
|
2015-09-09 22:14:47 +00:00
|
|
|
createButton("Send Desired State",CMD_SEND_DESIRED_STATE, isTrigger);
|
|
|
|
createButton("Create Box Collider",CMD_CREATE_BOX_COLLISION_SHAPE,isTrigger);
|
2015-10-27 21:55:46 +00:00
|
|
|
createButton("Create Cylinder Body",CMD_CREATE_RIGID_BODY,isTrigger);
|
2015-09-09 22:14:47 +00:00
|
|
|
createButton("Reset Simulation",CMD_RESET_SIMULATION,isTrigger);
|
2015-10-14 05:23:28 +00:00
|
|
|
createButton("Initialize Pose",CMD_INIT_POSE, isTrigger);
|
2016-04-19 23:52:47 +00:00
|
|
|
createButton("Set gravity", CMD_SEND_PHYSICS_SIMULATION_PARAMETERS, isTrigger);
|
2015-10-13 18:32:25 +00:00
|
|
|
|
|
|
|
|
2015-10-30 20:24:02 +00:00
|
|
|
if (m_physicsClientHandle && m_selectedBody>=0)
|
2015-10-13 18:32:25 +00:00
|
|
|
{
|
|
|
|
int numJoints = b3GetNumJoints(m_physicsClientHandle,m_selectedBody);
|
|
|
|
for (int i=0;i<numJoints;i++)
|
|
|
|
{
|
|
|
|
b3JointInfo info;
|
|
|
|
b3GetJointInfo(m_physicsClientHandle,m_selectedBody,i,&info);
|
|
|
|
b3Printf("Joint %s at q-index %d and u-index %d\n",info.m_jointName,info.m_qIndex,info.m_uIndex);
|
|
|
|
|
|
|
|
if (info.m_flags & JOINT_HAS_MOTORIZED_POWER)
|
|
|
|
{
|
|
|
|
if (m_numMotors<MAX_NUM_MOTORS)
|
|
|
|
{
|
|
|
|
char motorName[1024];
|
2016-04-19 23:52:47 +00:00
|
|
|
sprintf(motorName,"%s q", info.m_jointName);
|
|
|
|
// MyMotorInfo2* motorInfo = &m_motorTargetVelocities[m_numMotors];
|
|
|
|
MyMotorInfo2* motorInfo = &m_motorTargetPositions[m_numMotors];
|
2015-10-13 18:32:25 +00:00
|
|
|
motorInfo->m_velTarget = 0.f;
|
2016-04-19 23:52:47 +00:00
|
|
|
motorInfo->m_posTarget = 0.f;
|
2015-10-13 18:32:25 +00:00
|
|
|
motorInfo->m_uIndex = info.m_uIndex;
|
2016-04-19 21:19:18 +00:00
|
|
|
motorInfo->m_qIndex = info.m_qIndex;
|
2015-10-13 18:32:25 +00:00
|
|
|
|
2016-04-19 23:52:47 +00:00
|
|
|
// SliderParams slider(motorName,&motorInfo->m_velTarget);
|
|
|
|
// slider.m_minVal=-4;
|
|
|
|
// slider.m_maxVal=4;
|
|
|
|
SliderParams slider(motorName,&motorInfo->m_posTarget);
|
|
|
|
slider.m_minVal=-4;
|
|
|
|
slider.m_maxVal=4;
|
2015-10-13 18:32:25 +00:00
|
|
|
if (m_guiHelper && m_guiHelper->getParameterInterface())
|
|
|
|
{
|
|
|
|
m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
|
|
|
|
}
|
|
|
|
m_numMotors++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-09 22:14:47 +00:00
|
|
|
}
|
2015-08-07 07:13:26 +00:00
|
|
|
}
|
|
|
|
|
2015-10-13 18:32:25 +00:00
|
|
|
|
|
|
|
|
2015-07-14 22:30:17 +00:00
|
|
|
void PhysicsClientExample::initPhysics()
|
|
|
|
{
|
|
|
|
if (m_guiHelper && m_guiHelper->getParameterInterface())
|
|
|
|
{
|
2015-08-20 05:51:16 +00:00
|
|
|
int upAxis = 2;
|
|
|
|
m_guiHelper->setUpAxis(upAxis);
|
|
|
|
|
2015-08-07 07:13:26 +00:00
|
|
|
createButtons();
|
2015-07-14 22:30:17 +00:00
|
|
|
|
|
|
|
} else
|
|
|
|
{
|
2015-09-09 22:14:47 +00:00
|
|
|
MyCallback(CMD_LOAD_URDF, true, this);
|
|
|
|
MyCallback(CMD_STEP_FORWARD_SIMULATION,true,this);
|
|
|
|
MyCallback(CMD_STEP_FORWARD_SIMULATION,true,this);
|
|
|
|
MyCallback(CMD_RESET_SIMULATION,true,this);
|
2015-07-14 22:30:17 +00:00
|
|
|
}
|
|
|
|
|
2015-10-13 18:32:25 +00:00
|
|
|
m_selectedBody = -1;
|
|
|
|
m_prevSelectedBody = -1;
|
|
|
|
|
2016-05-18 16:34:42 +00:00
|
|
|
{
|
2016-05-31 17:23:04 +00:00
|
|
|
m_canvas = m_guiHelper->get2dCanvasInterface();
|
|
|
|
if (m_canvas)
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
m_canvasIndex = m_canvas->createCanvas("Synthetic Camera",camVisualizerWidth, camVisualizerHeight);
|
|
|
|
|
|
|
|
for (int i=0;i<camVisualizerWidth;i++)
|
|
|
|
{
|
|
|
|
for (int j=0;j<camVisualizerHeight;j++)
|
|
|
|
{
|
|
|
|
unsigned char red=255;
|
|
|
|
unsigned char green=255;
|
|
|
|
unsigned char blue=255;
|
|
|
|
unsigned char alpha=255;
|
|
|
|
if (i==j)
|
|
|
|
{
|
|
|
|
red = 0;
|
|
|
|
green=0;
|
|
|
|
blue=0;
|
|
|
|
}
|
|
|
|
m_canvas->setPixel(m_canvasIndex,i,j,red,green,blue,alpha);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_canvas->refreshImageData(m_canvasIndex);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-05-18 16:34:42 +00:00
|
|
|
}
|
|
|
|
|
2016-06-02 20:54:52 +00:00
|
|
|
if (m_options == eCLIENTEXAMPLE_SERVER)
|
|
|
|
{
|
|
|
|
m_isOptionalServerConnected = m_physicsServer.connectSharedMemory( m_guiHelper);
|
|
|
|
}
|
|
|
|
|
2016-06-04 02:03:56 +00:00
|
|
|
if (m_options == eCLIENTEXAMPLE_DIRECT)
|
|
|
|
{
|
|
|
|
m_physicsClientHandle = b3ConnectPhysicsDirect();
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
m_physicsClientHandle = b3ConnectSharedMemory(m_sharedMemoryKey);
|
|
|
|
//m_physicsClientHandle = b3ConnectPhysicsLoopback(SHARED_MEMORY_KEY);
|
|
|
|
}
|
|
|
|
|
2015-09-17 06:09:10 +00:00
|
|
|
if (!b3CanSubmitCommand(m_physicsClientHandle))
|
|
|
|
{
|
2015-08-06 18:59:31 +00:00
|
|
|
b3Warning("Cannot connect to physics client");
|
2015-07-14 22:30:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PhysicsClientExample::stepSimulation(float deltaTime)
|
|
|
|
{
|
2016-05-18 16:34:42 +00:00
|
|
|
|
|
|
|
if (m_options == eCLIENTEXAMPLE_SERVER)
|
|
|
|
{
|
|
|
|
for (int i=0;i<100;i++)
|
|
|
|
{
|
|
|
|
m_physicsServer.processClientCommands();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-13 18:32:25 +00:00
|
|
|
if (m_prevSelectedBody != m_selectedBody)
|
|
|
|
{
|
|
|
|
createButtons();
|
|
|
|
m_prevSelectedBody = m_selectedBody;
|
|
|
|
}
|
2015-10-14 05:23:28 +00:00
|
|
|
|
|
|
|
//while (!b3CanSubmitCommand(m_physicsClientHandle))
|
|
|
|
{
|
|
|
|
b3SharedMemoryStatusHandle status = b3ProcessServerStatus(m_physicsClientHandle);
|
|
|
|
bool hasStatus = (status != 0);
|
|
|
|
if (hasStatus)
|
2015-07-21 06:35:29 +00:00
|
|
|
{
|
2015-10-14 05:23:28 +00:00
|
|
|
|
|
|
|
int statusType = b3GetStatusType(status);
|
|
|
|
if (statusType == CMD_ACTUAL_STATE_UPDATE_COMPLETED)
|
2015-07-21 06:35:29 +00:00
|
|
|
{
|
2015-10-14 05:23:28 +00:00
|
|
|
//b3Printf("bla\n");
|
|
|
|
}
|
2016-05-20 01:37:15 +00:00
|
|
|
if (statusType ==CMD_CAMERA_IMAGE_COMPLETED)
|
|
|
|
{
|
2016-06-07 01:54:05 +00:00
|
|
|
// static int counter=0;
|
|
|
|
// char msg[1024];
|
|
|
|
// sprintf(msg,"Camera image %d OK\n",counter++);
|
2016-05-31 17:23:04 +00:00
|
|
|
b3CameraImageData imageData;
|
|
|
|
b3GetCameraImageData(m_physicsClientHandle,&imageData);
|
|
|
|
if (m_canvas && m_canvasIndex >=0)
|
|
|
|
{
|
|
|
|
for (int i=0;i<imageData.m_pixelWidth;i++)
|
|
|
|
{
|
|
|
|
for (int j=0;j<imageData.m_pixelHeight;j++)
|
|
|
|
{
|
2016-06-02 00:47:41 +00:00
|
|
|
int xIndex = int(float(i)*(float(camVisualizerWidth)/float(imageData.m_pixelWidth)));
|
|
|
|
int yIndex = int(float(j)*(float(camVisualizerHeight)/float(imageData.m_pixelHeight)));
|
2016-05-31 17:23:04 +00:00
|
|
|
btClamp(yIndex,0,imageData.m_pixelHeight);
|
|
|
|
btClamp(xIndex,0,imageData.m_pixelWidth);
|
|
|
|
int bytesPerPixel = 4;
|
|
|
|
|
|
|
|
int pixelIndex = (i+j*imageData.m_pixelWidth)*bytesPerPixel;
|
|
|
|
m_canvas->setPixel(m_canvasIndex,xIndex,camVisualizerHeight-1-yIndex,
|
2016-06-02 00:47:41 +00:00
|
|
|
|
2016-05-31 17:23:04 +00:00
|
|
|
imageData.m_rgbColorData[pixelIndex],
|
|
|
|
imageData.m_rgbColorData[pixelIndex+1],
|
|
|
|
imageData.m_rgbColorData[pixelIndex+2],
|
2016-06-02 00:47:41 +00:00
|
|
|
255);
|
|
|
|
// imageData.m_rgbColorData[pixelIndex+3]);
|
2016-05-31 17:23:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
m_canvas->refreshImageData(m_canvasIndex);
|
|
|
|
}
|
|
|
|
|
2016-06-07 01:54:05 +00:00
|
|
|
// b3Printf(msg);
|
2016-05-20 01:37:15 +00:00
|
|
|
}
|
|
|
|
if (statusType == CMD_CAMERA_IMAGE_FAILED)
|
|
|
|
{
|
2016-06-07 01:54:05 +00:00
|
|
|
b3Warning("Camera image FAILED\n");
|
2016-05-20 01:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-14 05:23:28 +00:00
|
|
|
if (statusType == CMD_URDF_LOADING_COMPLETED)
|
|
|
|
{
|
|
|
|
int bodyIndex = b3GetStatusBodyIndex(status);
|
|
|
|
if (bodyIndex>=0)
|
2015-10-13 18:32:25 +00:00
|
|
|
{
|
2015-10-14 05:23:28 +00:00
|
|
|
int numJoints = b3GetNumJoints(m_physicsClientHandle,bodyIndex);
|
|
|
|
|
|
|
|
for (int i=0;i<numJoints;i++)
|
|
|
|
{
|
|
|
|
b3JointInfo info;
|
|
|
|
b3GetJointInfo(m_physicsClientHandle,bodyIndex,i,&info);
|
|
|
|
b3Printf("Joint %s at q-index %d and u-index %d\n",info.m_jointName,info.m_qIndex,info.m_uIndex);
|
2015-07-21 06:35:29 +00:00
|
|
|
|
2015-10-14 05:23:28 +00:00
|
|
|
}
|
|
|
|
ComboBoxParams comboParams;
|
|
|
|
comboParams.m_comboboxId = bodyIndex;
|
|
|
|
comboParams.m_numItems = 1;
|
|
|
|
comboParams.m_startItem = 0;
|
|
|
|
comboParams.m_callback = MyComboBoxCallback;
|
|
|
|
comboParams.m_userPointer = this;
|
|
|
|
const char* bla = "bla";
|
|
|
|
const char* blarray[1];
|
|
|
|
blarray[0] = bla;
|
2015-10-13 18:32:25 +00:00
|
|
|
|
2015-10-14 05:23:28 +00:00
|
|
|
comboParams.m_items=blarray;//{&bla};
|
|
|
|
m_guiHelper->getParameterInterface()->registerComboBox(comboParams);
|
2015-09-03 21:18:22 +00:00
|
|
|
|
2015-10-13 18:32:25 +00:00
|
|
|
|
2015-10-14 05:23:28 +00:00
|
|
|
}
|
2015-10-13 18:32:25 +00:00
|
|
|
|
2015-10-14 05:23:28 +00:00
|
|
|
}
|
2015-07-14 22:30:17 +00:00
|
|
|
|
2015-10-14 05:23:28 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-17 06:09:10 +00:00
|
|
|
if (b3CanSubmitCommand(m_physicsClientHandle))
|
|
|
|
{
|
|
|
|
if (m_userCommandRequests.size())
|
|
|
|
{
|
|
|
|
//b3Printf("Outstanding user command requests: %d\n", m_userCommandRequests.size());
|
|
|
|
int commandId = m_userCommandRequests[0];
|
2015-07-14 22:30:17 +00:00
|
|
|
|
2015-09-17 06:09:10 +00:00
|
|
|
//a manual 'pop_front', we don't use 'remove' because it will re-order the commands
|
|
|
|
for (int i=1;i<m_userCommandRequests.size();i++)
|
|
|
|
{
|
|
|
|
m_userCommandRequests[i-1] = m_userCommandRequests[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
m_userCommandRequests.pop_back();
|
|
|
|
|
|
|
|
//for the CMD_RESET_SIMULATION we need to do something special: clear the GUI sliders
|
|
|
|
if (commandId ==CMD_RESET_SIMULATION)
|
|
|
|
{
|
2015-10-13 18:32:25 +00:00
|
|
|
m_selectedBody = -1;
|
2015-09-17 06:09:10 +00:00
|
|
|
m_numMotors=0;
|
|
|
|
createButtons();
|
2016-05-18 16:34:42 +00:00
|
|
|
b3SharedMemoryCommandHandle commandHandle = b3InitResetSimulationCommand(m_physicsClientHandle);
|
|
|
|
if (m_options == eCLIENTEXAMPLE_SERVER)
|
|
|
|
{
|
|
|
|
b3SubmitClientCommand(m_physicsClientHandle, commandHandle);
|
|
|
|
while (!b3CanSubmitCommand(m_physicsClientHandle))
|
|
|
|
{
|
|
|
|
m_physicsServer.processClientCommands();
|
|
|
|
b3SharedMemoryStatusHandle status = b3ProcessServerStatus(m_physicsClientHandle);
|
|
|
|
bool hasStatus = (status != 0);
|
|
|
|
if (hasStatus)
|
|
|
|
{
|
2016-06-07 01:54:05 +00:00
|
|
|
//int statusType = b3GetStatusType(status);
|
|
|
|
//b3Printf("Status after reset: %d",statusType);
|
2016-05-18 16:34:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
prepareAndSubmitCommand(commandId);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
prepareAndSubmitCommand(commandId);
|
|
|
|
}
|
2015-09-17 06:09:10 +00:00
|
|
|
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
if (m_numMotors)
|
|
|
|
{
|
|
|
|
enqueueCommand(CMD_SEND_DESIRED_STATE);
|
|
|
|
enqueueCommand(CMD_STEP_FORWARD_SIMULATION);
|
2016-05-18 16:34:42 +00:00
|
|
|
if (m_options != eCLIENTEXAMPLE_SERVER)
|
|
|
|
{
|
|
|
|
enqueueCommand(CMD_REQUEST_DEBUG_LINES);
|
|
|
|
}
|
2015-10-27 21:55:46 +00:00
|
|
|
//enqueueCommand(CMD_REQUEST_ACTUAL_STATE);
|
2015-09-17 06:09:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-14 22:30:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-07 07:13:26 +00:00
|
|
|
extern int gSharedMemoryKey;
|
|
|
|
|
2015-07-14 22:30:17 +00:00
|
|
|
|
|
|
|
class CommonExampleInterface* PhysicsClientCreateFunc(struct CommonExampleOptions& options)
|
|
|
|
{
|
2016-05-18 16:34:42 +00:00
|
|
|
PhysicsClientExample* example = new PhysicsClientExample(options.m_guiHelper, options.m_option);
|
2015-08-07 07:13:26 +00:00
|
|
|
if (gSharedMemoryKey>=0)
|
|
|
|
{
|
|
|
|
example->setSharedMemoryKey(gSharedMemoryKey);
|
|
|
|
}
|
|
|
|
return example;
|
2015-07-14 22:30:17 +00:00
|
|
|
}
|