1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| #include <ros/ros.h> #include <sensor_msgs/PointCloud2.h> #include <geometry_msgs/PoseStamped.h> #include <message_filters/subscriber.h> #include <message_filters/synchronizer.h> #include <message_filters/sync_policies/approximate_time.h>
void callback(const sensor_msgs::PointCloud2ConstPtr &msg0, const sensor_msgs::PointCloud2ConstPtr &msg1, const sensor_msgs::PointCloud2ConstPtr &msg2, const sensor_msgs::PointCloud2ConstPtr &msg3, const geometry_msgs::PoseStampedConstPtr &msg4) { std::cout << std::fixed << "/back_lidar: " << msg3->header.stamp.toSec() << std::endl << "/left_lidar: " << msg1->header.stamp.toSec() << std::endl << "/middle_lidar: " << msg0->header.stamp.toSec() << std::endl << "/pose_stamp: " << msg4->header.stamp.toSec() << std::endl << "/right_lidar: " << msg2->header.stamp.toSec() << std::endl; std::cout << "-------------------------------------------------" << std::endl; }
int main(int argc, char **argv){
ros::init(argc, argv, "ros_sync_test"); ros::NodeHandlePtr node_ptr; node_ptr.reset(new ros::NodeHandle);
std::string topic0, topic1, topic2, topic3, topic4; topic0 = "/mems_front/rslidar_points"; topic1 = "/mems_left/rslidar_points"; topic2 = "/mems_right/rslidar_points"; topic3 = "/mems_back/rslidar_points"; topic4 = "/loc/car_pose_1";
message_filters::Subscriber<sensor_msgs::PointCloud2> sub_topic0(*node_ptr, topic0, 1000); message_filters::Subscriber<sensor_msgs::PointCloud2> sub_topic1(*node_ptr, topic1, 1000); message_filters::Subscriber<sensor_msgs::PointCloud2> sub_topic2(*node_ptr, topic2, 1000); message_filters::Subscriber<sensor_msgs::PointCloud2> sub_topic3(*node_ptr, topic3, 1000); message_filters::Subscriber<geometry_msgs::PoseStamped> sub_topic4(*node_ptr, topic4, 1000);
typedef message_filters::sync_policies::ApproximateTime<sensor_msgs::PointCloud2, sensor_msgs::PointCloud2, sensor_msgs::PointCloud2, sensor_msgs::PointCloud2, geometry_msgs::PoseStamped> syncPolicy;
message_filters::Synchronizer<syncPolicy> sync(syncPolicy(100), sub_topic0, sub_topic1, sub_topic2, sub_topic3, sub_topic4);
sync.registerCallback(boost::bind(&callback, _1, _2, _3, _4, _5));
ros::spin(); return 0; }
|