Where should I indeed place my "ros::spin()"?
I am a freshman for ROS, and I find a interesting (for me) thing: I wrote a node, for example, let's call it my_node.cpp, it looks like this:
//in my_node.cpp
ros::init(argc, argv, "my_node");
ros::NodeHandle nh("");
ros::NodeHandle nh_local("~");
MyNode my_node(nh, nh_local);
ros::spin();
And in the construct function of MyNode, it looks like this:
MyNode (ros::Nodehandle nh, ros:Nodehandle nh_local)
{
message_filters::Subscriber<Image> image1_sub(nh, "image1", 1);
message_filters::Subscriber<Image> image2_sub(nh, "image2", 1);
typedef sync_policies::ApproximateTime<Image, Image> MySyncPolicy;
// ApproximateTime takes a queue size as its constructor argument, hence MySyncPolicy(10)
Synchronizer<MySyncPolicy> sync(MySyncPolicy(10), image1_sub, image2_sub);
sync.registerCallback(boost::bind(&callback, _1, _2));
}
In fact it just looks like the tutorial of ros wiki about message filter. The fact is, my callback function had never been called.When I add "ros::spin()" in the construct function, however, it works. The issue is, I had wrote the construction like this before:
MyNode (ros::Nodehandle nh, ros:Nodehandle nh_local)
{
auto my_sub_ = nh.subscribe("topic", 10, &mycallback, this);
}
And there was no "ros::spin()" in the construct function but there was one in my_node.cpp's main().And it works too. So where should I indeed place my ros::spin()?