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

Launch same node multiple times

asked 2022-01-13 05:41:21 -0600

Ariel gravatar image

updated 2022-01-13 07:56:03 -0600

Hi,

I have a simple publisher which I want to launch with different parameters (rate and length) using the same code:

#include "ros/ros.h"
#include "std_msgs/String.h"

int main(int argc, char **argv)
{
    std::string NODE_NAME = "pc_pub_string";
    ros::init(argc, argv, NODE_NAME);
    ros::NodeHandle n;

    ros::Publisher string_pub = n.advertise<std_msgs::String>(NODE_NAME, 100);

    // Get the sample rate of this node (set in the launch file)
    ros::NodeHandle np("~");
    int loop_rate_p, data_length_p;
    if(np.getParam("loop_rate", loop_rate_p)){
        ROS_INFO("Initialized [%s] node at %d Hz", NODE_NAME.c_str(), loop_rate_p);
    }
    else{
        ROS_ERROR("Couldn't get rate parameter");
        return 0;
    }
    if(np.getParam("data_length", data_length_p)){
        ROS_INFO("Initialized [%s] node with %d elements in data", NODE_NAME.c_str(), data_length_p);
    }
    else{
        ROS_ERROR("Couldn't get data length parameter");
        return 0;
    }
    ros::Rate loop_rate(loop_rate_p);

    // Populate string message (content doesn't matter for proof of concept)
    std_msgs::String str;
    for(int i=0; i<data_length_p; i++)
        str.data.push_back(i);

    while(ros::ok())
    {
        string_pub.publish(str);
        ros::spinOnce();
        loop_rate.sleep();
    }


    return 0;
}

For this, I am using this launch file:

<launch>

  <group ns="publisher">
    <node pkg="proof_of_concept_string_ips" name="pc_pub_string0" type="pc_pub_string">
      <param name="loop_rate" type="int" value="2" />
      <param name="data_length" type="int" value="4" />
      <remap from="publisher/pc_pub_string" to="publisher/pc_pub_string0"/>
    </node>

    <node pkg="proof_of_concept_string_ips" name="pc_pub_string1" type="pc_pub_string">
      <param name="loop_rate" type="int" value="4" />
      <param name="data_length" type="int" value="2" />
      <remap from="publisher/pc_pub_string" to="publisher/pc_pub_string1"/>
    </node>    

  </group>

</launch>

The issue is that in the topic list, I only see what I wrote in NODE_NAME = "pc_pub_string". However, with ros info I can see that there are two publisher. I was hoping to have two topics, one called pc_pub_string0 and another pc_pub_string1. This could be achieved by having 2 .cpp files and compile 2 different executables, but their code would be the same.

Is it possible to avoid having two executables and use only one to manipulate their parameters (also topic names to have multiple) from the launch file?

Thanks for the help.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2022-01-13 07:56:07 -0600

Ariel gravatar image

The error is in the launch file. The remap should not include the group name: <remap from="pc_pub_string" to="pc_pub_string0"/>

edit flag offensive delete link more
0

answered 2022-01-13 08:03:28 -0600

JunTuck gravatar image

You can use roscpp to retrieve the node name specified in the launch file, as suggested by #q30908.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2022-01-13 05:41:21 -0600

Seen: 168 times

Last updated: Jan 13 '22