From 812c67e221b3f0c8f6cf976c760b2872f833beca Mon Sep 17 00:00:00 2001 From: Erwin Coumans Date: Wed, 22 Jul 2015 18:06:05 -0700 Subject: [PATCH 1/2] fix various warnings, more work on shared memory API --- examples/SharedMemory/PhysicsClientC_API.cpp | 48 ++++++++++++++++++++ examples/SharedMemory/PhysicsClientC_API.h | 35 ++++++++++++++ examples/SharedMemory/SharedMemoryBlock.h | 48 ++++++++++++++++++++ test/SharedMemory/test.c | 11 +++++ 4 files changed, 142 insertions(+) create mode 100644 examples/SharedMemory/PhysicsClientC_API.cpp create mode 100644 examples/SharedMemory/PhysicsClientC_API.h create mode 100644 examples/SharedMemory/SharedMemoryBlock.h create mode 100644 test/SharedMemory/test.c diff --git a/examples/SharedMemory/PhysicsClientC_API.cpp b/examples/SharedMemory/PhysicsClientC_API.cpp new file mode 100644 index 000000000..4b5c20a9a --- /dev/null +++ b/examples/SharedMemory/PhysicsClientC_API.cpp @@ -0,0 +1,48 @@ +#include "PhysicsClientC_API.h" +#include "PhysicsClient.h" +#include "Bullet3Common/b3Scalar.h" + +int b3InitPhysicsParamCommand(struct SharedMemoryCommand* command) +{ + b3Assert(command); + command->m_type = CMD_SEND_PHYSICS_SIMULATION_PARAMETERS; + command->m_updateFlags = 0; + + return 0; +} +int b3PhysicsParamSetGravity(struct SharedMemoryCommand* command, double gravx,double gravy, double gravz) +{ + b3Assert(command->m_type == CMD_SEND_PHYSICS_SIMULATION_PARAMETERS); + command->m_physSimParamArgs.m_gravityAcceleration[0] = gravx; + command->m_physSimParamArgs.m_gravityAcceleration[1] = gravy; + command->m_physSimParamArgs.m_gravityAcceleration[2] = gravz; + command->m_updateFlags |= SIM_PARAM_UPDATE_GRAVITY; + return 0; +} + +#if 0 + +#include "SharedMemoryBlock.h" + +#define B3_DECLARE_HANDLE(name) typedef struct name##__ { int unused; } *name + +B3_DECLARE_HANDLE(b3PhysicsClientHandle); + +b3PhysicsClientHandle b3ConnectSharedMemory(int memKey, int allowSharedMemoryInitialization); + +void b3DisconnectSharedMemory(b3PhysicsClientHandle physClient); + +int b3ProcessServerStatus(b3PhysicsClientHandle physClient, struct SharedMemoryStatus* status); + +int b3CanSubmitCommand(b3PhysicsClientHandle physClient); + +int b3SubmitClientCommand(b3PhysicsClientHandle physClient, struct SharedMemoryCommand* command); + +int b3GetNumPoweredJoints(b3PhysicsClientHandle physClient); + +void b3GetPoweredJointInfo(int linkIndex, struct PoweredJointInfo* info); + +int b3InitPhysicsParamCommand(struct SharedMemoryCommand* command); +int b3PhysicsParamSetGravity(struct SharedMemoryCommand* command, double gravx,double gravy, double gravz); + +#endif diff --git a/examples/SharedMemory/PhysicsClientC_API.h b/examples/SharedMemory/PhysicsClientC_API.h new file mode 100644 index 000000000..de90abe04 --- /dev/null +++ b/examples/SharedMemory/PhysicsClientC_API.h @@ -0,0 +1,35 @@ +#ifndef PHYSICS_CLIENT_C_API_H +#define PHYSICS_CLIENT_C_API_H + +#include "SharedMemoryBlock.h" + +#define B3_DECLARE_HANDLE(name) typedef struct name##__ { int unused; } *name + +B3_DECLARE_HANDLE(b3PhysicsClientHandle); + +#ifdef __cplusplus +extern "C" { +#endif + +b3PhysicsClientHandle b3ConnectSharedMemory(int memKey, int allowSharedMemoryInitialization); + +void b3DisconnectSharedMemory(b3PhysicsClientHandle physClient); + +int b3ProcessServerStatus(b3PhysicsClientHandle physClient, struct SharedMemoryStatus* status); + +int b3CanSubmitCommand(b3PhysicsClientHandle physClient); + +int b3SubmitClientCommand(b3PhysicsClientHandle physClient, struct SharedMemoryCommand* command); + +int b3GetNumPoweredJoints(b3PhysicsClientHandle physClient); + +void b3GetPoweredJointInfo(int linkIndex, struct PoweredJointInfo* info); + +int b3InitPhysicsParamCommand(struct SharedMemoryCommand* command); +int b3PhysicsParamSetGravity(struct SharedMemoryCommand* command, double gravx,double gravy, double gravz); + +#ifdef __cplusplus +} +#endif + +#endif //PHYSICS_CLIENT_C_API_H diff --git a/examples/SharedMemory/SharedMemoryBlock.h b/examples/SharedMemory/SharedMemoryBlock.h new file mode 100644 index 000000000..b3d2709d0 --- /dev/null +++ b/examples/SharedMemory/SharedMemoryBlock.h @@ -0,0 +1,48 @@ +#ifndef SHARED_MEMORY_BLOCK_H +#define SHARED_MEMORY_BLOCK_H + +#define SHARED_MEMORY_KEY 12347 +#define SHARED_MEMORY_MAGIC_NUMBER 64738 +#define SHARED_MEMORY_MAX_COMMANDS 32 +#define SHARED_MEMORY_MAX_STREAM_CHUNK_SIZE (256*1024) + +#include "SharedMemoryCommands.h" + +struct SharedMemoryBlock +{ + int m_magicId; + struct SharedMemoryCommand m_clientCommands[SHARED_MEMORY_MAX_COMMANDS]; + struct SharedMemoryStatus m_serverCommands[SHARED_MEMORY_MAX_COMMANDS]; + + int m_numClientCommands; + int m_numProcessedClientCommands; + + int m_numServerCommands; + int m_numProcessedServerCommands; + + //m_bulletStreamDataClientToServer is a way for the client to create collision shapes, rigid bodies and constraints + //the Bullet data structures are more general purpose than the capabilities of a URDF file. + char m_bulletStreamDataClientToServer[SHARED_MEMORY_MAX_STREAM_CHUNK_SIZE]; + + //m_bulletStreamDataServerToClient is used to send (debug) data from server to client, for + //example to provide all details of a multibody including joint/link names, after loading a URDF file. + char m_bulletStreamDataServerToClient[SHARED_MEMORY_MAX_STREAM_CHUNK_SIZE]; +}; + + +inline void InitSharedMemoryBlock(struct SharedMemoryBlock* sharedMemoryBlock) +{ + sharedMemoryBlock->m_numClientCommands = 0; + sharedMemoryBlock->m_numServerCommands = 0; + sharedMemoryBlock->m_numProcessedClientCommands=0; + sharedMemoryBlock->m_numProcessedServerCommands=0; + sharedMemoryBlock->m_magicId = SHARED_MEMORY_MAGIC_NUMBER; +} + +#define SHARED_MEMORY_SIZE sizeof(SharedMemoryBlock) + + + + +#endif //SHARED_MEMORY_BLOCK_H + diff --git a/test/SharedMemory/test.c b/test/SharedMemory/test.c new file mode 100644 index 000000000..894fe6ef0 --- /dev/null +++ b/test/SharedMemory/test.c @@ -0,0 +1,11 @@ +#include "SharedMemoryBlock.h" +#include "PhysicsClientC_API.h" + +struct test +{ +}; +#include +int main(int argc, char* argv[]) +{ +printf("hello world\n"); +} From 0fe6d343bcab964d9fe0c4e7e7196664a148ed43 Mon Sep 17 00:00:00 2001 From: erwin coumans Date: Thu, 23 Jul 2015 10:51:09 -0700 Subject: [PATCH 2/2] move m_updateFlags to command, and make it 64bit --- examples/SharedMemory/PhysicsServer.cpp | 2 +- examples/SharedMemory/RobotControlExample.cpp | 2 +- examples/SharedMemory/SharedMemoryCommands.h | 9 ++++----- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/examples/SharedMemory/PhysicsServer.cpp b/examples/SharedMemory/PhysicsServer.cpp index 7f6c77bb9..7614a7632 100644 --- a/examples/SharedMemory/PhysicsServer.cpp +++ b/examples/SharedMemory/PhysicsServer.cpp @@ -563,7 +563,7 @@ void PhysicsServerSharedMemory::processClientCommands() case CMD_SEND_PHYSICS_SIMULATION_PARAMETERS: { - if (clientCmd.m_physSimParamArgs.m_updateFlags&SIM_PARAM_UPDATE_GRAVITY) + if (clientCmd.m_updateFlags&SIM_PARAM_UPDATE_GRAVITY) { btVector3 grav(clientCmd.m_physSimParamArgs.m_gravityAcceleration[0], clientCmd.m_physSimParamArgs.m_gravityAcceleration[1], diff --git a/examples/SharedMemory/RobotControlExample.cpp b/examples/SharedMemory/RobotControlExample.cpp index 73d757816..418972fba 100644 --- a/examples/SharedMemory/RobotControlExample.cpp +++ b/examples/SharedMemory/RobotControlExample.cpp @@ -104,7 +104,7 @@ void MyCallback2(int buttonId, bool buttonState, void* userPtr) command.m_physSimParamArgs.m_gravityAcceleration[0] = 0; command.m_physSimParamArgs.m_gravityAcceleration[1] = 0; command.m_physSimParamArgs.m_gravityAcceleration[2] = -10; - command.m_physSimParamArgs.m_updateFlags = SIM_PARAM_UPDATE_GRAVITY; + command.m_updateFlags = SIM_PARAM_UPDATE_GRAVITY; cl->enqueueCommand(command); break; diff --git a/examples/SharedMemory/SharedMemoryCommands.h b/examples/SharedMemory/SharedMemoryCommands.h index a2921e69c..2c7dd04e4 100644 --- a/examples/SharedMemory/SharedMemoryCommands.h +++ b/examples/SharedMemory/SharedMemoryCommands.h @@ -132,9 +132,6 @@ enum EnumUpdateFlags ///The control mode determines the state variables used for motor control. struct SendPhysicsSimulationParameters { - //a bit fields to tell which parameters need updating, see SIM_PARAM_UPDATE_DELTA_TIME etc. - //for example m_updateFlags = SIM_PARAM_UPDATE_DELTA_TIME | SIM_PARAM_UPDATE_NUM_SOLVER_ITERATIONS; - int m_updateFlags; double m_deltaTime; double m_gravityAcceleration[3]; @@ -169,9 +166,11 @@ struct SendActualStateArgs struct SharedMemoryCommand { int m_type; - - smUint64_t m_timeStamp; int m_sequenceNumber; + smUint64_t m_timeStamp; + //a bit fields to tell which parameters need updating + //for example m_updateFlags = SIM_PARAM_UPDATE_DELTA_TIME | SIM_PARAM_UPDATE_NUM_SOLVER_ITERATIONS; + smUint64_t m_updateFlags; union {