v_repExtRosBridge.cpp
Go to the documentation of this file.
00001 // Copyright 2006-2013 Dr. Marc Andreas Freese. All rights reserved. 
00002 // marc@coppeliarobotics.com
00003 // www.coppeliarobotics.com
00004 // 
00005 // -------------------------------------------------------------------
00006 // This file is distributed in the hope that it will be useful,
00007 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00008 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00009 // 
00010 // You are free to use/modify/distribute this file for whatever purpose!
00011 // -------------------------------------------------------------------
00012 //
00013 // This file was automatically created for V-REP release V3.0.5 on October 26th 2013
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> // required for PathRemoveFileSpec function
00029         #define WIN_AFX_MANAGE_STATE AFX_MANAGE_STATE(AfxGetStaticModuleState())
00030 #endif /* _WIN32 */
00031 #if defined (__linux) || defined (__APPLE__)
00032         #define WIN_AFX_MANAGE_STATE
00033 #endif /* __linux || __APPLE__ */
00034 
00035 LIBRARY vrepLib;
00036 
00037 static GenericObjectContainer* objectContainer = NULL;
00038 
00039 VREP_DLLEXPORT unsigned char v_repStart(void* reservedPointer,int reservedInt)
00040 { // This is called just once, at the start of V-REP.
00041         WIN_AFX_MANAGE_STATE;
00042 
00043         // Dynamically load and bind V-REP functions:
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             // output some error if desired
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 /* __linux || __APPLE__ */
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); // Means error, V-REP will unload this plugin
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); // Means error, V-REP will unload this plugin
00076         }
00077 
00078         // Check the V-REP version:
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); // Means error, V-REP will unload this plugin
00088         }
00089 
00090         //Initialize ROS
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         // Do the normal plugin initialization:
00104         simLockInterface(1);
00105 
00106     objectContainer = new GenericObjectContainer();
00107 
00108         // Register lua collbacks
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); // initialization went fine, we return the version number of this plugin (can be queried with simGetModuleName)
00137 
00138 }
00139 
00140 VREP_DLLEXPORT void v_repEnd()
00141 { // This is called just once, at the end of V-REP
00142         WIN_AFX_MANAGE_STATE;
00143 
00144         delete objectContainer;
00145         objectContainer=NULL;
00146 
00147         unloadVrepLibrary(vrepLib); // release the library
00148 }
00149 
00150 VREP_DLLEXPORT void* v_repMessage(int message,int* auxiliaryData,void* customData,int* replyData)
00151 { // This is called quite often. Just watch out for messages/events you want to handle
00152         WIN_AFX_MANAGE_STATE;
00153 
00154         simLockInterface(1);
00155         static bool refreshDlgFlag=true;
00156 
00157         // This function should not generate any error messages:
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             // V-REP dialogs were refreshed. Maybe a good idea to refresh this plugin's dialog too
00166             refreshDlgFlag=true;
00167         }
00168 
00169 
00170         if (message==sim_message_eventcallback_menuitemselected){
00171             // A custom menu bar entry was selected..
00172         }
00173 
00174         if (message==sim_message_eventcallback_instancepass){
00175             // It is important to always correctly react to events in V-REP. This message is the most convenient way to do so:
00176 
00177                 const int flags=auxiliaryData[0];
00178                 const bool sceneContentChanged=((flags&(1+2+4+8+16+32+64+256))!=0); // object erased, created, model or scene loaded, und/redo called, instance switched, or object scaled since last sim_message_eventcallback_instancepass message
00179                 const bool instanceSwitched=((flags&64)!=0);
00180 
00181                 if (instanceSwitched)
00182                 {
00183                         // React to an instance switch here!!
00184                 }
00185 
00186                 if (sceneContentChanged)
00187                 { // we actualize plugin objects for changes in the scene
00188                         objectContainer->actualizeForSceneContent();
00189                         refreshDlgFlag=true;
00190                 }
00191         }
00192 
00193         if (message==sim_message_eventcallback_moduleopen)
00194         { // A script called simOpenModule (by default the main script). Is only called during simulation.
00195                 if ( (customData==NULL)||(_stricmp(pluginName,(char*)customData)==0) ) // is the command also meant for this plugin?
00196                         objectContainer->startOfSimulation();                           // yes!
00197         }
00198 
00199         if (message==sim_message_eventcallback_modulehandle){
00200             // A script called simHandleModule (by default the main script). Is only called during simulation.
00201 
00202 
00203                 if ( (customData==NULL)||(_stricmp(pluginName,(char*)customData)==0) ) // is the command also meant for this plugin?
00204                 {
00205                         //clock_t init, final;
00206 
00207                         //init=clock();
00208                         objectContainer->handleSimulation();
00209                         //final=clock()-init;
00210                         //std::cout << (double)final / ((double)CLOCKS_PER_SEC) << std::endl;
00211                 }// yes!
00212         }
00213 
00214 
00215 
00216         if (message==sim_message_eventcallback_moduleclose){
00217             // A script called simCloseModule (by default the main script). Is only called during simulation.
00218                 if ( (customData==NULL)||(_stricmp(pluginName,(char*)customData)==0) ) // is the command also meant for this plugin?
00219                         objectContainer->endOfSimulation();                                     // yes!
00220         }
00221 
00222         if (message==sim_message_eventcallback_instanceswitch){
00223         // Here the user switched the instance. React to this message in a similar way as you would react to a full
00224         // scene content change. In this plugin example, we react to an instance switch by reacting to the
00225         // sim_message_eventcallback_instancepass message and checking the bit 6 (64) of the auxiliaryData[0]
00226         // (see here above)
00227         }
00228 
00229         if (message==sim_message_eventcallback_broadcast){
00230             // Here we have a plugin that is broadcasting data (the broadcaster will also receive this data!)
00231         }
00232 
00233         if (message==sim_message_eventcallback_scenesave){
00234             // The scene is about to be saved. If required do some processing here (e.g. add custom scene data to be serialized with the scene)
00235         }
00236 
00237         if (message==sim_message_eventcallback_simulationabouttostart){
00238             // Simulation is about to start
00239         }
00240 
00241         if (message==sim_message_eventcallback_simulationended){
00242             // Simulation just ended
00243         }
00244 
00245         // You can add more messages to handle here
00246 
00247         if ((message==sim_message_eventcallback_guipass)&&refreshDlgFlag){
00248             // handle refresh of the plugin's dialog:
00249                 refreshDlgFlag=false;
00250         }
00251 
00252         simSetIntegerParameter(sim_intparam_error_report_mode,errorModeSaved); // restore previous settings
00253         simLockInterface(0);
00254         return(retVal);
00255 }


vrep_ros_plugin
Author(s): Riccardo Spica , Giovanni Claudio
autogenerated on Sat Jun 8 2019 20:22:41