sync_drivers.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright 2015-2019 Autoware Foundation. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* ----header---- */
  17. /* common header */
  18. #include "ros/ros.h"
  19. #include <sstream>
  20. #include <boost/circular_buffer.hpp>
  21. #include "std_msgs/Header.h"
  22. /* user header */
  23. #include "sensor_msgs/Image.h"
  24. #include "sensor_msgs/PointCloud2.h"
  25. #include "autoware_msgs/SyncTimeDiff.h"
  26. /* ----var---- */
  27. /* common var */
  28. /* user var */
  29. sensor_msgs::Image image_raw_buf;
  30. ros::Publisher points_raw__pub;
  31. ros::Publisher image_raw__pub;
  32. ros::Publisher time_diff_pub;
  33. bool is_sim;
  34. double fabs_time_diff(const std_msgs::Header *timespec1, const std_msgs::Header *timespec2)
  35. {
  36. double time1 = (double)timespec1->stamp.sec + (double)timespec1->stamp.nsec/1000000000L;
  37. double time2 = (double)timespec2->stamp.sec + (double)timespec2->stamp.nsec/1000000000L;
  38. return fabs(time1 - time2);
  39. }
  40. void image_raw_callback(sensor_msgs::Image image_raw_msg)
  41. {
  42. image_raw_buf = image_raw_msg;
  43. }
  44. void points_raw_callback(const sensor_msgs::PointCloud2::ConstPtr& points_raw_msg)
  45. {
  46. autoware_msgs::SyncTimeDiff time_diff_msg;
  47. time_diff_msg.header.frame_id = "0";
  48. time_diff_msg.header.stamp = points_raw_msg->header.stamp;
  49. time_diff_msg.time_diff = fabs_time_diff(&(points_raw_msg->header), &image_raw_buf.header)*1000.0; //msec
  50. time_diff_msg.camera = image_raw_buf.header.stamp;
  51. time_diff_msg.lidar = points_raw_msg->header.stamp;
  52. time_diff_pub.publish(time_diff_msg);
  53. image_raw_buf.header.stamp = points_raw_msg->header.stamp;
  54. image_raw__pub.publish(image_raw_buf);
  55. points_raw__pub.publish(points_raw_msg);
  56. }
  57. int main(int argc, char **argv)
  58. {
  59. ros::init(argc, argv, "sync_drivers");
  60. ros::NodeHandle nh;
  61. ros::NodeHandle private_nh("~");
  62. ros::Subscriber image_raw_sub = nh.subscribe("/image_raw", 1, image_raw_callback);
  63. ros::Subscriber points_raw_sub;
  64. points_raw_sub = nh.subscribe("/points_raw", 1, points_raw_callback);
  65. image_raw__pub = nh.advertise<sensor_msgs::Image>("image_raw", 1);
  66. points_raw__pub = nh.advertise<sensor_msgs::PointCloud2>("points_raw", 1);
  67. time_diff_pub = nh.advertise<autoware_msgs::SyncTimeDiff>("/time_difference", 1);
  68. ros::spin();
  69. return 0;
  70. }