Ask Your Question

Publish unknown number of topics?

asked 2013-10-29 02:39:24 -0500

Barbas gravatar image

updated 2013-11-14 10:49:15 -0500

tfoote gravatar image


I'm trying to adapt the Euclidean Cluster Extraction tutorial from PCL to work with ROS.

I can get the tutorial working as it should, my question is with handling the detected clusters.

My idea was to publish each cluster in a different topic so I can look at them separately in rviz. My question is how can I do that?

Currently I'm creating one publisher as a global variable, and using nodehandle.advertise<sensor_msgs::PointCloud2>() in main() to create a topic and publish on it. This means that I can only publish only one pointcloud through this.

Is it possible to create a number of publishers, unknown at compile time, since I don't know how many objects will be detected, that each publish on a different topic?

How else would you recommend I use/view the vector of PointClouds that get created by the cluster extraction? The tutorial extracts them to files, but I would rather view them in rviz somehow.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2013-10-29 04:09:48 -0500

dornhege gravatar image

You can just create multiple Publisher within your node dynamically. They don't need to be static variables.

As to viewing them: That might be annoying as you need to add an extra display for each. For clustering I'd rather send one point cloud with colors and choose a random color per cluster.

edit flag offensive delete link more


But how can I get access to my nodehandler in the callback function, where the Publishers would be dynamically created? Is it possible to pass a reference to it in the callback?

Barbas gravatar image Barbas  ( 2013-10-29 05:51:50 -0500 )edit

If you mean the NodeHandle you can create those on the fly.

dornhege gravatar image dornhege  ( 2013-10-29 06:52:56 -0500 )edit

OK what I've tried now is: -Create a vector of publishers inside the callback function. -In a for loop initialize the publishers, using `nodehandle.advertise<sensor_msgs::pointcloud2>()` -Convert the cluster clouds to ros, and publish them. My problem is that apart from the first publisher, all the other publishers do not receive messages. Is there some other way to publish the different clouds in different topics?

Barbas gravatar image Barbas  ( 2013-10-29 08:11:41 -0500 )edit

If you publish immediately after creating, there won't be a connection yet. Try waiting until getNumSubscribers is > 0 or create a bunch of publishers ahead.

dornhege gravatar image dornhege  ( 2013-10-29 08:18:25 -0500 )edit

Thanks, for the help, got it working now. Here a gist of the code, if anyone wants to take a look:

Barbas gravatar image Barbas  ( 2013-10-29 08:38:23 -0500 )edit

Sorry, I know this is a really old post, but would you happen to have that code available? @Barbas

Tanmay gravatar image Tanmay  ( 2015-08-21 05:50:36 -0500 )edit

hello Barbas, i want use your concept for publishing unknown topic, can you send your code again because this link does not work. thanks in advance.

kerollosghobrial gravatar image kerollosghobrial  ( 2015-10-31 08:56:15 -0500 )edit

i know its very old post, but can i see your code please? i'm facing the same problem right now

alfan gravatar image alfan  ( 2020-05-27 02:26:06 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2013-10-29 02:39:24 -0500

Seen: 589 times

Last updated: May 27 '20