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

Multiple subscriptions via subscriber pool

asked 2016-02-16 09:19:17 -0600

user23fj239 gravatar image

updated 2016-02-16 11:08:57 -0600

I had a look at the overview of subscribing but nothing is mentioned about short cutting via having a subscriber pool. Then initialize the subscribers inside one for-loop, which makes the code for multiple subscriptions shorter.
So instead of the conventional way:

ros::NodeHandle nh_;
ros::Subscriber sub_acc = nh_.subscribe(strings[1], 300, callbFloat64);
ros::Subscriber sub_mag = nh_.subscribe(strings[2], 300, callbFloat64);
ros::Subscriber sub_gyro = nh_.subscribe(strings[4], 300, callbFloat64);
ros::Subscriber sub_prox = nh_.subscribe(strings[8], 300, callbFloat64);

I thought of declaring a pool which gets initializied inside one loop. And it does sound good :)

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2016-02-16 11:11:36 -0600

user23fj239 gravatar image

Actually this works, also not sure I the right way to do it.

ros::Subscriber spool [20];
    string strings[20] =             //string storage
    {
    [0] = "topic_0",
    [1] = "topic_1",
    [2] = "topic_2",
    [3] = "topic_3",
    ...
    }
    ros::NodeHandle nh_;

    for(int i = 0; i<20; i++){
        spool[i] = nh_.subscribe(strings[i], 300, callback);
    }
   //dedicated spinner thread, could also simply be: while(ros::ok()) ros::spin();
   tpool[pool++] = boost::thread(spinner);
edit flag offensive delete link more

Comments

It'll probably work. Unless you have very specific needs, I'd use something like a MultiThreadedSpinner instead of all the boost threads. The Subscriber 'pool' just saves a few local variables.

gvdhoorn gravatar image gvdhoorn  ( 2016-02-17 02:14:03 -0600 )edit

Indeed, but the info on how to start the thread is not to extended and I could not implement it ´ros::MultiThreadedSpinner spinner(4); // Use 4 threads spinner.spin(); // spin() will not return until the node has been shutdown´

user23fj239 gravatar image user23fj239  ( 2016-02-17 05:16:24 -0600 )edit

I'm a bit unsure about what you mean. MultiThreadedSpinner.spin() is basically ros::spin() but with the multithreaded spinner. It blocks. If you don't want to block, use AsyncSpinner. Documentation on the same wiki page.

gvdhoorn gravatar image gvdhoorn  ( 2016-02-17 05:33:56 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-02-16 09:19:17 -0600

Seen: 239 times

Last updated: Feb 16 '16