laser_odom.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include <memory>
  2. #include <iostream>
  3. #include <ros/ros.h>
  4. #include <ros/time.h>
  5. #include <ros/duration.h>
  6. #include <pcl_ros/point_cloud.h>
  7. #include <tf_conversions/tf_eigen.h>
  8. #include <tf/transform_listener.h>
  9. #include <tf/transform_broadcaster.h>
  10. #include <std_msgs/Time.h>
  11. #include <nav_msgs/Odometry.h>
  12. #include <sensor_msgs/PointCloud2.h>
  13. #include <geometry_msgs/TransformStamped.h>
  14. #include <geometry_msgs/PoseWithCovarianceStamped.h>
  15. #include <nodelet/nodelet.h>
  16. #include <pluginlib/class_list_macros.h>
  17. #include <pcl/filters/voxel_grid.h>
  18. #include <pcl/filters/passthrough.h>
  19. #include <pcl/filters/approximate_voxel_grid.h>
  20. #include <hdl_graph_slam/ros_utils.hpp>
  21. #include <hdl_graph_slam/registrations.hpp>
  22. #include <hdl_graph_slam/ScanMatchingStatus.h>
  23. #include <laser_geometry/laser_geometry.h>
  24. #include <fast_gicp/gicp/fast_gicp.hpp>
  25. #include <queue>
  26. #include <thread>
  27. #include <mutex>
  28. #include <glog/logging.h>
  29. #include <new>
  30. #undef new
  31. typedef pcl::PointXYZI PointT;
  32. struct MatchPCL {
  33. ros::Time time;
  34. pcl::PointCloud<PointT> cloud;
  35. };
  36. // namespace laser_odom_ns {
  37. class Laser_Odom {
  38. private:
  39. laser_geometry::LaserProjection projector;
  40. sensor_msgs::PointCloud2 cloud2; // 点云数据类型转换的中间变量
  41. pcl::PointCloud<PointT>::Ptr cloud; // 点云数据
  42. pcl::PointCloud<PointT>::Ptr trans_cloud; // 点云数据
  43. pcl::PointCloud<PointT>::ConstPtr keyframe; // 关键帧
  44. ros::Time prev_time;
  45. Eigen::Matrix4f prev_trans; // 地图起点到当前 帧 的放射变换
  46. Eigen::Matrix4f keyframe_pose; // 地图起点到当前 关键帧 的仿射变换
  47. ros::Time keyframe_stamp; // 关键帧的时间戳
  48. // pcl::Registration<PointT, PointT>::Ptr registration;
  49. fast_gicp::FastGICP<PointT, PointT>::Ptr registration;
  50. pcl::Filter<PointT>::Ptr downsample_filter;
  51. double keyframe_delta_trans = 0.25;
  52. double keyframe_delta_angle = 0.15;
  53. double keyframe_delta_time = 1.0;
  54. // std::string odom_frame_id = "odom";
  55. tf::TransformBroadcaster keyframe_broadcaster;
  56. tf::TransformBroadcaster odom_broadcaster;
  57. ros::NodeHandle nh;
  58. ros::Publisher odom_pub; // 里程计发布
  59. ros::Publisher path_pub;
  60. // ros::Publisher read_until_pub; //
  61. ros::Publisher aligned_points_pub;
  62. ros::Publisher map_points_pub;
  63. ros::Publisher keyframe_points_pub;
  64. static Laser_Odom* laser_odom;
  65. Eigen::Matrix4f odom;
  66. double downsample_resolution = 0.1; // 下采样分辨率
  67. ros::NodeHandle private_nh;
  68. private:
  69. void procThread();
  70. std::mutex mutex_;
  71. std::queue<MatchPCL> clouds_;
  72. public:
  73. Laser_Odom();
  74. ~Laser_Odom();
  75. void init();
  76. void cloud_callback(const sensor_msgs::LaserScan::ConstPtr& laser_scan_msg);
  77. Eigen::Matrix4f matching(const ros::Time& stamp, const pcl::PointCloud<PointT>::ConstPtr& cloud);
  78. pcl::PointCloud<PointT>::ConstPtr downsample(const pcl::PointCloud<PointT>::ConstPtr& cloud);
  79. geometry_msgs::TransformStamped matrix2transform(const ros::Time& stamp, const Eigen::Matrix4f& pose, const std::string& frame_id, const std::string& child_frame_id);
  80. void publish_odometry(const ros::Time& stamp, const std::string& base_frame_id, const Eigen::Matrix4f& pose);
  81. void publish_keyfrrame(const ros::Time& stamp, const std::string& base_frame_id, const Eigen::Matrix4f& pose); // 发布里程计数据
  82. };
  83. // }; // namespace laser_odom_ns