Robotics StackExchange | Archived questions

ros::SubscribeOptions::create doesn't take object T* Obj pointer.

Hi,

SubscribeOptions::create function doesn't support setting class member function as callback function? I cannot see the argument option in its signature.

template<class M >
static SubscribeOptions     create (const std::string &topic, uint32_t queue_size, const boost::function< void(const boost::shared_ptr< M const > &)> &callback, const VoidConstPtr &tracked_object, CallbackQueueInterface *queue) 

Like subscribe does below. Is it possible to add a class member function as callback function to the SubscribeOptions?

 Subscriber subscribe(const std::string& topic, uint32_t queue_size, 
                         void(T::*fp)(const boost::shared_ptr<M const>&) const, T* obj, 
                          const TransportHints& transport_hints = TransportHints())

Asked by cascais on 2019-01-25 02:03:09 UTC

Comments

Can you show us how you've tried to use this?

Using member functions should be/is definitely supported, so perhaps you're not providing the correct arguments to the function.

Asked by gvdhoorn on 2019-01-25 03:49:19 UTC

@gvdhoorn, I have not tried to use the function yet but from the interface to create SubscribeOptions like pasted in question, I cannot see the possibility to give a pointer to the instance. That's why I assumed it is not supported. Shall I use boost::bind?

Asked by cascais on 2019-01-25 05:56:03 UTC

Ah, wait. I'd missed you were trying to use SubscribeOptions. The create(..) function does seem to only take the "simple" type of callbacks, but perhaps the initByFullCallbackType(..) function can help there.

I've not used this myself, so I don't know whether it will work, but ..

Asked by gvdhoorn on 2019-01-25 06:31:58 UTC

.. the docs say (from here):

Templated initialization, templated on callback parameter type. Supports any callback parameters supported by the SubscriptionCallbackAdapter.

Asked by gvdhoorn on 2019-01-25 06:32:34 UTC

@gvdhoorn. Hmm I cannot find the definition of SubscriptionCallbackAdapter..

Asked by cascais on 2019-01-25 06:55:29 UTC

It's in subscription_callback_helper.h. Docs here. But again: I don't know whether this will work.

Asked by gvdhoorn on 2019-01-25 06:57:25 UTC

Did someone solve this?

Asked by Marcel Usai on 2020-03-25 13:34:21 UTC

Answers

This is, what I did:

  1. Defined a Function to get me some ros::SubscribeOptions with class member function and object:
template <class M, class T>
ros::SubscribeOptions getSubscribeOptions(
    const std::string &topic, uint32_t queue_size,
    void (T::*fp)(const boost::shared_ptr<M const> &),
    const boost::shared_ptr<T> &obj,
    const ros::TransportHints &transport_hints = ros::TransportHints()) {
  ros::SubscribeOptions ops;
  ops.template init<M>(topic, queue_size, boost::bind(fp, obj.get(), _1));
  ops.tracked_object = obj;
  ops.transport_hints = transport_hints;
  return ops;
}
  1. Use it.
ros::SubscribeOptions ops = getSubscribeOptions( "chatter", 1000, &WebClient::callback, WebClientObject);
ros::Subscriber Sub = n.subscribe(ops);

That seems to work right now. I hope this answers the question.

Asked by FreitagS on 2020-04-20 09:13:31 UTC

Comments