add rudimentary command logging for shared memory physics server

This commit is contained in:
erwin coumans
2015-10-30 10:30:48 -07:00
parent 819c4f1951
commit c68c215ead
8 changed files with 146 additions and 4 deletions

View File

@@ -215,6 +215,11 @@ static ExampleEntry gDefaultExamples[]=
ExampleEntry(1,"Physics Server", "Create a physics server that communicates with a physics client over shared memory", ExampleEntry(1,"Physics Server", "Create a physics server that communicates with a physics client over shared memory",
PhysicsServerCreateFunc), PhysicsServerCreateFunc),
ExampleEntry(1,"Physics Server (Logging)", "Create a physics server that communicates with a physics client over shared memory. It will log all commands to a file.",
PhysicsServerCreateFunc,PHYSICS_SERVER_ENABLE_COMMAND_LOGGING),
ExampleEntry(1,"Physics Server (Replay Log)", "Create a physics server that replay a command log from disk.",
PhysicsServerCreateFunc,PHYSICS_SERVER_REPLAY_FROM_COMMAND_LOG),
ExampleEntry(1, "Physics Client", "Create a physics client that can communicate with a physics server over shared memory", PhysicsClientCreateFunc), ExampleEntry(1, "Physics Client", "Create a physics client that can communicate with a physics server over shared memory", PhysicsClientCreateFunc),
#ifdef ENABLE_LUA #ifdef ENABLE_LUA
ExampleEntry(1,"Lua Script", "Create the dynamics world, collision shapes and rigid bodies using Lua scripting", ExampleEntry(1,"Lua Script", "Create the dynamics world, collision shapes and rigid bodies using Lua scripting",

View File

@@ -71,6 +71,7 @@ extern bool useShadowMap;
static bool visualWireframe=false; static bool visualWireframe=false;
static bool renderVisualGeometry=true; static bool renderVisualGeometry=true;
static bool renderGrid = true; static bool renderGrid = true;
static bool renderGui = true;
static bool enable_experimental_opencl = false; static bool enable_experimental_opencl = false;
int gDebugDrawFlags = 0; int gDebugDrawFlags = 0;
@@ -170,6 +171,7 @@ void MyKeyboardCallback(int key, int state)
if (key=='g' && state) if (key=='g' && state)
{ {
renderGrid = !renderGrid; renderGrid = !renderGrid;
renderGui = !renderGui;
} }
@@ -1041,7 +1043,7 @@ void OpenGLExampleBrowser::update(float deltaTime)
} }
static int toggle = 1; static int toggle = 1;
if (renderGrid) if (renderGui)
{ {
if (!pauseSimulation) if (!pauseSimulation)
processProfileData(s_profWindow,false); processProfileData(s_profWindow,false);

View File

@@ -108,18 +108,82 @@ struct InternalBodyHandle : public InteralBodyData
return m_nextFreeHandle; return m_nextFreeHandle;
} }
}; };
class btCommandChunk
{
public:
int m_chunkCode;
int m_length;
void *m_oldPtr;
int m_dna_nr;
int m_number;
};
struct CommandLogger struct CommandLogger
{ {
FILE* m_file; FILE* m_file;
void writeHeader(unsigned char* buffer) const
{
#ifdef BT_USE_DOUBLE_PRECISION
memcpy(buffer, "BT3CMDd", 7);
#else
memcpy(buffer, "BT3CMDf", 7);
#endif //BT_USE_DOUBLE_PRECISION
int littleEndian= 1;
littleEndian= ((char*)&littleEndian)[0];
if (sizeof(void*)==8)
{
buffer[7] = '-';
} else
{
buffer[7] = '_';
}
if (littleEndian)
{
buffer[8]='v';
} else
{
buffer[8]='V';
}
buffer[9] = 0;
buffer[10] = 0;
buffer[11] = 0;
int ver = btGetVersion();
if (ver>=0 && ver<999)
{
sprintf((char*)&buffer[9],"%d",ver);
}
}
void logCommand(SharedMemoryBlock* testBlock1) void logCommand(SharedMemoryBlock* testBlock1)
{ {
//fwrite(buf,buffSize+sizeof(int),1,m_file); btCommandChunk chunk;
chunk.m_chunkCode = testBlock1->m_clientCommands[0].m_type;
chunk.m_oldPtr = 0;
chunk.m_dna_nr = 0;
chunk.m_length = sizeof(SharedMemoryCommand);
chunk.m_number = 1;
fwrite((const char*)&chunk,sizeof(btCommandChunk), 1,m_file);
fwrite((const char*)&testBlock1->m_clientCommands[0],sizeof(SharedMemoryCommand),1,m_file);
} }
CommandLogger(const char* fileName) CommandLogger(const char* fileName)
{ {
m_file = fopen(fileName,"wb"); m_file = fopen(fileName,"wb");
unsigned char buf[15];
buf[12] = 12;
buf[13] = 13;
buf[14] = 14;
writeHeader(buf);
fwrite(buf,12,1,m_file);
} }
virtual ~CommandLogger() virtual ~CommandLogger()
{ {
@@ -127,6 +191,41 @@ struct CommandLogger
} }
}; };
struct CommandLogPlayback
{
unsigned char* m_header[12];
FILE* m_file;
CommandLogPlayback(const char* fileName)
{
m_file = fopen(fileName,"rb");
if (m_file)
{
fread(m_header,12,1,m_file);
}
}
virtual ~CommandLogPlayback()
{
if (m_file)
{
fclose(m_file);
m_file=0;
}
}
bool processNextCommand(SharedMemoryCommand* cmd)
{
btCommandChunk chunk;
size_t s = fread((void*)&chunk,sizeof(btCommandChunk),1,m_file);
if (s==1)
{
s = fread(cmd,sizeof(SharedMemoryCommand),1,m_file);
return (s==1);
}
return false;
}
};
struct PhysicsServerInternalData struct PhysicsServerInternalData
{ {
///handle management ///handle management
@@ -219,6 +318,8 @@ struct PhysicsServerInternalData
SharedMemoryInterface* m_sharedMemory; SharedMemoryInterface* m_sharedMemory;
SharedMemoryBlock* m_testBlock1; SharedMemoryBlock* m_testBlock1;
CommandLogger* m_commandLogger; CommandLogger* m_commandLogger;
CommandLogPlayback* m_logPlayback;
bool m_isConnected; bool m_isConnected;
btScalar m_physicsDeltaTime; btScalar m_physicsDeltaTime;
btAlignedObjectArray<btMultiBodyJointFeedback*> m_multiBodyJointFeedbacks; btAlignedObjectArray<btMultiBodyJointFeedback*> m_multiBodyJointFeedbacks;
@@ -260,6 +361,7 @@ struct PhysicsServerInternalData
:m_sharedMemory(0), :m_sharedMemory(0),
m_testBlock1(0), m_testBlock1(0),
m_commandLogger(0), m_commandLogger(0),
m_logPlayback(0),
m_isConnected(false), m_isConnected(false),
m_physicsDeltaTime(1./240.), m_physicsDeltaTime(1./240.),
m_dynamicsWorld(0), m_dynamicsWorld(0),
@@ -756,6 +858,19 @@ void PhysicsServerSharedMemory::processClientCommands()
{ {
if (m_data->m_isConnected && m_data->m_testBlock1) if (m_data->m_isConnected && m_data->m_testBlock1)
{ {
if (m_data->m_logPlayback)
{
if (m_data->m_testBlock1->m_numServerCommands>m_data->m_testBlock1->m_numProcessedServerCommands)
{
m_data->m_testBlock1->m_numProcessedServerCommands++;
}
//push a command from log file
bool hasCommand = m_data->m_logPlayback->processNextCommand(&m_data->m_testBlock1->m_clientCommands[0]);
if (hasCommand)
{
m_data->m_testBlock1->m_numClientCommands++;
}
}
///we ignore overflow of integer for now ///we ignore overflow of integer for now
if (m_data->m_testBlock1->m_numClientCommands> m_data->m_testBlock1->m_numProcessedClientCommands) if (m_data->m_testBlock1->m_numClientCommands> m_data->m_testBlock1->m_numProcessedClientCommands)
{ {
@@ -1738,3 +1853,9 @@ void PhysicsServerSharedMemory::enableCommandLogging(bool enable, const char* fi
} }
} }
} }
void PhysicsServerSharedMemory::replayFromLogFile(const char* fileName)
{
CommandLogPlayback* pb = new CommandLogPlayback(fileName);
m_data->m_logPlayback = pb;
}

View File

@@ -55,6 +55,8 @@ public:
void renderScene(); void renderScene();
void enableCommandLogging(bool enable, const char* fileName); void enableCommandLogging(bool enable, const char* fileName);
void replayFromLogFile(const char* fileName);
}; };

View File

@@ -34,6 +34,13 @@ public:
m_physicsServer.enableCommandLogging(true,"BulletPhysicsCommandLog.bin"); m_physicsServer.enableCommandLogging(true,"BulletPhysicsCommandLog.bin");
} }
void replayFromLogFile()
{
m_physicsServer.replayFromLogFile("BulletPhysicsCommandLog.bin");
}
virtual void resetCamera() virtual void resetCamera()
{ {
float dist = 5; float dist = 5;
@@ -273,6 +280,10 @@ class CommonExampleInterface* PhysicsServerCreateFunc(struct CommonExampleOpt
{ {
example->enableCommandLogging(); example->enableCommandLogging();
} }
if (options.m_option & PHYSICS_SERVER_REPLAY_FROM_COMMAND_LOG)
{
example->replayFromLogFile();
}
return example; return example;
} }

View File

@@ -4,6 +4,7 @@
enum PhysicsServerOptions enum PhysicsServerOptions
{ {
PHYSICS_SERVER_ENABLE_COMMAND_LOGGING=1, PHYSICS_SERVER_ENABLE_COMMAND_LOGGING=1,
PHYSICS_SERVER_REPLAY_FROM_COMMAND_LOG=2,
}; };
class CommonExampleInterface* PhysicsServerCreateFunc(struct CommonExampleOptions& options); class CommonExampleInterface* PhysicsServerCreateFunc(struct CommonExampleOptions& options);

View File

@@ -2,7 +2,7 @@
#define SHARED_MEMORY_BLOCK_H #define SHARED_MEMORY_BLOCK_H
#define SHARED_MEMORY_MAGIC_NUMBER 64738 #define SHARED_MEMORY_MAGIC_NUMBER 64738
#define SHARED_MEMORY_MAX_COMMANDS 32 #define SHARED_MEMORY_MAX_COMMANDS 4
#define SHARED_MEMORY_MAX_STREAM_CHUNK_SIZE (256*1024) #define SHARED_MEMORY_MAX_STREAM_CHUNK_SIZE (256*1024)
#include "SharedMemoryCommands.h" #include "SharedMemoryCommands.h"

View File

@@ -26,7 +26,7 @@
#define SHARED_MEMORY_SERVER_TEST_C #define SHARED_MEMORY_SERVER_TEST_C
#define MAX_DEGREE_OF_FREEDOM 256 #define MAX_DEGREE_OF_FREEDOM 64
#define MAX_NUM_SENSORS 256 #define MAX_NUM_SENSORS 256
#define MAX_URDF_FILENAME_LENGTH 1024 #define MAX_URDF_FILENAME_LENGTH 1024
#define MAX_FILENAME_LENGTH MAX_URDF_FILENAME_LENGTH #define MAX_FILENAME_LENGTH MAX_URDF_FILENAME_LENGTH