ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

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

Hi,

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;
bag.open("mydata.bag", rosbag::bagmode::Read);
std::vector<std::string> bag_topics;

bag_topics.push_back(std::string("/points_core"));
bag_topics.push_back(std::string("/time_reference"));

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>();
           ros_pub.publish(ptcld2);

        }   
    }// foreach
    ros::spinOnce();
    loop_rate.sleep();
}//while

bag.close();

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
0

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

Question Tools

1 follower

Stats

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

Seen: 780 times

Last updated: Jan 10 '20