localizer.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * \file localizer.h
  3. * \brief 简要介绍
  4. * \details 详细说明
  5. * \author lenotary
  6. * \version 1.0
  7. * \date 2021/6/23
  8. * \pre 先决条件。(有的话才添加。)
  9. * \bug 存在的bug。(有的话才添加,注明“还未发现”也可以。)
  10. * \warning 警告。(有的话才添加。)
  11. * \copyright 版权声明。(通常只写遵循什么协议,版权详细内容应放在LICENSE文件中,并且应该与LICENSE文件的内容统一,不能自相矛盾。)
  12. * \since 一些历史情况记录。(有的话才添加。)
  13. */
  14. //
  15. // Created by lenotary on 2021/6/23.
  16. //
  17. #ifndef SRC_LOCALIZER_H
  18. #define SRC_LOCALIZER_H
  19. #include <thread>
  20. #include <mutex>
  21. #include <geometry_msgs/PoseWithCovarianceStamped.h>
  22. #include <geometry_msgs/TwistStamped.h>
  23. #include <nav_msgs/Odometry.h>
  24. #include <nav_msgs/Path.h>
  25. #include <sensor_msgs/PointCloud2.h>
  26. #include <sensor_msgs/Imu.h>
  27. #include <std_msgs/Float32.h>
  28. #include <std_msgs/String.h>
  29. #include <ros/ros.h>
  30. #include <ros/node_handle.h>
  31. #include <pcl/point_types.h>
  32. #include <pcl_conversions/pcl_conversions.h>
  33. #include <pcl_ros/transforms.h>
  34. #include <pcl/registration/ndt.h>
  35. #include <diagnostic_msgs/DiagnosticArray.h>
  36. #include <tf2/transform_datatypes.h>
  37. #include <tf2_geometry_msgs/tf2_geometry_msgs.h>
  38. #include <tf2_ros/transform_broadcaster.h>
  39. #include <tf2_eigen/tf2_eigen.h>
  40. #include <tf2_ros/transform_listener.h>
  41. #include <sensor_msgs/PointCloud2.h>
  42. #include <geometry_msgs/PoseWithCovarianceStamped.h>
  43. #include <geometry_msgs/TransformStamped.h>
  44. #include "extended_kalman_filter.h"
  45. #include <fast_gicp/gicp/fast_vgicp.hpp>
  46. #include "common.h"
  47. #include <fast_gicp/ndt/ndt_cuda.hpp>
  48. //#include <fast_gicp/gicp/fast_vgicp_cuda.hpp>
  49. #define MAX_PATH_SIZE 1000000
  50. class Localizer {
  51. public:
  52. Localizer(ros::NodeHandle& nh, ros::NodeHandle &privateNh);
  53. ~Localizer();
  54. bool GetTransform(const std::string &cFrameTarget, const std::string &cFrameSource, const geometry_msgs::TransformStamped::Ptr &pTransformStamped);
  55. static void Split(const std::string& s, std::vector<double>& tokens, const std::string& cDelimiters = " ");
  56. private:
  57. ExtendedKalmanFilter m_iExtendedKalmanFilter_;
  58. ros::NodeHandle m_iNh_;
  59. ros::NodeHandle m_iNhPrivate_;
  60. //订阅的话题
  61. ros::Subscriber m_iSubInitPose_;
  62. ros::Subscriber m_iSubMap_;
  63. ros::Subscriber m_iSubLidarCloud_;
  64. ros::Subscriber m_iSubOdom_;
  65. //发布的话题
  66. ros::Publisher m_iPubAlignedPointCloud_;
  67. ros::Publisher m_iPubOdom_;
  68. ros::Publisher m_iPubExeTime_; //计算时间
  69. ros::Publisher m_iPubAlignedScore_;
  70. ros::Publisher m_iPubDiagnostics_;
  71. ros::Publisher m_iPubTrajectory_;
  72. ros::Publisher m_iPubAlignedPose_;
  73. ros::Publisher m_iPubLocalizerState_;
  74. tf2_ros::Buffer m_iTf2Buffer_;
  75. tf2_ros::TransformListener m_iTf2Listener_;
  76. tf2_ros::TransformBroadcaster m_iTfBroadcaster_;
  77. //map
  78. std::map<std::string,std::string> m_iMapKeyValue_;
  79. geometry_msgs::PoseWithCovarianceStamped m_iInitialPoseMsg_;
  80. std::string m_cFrameMap_;
  81. std::string m_cFrameBase_;
  82. bool m_bInitPose_;
  83. bool m_bIsGetMap_;
  84. //ekf
  85. Eigen::MatrixXd m_iMatrixXdEkfQ;
  86. Eigen::MatrixXd m_iMatrixXdEkfR;
  87. double m_dOdomToLidar_;
  88. //aligned
  89. std::string m_cRegName_;
  90. fast_gicp::FastVGICP<pcl::PointXYZ, pcl::PointXYZ> m_iAlignedFVGIcpMt_;
  91. // fast_gicp::FastVGICPCuda<pcl::PointXYZ, pcl::PointXYZ> m_iAlignedFVGIcpCuda_;
  92. pcl::NormalDistributionsTransform<pcl::PointXYZ,pcl::PointXYZ> m_iAlignedNdt_;
  93. Eigen::Matrix4f m_iPreTrans_, m_iDeltaTrans_;
  94. bool m_bIsAligned_;
  95. bool m_bIsLocation_;
  96. geometry_msgs::PoseStamped m_iMsgPoseStampedResult_;
  97. std::queue<geometry_msgs::PoseStamped> m_qMsgPoseStampedResult_;
  98. std::mutex m_iMtxMap_;
  99. std::thread m_tThreadPointCloud_;
  100. double m_dExeTime_;
  101. double m_dScore_;
  102. double m_dCoverScoreThreshold_;
  103. double m_dMaxAlignedTime_;
  104. pcl::PointCloud<pcl::PointXYZ>::Ptr m_pAlignedPoses_;
  105. nav_msgs::Path m_iPathWithEkf_;
  106. //lidar
  107. double m_dTimeLidar_;
  108. std::queue<sensor_msgs::PointCloud2> m_qPointCloudInput_;
  109. //地图
  110. pcl::PointCloud<pcl::PointXYZ>::Ptr m_pPointClodMap_;
  111. //odom
  112. std::queue<nav_msgs::Odometry> m_qOdom_;
  113. private:
  114. void __InitLocalizer();
  115. void __OnCallbackInitPose(const geometry_msgs::PoseWithCovarianceStamped::ConstPtr &msg);
  116. void __OnCallBackMap(const sensor_msgs::PointCloud2::ConstPtr &msg);
  117. void __OnCallBackPointCloud(const sensor_msgs::PointCloud2::ConstPtr &msg);
  118. void __OnCallBackOdom(const nav_msgs::Odometry::ConstPtr &msg);
  119. void __PublishTF(const std::string &cFrame, const std::string &cFrameChild,const geometry_msgs::PoseStamped &iPoseMsg);
  120. template <typename Registration>
  121. double __AlignedPcl(Registration& reg,const pcl::PointCloud<pcl::PointXYZ>::ConstPtr& source, const Eigen::Matrix4f &iMatrix4fInitPose,Eigen::Matrix4f &iMatrix4fTransform);
  122. template <typename Registration>
  123. double __Aligned(Registration& reg,const pcl::PointCloud<pcl::PointXYZ>::ConstPtr& source, const Eigen::Matrix4f &iMatrix4fInitPose,Eigen::Matrix4f &iMatrix4fTransform);
  124. void __InitAligned(pcl::PointCloud<pcl::PointXYZ>::Ptr pPointCloudMap);
  125. void __ProcessPointCloud(void);
  126. void __Relocation(sensor_msgs::PointCloud2 iMsgCloudInput);
  127. };
  128. #endif //SRC_LOCALIZER_H