00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "vrep_ros_plugin/v_repExtRosBridge.h"
00016 #include "vrep_ros_plugin/access.h"
00017 #include <iostream>
00018 #include "v_repLib.h"
00019
00020 #include <time.h>
00021
00022 #include "vrep_ros_plugin/GenericObjectContainer.h"
00023
00024 #include <ros/ros.h>
00025 #include <boost/bind.hpp>
00026
00027 #ifdef _WIN32
00028 #include <shlwapi.h>
00029 #define WIN_AFX_MANAGE_STATE AFX_MANAGE_STATE(AfxGetStaticModuleState())
00030 #endif
00031 #if defined (__linux) || defined (__APPLE__)
00032 #define WIN_AFX_MANAGE_STATE
00033 #endif
00034
00035 LIBRARY vrepLib;
00036
00037 static GenericObjectContainer* objectContainer = NULL;
00038
00039 VREP_DLLEXPORT unsigned char v_repStart(void* reservedPointer,int reservedInt)
00040 {
00041 WIN_AFX_MANAGE_STATE;
00042
00043
00044 char curDirAndFile[1024];
00045 #ifdef _WIN32
00046 GetModuleFileName(NULL,curDirAndFile,1023);
00047 PathRemoveFileSpec(curDirAndFile);
00048 #elif defined (__linux) || defined (__APPLE__)
00049 if (getcwd(curDirAndFile, sizeof(curDirAndFile))==NULL){
00050
00051 }
00052 #endif
00053
00054 std::string currentDirAndPath(curDirAndFile);
00055 std::string temp(currentDirAndPath);
00056
00057 #ifdef _WIN32
00058 temp+="\\v_rep.dll";
00059 #elif defined (__linux)
00060 temp+="/libv_rep.so";
00061 #elif defined (__APPLE__)
00062 temp+="/libv_rep.dylib";
00063 #endif
00064
00065 vrepLib=loadVrepLibrary(temp.c_str());
00066 if (vrepLib==NULL)
00067 {
00068 std::cout << "Error, could not find or correctly load v_rep.dll. Cannot start '" << pluginName << "' plugin.\n";
00069 return(0);
00070 }
00071 if (getVrepProcAddresses(vrepLib)==0)
00072 {
00073 std::cout << "Error, could not find all required functions in v_rep.dll. Cannot start '" << pluginName << "' plugin.\n";
00074 unloadVrepLibrary(vrepLib);
00075 return(0);
00076 }
00077
00078
00079 int vrepVer;
00080 simGetIntegerParameter(sim_intparam_program_version,&vrepVer);
00081 if (vrepVer<VREP_VERSION_MAJOR*1e4+VREP_VERSION_MINOR*1e2+VREP_VERSION_PATCH)
00082 {
00083 std::cout << "Sorry, your V-REP copy is somewhat old, V-REP" <<
00084 VREP_VERSION_MAJOR << "." << VREP_VERSION_MINOR << "." << VREP_VERSION_PATCH <<
00085 "or higher is required. Cannot start '" << pluginName << "' plugin.\n";
00086 unloadVrepLibrary(vrepLib);
00087 return(0);
00088 }
00089
00090
00091 if(!ros::isInitialized()){
00092 int argc = 0;
00093 ros::init(argc,NULL,"vrep");
00094 }
00095
00096 if(!ros::master::check())
00097 {
00098 std::cout << "ROS master is not running. Cannot start '" << pluginName << "' plugin.\n";
00099 return (0);
00100 }
00101
00102
00103
00104 simLockInterface(1);
00105
00106 objectContainer = new GenericObjectContainer();
00107
00108
00109 const int inArgsSimExtGetAllCustomData[]={1,sim_lua_arg_int};
00110 simRegisterCustomLuaFunction("simExtGetAllCustomData",
00111 "string customData=simExtGetAllCustomData(number objectHandle)",
00112 inArgsSimExtGetAllCustomData, GenericObjectContainer::simExtGetAllCustomData);
00113 const int inArgsSimExtGetCustomDataFromHeader[]={2,sim_lua_arg_int,sim_lua_arg_int};
00114 simRegisterCustomLuaFunction("simExtGetCustomDataFromHeader",
00115 "number data=simExtGetCustomDataFromHeader(number objectHandle, number dataHeader)",
00116 inArgsSimExtGetCustomDataFromHeader, GenericObjectContainer::simExtGetCustomDataFromHeader);
00117 const int inArgsSimExtSetFloatCustomDataFromHeader[]={3,sim_lua_arg_int,sim_lua_arg_int,sim_lua_arg_float};
00118 simRegisterCustomLuaFunction("simExtSetFloatCustomDataFromHeader",
00119 "number data=simExtSetFloatCustomDataFromHeader(number objectHandle, number dataHeader, number value)",
00120 inArgsSimExtSetFloatCustomDataFromHeader, GenericObjectContainer::simExtSetFloatCustomDataFromHeader);
00121 const int inArgsSimExtSetIntCustomDataFromHeader[]={3,sim_lua_arg_int,sim_lua_arg_int,sim_lua_arg_int};
00122 simRegisterCustomLuaFunction("simExtSetIntCustomDataFromHeader",
00123 "number data=simExtSetIntCustomDataFromHeader(number objectHandle, number dataHeader, number value)",
00124 inArgsSimExtSetIntCustomDataFromHeader, GenericObjectContainer::simExtSetIntCustomDataFromHeader);
00125
00126 CustomDataHeaders::registerCustomDataHeaders();
00127
00128
00129
00130
00131 simLockInterface(0);
00132 return(PLUGIN_VERSION);
00133
00134 }
00135
00136 VREP_DLLEXPORT void v_repEnd()
00137 {
00138 WIN_AFX_MANAGE_STATE;
00139
00140 delete objectContainer;
00141 objectContainer=NULL;
00142
00143 unloadVrepLibrary(vrepLib);
00144 }
00145
00146 VREP_DLLEXPORT void* v_repMessage(int message,int* auxiliaryData,void* customData,int* replyData)
00147 {
00148 WIN_AFX_MANAGE_STATE;
00149
00150 simLockInterface(1);
00151 static bool refreshDlgFlag=true;
00152
00153
00154 int errorModeSaved;
00155 simGetIntegerParameter(sim_intparam_error_report_mode,&errorModeSaved);
00156 simSetIntegerParameter(sim_intparam_error_report_mode,sim_api_errormessage_ignore);
00157
00158 void* retVal=NULL;
00159
00160 if (message==sim_message_eventcallback_refreshdialogs){
00161
00162 refreshDlgFlag=true;
00163 }
00164
00165
00166 if (message==sim_message_eventcallback_menuitemselected){
00167
00168 }
00169
00170 if (message==sim_message_eventcallback_instancepass){
00171
00172
00173 const int flags=auxiliaryData[0];
00174 const bool sceneContentChanged=((flags&(1+2+4+8+16+32+64+256))!=0);
00175 const bool instanceSwitched=((flags&64)!=0);
00176
00177 if (instanceSwitched)
00178 {
00179
00180 }
00181
00182 if (sceneContentChanged)
00183 {
00184 objectContainer->actualizeForSceneContent();
00185 refreshDlgFlag=true;
00186 }
00187 }
00188
00189 if (message==sim_message_eventcallback_moduleopen)
00190 {
00191 if ( (customData==NULL)||(_stricmp(pluginName,(char*)customData)==0) )
00192 objectContainer->startOfSimulation();
00193 }
00194
00195 if (message==sim_message_eventcallback_modulehandle){
00196
00197
00198
00199 if ( (customData==NULL)||(_stricmp(pluginName,(char*)customData)==0) )
00200 {
00201
00202
00203
00204 objectContainer->handleSimulation();
00205
00206
00207 }
00208 }
00209
00210
00211
00212 if (message==sim_message_eventcallback_moduleclose){
00213
00214 if ( (customData==NULL)||(_stricmp(pluginName,(char*)customData)==0) )
00215 objectContainer->endOfSimulation();
00216 }
00217
00218 if (message==sim_message_eventcallback_instanceswitch){
00219
00220
00221
00222
00223 }
00224
00225 if (message==sim_message_eventcallback_broadcast){
00226
00227 }
00228
00229 if (message==sim_message_eventcallback_scenesave){
00230
00231 }
00232
00233 if (message==sim_message_eventcallback_simulationabouttostart){
00234
00235 }
00236
00237 if (message==sim_message_eventcallback_simulationended){
00238
00239 }
00240
00241
00242
00243 if ((message==sim_message_eventcallback_guipass)&&refreshDlgFlag){
00244
00245 refreshDlgFlag=false;
00246 }
00247
00248 simSetIntegerParameter(sim_intparam_error_report_mode,errorModeSaved);
00249 simLockInterface(0);
00250 return(retVal);
00251 }