Thread: ROS subscriber callback as member function does not get called

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    3

    ROS subscriber callback as member function does not get called

    Hi,

    There is already a thread with exactly the same problem I have, but the answer to solve the problem isn't stated at the end. Anyone has an idea about how to solve this?

    Problem with callback as classmember

    Thanks

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I can't see the solution for the problem and it doesn't even say what the problem actually was, so you're going to have to post some code. Put a compilable example that is as simple as possible while still exhibiting the problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > There is already a thread with exactly the same problem I have, but the answer to solve the problem isn't stated at the end.
    So you were hoping to score a quick homework win by googling code, found something almost there - pulse racing as you scrolled down to reveal the answer to your homework, only for your hopes to be dashed on the rock of disappointment.

    > Anyone has an idea about how to solve this?
    Well the OP of the thread did.

    Google plus "does anyone know" won't make you a programmer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Feb 2015
    Posts
    3
    No need to be rude, this is the first time I post a thread in a forum. Sorry for not providing enough information.

    Here is a short version of my code. It is I believe pretty much the same as the similar thread I referred to. If I'm not doing exactly the same thing, I don't know what's different. Since I removed most of it for briefty, I doubt it will compile, but the main parts are there.

    Code:
    int main( int argc, char **argv )
    {
        // Initialize the ROS system
        ros::init( argc, argv, "elikos_aiAgent" );
    
        // Establish this program as a ROS node
        ros::NodeHandle nh;
        ros::Rate r(30);    //10 hz
    
        // Create the quad AI Agent
        elikos_ai::Agent agent(&nh);
        agent.init();
    
        while (ros::ok())
        {
            // stuff
    
            r.sleep();
        }
    
        agent.destroy();
    }
    
    class Agent
    {
    
    public:
    
        Agent( ros::NodeHandle* nh );
        ~Agent();
        void init();
        void destroy();
        void run();
    
        void receiveRobotsPosCallback( const elikos_ros::RobotsPos& msg );
    
    private:
        ros::NodeHandle& nh_;
    
        void setSubscribers();
    
        std::map<std::string, ros::Publisher> rosPublishers_;
         std::map<std::string, ros::Subscriber> rosSubscribers_;
    
        // I've tested with this version too (the subscriber not being in a map)
        // ros::Subscriber sub_;
    };
    
    Agent::Agent( ros::NodeHandle* nh ) : nh_(*nh)
    {}
    
    void Agent::init()
    {
        setSubscribers();
    }
    
    void Agent::setSubscribers()
    {
        ROS_INFO_STREAM( "Agent::setSubscribers" );
        // Subscribe to all robots' positions' topics
        std::string robotsPosTopic = TOPICS_NAMES[robotsPos];
         ros::Subscriber sub =  nh_.subscribe(robotsPosTopic, 1000,  &Agent::receiveRobotsPosCallback, this );
        rosSubscribers_.insert( std::pair<std::string,ros::Subscriber>(robotsPosTopic, sub) );
        ROS_INFO_STREAM( "Agent::setSubscribers, end" );
    
        // I've tested with this version too (the subscriber not being in a map)
        // sub_ = nh_.subscribe(robotsPosTopic, 1000, &Agent::receiveRobotsPosCallback, this );
    }
    
    void Agent::receiveRobotsPosCallback( const elikos_ros::RobotsPos& msg )
    {
        std::cout << "AGENT CALLBACK" << std::endl;
        ROS_INFO_STREAM( "Agent::callback -- Push RobotsPos message" );
        queueRobotsPos_.push( msg );
    }
    With this code, here are the console logs (with ROS_INFO_STREAM) I get:

    The terminal running the ai_agent node (executable):

    [ INFO] [1425144919.193049638]: Agent::setSubscribers
    [ INFO] [1425144919.198125285]: Agent::setSubscribers, end

    In another terminal, I checked if ROS receives any elikos_ros::RobotsPos messages at all, and it does. This means that any node subscribed to the RobotsPos topic should see their callback function called (where my program fails).

    I tried several variations but none made it work, though I followed the given tutorials from the library and discussed it with coworkers. The library tutorials :

    roscpp/Overview/Publishers and Subscribers - ROS Wiki (see point 2.3.2)

    As in the previous thread, it was suggested to look into the ros::NodeHandle::subscribe() function, I tried to find the source but only managed to find the header file and couldn't find anything from this point:

    roscpp: node_handle.h Source File

    I tried the solution in this post, but it didn't work:

    Subscriber callback not being called in C++ - ROS Answers: Open Source Q&A Forum

    Thanks for you help
    Last edited by myclaa; 02-28-2015 at 11:55 AM.

  5. #5
    Registered User
    Join Date
    Feb 2015
    Posts
    3
    I found the program with my program. Here is the solution:

    There wasn't a problem in the ros::NodeHandle::subscribe() method after all. It was in the main function, in which there was one missing call:

    Code:
    ros::spinOnce();
    So the corrected main function looks like:

    Code:
    int main( int argc, char **argv )
    {
        // Initialize the ROS system
        ros::init( argc, argv, "elikos_aiAgent" );
    
        // Establish this program as a ROS node
        ros::NodeHandle nh;
        ros::Rate r(30);    //10 hz
    
        // Create the quad AI Agent
        elikos_ai::Agent agent(&nh);
        agent.init();
    
        while (ros::ok())
        {
            
            // stuff
    
            ros::spinOnce(); // the missing call
            r.sleep();
        }
    
        agent.destroy();
    }
    Here are the resources that I consulted if it can help somebody else that runs into the same problem:

    ROS library reference: roscpp/Overview/Callbacks and Spinning - ROS Wiki
    A forum thread with explanations: ROS callbacks, threads and spinning - ROS Answers: Open Source Q&A Forum

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-15-2011, 10:49 PM
  2. Callback into a class member function
    By kovacsbv in forum C++ Programming
    Replies: 4
    Last Post: 05-23-2009, 10:06 AM
  3. Replies: 3
    Last Post: 07-19-2008, 03:12 PM
  4. Ptr-to-member as callback in a c-function
    By pheres in forum C++ Programming
    Replies: 1
    Last Post: 06-26-2007, 12:41 AM
  5. Is it possible to have callback function as a class member?
    By Aidman in forum Windows Programming
    Replies: 11
    Last Post: 08-01-2003, 11:45 AM

Tags for this Thread