exit gracefully and call destructor of the current active example
fflush after printf implemented stepForward and Shutdown for the SharedMemory client/server
This commit is contained in:
@@ -3,11 +3,14 @@
|
||||
|
||||
#include "../CommonInterfaces/CommonMultiBodyBase.h"
|
||||
#include "PosixSharedMemory.h"
|
||||
#include "SharedMemoryCommon.h"
|
||||
|
||||
class PhysicsClient : public CommonMultiBodyBase
|
||||
class PhysicsClient : public SharedMemoryCommon
|
||||
{
|
||||
SharedMemoryInterface* m_sharedMemory;
|
||||
SharedMemoryExampleData* m_testBlock1;
|
||||
int m_counter;
|
||||
bool m_wantsTermination;
|
||||
|
||||
public:
|
||||
|
||||
@@ -27,11 +30,17 @@ public:
|
||||
m_guiHelper->resetCamera(dist,pitch,yaw,targetPos[0],targetPos[1],targetPos[2]);
|
||||
}
|
||||
|
||||
virtual bool wantsTermination()
|
||||
{
|
||||
return m_wantsTermination;
|
||||
}
|
||||
};
|
||||
|
||||
PhysicsClient::PhysicsClient(GUIHelperInterface* helper)
|
||||
:CommonMultiBodyBase(helper),
|
||||
m_testBlock1(0)
|
||||
:SharedMemoryCommon(helper),
|
||||
m_testBlock1(0),
|
||||
m_counter(0),
|
||||
m_wantsTermination(false)
|
||||
{
|
||||
b3Printf("Started PhysicsClient");
|
||||
m_sharedMemory = new PosixSharedMemory();
|
||||
@@ -55,6 +64,15 @@ void PhysicsClient::initPhysics()
|
||||
b3Error("Error: please start server before client");
|
||||
m_sharedMemory->releaseSharedMemory(SHARED_MEMORY_KEY, SHARED_MEMORY_SIZE);
|
||||
m_testBlock1 = 0;
|
||||
|
||||
} else
|
||||
{
|
||||
//submit a 'load urdf' command to get things started
|
||||
|
||||
b3Printf("Client created CMD_LOAD_URDF");
|
||||
m_testBlock1->m_clientCommands[0].m_type =CMD_LOAD_URDF;
|
||||
sprintf(m_testBlock1->m_clientCommands[0].m_urdfArguments.m_urdfFileName,"r2d2.urdf");
|
||||
m_testBlock1->m_numClientCommands++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,20 +81,60 @@ void PhysicsClient::initPhysics()
|
||||
void PhysicsClient::stepSimulation(float deltaTime)
|
||||
{
|
||||
|
||||
static int once = true;
|
||||
|
||||
if (m_testBlock1)
|
||||
{
|
||||
if (once)
|
||||
{
|
||||
once=false;
|
||||
//check progress and submit further commands
|
||||
//we ignore overflow right now
|
||||
|
||||
b3Printf("Client created CMD_LOAD_URDF");
|
||||
m_testBlock1->m_clientCommands[0].m_type =CMD_LOAD_URDF;
|
||||
sprintf(m_testBlock1->m_clientCommands[0].m_urdfArguments.m_urdfFileName,"r2d2.urdf");
|
||||
m_testBlock1->m_numClientCommands++;
|
||||
if (m_testBlock1->m_numServerCommands> m_testBlock1->m_numProcessedServerCommands)
|
||||
{
|
||||
btAssert(m_testBlock1->m_numServerCommands==m_testBlock1->m_numProcessedServerCommands+1);
|
||||
|
||||
const SharedMemoryCommand& serverCmd =m_testBlock1->m_serverCommands[0];
|
||||
|
||||
//consume the command
|
||||
switch (serverCmd.m_type)
|
||||
{
|
||||
|
||||
case CMD_STEP_FORWARD_SIMULATION_COMPLETED:
|
||||
case CMD_URDF_LOADING_COMPLETED:
|
||||
{
|
||||
//submit a 'step simulation' request
|
||||
|
||||
if (m_counter<10)
|
||||
{
|
||||
m_testBlock1->m_clientCommands[0].m_type =CMD_STEP_FORWARD_SIMULATION;
|
||||
m_testBlock1->m_clientCommands[0].m_stepSimulationArguments.m_deltaTimeInSeconds = 1./60.;
|
||||
m_testBlock1->m_numClientCommands++;
|
||||
m_counter++;
|
||||
|
||||
} else
|
||||
{
|
||||
m_wantsTermination = true;
|
||||
m_testBlock1->m_clientCommands[0].m_type =CMD_SHUTDOWN;
|
||||
m_testBlock1->m_numClientCommands++;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case CMD_URDF_LOADING_FAILED:
|
||||
{
|
||||
b3Printf("Server failed loading the URDF...");
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
b3Error("Unknown server command");
|
||||
btAssert(0);
|
||||
}
|
||||
};
|
||||
m_testBlock1->m_numProcessedServerCommands++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
|
||||
#include "PhysicsServer.h"
|
||||
|
||||
#include "../CommonInterfaces/CommonMultiBodyBase.h"
|
||||
#include "PosixSharedMemory.h"
|
||||
#include "../Importers/ImportURDFDemo/MyURDFImporter.h"
|
||||
#include "../Importers/ImportURDFDemo/MyMultiBodyCreator.h"
|
||||
#include "../Importers/ImportURDFDemo/URDF2Bullet.h"
|
||||
|
||||
#include "SharedMemoryCommon.h"
|
||||
|
||||
|
||||
|
||||
class PhysicsServer : public CommonMultiBodyBase
|
||||
class PhysicsServer : public SharedMemoryCommon
|
||||
{
|
||||
SharedMemoryInterface* m_sharedMemory;
|
||||
SharedMemoryExampleData* m_testBlock1;
|
||||
|
||||
bool m_wantsShutdown;
|
||||
|
||||
public:
|
||||
|
||||
PhysicsServer(GUIHelperInterface* helper);
|
||||
@@ -25,6 +25,8 @@ public:
|
||||
|
||||
virtual void stepSimulation(float deltaTime);
|
||||
|
||||
void releaseSharedMemory();
|
||||
|
||||
bool loadUrdf(const char* fileName, const btVector3& pos, const btQuaternion& orn,
|
||||
bool useMultiBody, bool useFixedBase);
|
||||
|
||||
@@ -37,25 +39,39 @@ public:
|
||||
m_guiHelper->resetCamera(dist,pitch,yaw,targetPos[0],targetPos[1],targetPos[2]);
|
||||
}
|
||||
|
||||
virtual bool wantsTermination();
|
||||
};
|
||||
|
||||
PhysicsServer::PhysicsServer(GUIHelperInterface* helper)
|
||||
:CommonMultiBodyBase(helper),
|
||||
m_testBlock1(0)
|
||||
:SharedMemoryCommon(helper),
|
||||
m_testBlock1(0),
|
||||
m_wantsShutdown(false)
|
||||
{
|
||||
b3Printf("Started PhysicsServer\n");
|
||||
m_sharedMemory = new PosixSharedMemory();
|
||||
}
|
||||
|
||||
PhysicsServer::~PhysicsServer()
|
||||
void PhysicsServer::releaseSharedMemory()
|
||||
{
|
||||
if (m_testBlock1)
|
||||
{
|
||||
m_testBlock1->m_magicId = 0;
|
||||
b3Printf("magic id = %d\n",m_testBlock1->m_magicId);
|
||||
btAssert(m_sharedMemory);
|
||||
m_sharedMemory->releaseSharedMemory(SHARED_MEMORY_KEY, SHARED_MEMORY_SIZE);
|
||||
}
|
||||
m_sharedMemory->releaseSharedMemory(SHARED_MEMORY_KEY, SHARED_MEMORY_SIZE);
|
||||
delete m_sharedMemory;
|
||||
if (m_sharedMemory)
|
||||
{
|
||||
|
||||
delete m_sharedMemory;
|
||||
m_sharedMemory = 0;
|
||||
m_testBlock1 = 0;
|
||||
}
|
||||
}
|
||||
|
||||
PhysicsServer::~PhysicsServer()
|
||||
{
|
||||
releaseSharedMemory();
|
||||
}
|
||||
|
||||
void PhysicsServer::initPhysics()
|
||||
@@ -86,6 +102,12 @@ void PhysicsServer::initPhysics()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool PhysicsServer::wantsTermination()
|
||||
{
|
||||
return m_wantsShutdown;
|
||||
}
|
||||
|
||||
bool PhysicsServer::loadUrdf(const char* fileName, const btVector3& pos, const btQuaternion& orn,
|
||||
bool useMultiBody, bool useFixedBase)
|
||||
{
|
||||
@@ -103,17 +125,21 @@ bool PhysicsServer::loadUrdf(const char* fileName, const btVector3& pos, const b
|
||||
int rootLinkIndex = u2b.getRootLinkIndex();
|
||||
// printf("urdf root link index = %d\n",rootLinkIndex);
|
||||
MyMultiBodyCreator creation(m_guiHelper);
|
||||
bool m_useMultiBody = true;
|
||||
|
||||
ConvertURDF2Bullet(u2b,creation, tr,m_dynamicsWorld,useMultiBody,u2b.getPathPrefix());
|
||||
btMultiBody* mb = creation.getBulletMultiBody();
|
||||
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void PhysicsServer::stepSimulation(float deltaTime)
|
||||
{
|
||||
|
||||
bool wantsShutdown = false;
|
||||
|
||||
if (m_testBlock1)
|
||||
{
|
||||
///we ignore overflow of integer for now
|
||||
@@ -124,6 +150,7 @@ void PhysicsServer::stepSimulation(float deltaTime)
|
||||
btAssert(m_testBlock1->m_numClientCommands==m_testBlock1->m_numProcessedClientCommands+1);
|
||||
|
||||
const SharedMemoryCommand& clientCmd =m_testBlock1->m_clientCommands[0];
|
||||
m_testBlock1->m_numProcessedClientCommands++;
|
||||
|
||||
//consume the command
|
||||
switch (clientCmd.m_type)
|
||||
@@ -148,19 +175,45 @@ void PhysicsServer::stepSimulation(float deltaTime)
|
||||
|
||||
}
|
||||
m_testBlock1->m_numServerCommands++;
|
||||
break;
|
||||
}
|
||||
case CMD_STEP_FORWARD_SIMULATION:
|
||||
{
|
||||
|
||||
b3Printf("Step simulation request");
|
||||
double timeStep = clientCmd.m_stepSimulationArguments.m_deltaTimeInSeconds;
|
||||
m_dynamicsWorld->stepSimulation(timeStep);
|
||||
|
||||
SharedMemoryCommand& serverCmd =m_testBlock1->m_serverCommands[0];
|
||||
|
||||
serverCmd.m_type =CMD_STEP_FORWARD_SIMULATION_COMPLETED;
|
||||
m_testBlock1->m_numServerCommands++;
|
||||
|
||||
break;
|
||||
}
|
||||
case CMD_SHUTDOWN:
|
||||
{
|
||||
wantsShutdown = true;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
|
||||
b3Error("Unsupported command encountered");
|
||||
btAssert(0);
|
||||
}
|
||||
};
|
||||
|
||||
m_testBlock1->m_numProcessedClientCommands++;
|
||||
|
||||
|
||||
//process the command right now
|
||||
|
||||
}
|
||||
}
|
||||
if (wantsShutdown)
|
||||
{
|
||||
m_wantsShutdown = true;
|
||||
releaseSharedMemory();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ enum SharedMemoryClientCommand{
|
||||
CMD_LOAD_URDF,
|
||||
CMD_STATE_UPDATED,
|
||||
CMD_STEP_FORWARD_SIMULATION, //includes CMD_REQUEST_STATE
|
||||
CMD_SHUTDOWN,
|
||||
CMD_MAX_CLIENT_COMMANDS
|
||||
};
|
||||
|
||||
|
||||
@@ -20,8 +20,7 @@ subject to the following restrictions:
|
||||
|
||||
#include "../CommonInterfaces/CommonExampleInterface.h"
|
||||
#include "../CommonInterfaces/CommonGUIHelperInterface.h"
|
||||
|
||||
|
||||
#include "SharedMemoryCommon.h"
|
||||
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
@@ -33,18 +32,22 @@ int main(int argc, char* argv[])
|
||||
DummyGUIHelper noGfx;
|
||||
|
||||
CommonExampleOptions options(&noGfx);
|
||||
CommonExampleInterface* example = 0;
|
||||
SharedMemoryCommon* example = 0;
|
||||
|
||||
if (args.CheckCmdLineFlag("client"))
|
||||
{
|
||||
example = PhysicsClientCreateFunc(options);
|
||||
example = (SharedMemoryCommon*)PhysicsClientCreateFunc(options);
|
||||
}else
|
||||
{
|
||||
example = PhysicsServerCreateFunc(options);
|
||||
example = (SharedMemoryCommon*)PhysicsServerCreateFunc(options);
|
||||
}
|
||||
|
||||
example->initPhysics();
|
||||
example->stepSimulation(1.f/60.f);
|
||||
while (!example->wantsTermination())
|
||||
{
|
||||
example->stepSimulation(1.f/60.f);
|
||||
}
|
||||
|
||||
example->exitPhysics();
|
||||
|
||||
delete example;
|
||||
|
||||
Reference in New Issue
Block a user