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 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); // initialization went fine, we return the version number of this plugin (can be queried with simGetModuleName)
00133 
00134 }
00135 
00136 VREP_DLLEXPORT void v_repEnd()
00137 { // This is called just once, at the end of V-REP
00138         WIN_AFX_MANAGE_STATE;
00139 
00140         delete objectContainer;
00141         objectContainer=NULL;
00142 
00143         unloadVrepLibrary(vrepLib); // release the library
00144 }
00145 
00146 VREP_DLLEXPORT void* v_repMessage(int message,int* auxiliaryData,void* customData,int* replyData)
00147 { // This is called quite often. Just watch out for messages/events you want to handle
00148         WIN_AFX_MANAGE_STATE;
00149 
00150         simLockInterface(1);
00151         static bool refreshDlgFlag=true;
00152 
00153         // This function should not generate any error messages:
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             // V-REP dialogs were refreshed. Maybe a good idea to refresh this plugin's dialog too
00162             refreshDlgFlag=true;
00163         }
00164 
00165 
00166         if (message==sim_message_eventcallback_menuitemselected){
00167             // A custom menu bar entry was selected..
00168         }
00169 
00170         if (message==sim_message_eventcallback_instancepass){
00171             // It is important to always correctly react to events in V-REP. This message is the most convenient way to do so:
00172 
00173                 const int flags=auxiliaryData[0];
00174                 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
00175                 const bool instanceSwitched=((flags&64)!=0);
00176 
00177                 if (instanceSwitched)
00178                 {
00179                         // React to an instance switch here!!
00180                 }
00181 
00182                 if (sceneContentChanged)
00183                 { // we actualize plugin objects for changes in the scene
00184                         objectContainer->actualizeForSceneContent();
00185                         refreshDlgFlag=true;
00186                 }
00187         }
00188 
00189         if (message==sim_message_eventcallback_moduleopen)
00190         { // A script called simOpenModule (by default the main script). Is only called during simulation.
00191                 if ( (customData==NULL)||(_stricmp(pluginName,(char*)customData)==0) ) // is the command also meant for this plugin?
00192                         objectContainer->startOfSimulation();                           // yes!
00193         }
00194 
00195         if (message==sim_message_eventcallback_modulehandle){
00196             // A script called simHandleModule (by default the main script). Is only called during simulation.
00197 
00198 
00199                 if ( (customData==NULL)||(_stricmp(pluginName,(char*)customData)==0) ) // is the command also meant for this plugin?
00200                 {
00201                         //clock_t init, final;
00202 
00203                         //init=clock();
00204                         objectContainer->handleSimulation();
00205                         //final=clock()-init;
00206                         //std::cout << (double)final / ((double)CLOCKS_PER_SEC) << std::endl;
00207                 }// yes!
00208         }
00209 
00210 
00211 
00212         if (message==sim_message_eventcallback_moduleclose){
00213             // A script called simCloseModule (by default the main script). Is only called during simulation.
00214                 if ( (customData==NULL)||(_stricmp(pluginName,(char*)customData)==0) ) // is the command also meant for this plugin?
00215                         objectContainer->endOfSimulation();                                     // yes!
00216         }
00217 
00218         if (message==sim_message_eventcallback_instanceswitch){
00219         // Here the user switched the instance. React to this message in a similar way as you would react to a full
00220         // scene content change. In this plugin example, we react to an instance switch by reacting to the
00221         // sim_message_eventcallback_instancepass message and checking the bit 6 (64) of the auxiliaryData[0]
00222         // (see here above)
00223         }
00224 
00225         if (message==sim_message_eventcallback_broadcast){
00226             // Here we have a plugin that is broadcasting data (the broadcaster will also receive this data!)
00227         }
00228 
00229         if (message==sim_message_eventcallback_scenesave){
00230             // 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)
00231         }
00232 
00233         if (message==sim_message_eventcallback_simulationabouttostart){
00234             // Simulation is about to start
00235         }
00236 
00237         if (message==sim_message_eventcallback_simulationended){
00238             // Simulation just ended
00239         }
00240 
00241         // You can add more messages to handle here
00242 
00243         if ((message==sim_message_eventcallback_guipass)&&refreshDlgFlag){
00244             // handle refresh of the plugin's dialog:
00245                 refreshDlgFlag=false;
00246         }
00247 
00248         simSetIntegerParameter(sim_intparam_error_report_mode,errorModeSaved); // restore previous settings
00249         simLockInterface(0);
00250         return(retVal);
00251 }


vrep_ros_plugin
Author(s): Riccardo Spica , Giovanni Claudio
autogenerated on Wed Sep 9 2015 18:54:51