Ask Your Question

Reading and Publishing bag file with PointCloud2 and TimeReference topics in C++

asked 2020-01-09 23:38:54 -0500

sunkingac4 gravatar image

updated 2020-01-09 23:58:18 -0500


I have a bag file with 2 topics.

First topic has sequence of lidar data of PointCloud2 message type and the other topic has TimeReference message type.

I have read to read the bag file messages and publish the PointCloud2 messages (around 500 messages) in sync with the TimeReference messages (around 150 messages) in C++.

The below code is publishing the data without synchronization.

ros::init (argc, argv, "ros_create");
ros::NodeHandle nh;
ros_pub = nh.advertise<sensor_msgs::PointCloud2> ("ros_output", 1);
ros::Publisher ros_pub;

rosbag::Bag bag;"mydata.bag", rosbag::bagmode::Read);
std::vector<std::string> bag_topics;


rosbag::View view(bag, rosbag::TopicQuery(bag_topics));
ros::Rate loop_rate(1);
while (ros::ok())
    foreach(rosbag::MessageInstance const m, view)
        if ( std::strcmp( m.getTopic().c_str(), "/time_reference") == 0)
            // Time Reference Messages...
        if ( std::strcmp( m.getTopic().c_str(), "/points_core") == 0)
           sensor_msgs::PointCloud2::Ptr ptcld2 = m.instantiate<sensor_msgs::PointCloud2>();

    }// foreach


How to achieve the synchronisation between these two topic messages?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2020-01-10 02:40:45 -0500

pavel92 gravatar image

If you want to synchronize topics then check message_filters. Here you can find tutorials and examples of how to sync topics based on conditions you specify.
Also you can check this question and this example for more information on the use of message_filters.

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

1 follower


Asked: 2020-01-09 23:38:54 -0500

Seen: 514 times

Last updated: Jan 10 '20