0
$\begingroup$

Rosanswers logo

[Ubuntu 13.10, Hydro Desktop-Full from source, C++]

I am having difficulty setting up callback functions for my ROS Topic subscribers, and would appreciate help.

Since all my Topics use the std_msgs::String type, I've defined the class shown below with a generic callback function and an instance variable which identifies the specific Topic.

class myCallback {

        const char * eventtype;

public:

        myCallback(const char * eventType) : eventtype(eventType) {}

        void generic_callback(const std_msgs::String::ConstPtr& msg) {
            std::cerr << this->eventtype << " heard: " << msg->data.c_str() << std::endl;
        }

};

For the ROS functionality in my project, I've created a separate class gwrbclass.cpp & gwrbclass.h, which include the myCallback class. In the header file, I define myCallback *callbackList[NUM_EVENT_TYPES] as a private variable. In the implementation, I instantiate the required myCallback objects for each entry in the callbackList and try to bind it to the node's subscriber object as shown below:

for (int j=0; j<NUM_EVENT_TYPES; j++) {
    callbackList[j] = new myCallback(eventName);
    subList[j] = (sNode[j])->subscribe(eventName, 1000, callbackList[j]->generic_callback);  << ERROR OCCURRING HERE
}

I'm getting an error stating : reference to non-static member function must be called , and as far as I can see, the method I'm calling is not static and therefore should work.
If I use subList[j] = (sNode[j])->subscribe(...., &callbackList[j]->generic_callback); the error becomes : cannot create a non-constant pointer to member function.

I'm confused as to why I'm getting this error (which I believe has something to do with std::bind, function typedef, wrapper functions, etc), and would appreciate some help as I can't figure out the syntax.


Originally posted by Nap on ROS Answers with karma: 302 on 2014-05-04

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

You need to use the class member version of subscribe -- the doxygen for it is here.

Basically, it would be:

(sNode[j])->subscribe(eventName, 1000, &myCallback::generic_callback, callbackList[j]);

Originally posted by fergs with karma: 13902 on 2014-05-04

This answer was ACCEPTED on the original site

Post score: 2


Original comments

Comment by Nap on 2014-05-04:
Thank you Fergs, exactly what was needed. :)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.