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

Create publisher from string parameter dynamically

asked 2018-03-08 08:59:35 -0600

fabbro gravatar image

Hi, I searched on the internet without any luck.

Is there any way in ROS/C++ to solve the following problem? :

I want to have a launch file in which I want to specify a parameter tags in the following way:

<param name="tags" type="string" value="1, 15, 189"/>

and then in the .cpp file associated I would like to create different publishers (or maybe it is easier to create an array of publishers) which will publish on the topics:

/pose/tag_1 /pose/tag_15 /pose/tag_189

(the conversion from string to an array of ints is not an issue. I know how to do it.

Of course, I want that the tags parameter can be changed from the launch file and therefore the creation of the topics is in some way dynamic.

Thanks for your time.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2018-03-08 09:05:01 -0600

updated 2018-03-08 10:06:53 -0600

You can access ROS param's within a node using rosparam: http://wiki.ros.org/roscpp/Overview/P...

You could also pass in the topic names via args or params in the node's local namespace. See http://wiki.ros.org/roslaunch/XML/node


Example:

<node name="my_node" pkg="my_node_pkg" type="my_node" 
  args="tag_1, tag_2, tag_189" 
/>

or

<node name="my_node" pkg="my_node_pkg" type="my_node" 
  <param name="tags" value="1, 15, 189" />
/>

Edit:
Inside your node, once you've retrieved the args/params, you can do something like this (untested):

...
// Assuming some iterable object 'tags' containing your args/param in some format
ros::NodeHandle n;

std::vector<ROS::Publisher> publisherVec;
for (int i=0; i<tags.size(); i++) {
    publisherVec.push_back(<n.advertise<std_msgs::String>(tags[i], 100));
}
...
edit flag offensive delete link more

Comments

Hi, thanks for the fast reply but this is not what I asked. I know how to write the parameters in the launch file. My problem was to then create some sort of array of publishers.

Anyway, I solved my problem I think/hope and I will post the solution soon. Thanks again.

fabbro gravatar image fabbro  ( 2018-03-08 09:23:20 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-03-08 08:59:35 -0600

Seen: 574 times

Last updated: Mar 08 '18