ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
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);