Initialize embedded Python node in C++ node
I have a C++ node which uses some embedded Python code using boost::python. Since both the C++ and the Python code should interact with ROS, I need to initialise the node on both sides. I tried using the following code:
// Python interpreter has to be initialized before calling any Python code.
Py_Initialize();
// Set `sys.argv` (needs to be done before calling ros::init!)
PySys_SetArgv(argc, argv);
// initialize node for C++
ros::init(argc, argv, "node_name");
// initialize node for Python
py::object rospy = py::import("rospy");
rospy.attr("init_node")("node_name");
Now this has the issue that you cannot have two nodes with the same name. I get the following output which apparently comes from the Python side:
shutdown request: new node registered with same name
Using different node names in the code has no effect when using launch files as they overwrite the name. It would probably work by parsing the argv
manually for the __name:=node_name
argument and modify it for the Python side but I was wondering if there is a better solution.
Ideally I would like to use the same name for both nodes, since from the outside it should not matter if, for example, a topic is advertised by the C++ or the Python part. Is this possible by any means? And if not, is there already a common workaround for the problem?
I'm curious whether this is possible: using Boost Python both client libraries are used in one process. ROS 1 has a fundamental limitation of 1 node per process, not sure whether it applies here.
Passing a 'cleaned'
argv
to Python side might work. I don't think there is a cleaner way to do thisAs an update on this: Overwriting the
__name
argument inargv
before passing it to Python works to use a different name via launch file. However, I did not find a way to use the same name for both sides (which means that, for example, launch parameters are not recognised on the Python side).