I have a ROS1 project that I'm trying to port to ROS2, and part of the functionality of that project involved creating multiple node handles that had different namespaces, and then each node handle would have their own values for different parameter names. I've been trying to recreate the same functionality in ROS2, and I've managed to create a small example. Here's the entirety of my node:

#include <rclcpp/rclcpp.hpp>

int main(int argc, char **argv)
    rclcpp::init(argc, argv);

    // Create Node to pass around
    rclcpp::Node::SharedPtr minimalExampleNode(new rclcpp::Node("minimal_example_node", "minimal_example"));

    // Create two sub nodes with different namespaces
    std::shared_ptr<rclcpp::Node> subNodeA = minimalExampleNode->create_sub_node("namespace_a");
    std::shared_ptr<rclcpp::Node> subNodeB = minimalExampleNode->create_sub_node("namespace_b");

    // Each one has its own version of the same parameter
    subNodeA->declare_parameter("param_name", 0.0);
    subNodeB->declare_parameter("param_name", 1.0);

    // Start

    return EXIT_SUCCESS;

This crashes when I try to run it, complaining that "param_name" is declared twice. I noticed too that when I print out the value of subNodeA->get_namespace() I just see "/minimal_example", where I would expect it to print "/minimal_example/namespace_a." Obviously I could get around the issue by doing something like:

    subNodeA->declare_parameter("namespace_a/param_name", 0.0);
    subNodeB->declare_parameter("namespace_b/param_name", 1.0);

And that does work, but the full project I'm trying to port is pretty complex and this would be tricky to do, so I'd like to avoid that.

I noticed if I do a "ros2 param list" that the top-level minimalExampleNode does seem to prepend its namespace onto all of its parameters, because when I run that command I see the following:


Is there any way to make the sub-nodes do the same thing? Maybe by setting something in NodeOptions?



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.