0
$\begingroup$

Rosanswers logo

Hi, I am new to ROS and I need a help for time synchronization. When I synchronize Velodyne points with ZED Images, they are synchronized without any problem (I attached this working code below). But, when I synchronize Velodyne points with other PointCloud2 data which is published by a node that I created, the callback function in my code never get called. I think both have the same type of time header since they both are PointCloud2 but I don't know why the time synchronizer doesn't work in this case. I tried to use Approximate/Exact but none of them works. Please advise.

Working code.

ros::Publisher data1_pub;

ros::Publisher data2_pub;

void callback(const sensor_msgs::Image::ConstPtr& data1, const sensor_msgs::PointCloud2::ConstPtr& data2)
{
  ROS_INFO("Sync_Callback");

  data1_pub.publish(data1);
  
 data2_pub.publish(data2);

}

int main(int argc, char** argv)

{

  ros::init(argc, argv, "TimeSynchronizer");

  std::string data1_topic = "/zed/right/image_rect_color";
 
  std::string data2_topic = "/velodyne_points";
  
  ros::NodeHandle nh;
 
  message_filters::Subscriber<sensor_msgs::Image> data1_sub(nh, data1_topic, 1000);
  
  message_filters::Subscriber<sensor_msgs::PointCloud2> data2_sub(nh, data2_topic, 1000);
  
  data1_pub = nh.advertise<sensor_msgs::Image>("/synchronized" + data1_topic, 1000);
  
  data2_pub = nh.advertise<sensor_msgs::PointCloud2>("/synchronized" + data2_topic, 1000);
  
  TimeSynchronizer<Image, PointCloud2> sync(data1_sub, data2_sub,10);
  
   sync.registerCallback(boost::bind(&callback, _1, _2));

  ros::spin();

  return 0;

}

Not Working code.

ros::Publisher data1_pub;

ros::Publisher data2_pub;

void callback(const sensor_msgs::PointCloud2::ConstPtr& data1, const sensor_msgs::PointCloud2::ConstPtr& data2)
{

  ROS_INFO("Sync_Callback");

  data1_pub.publish(data1);
  
  data2_pub.publish(data2);

}

int main(int argc, char** argv)

{

  ros::init(argc, argv, "TimeSynchronizer");

  std::string data1_topic = "/velodyne_points";
 
  std::string data2_topic = "/chatter";
  
  ros::NodeHandle nh;
 
  message_filters::Subscriber<sensor_msgs::PointCloud2> data1_sub(nh, data1_topic, 1000);
  
  message_filters::Subscriber<sensor_msgs::PointCloud2> data2_sub(nh, data2_topic, 1000);
  
  data1_pub = nh.advertise<sensor_msgs::PointCloud2>("/synchronized" + data1_topic, 1000);
  
  data2_pub = nh.advertise<sensor_msgs::PointCloud2>("/synchronized" + data2_topic, 1000);
  
  TimeSynchronizer<PointCloud2, PointCloud2> sync(data1_sub, data2_sub,10);
  
   sync.registerCallback(boost::bind(&callback, _1, _2));

  ros::spin();

  return 0;
}

"talker" node which publishes PointCloud2 data to /chatter topic

int main (int argc, char **argv){

    ros::init(argc, argv, "talker");

    ros::NodeHandle n;

        ros::Publisher chatter_pub = n.advertise<sensor_msgs::PointCloud2>("chatter", 1000);
    
        ros::Rate loop_rate(30); 

        int speed = 100;

    int count = 0;

    while (ros::ok()){

                sensor_msgs::PointCloud2 msg;

                msg.height = speed;

                ROS_INFO("%d",msg.height);
      
        chatter_pub.publish(msg);

                ros::spinOnce(); 

        loop_rate.sleep();

        ++count;

    }

    return 0;

}

Originally posted by roskinetic on ROS Answers with karma: 39 on 2018-10-03

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

The time synchronization uses the header timestamp to find pairs of messages. Make sure to set the header timestamp in your talker node.

msg.header.stamp = ros::Time::now();

Originally posted by kmhallen with karma: 1416 on 2018-10-03

This answer was ACCEPTED on the original site

Post score: 0


Original comments

Comment by roskinetic on 2018-10-04:
Thank you! It works now.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.