Add nested namespace to nodes
Hi,
I have an application where two nodes are created.
/app_ns/first_node
/second_node
As you can see, I'm specifying a namespace for one of the nodes, while the other has the default ""
.
My goal, as the title says, is to add a namespace to both nodes, for example via command-line, but I'm open to other approaches.
What I would like to get is
/user_ns/app_ns/first_node
/user_ns/second_node
Unfortunately, the only way I know for specifying namespace is through the remapping arguments, which does not work in this case. For example I tried the following
$ ros2 run my_pkg my_app -r __ns:=/user_ns
$ ros2 node list
/user_ns/first_node
/user_ns/second_node
Do you know how could I get my desired output? I.e. how can I add/prepend a namespace while keeping whatever namespace the executable is already setting? Thank you
Asked by alsora on 2021-03-20 15:11:46 UTC
Answers
I found a solution in the ROS 2 nav stack where in the constructor of a node there is the following
Costmap2DROS::Costmap2DROS(
const std::string & name,
const std::string & parent_namespace,
const std::string & local_namespace)
: nav2_util::LifecycleNode(name, "", true,
// NodeOption arguments take precedence over the ones provided on the command line
// use this to make sure the node is placed on the provided namespace
rclcpp::NodeOptions().arguments({
"--ros-args", "-r", std::string("__ns:=") +
nav2_util::add_namespaces(parent_namespace, local_namespace),
"--ros-args", "-r", name + ":" + std::string("__node:=") + name
})),
this does not look the most elegant solution, but it's definitely working.
Asked by alsora on 2021-03-23 15:18:05 UTC
Comments