How to use ApproximateTime policy in class in cpp?
Hi all. I am new in ROS and especially roscpp. So, my purpose is to get range values from 5 infrared sensors and print them in callback. After searching in google, I found that I have to use message_filters/ApproximateTime policy. I tried my best to apply. After compiling I have got many errors. While I could correct some of them, I couldn't handle others. Please, can you help me in order to find the source of error? Thank you very much :)
Edit: So, basically I made some corrections on my code and it compiles without any error. But, the problem is the allback function doesn't be called. What can be the problem? Thank you :)
My code (Didn't append main function):
#include <ros/ros.h>
#include <nav_msgs/Odometry.h>
#include <sensor_msgs/Range.h>
#include <message_filters/subscriber.h>
#include <message_filters/synchronizer.h>
#include <message_filters/sync_policies/approximate_time.h>
#include <math.h>
using namespace nav_msgs;
using namespace sensor_msgs;
using namespace message_filters;
/*
sensors 0-4 are ultrasonic sensors
sensors 5-10 are infrared sensors
*/
class AvoidObstacle
{
private:
ros::NodeHandle nh;
public:
void ReadDistanceSensors(const RangeConstPtr&,const RangeConstPtr&,const RangeConstPtr&,const RangeConstPtr&,const RangeConstPtr&);
AvoidObstacle();
};
AvoidObstacle::AvoidObstacle()
{
message_filters::Subscriber<Range> sonar0_sub(nh, "/robot0/sonar_0", 1);
message_filters::Subscriber<Range> sonar1_sub(nh, "/robot0/sonar_1", 1);
message_filters::Subscriber<Range> sonar2_sub(nh, "/robot0/sonar_2", 1);
message_filters::Subscriber<Range> sonar3_sub(nh, "/robot0/sonar_3", 1);
typedef sync_policies::ApproximateTime<Range, Range, Range, Range, Range> MySyncPolicy;
Synchronizer<MySyncPolicy> sync(MySyncPolicy(10), sonar0_sub, sonar1_sub, sonar2_sub, sonar3_sub, sonar4_sub);
sync.registerCallback(boost::bind(&AvoidObstacle::ReadDistanceSensors, this, _1, _2, _3, _4, _5));
ROS_INFO("Hello");
}
void AvoidObstacle::ReadDistanceSensors(const RangeConstPtr& sn0,const RangeConstPtr& sn1,const RangeConstPtr& sn2,const RangeConstPtr& sn3,const RangeConstPtr& sn4)
{
ROS_INFO("range: %lf", sn0->range);
}