Ask Your Question
0

Problem with image_transport callback

asked 2017-01-17 09:57:16 -0500

Abind gravatar image

I have the next scenario,

class controller :
        {
        public:
            controller(ros::NodeHandle node);
            ~controller(void);
            // The callbacks are implemented correctly
            void imageCallback(const sensor_msgs::ImageConstPtr& msg);
            void navdataCallback(const ardrone_autonomy::Navdata& data);
            void foo(){
                // Do Something
                std::thread foo_thread(&controller::foo2, this);
                foo_thread.join();
            }
            void foo2(){
                // Do Something
                while (something){
                    // Do Something
                    std::this_thread::sleep_for(std::chrono::milliseconds(20));
                    ros::spinOnce();
                }
            }
       private:
            //Other Stuff
    }

and the main program is

int main(int argc, char **argv)
{
  ros::init(argc,argv, "main");
  ros::NodeHandle n;
  controller control(n);
  image_transport::ImageTransport it(n);
  image_transport::Subscriber sub   = it.subscribe("ardrone/image_raw", 1, &controller::imageCallback, &control);
  ros::Subscriber navdata           = n.subscribe("ardrone/navdata", 1000, &controller::navdataCallback, &control);
  while(ros::ok()){
    ros::spin();
  }
  return 0;
}

the problem is when I process data in foo2() the navdata messages arrives fine, but stop reciving image data until the end of the thread.

How can I resolve this? Is my approach correct? May be the ros::spinOnce is not placed correctly?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2017-01-19 19:38:13 -0500

Abind gravatar image

I will answer my own question, is not a good idea use ros::spin in diferents threads, you need to use multi thread spining.

So, in order to solve this I just put all the callbacks in a different thread, so I just need to use ros::spin in just one thread and problem solved :)

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2017-01-17 09:57:16 -0500

Seen: 126 times

Last updated: Jan 19 '17