Problem with callback as classmember

This is a discussion on Problem with callback as classmember within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying to call a callback as classmember, but what happens is all code being executed except the callback. ...

  1. #1
    hvn
    hvn is offline
    Registered User
    Join Date
    May 2013
    Posts
    6

    Problem with callback as classmember

    Hi,

    I'm trying to call a callback as classmember, but what happens is all code being executed except the callback.
    Code:
    class Pera{
    public:
    Pera(ros::NodeHandle* _n):pos_x{
    ROS_INFO("test 1");
    ls_sub = n.subscribe("/gazebo/link_states", 1, &Pera::callback, this);
    }
    
    void callback(gazebo_msgs::Linkstates msgs_ls{
    ROS_INFO("something");
    }
    
    ....
    
    };
    At the moment, only "test 1" is printed, while "something" isnt. According to other examples I've found, there is a & missing in void callback, but when I put an & like
    Code:
    ::LinkStates&
    , it seems fine but compilation ends in error. Other solutions I've tried end in error as well. Can someone please help me?

    Thank you.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    681
    Post the actual code, preferably a compilable one with main(), and not a piece of single incomplete class, yet with a missing parenthesis in function definition...
    I never put signature, but I decided to make an exception.

  3. #3
    hvn
    hvn is offline
    Registered User
    Join Date
    May 2013
    Posts
    6
    I removed some printing code, but the structure is intact. Since I'm using specific libraries, I doubt you can instantly compile it.
    Thanks for helping.

    Code:
    #include "ros/ros.h"
    #include "gazebo_msgs/LinkStates.h"
    
    using namespace std;
    
    double twist_lin_x, twist_lin_y, twist_lin_z,
            pos_x, pos_y, pos_z,
            twist_ang_x, twist_ang_y, twist_ang_z,
            or_x, or_y, or_z, or_w,
            effort  = 0.0;
    int loop = 0;
    std_msgs::Float64 msg;
    
    class Pera
    {
    public:
        Pera(ros::NodeHandle* _n):pos_x(0){
            //providing callback (a member) and a state (this)
            n = *_n;
            ROS_INFO("Subscribing to link_states");
            ls_sub = n.subscribe("/gazebo/link_states", 1, &Pera::callback, this);
            ROS_INFO("Subscribed");
        }
    
        void callback(gazebo_msgs::LinkStates msgs_ls){
                ROS_INFO("-------------------");
                ROS_INFO("Position x hand     : %.15lf", msgs_ls.pose[43].position.x);
                pos_x = msgs_ls.pose[43].position.x;
        }
    
        void run(){
            ROS_INFO("Declaring the right hand");
            std::string right_hand = (std::string)"amigo::hand_right";
    
            ROS_INFO("Advertising to set_link_state");
            ros::Publisher ls_pub = n.advertise<gazebo_msgs::LinkState>("/gazebo/set_link_state", 1);
            ROS_INFO("Creating instance");
            gazebo_msgs::LinkState linkState;
    
            ROS_INFO("Assigning link");
            linkState.link_name = right_hand;
            ROS_INFO("Assigning reference");
            linkState.reference_frame = right_hand;
    
            while (ros::ok())
            {
                ROS_INFO("Assigning pos x: %lf", pos_x);
                linkState.pose.position.x = pos_x;
            }
        }
    
    protected:
        // state
        ros::NodeHandle n;
        ros::Subscriber ls_sub;
        double pos_x;
    };
    
    int main(int argc, char **argv)
    {
        ROS_INFO("init");
        ros::init(argc, argv, "arm_joint_ik_receiver");
        ros::NodeHandle n;
    
        ROS_INFO("Calling PERA");
        Pera pera(&n);
        pera.run();
        ROS_INFO("Terminating");
    
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    681
    Code:
    class Pera
    {
    public:
        Pera(ros::NodeHandle* _n):pos_x(0){
            //providing callback (a member) and a state (this)
            n = *_n;
            ROS_INFO("Subscribing to link_states");
            ls_sub = n.subscribe("/gazebo/link_states", 1, &Pera::callback, this);
            ROS_INFO("Subscribed");
        }
    _n should be passed by const reference (const ros::NodeHandle& _n).

    n.subscribe() is called and a callback is passed, which means that n.subscribe() is the function which should call Pera::callback like this:

    (ptr->*func)(msgs_ls)

    where "ptr" is of type "Pera*", "func" is a pointer to Pera::callback,
    and "msgs_ls" is your argument.

    You didn't provide code for n.subscribe(), so how can anybody know why the callback isn't called?
    I never put signature, but I decided to make an exception.

  5. #5
    hvn
    hvn is offline
    Registered User
    Join Date
    May 2013
    Posts
    6
    Quote Originally Posted by kmdv View Post
    n.subscribe() is called and a callback is passed, which means that n.subscribe() is the function which should call Pera::callback like this:

    (ptr->*func)(msgs_ls)

    where "ptr" is of type "Pera*", "func" is a pointer to Pera::callback,
    and "msgs_ls" is your argument.
    So you mean like Pera->*callback(msgs_ls) ? Can you give more information, because this doesn't compile for me..

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,214
    Can you give more information, because this doesn't compile for me.
    O_o

    Nor should it.

    That's also not what he said.

    Soma

  7. #7
    hvn
    hvn is offline
    Registered User
    Join Date
    May 2013
    Posts
    6
    Quote Originally Posted by phantomotap View Post
    Nor should it.

    That's also not what he said.
    So, since I apparently didn't get it, what did he say?

  8. #8
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    681
    Quote Originally Posted by hvn View Post
    So, since I apparently didn't get it, what did he say?
    The bug probably lies inside ros::NodeHandle::subscribe(). You have to post code of that function.

    Do you know the difference between "passing a pointer to function" and "calling function pointed to by a pointer"?
    I never put signature, but I decided to make an exception.

  9. #9
    hvn
    hvn is offline
    Registered User
    Join Date
    May 2013
    Posts
    6
    Quote Originally Posted by kmdv View Post
    The bug probably lies inside ros::NodeHandle::subscribe(). You have to post code of that function.
    That's a library I don't have the code of.
    Do you know the difference between "passing a pointer to function" and "calling function pointed to by a pointer"?
    Reading about it now...thank you

  10. #10
    hvn
    hvn is offline
    Registered User
    Join Date
    May 2013
    Posts
    6
    Thank you for the hint. Found the cause.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with settgin a callback under directshow
    By abachler in forum Windows Programming
    Replies: 6
    Last Post: 06-24-2009, 11:32 AM
  2. classmember WndProc isn't called ...
    By Greenhorn__ in forum Windows Programming
    Replies: 10
    Last Post: 07-19-2008, 11:40 AM
  3. Copy Constructor with classmember CArray
    By zikje in forum C++ Programming
    Replies: 4
    Last Post: 02-28-2006, 09:54 AM
  4. gtk callback problem in C++
    By curlious in forum Linux Programming
    Replies: 1
    Last Post: 01-01-2005, 12:31 AM
  5. problem with UDP WSAAsyncSelect socket, no callback message received???
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-29-2004, 10:59 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21