clamp_safety_detect.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // Created by zx on 22-8-30.
  3. //
  4. #pragma once
  5. #include <pcl/point_types.h>
  6. #include <pcl/point_cloud.h>
  7. #include <pcl/io/pcd_io.h>
  8. #include <pcl/segmentation/sac_segmentation.h>
  9. #include <pcl/ModelCoefficients.h>
  10. #include <pcl/filters/extract_indices.h>
  11. #include <pcl/segmentation/extract_clusters.h>
  12. #include <pcl/point_representation.h>
  13. #include <pcl/filters/statistical_outlier_removal.h>
  14. #include <pcl/visualization/cloud_viewer.h>
  15. #include <pcl/common/common_headers.h>
  16. #include <glog/logging.h>
  17. #include <iostream>
  18. #include <cmath>
  19. #if __VIEW__PCL
  20. #include <pcl/visualization/pcl_visualizer.h>
  21. #endif
  22. //ax+by+c=0
  23. typedef struct {
  24. float a;
  25. float b;
  26. float c;
  27. float cx;
  28. float cy;
  29. float theta_by_x;
  30. float cov;
  31. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud;
  32. } Line;
  33. class Safety_status {
  34. public:
  35. Safety_status() {
  36. clamp_completed = false;
  37. wheel_exist = false;
  38. cx = 0;
  39. gap = 0;
  40. time_point = std::chrono::steady_clock::now();
  41. timeout_s = 0.5;
  42. };
  43. ~Safety_status() = default;
  44. Safety_status(const Safety_status &other) {
  45. clamp_completed = other.clamp_completed;
  46. wheel_exist = other.wheel_exist;
  47. cx = other.cx;
  48. gap = other.gap;
  49. time_point = other.time_point;
  50. timeout_s = other.timeout_s;
  51. }
  52. Safety_status &operator=(const Safety_status &other) {
  53. clamp_completed = other.clamp_completed;
  54. wheel_exist = other.wheel_exist;
  55. cx = other.cx;
  56. gap = other.gap;
  57. time_point = other.time_point;
  58. timeout_s = other.timeout_s;
  59. return *this;
  60. }
  61. void set_timeout(float s) {
  62. timeout_s = s;
  63. }
  64. bool is_timeout() const {
  65. auto t2 = std::chrono::steady_clock::now();
  66. auto duration = std::chrono::duration_cast<std::chrono::microseconds>(t2 - time_point);
  67. double time = double(duration.count()) * std::chrono::microseconds::period::num /
  68. std::chrono::microseconds::period::den;
  69. DLOG(INFO) << "time " << time;
  70. return time > timeout_s;
  71. }
  72. bool clamp_completed;
  73. bool wheel_exist; //点云直线是否是轮子
  74. float cx;
  75. float gap; //轮子距离雷达
  76. std::chrono::steady_clock::time_point time_point;
  77. float timeout_s;
  78. std::mutex mutex_;
  79. };
  80. class clamp_safety_detect {
  81. public:
  82. clamp_safety_detect();
  83. ~clamp_safety_detect();
  84. #if __VIEW__PCL
  85. void set_viewer(pcl::visualization::PCLVisualizer* m_viewer,int port);
  86. #endif
  87. bool detect(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, Safety_status &safety);
  88. bool fit_line(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud, Line &line);
  89. bool check_wheel_line(const Line &line, Safety_status &safety);
  90. protected:
  91. std::vector<pcl::PointCloud<pcl::PointXYZ>::Ptr> classify_cloud(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud);
  92. bool check_clamp_lines(const Line &line1, const Line &line2, Safety_status &safety);
  93. #if __VIEW__PCL
  94. void view_cloud(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud,std::string name,int r,int g,int b);
  95. pcl::visualization::PCLVisualizer* m_viewer;
  96. int m_port;
  97. #endif
  98. };