Gazebo | Ignition | Community
Ask Your Question

Revision history [back]

Topic name or index in Subscriber callback

Hi,

I am creating a model plugin that subscribes to many topics (same name, but only a number changes) and I want all the subscribers to call the same callback. However, if I do that I am not able to identify which subscriber (or topic name) triggered the callback at a given time.

In ROS I saw that it is possible to do this using boost::bind but I cannot make it work in Gazebo, basically because I think the API does not allow it.

Can somebody verify that this is not possible? Or much better, tell me how to identify which subscriber triggers the callback? Thanks!

Topic name or index in Subscriber callback

Hi,

I am creating a model plugin that subscribes to many topics (same name, but only a number changes) and I want all the subscribers to call the same callback. However, if I do that I am not able to identify which subscriber (or topic name) triggered the callback at a given time.

In ROS I saw that it is possible to do this using boost::bind but I cannot make it work in Gazebo, basically because I think the API does not allow it.

Can somebody verify that this is not possible? Or much better, tell me how to identify which subscriber triggers the callback? callback?

EDIT: Posting the code I have tried and corresponding error:

 std::vector<gazebo::transport::SubscriberPtr> _subs;
 size_t n = 10;
 for (size_t i = 0; i < n; ++i) {
    std::stringstream ss;
    ss << "~/ns/base_link/sensor_" << i;
    _subs.emplace_back(
         nh->Subscribe(ss.str(),
         boost::bind(&MyClass::cb, this, _1, i))
     );
}

And the error (removed verbose data):

error: no matching function for call to gazebo::transport::Node::Subscribe(std::basic_stringstream<char>::__string_type, boost::_bi::bind_t<void, boost::_mfi::mf2<void, MyClass, const boost::shared_ptr<const gazebo::msgs::LaserScanStamped>&, long unsigned int>, boost::_bi::list3<boost::_bi::value<MyClass*>, boost::arg<1>, boost::_bi::value<long unsigned int> > >)
      i)));
        ^
note: candidates are:
template<class M, class T> gazebo::transport::SubscriberPtr gazebo::transport::Node::Subscribe(const string&, void           (T::*)(const boost::shared_ptr<const T>&), T*, bool)
   SubscriberPtr Subscribe(const std::string &_topic,
                 ^
/usr/include/gazebo-5.1/gazebo/transport/Node.hh:175:21: note:   template argument deduction/substitution failed:
 mismatched types void (T::*)(const boost::shared_ptr<const T>&) and boost::_bi::bind_t<void, boost::_mfi::mf2<void, MyClass, const boost::shared_ptr<const gazebo::msgs::LaserScanStamped>&, long unsigned int>, boost::_bi::list3<boost::_bi::value<MyClass*>, boost::arg<1>, boost::_bi::value<long unsigned int> > >
      i)));

I get 4 similar errors, one for each Subscribe overload.

Thanks!