Can I access the absolute or parent namespace of a node from within a launch file?
Hi!
I'm wondering if there is a way to determine a node's absolute or parent namespace from within a launch file at run-time. The reason why I would like to be able to do this is as follows.
First, I use many nested launch files, which contain groups with their own namespaces. For this to work, my launch files need to avoid explicitly setting absolute namespaces, which maximises launch file reuse. (Note that reading absolute or parent namespaces at run-time, on the other hand, would not reduce reuse, so it's not a problem.)
Second, my nodes always read and write parameters and topics within their own private namespaces. This allows intuitive grouping of parameters and topics, and since all nodes always do the same thing it's easy to keep track of things. But most importantly, this avoids polluting the namespace in which the node was started; in particular, this allows multiple nodes to publish identically named topics for the same robot or namespace (without requiring me to look for and remap all potential conflicts).
For example, imagine we have a robot named Marvin, with its own group/namespace "marvin". Now image that marvin uses a robot base controller node called "base_controller", and an IMU-based position estimation node called "imu". The odometry topic produced by the base_driver might be "/marvin/base_controller/odom", and the IMU node might also produce an "odometry" estimate "/marvin/imu/odom". Without the private namespace we'd have to rely on remapping to achieve unique topic names like "base_controller_odom" and "imu_odom", which doesn't scale and gets very messy (what about the second IMU?). Namespaces seem to be a perfect way to avoid this.
The problem arises, of course, when I want to connect two nodes to the same topic. Since both publishers and subscribers will interpret all names as private by default, I can only pass topic names between them if I use absolute paths, or paths which are relative to a common parent node.
For example, if a navigation node, which uses its own "/marvin/navigation/" namespace by default, wants to read from Marvin's base controller's odometry topic "/marvin/base_controller/odom", I either need to tell the navigation node to read "/marvin/base_controller/odom", or I need to tell both to use something like "/marvin/odom". In either case, the desired topic needs to be specified as a global name, or it needs to use a common parent, in order to override the private namespaces used by both nodes.
...
Solution 1. For now I solve this in what I guess is the standard way, by not using private namespaces for topics. I still use the node's name internally to put everything in the private namespace by default anyway, but I can then remap topics relative to the node's parent namespace in the launch file. But this causes a bit more work for each node, and I really like the idea of every node using its own namespace ...