v_repExtGrlCisstInverseKinematics/v_repExtGrlInverseKinematics.cpp
Go to the documentation of this file.
1 // Copyright 2006-2014 Coppelia Robotics GmbH. All rights reserved.
2 // marc@coppeliarobotics.com
3 // www.coppeliarobotics.com
4 //
5 // -------------------------------------------------------------------
6 // THIS FILE IS DISTRIBUTED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
7 // WARRANTY. THE USER WILL USE IT AT HIS/HER OWN RISK. THE ORIGINAL
8 // AUTHORS AND COPPELIA ROBOTICS GMBH WILL NOT BE LIABLE FOR DATA LOSS,
9 // DAMAGES, LOSS OF PROFITS OR ANY OTHER KIND OF LOSS WHILE USING OR
10 // MISUSING THIS SOFTWARE.
11 //
12 // You are free to use/modify/distribute this file for whatever purpose!
13 // -------------------------------------------------------------------
14 //
15 // This file was automatically created for V-REP release V3.2.0 on Feb. 3rd 2015
16 
17 #include <memory>
18 #include <boost/log/trivial.hpp>
19 #include <boost/exception/diagnostic_information.hpp>
22 
23 #include "v_repLib.h"
24 
25 #ifdef _WIN32
26  #include <shlwapi.h>
27  #pragma comment(lib, "Shlwapi.lib")
28 #endif /* _WIN32 */
29 
30 #if defined (__linux) || defined (__APPLE__)
31  #include <unistd.h>
32  #include <string.h>
33  #define _stricmp(x,y) strcasecmp(x,y)
34 #endif
35 
36 #define PLUGIN_VERSION 1
37 
38 LIBRARY vrepLib; // the V-REP library that we will dynamically load and bind
39 
40 
41 #define CONCAT(x,y,z) x y z
42 #define strConCat(x,y,z) CONCAT(x,y,z)
43 #define LUA_GET_SENSOR_DATA_COMMAND "simExtSkeleton_getSensorData"
44 
45 
46 
47 std::shared_ptr<grl::VrepInverseKinematicsController> InverseKinematicsControllerPG;
48 
49 /*int getPathPosVectorFromObjectPose(int objectHandle, float relativeDistance) //This might be helpful if we decide to implement all the code in the plugin (instead of the lua script
50 {
51  float positionOnPath[3];
52  float eulerAngles[3];
53 
54  simGetPositionOnPath(objectHandle, relativeDistance, positionOnPath);
55  simGetOrientationOnPath(objectHandle, eulerAngles);
56  int o = getQuaternionFromEulerAngle(positionOnPath, eulerAngles);
57  return (p,o);
58 }
59 */
60 
62 {
64 
65  BOOST_LOG_TRIVIAL(info) << "v_repExtInverseKinematicsController Starting Inverse Kinematics Plugin\n";
66  InverseKinematicsControllerPG=std::make_shared<grl::VrepInverseKinematicsController>();
67  InverseKinematicsControllerPG->construct();
68  }
69 }
70 
72 {
73  BOOST_LOG_TRIVIAL(info) << "v_repExtInverseKinematicsController Starting Inverse Kinematics Plugin Data Collection\n";
74  InverseKinematicsControllerPG=std::make_shared<grl::VrepInverseKinematicsController>();
75  InverseKinematicsControllerPG->construct();
76 }
77 
79 {
80 
81  BOOST_LOG_TRIVIAL(info) << "Ending v_repExtInverseKinematicsController plugin\n";
83 }
84 
86 {
88  //InverseKinematicsControllerPG->addFrame();
89  }
90 }
91 
93 {
95  //InverseKinematicsControllerPG->estimateHandEyeScrew();
96  }
97 }
98 
99 
101 {
103  //InverseKinematicsControllerPG->applyEstimate();
104  }
105 }
106 
107 
109 {
111  //InverseKinematicsControllerPG->restoreSensorPosition();
112  }
113 }
114 
115 /// @todo implement and connect up this function
116 /// Returns the current transform estimate in a format that vrep understands
118 {
120  }
121 }
122 
123 
124 
125 // This is the plugin start routine (called just once, just after the plugin was loaded):
126 VREP_DLLEXPORT unsigned char v_repStart(void* reservedPointer,int reservedInt)
127 {
128  // Dynamically load and bind V-REP functions:
129  // ******************************************
130  // 1. Figure out this plugin's directory:
131  char curDirAndFile[1024];
132 #ifdef _WIN32
133  GetModuleFileName(NULL,curDirAndFile,1023);
134  PathRemoveFileSpec(curDirAndFile);
135 #elif defined (__linux) || defined (__APPLE__)
136  getcwd(curDirAndFile, sizeof(curDirAndFile));
137 #endif
138  std::string currentDirAndPath(curDirAndFile);
139  // 2. Append the V-REP library's name:
140  std::string temp(currentDirAndPath);
141 #ifdef _WIN32
142  temp+="\\v_rep.dll";
143 #elif defined (__linux)
144  temp+="/libv_rep.so";
145 #elif defined (__APPLE__)
146  temp+="/libv_rep.dylib";
147 #endif /* __linux || __APPLE__ */
148  // 3. Load the V-REP library:
149  vrepLib=loadVrepLibrary(temp.c_str());
150  if (vrepLib==NULL)
151  {
152  BOOST_LOG_TRIVIAL(error) << "Error, could not find or correctly load the V-REP library. Cannot start 'PluginSkeleton' plugin.\n";
153  return(0); // Means error, V-REP will unload this plugin
154  }
156  {
157  BOOST_LOG_TRIVIAL(error) << "Error, could not find all required functions in the V-REP library. Cannot start 'PluginSkeleton' plugin.\n";
159  return(0); // Means error, V-REP will unload this plugin
160  }
161  // ******************************************
162 
163  // Check the version of V-REP:
164  // ******************************************
165  int vrepVer;
167  if (vrepVer<20604) // if V-REP version is smaller than 2.06.04
168  {
169  BOOST_LOG_TRIVIAL(error) << "Sorry, your V-REP copy is somewhat old. Cannot start 'PluginSkeleton' plugin.\n";
171  return(0); // Means error, V-REP will unload this plugin
172  }
173  // ******************************************
174 
175 
176  int noArgs[]={0}; // no input arguments
177  simRegisterCustomLuaFunction("simExtGrlInverseKinematicsStart","number result=simExtGrlInverseKinematicsStart()",noArgs,LUA_SIM_EXT_GRL_IK_START);
178  simRegisterCustomLuaFunction("simExtGrlInverseKinematicsStop","number result=simExtGrlInverseKinematicsStop()",noArgs,LUA_SIM_EXT_GRL_IK_STOP);
179  simRegisterCustomLuaFunction("simExtGrlInverseKinematicsReset","number result=simExtGrlInverseKinematicsReset()",noArgs,LUA_SIM_EXT_GRL_IK_RESET);
180  simRegisterCustomLuaFunction("simExtGrlInverseKinematicsAddFrame","number result=simExtGrlInverseKinematicsAddFrame()",noArgs,LUA_SIM_EXT_GRL_IK_ADD_FRAME);
181  simRegisterCustomLuaFunction("simExtGrlInverseKinematicsFindTransform","number result=simExtGrlInverseKinematicsFindTransform()",noArgs,LUA_SIM_EXT_GRL_IK_FIND_TRANSFORM);
182  simRegisterCustomLuaFunction("simExtGrlInverseKinematicsApplyTransform","number result=simExtGrlInverseKinematicsApplyTransform()",noArgs,LUA_SIM_EXT_GRL_IK_APPLY_TRANSFORM);
183  simRegisterCustomLuaFunction("simExtGrlInverseKinematicsRestoreSensorPosition","number result=simExtGrlInverseKinematicsRestoreSensorPosition()",noArgs,LUA_SIM_EXT_GRL_IK_RESTORE_SENSOR_POSITION);
184 
185 
186  // ******************************************
187 
188  BOOST_LOG_TRIVIAL(info) << "Inverse Kinematics plugin initialized. Build date/time: " << __DATE__ << " " << __TIME__ <<"\n";
189 
190  return(PLUGIN_VERSION); // initialization went fine, we return the version number of this plugin (can be queried with simGetModuleName)
191 }
192 
193 // This is the plugin end routine (called just once, when V-REP is ending, i.e. releasing this plugin):
194 VREP_DLLEXPORT void v_repEnd()
195 {
196  // Here you could handle various clean-up tasks
197 
198  /////////////////////////
199  // PUT OBJECT RESET CODE HERE
200  // close out as necessary
201  ////////////////////
202 
204 
205  unloadVrepLibrary(vrepLib); // release the library
206 }
207 
208 // This is the plugin messaging routine (i.e. V-REP calls this function very often, with various messages):
209 VREP_DLLEXPORT void* v_repMessage(int message,int* auxiliaryData,void* customData,int* replyData)
210 { // This is called quite often. Just watch out for messages/events you want to handle
211  // Keep following 5 lines at the beginning and unchanged:
212  static bool refreshDlgFlag=true;
213  int errorModeSaved;
216  void* retVal=NULL;
217 
218  // Here we can intercept many messages from V-REP (actually callbacks). Only the most important messages are listed here.
219  // For a complete list of messages that you can intercept/react with, search for "sim_message_eventcallback"-type constants
220  // in the V-REP user manual.
221 
222 
224  refreshDlgFlag=true; // V-REP dialogs were refreshed. Maybe a good idea to refresh this plugin's dialog too
225 
227  { // A custom menu bar entry was selected..
228  // here you could make a plugin's main dialog visible/invisible
229  }
230 
232  { // This message is sent each time the scene was rendered (well, shortly after) (very often)
233  // It is important to always correctly react to events in V-REP. This message is the most convenient way to do so:
234 
235  int flags=auxiliaryData[0];
236  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
237  bool instanceSwitched=((flags&64)!=0);
238 
239  if (instanceSwitched)
240  {
241  // React to an instance switch here!!
242  }
243 
244  if (sceneContentChanged)
245  { // we actualize plugin objects for changes in the scene
246  refreshDlgFlag=true; // always a good idea to trigger a refresh of this plugin's dialog here
247  }
248 
249 
250 
251  //...
252  //////////////
253  // PUT MAIN CODE HERE
254 
255  /////////////
256  auto simulationState = simGetSimulationState();
257  if (simulationState == sim_simulation_advancing_abouttostop) //checks if the simulation is still running
258  {
260  //if(InverseKinematicsControllerPG) BOOST_LOG_TRIVIAL(info) << "current simulation time:" << simGetSimulationTime() << std::endl; // gets simulation time point
261  }
262  // make sure it is "right" (what does that mean?)
263 
264 
265  // find the v-rep C functions to do the following:
266  ////////////////////////////////////////////////////
267  // Use handles that were found at the "start" of this simulation running
268 
269  // next few Lines get the joint angles, torque, etc from the simulation
270  if (InverseKinematicsControllerPG && simulationState == sim_simulation_advancing_running)// && InverseKinematicsControllerPG->allHandlesSet == true // allHandlesSet now handled internally
271  {
272 
273  // run one loop synchronizing the arm and plugin
275 
276  }
277  }
278 
280  { // The main script is about to be run (only called while a simulation is running (and not paused!))
281 
282  }
283 
285  { // Simulation is about to start
286 
287  /////////////////////////
288  // PUT OBJECT STARTUP CODE HERE
289  ////////////////////
290  // get the handles to all the objects, joints, etc that we need
291  /////////////////////
292  // simGetObjectHandle
293 
294  try {
295  //InverseKinematicsControllerPG = std::make_shared<grl::VrepInverseKinematicsController>();
296  //InverseKinematicsControllerPG->construct();
297  //InverseKinematicsControllerPG->run_one(); // for debugging purposes only
298  //InverseKinematicsControllerPG.reset(); // for debugging purposes only
299  } catch (boost::exception& e){
300  // log the error and print it to the screen, don't release the exception
301  std::string initerr("v_repExtInverseKinematicsController plugin initialization error:\n" + boost::diagnostic_information(e));
302  simAddStatusbarMessage( initerr.c_str());
303  BOOST_LOG_TRIVIAL(error) << initerr;
304  }
305  }
306 
308  { // Simulation just ended
309 
310  /////////////////////////
311  // PUT OBJECT RESET CODE HERE
312  // close out as necessary
313  ////////////////////
314 
315  }
316 
318  { // A script called simOpenModule (by default the main script). Is only called during simulation.
319  if ( (customData==NULL)||(_stricmp("PluginSkeleton",(char*)customData)==0) ) // is the command also meant for this plugin?
320  {
321  // we arrive here only at the beginning of a simulation
322  }
323  }
324 
326  { // A script called simHandleModule (by default the main script). Is only called during simulation.
327  if ( (customData==NULL)||(_stricmp("PluginSkeleton",(char*)customData)==0) ) // is the command also meant for this plugin?
328  {
329  // we arrive here only while a simulation is running
330  }
331  }
332 
334  { // A script called simCloseModule (by default the main script). Is only called during simulation.
335  if ( (customData==NULL)||(_stricmp("PluginSkeleton",(char*)customData)==0) ) // is the command also meant for this plugin?
336  {
337  // we arrive here only at the end of a simulation
338  }
339  }
340 
342  { // Here the user switched the scene. React to this message in a similar way as you would react to a full
343  // scene content change. In this plugin example, we react to an instance switch by reacting to the
344  // sim_message_eventcallback_instancepass message and checking the bit 6 (64) of the auxiliaryData[0]
345  // (see here above)
346 
347  }
348 
350  { // Here we have a plugin that is broadcasting data (the broadcaster will also receive this data!)
351 
352  }
353 
355  { // 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)
356 
357  }
358 
359  // You can add many more messages to handle here
360 
361  if ((message==sim_message_eventcallback_guipass)&&refreshDlgFlag)
362  { // handle refresh of the plugin's dialogs
363  // ...
364  refreshDlgFlag=false;
365  }
366 
367  // Keep following unchanged:
368  simSetIntegerParameter(sim_intparam_error_report_mode,errorModeSaved); // restore previous settings
369  return(retVal);
370 }
371 
VREP_DLLEXPORT unsigned char v_repStart(void *reservedPointer, int reservedInt)
int getVrepProcAddresses(LIBRARY lib)
Definition: v_repLib.cpp:672
LIBRARY loadVrepLibrary(const char *pathAndFilename)
Definition: v_repLib.cpp:622
std::shared_ptr< grl::VrepInverseKinematicsController > InverseKinematicsControllerPG
ptrSimSetIntegerParameter simSetIntegerParameter
Definition: v_repLib.cpp:51
ptrSimGetIntegerParameter simGetIntegerParameter
Definition: v_repLib.cpp:52
void unloadVrepLibrary(LIBRARY lib)
Definition: v_repLib.cpp:641
VREP_DLLEXPORT void * v_repMessage(int message, int *auxiliaryData, void *customData, int *replyData)
ptrSimRegisterCustomLuaFunction simRegisterCustomLuaFunction
Definition: v_repLib.cpp:208
void LUA_SIM_EXT_GRL_IK_RESTORE_SENSOR_POSITION(SLuaCallBack *p)
ptrSimGetSimulationState simGetSimulationState
Definition: v_repLib.cpp:106
ptrSimAddStatusbarMessage simAddStatusbarMessage
Definition: v_repLib.cpp:117