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 inArgsSimExtSetFloatArrayCustomDataFromHeader[]={3,sim_lua_arg_int,sim_lua_arg_int,sim_lua_arg_float|sim_lua_arg_table};
00122 simRegisterCustomLuaFunction("simExtSetFloatArrayCustomDataFromHeader",
00123 "number data=simExtSetFloatArrayCustomDataFromHeader(number objectHandle, number dataHeader, table value)",
00124 inArgsSimExtSetFloatArrayCustomDataFromHeader, GenericObjectContainer::simExtSetFloatArrayCustomDataFromHeader);
00125 const int inArgsSimExtSetIntCustomDataFromHeader[]={3,sim_lua_arg_int,sim_lua_arg_int,sim_lua_arg_int};
00126 simRegisterCustomLuaFunction("simExtSetIntCustomDataFromHeader",
00127 "number data=simExtSetIntCustomDataFromHeader(number objectHandle, number dataHeader, number value)",
00128 inArgsSimExtSetIntCustomDataFromHeader, GenericObjectContainer::simExtSetIntCustomDataFromHeader);
00129
00130 CustomDataHeaders::registerCustomDataHeaders();
00131
00132
00133
00134
00135 simLockInterface(0);
00136 return(PLUGIN_VERSION);
00137
00138 }
00139
00140 VREP_DLLEXPORT void v_repEnd()
00141 {
00142 WIN_AFX_MANAGE_STATE;
00143
00144 delete objectContainer;
00145 objectContainer=NULL;
00146
00147 unloadVrepLibrary(vrepLib);
00148 }
00149
00150 VREP_DLLEXPORT void* v_repMessage(int message,int* auxiliaryData,void* customData,int* replyData)
00151 {
00152 WIN_AFX_MANAGE_STATE;
00153
00154 simLockInterface(1);
00155 static bool refreshDlgFlag=true;
00156
00157
00158 int errorModeSaved;
00159 simGetIntegerParameter(sim_intparam_error_report_mode,&errorModeSaved);
00160 simSetIntegerParameter(sim_intparam_error_report_mode,sim_api_errormessage_ignore);
00161
00162 void* retVal=NULL;
00163
00164 if (message==sim_message_eventcallback_refreshdialogs){
00165
00166 refreshDlgFlag=true;
00167 }
00168
00169
00170 if (message==sim_message_eventcallback_menuitemselected){
00171
00172 }
00173
00174 if (message==sim_message_eventcallback_instancepass){
00175
00176
00177 const int flags=auxiliaryData[0];
00178 const bool sceneContentChanged=((flags&(1+2+4+8+16+32+64+256))!=0);
00179 const bool instanceSwitched=((flags&64)!=0);
00180
00181 if (instanceSwitched)
00182 {
00183
00184 }
00185
00186 if (sceneContentChanged)
00187 {
00188 objectContainer->actualizeForSceneContent();
00189 refreshDlgFlag=true;
00190 }
00191 }
00192
00193 if (message==sim_message_eventcallback_moduleopen)
00194 {
00195 if ( (customData==NULL)||(_stricmp(pluginName,(char*)customData)==0) )
00196 objectContainer->startOfSimulation();
00197 }
00198
00199 if (message==sim_message_eventcallback_modulehandle){
00200
00201
00202
00203 if ( (customData==NULL)||(_stricmp(pluginName,(char*)customData)==0) )
00204 {
00205
00206
00207
00208 objectContainer->handleSimulation();
00209
00210
00211 }
00212 }
00213
00214
00215
00216 if (message==sim_message_eventcallback_moduleclose){
00217
00218 if ( (customData==NULL)||(_stricmp(pluginName,(char*)customData)==0) )
00219 objectContainer->endOfSimulation();
00220 }
00221
00222 if (message==sim_message_eventcallback_instanceswitch){
00223
00224
00225
00226
00227 }
00228
00229 if (message==sim_message_eventcallback_broadcast){
00230
00231 }
00232
00233 if (message==sim_message_eventcallback_scenesave){
00234
00235 }
00236
00237 if (message==sim_message_eventcallback_simulationabouttostart){
00238
00239 }
00240
00241 if (message==sim_message_eventcallback_simulationended){
00242
00243 }
00244
00245
00246
00247 if ((message==sim_message_eventcallback_guipass)&&refreshDlgFlag){
00248
00249 refreshDlgFlag=false;
00250 }
00251
00252 simSetIntegerParameter(sim_intparam_error_report_mode,errorModeSaved);
00253 simLockInterface(0);
00254 return(retVal);
00255 }