I firgured it out!
Put the below code into class member.
typedef message_filters::sync_policies::ApproximateTime<sensor_msgs::Image, sensor_msgs::Image> sync_pol;
message_filters::Synchronizer<sync_pol> *sync;
And then in the ClientHandler::ClientHandler
function put those two lines.
sync = new message_filters::Synchronizer<sync_pol>(sync_pol(10), rgb_sub,depth_sub);
sync->registerCallback(boost::bind(&ClientHandler::GrabRGBD,this,_1,_2));
For a more general view, based on @ravijoshi 's code, I modified it into two topics.
#include <ros/ros.h>
#include <message_filters/subscriber.h>
#include <sensor_msgs/Image.h>
using namespace sensor_msgs;
using namespace message_filters;
class ClientHandler
{
public:
ClientHandler(ros::NodeHandle& nh, std::string& topic_name_rgb,std::string& topic_name_sub);
void GrabRGBD(const ImageConstPtr& image_rgb,const ImageConstPtr& image_depth);
private:
ros::NodeHandle n;
std::string topic_rgb,topic_depth;
//add members in two topics situation
message_filters::Subscriber<Image> rgb_sub;
message_filters::Subscriber<Image> depth_sub;
typedef message_filters::sync_policies::ApproximateTime<sensor_msgs::Image, sensor_msgs::Image> sync_pol;
message_filters::Synchronizer<sync_pol> *sync;
};
ClientHandler::ClientHandler(ros::NodeHandle& nh, std::string& topic_name_rgb,std::string& topic_name_depth) : n(nh), topic_rgb(topic_name_rgb),topic_depth(topic_name_depth)
{
// the following declaration does not work. hence it is commented.
// the subscriber is in a local scope
// message_filters::Subscriber<Image> rgb_sub(n, topic, 1);
// let's make them a class member and then call subscribe as shown below
rgb_sub.subscribe(n, topic_rgb, 1);
depth_sub.subscribe(n,topic_depth,1);
// here is for the synchronizer
sync = new message_filters::Synchronizer<sync_pol>(sync_pol(10), rgb_sub,depth_sub);
// now bind a callback
sync->registerCallback(boost::bind(&ClientHandler::GrabRGBD,this,_1,_2));
}
void ClientHandler::GrabRGBD(const ImageConstPtr& image_rgb , const ImageConstPtr& image_depth)
{
ROS_INFO("Sequence: [%d] [%d]", image_rgb->header.seq, imgage_depth->header.seq);
}
int main(int argc, char** argv)
{
ros::init(argc, argv, "image_listener");
ros::NodeHandle n;
std::string topic_rgb = "/usb_cam/image_raw";
std::string topic_depth = "/usb_cam/camera_info";
ClientHandler ClientHandler(n, topic_rgb,topic_depth);
ros::spin();
return 0;
}
Now it can receive two topics at the same time!
Hope this will help you guys with the same problem!
Hi @ravijoshi, can you please help me on this? Thank you so much!