marcoesposito1988 / cython_catkin_example

Example of integration between Cython and ROS/catkin
18 stars 8 forks source link

Using cython for a script with nodehandle #1

Open a-z-e-r-i-l-a opened 4 years ago

a-z-e-r-i-l-a commented 4 years ago

Hi, You have a very nice example for using cython and catkin. I wanted to ask if it is also usable for interfacing a c++ script in ros that has nodehandle. I have made a ros_control hardware interface and want to now interface it with a python simulator. My choices seems to be Cython, boost/python, swig, cffi and etc. Here I noticed a possible solution with boost/python and they mention something specific about having nodehandle and my other question was if you may think there might be an advantage in using cython compared to boost/python.

Thanks.

marcoesposito1988 commented 4 years ago

Hi @a-z-e-r-i-l-a,

in principle you could use cython for this purpose. You would still have to apply the fix mentioned in that tutorial, however. The reason is that rospy and roscpp can be used in the same process, but in this case run in parallel to each other. Initing rospy is not enough; you have to also init roscpp explicitely. Otherwise you will encounter an error when you try to instanciate a roscpp node handle in the C++ code. To be sure, you can call the roscpp init function multiple times, e.g. in the constructor of every class that needs it; it will just do nothing after the first time.

If you do not have any specific reason to use cython, I would suggest you to stick with the "official" solution with boost python. Since it is likely that more people use it, it is also more likely that you will be able to google a solution when you encounter a problem. Otherwise you may waste a lot of time only to get stuck with a problem nobody has ever encountered (and reported about it).

a-z-e-r-i-l-a commented 4 years ago

Hi @marcoesposito1988 , I had a shot question:

in Python3 initializing roscpp like how it was described here didn't work for me and geve the error:

    from moveit_ros_planning_interface._moveit_roscpp_initializer import roscpp_init
ImportError: dynamic module does not define module export function (PyInit__moveit_roscpp_initializer)

I will be really happy if you had any suggestion or recommendation to get around this issue:)

marcoesposito1988 commented 4 years ago

This sounds like a python 2 vs python 3 problem, i.e. the python 3 interpreter is trying to load a plugin that was compiled for python 2.

If you are using ROS melodic, it should still provide binaries for python 2. You should be able to use python 3, but you will need to recompile a lot of stuff and apply some hacks.

There are many guides available (you can google for "ros python3"), e.g.: https://www.miguelalonsojr.com/blog/robotics/ros/python3/2019/08/20/ros-melodic-python-3-build.html