ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

How can I set a node name when using composing nodes with rclcpp?

asked 2021-01-07 21:31:34 -0600

Kurtoid gravatar image

updated 2021-01-07 21:49:44 -0600

I'm initializing nodes like so:

 rclcpp::executors::SingleThreadedExecutor exec;
 std::vector<rclcpp::Parameter> laser_params = {rclcpp::Parameter("ip_address", "192.168.0.11")};
 rclcpp::NodeOptions laser_options;
 laser_options.parameter_overrides(laser_params);
 auto laser_node = std::make_shared<urg_node::UrgNode>(laser_options);
 exec.add_node(laser_node);

Which works fine, until I add a second node of the same type. I feel like I should be able to pass in a name argument to the node constructor, similar to rclcpp::Node. I noticed that many ros2-included nodes, like laser_proc and urg_node have hardcoded node names. For now, I've modified urg_node's constructor to have a name argument, which works, but I don't feel like I'm supposed to do this for all other nodes as well.

When using a launch.py file with ComposableNodeContainer and ComposableNode, I can pass a name argument to a node and be done with it.

How should I set a node name when running nodes in rclcpp?

edit retag flag offensive close merge delete

Comments

I think I'm supposed to be remapping something, but I don't know how.

Kurtoid gravatar image Kurtoid  ( 2021-01-07 21:34:07 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted
2

answered 2021-01-08 14:53:12 -0600

tfoote gravatar image

As you mentioned you can pass a name argument to the Launch: https://index.ros.org/doc/ros2/Tutori...

You can also pass a name argument on the command line: https://index.ros.org/doc/ros2/Tutori...

If you're constructing the NodeOptions manually you can pass arguments to the NodeOptoins

edit flag offensive delete link more
1

answered 2022-11-11 06:37:17 -0600

Andy Blight gravatar image

This is for anyone else who wasted half a day trying to figure out how to create two instance of the same node with different names and topics. The hints above and this question pointed me in the right direction.

I created two instances of the same node as follows:

  // Create the encoder nodes.
  rclcpp::NodeOptions options;
  options.arguments({"--ros-args", "-r", "__node:=encoder_left"});
  auto encoder_node_left = std::make_shared<EncoderNode>(options);
  options.arguments({"--ros-args", "-r", "__node:=encoder_right"});
  auto encoder_node_right = std::make_shared<EncoderNode>(options);

I then created matching topic names encoder/left and encoder/right with code in the EncoderNode constructor as follows:

   // Create the publisher using a mangled node name.
  std::string topic_name = get_name();
  // Replace _ with /
  size_t index = topic_name.find('_');
  topic_name[index] = '/';
  publisher_ = create_publisher<robot_msgs::msg::Encoders>(topic_name, 10);
edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2021-01-07 21:31:34 -0600

Seen: 1,567 times

Last updated: Nov 11 '22