region_worker.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // Created by zx on 2019/12/9.
  3. //
  4. #include "region_worker.h"
  5. //#include "plc_data.h"
  6. Region_worker::Region_worker()
  7. {
  8. m_region_worker_status = E_UNKNOW;
  9. mp_cloud_collection_mutex = NULL;
  10. mp_cloud_collection = NULL;
  11. mp_auto_detect_thread = NULL;
  12. }
  13. /**
  14. * 析构
  15. * */
  16. Region_worker::~Region_worker()
  17. {
  18. }
  19. Error_manager Region_worker::init(Point2D_tool::Point2D_box point2D_box, std::mutex* p_cloud_collection_mutex,
  20. pcl::PointCloud<pcl::PointXYZ>::Ptr p_cloud_collection)
  21. {
  22. m_detector.init(point2D_box);
  23. mp_cloud_collection_mutex = p_cloud_collection_mutex;
  24. mp_cloud_collection = p_cloud_collection;
  25. m_auto_detect_condition.reset(false, false, false);
  26. mp_auto_detect_thread = new std::thread(&Region_worker::auto_detect_thread_fun, this);
  27. m_region_worker_status = E_READY;
  28. return Error_code::SUCCESS;
  29. }
  30. //唤醒自动预测的算法线程
  31. Error_manager Region_worker::wake_auto_detect()
  32. {
  33. //唤醒一次
  34. m_auto_detect_condition.notify_all(false, true);
  35. return Error_code::SUCCESS;
  36. }
  37. // 获得中心点、角度等测量数据
  38. Error_manager Region_worker::get_wheel_result(std::mutex* p_cloud_mutex, pcl::PointCloud<pcl::PointXYZ>::Ptr p_cloud_in, Common_data::Car_wheel_information& car_wheel_information)
  39. {
  40. return m_detector.detect(p_cloud_mutex, p_cloud_in, car_wheel_information);
  41. }
  42. // 获得中心点、角度等测量数据, 新算法, 可以测量前轮旋转
  43. Error_manager Region_worker::get_wheel_result_ex(std::mutex* p_cloud_mutex, pcl::PointCloud<pcl::PointXYZ>::Ptr p_cloud_in, Common_data::Car_wheel_information& car_wheel_information)
  44. {
  45. return m_detector.detect_ex(p_cloud_mutex, p_cloud_in, car_wheel_information);
  46. }
  47. cv::RotatedRect Region_worker::create_rotate_rect(float length,float width,float angle,float cx,float cy)
  48. {
  49. const double C_PI=3.14159265;
  50. float theta=C_PI*(angle/180.0);
  51. float a00=cos(theta);
  52. float a01=-sin(theta);
  53. float a10=sin(theta);
  54. float a11=cos(theta);
  55. cv::Point2f point[4];
  56. point[0].x=-length/2.0;
  57. point[0].y=-width/2.0;
  58. point[1].x=-length/2.0;
  59. point[1].y=width/2.0;
  60. point[2].x=length/2.0;
  61. point[2].y=-width/2.0;
  62. point[3].x=length/2.0;
  63. point[3].y=width/2.0;
  64. std::vector<cv::Point2f> point_vec;
  65. for(int i=0;i<4;++i)
  66. {
  67. float x=point[i].x*a00+point[i].y*a01+cx;
  68. float y=point[i].x*a10+point[i].y*a11+cy;
  69. point_vec.push_back(cv::Point2f(x,y));
  70. }
  71. return cv::minAreaRect(point_vec);
  72. }
  73. //自动预测的线程函数
  74. void Region_worker::auto_detect_thread_fun()
  75. {
  76. LOG(INFO) << " Wanji_manager::collect_cloud_thread_fun() start "<< this;
  77. Error_manager t_error;
  78. Error_manager t_result;
  79. while (m_auto_detect_condition.is_alive())
  80. {
  81. //暂时固定为一个扫描周期, 就循环一次
  82. //后期可以根据每个小雷达的刷新情况, 实时更改总点云.....
  83. m_auto_detect_condition.wait();
  84. if ( m_auto_detect_condition.is_alive() )
  85. {
  86. // std::cout << " huli test :::: " << " = " << "---------------------------------------------------------------------------------" << std::endl;
  87. //
  88. // std::cout << " huli test :::: " << " mp_cloud_collection->size() = " << mp_cloud_collection->size() << std::endl;
  89. //
  90. // auto start = std::chrono::system_clock::now();
  91. t_error = get_wheel_result_ex(mp_cloud_collection_mutex, mp_cloud_collection, m_car_wheel_information);
  92. // auto end = std::chrono::system_clock::now();
  93. // auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
  94. // std::cout << "花费了"
  95. // << double(duration.count()*1000) * std::chrono::microseconds::period::num / std::chrono::microseconds::period::den << "毫秒" << std::endl;
  96. if ( t_error == SUCCESS )
  97. {
  98. // std::cout << " huli test :::: " << " x = " << m_car_wheel_information.center_x << std::endl;
  99. // std::cout << " huli test :::: " << " y = " << m_car_wheel_information.center_y << std::endl;
  100. // std::cout << " huli test :::: " << " c = " << m_car_wheel_information.car_angle << std::endl;
  101. // std::cout << " huli test :::: " << " wheelbase = " << m_car_wheel_information.wheel_base << std::endl;
  102. // std::cout << " huli test :::: " << " width = " << m_car_wheel_information.wheel_width << std::endl;
  103. // std::cout << " huli test :::: " << " front_theta = " << m_car_wheel_information.front_theta << std::endl;
  104. // std::cout << " huli test :::: " << " correctness = " << m_car_wheel_information.correctness << std::endl;
  105. m_detect_updata_time = std::chrono::system_clock::now();
  106. }
  107. else
  108. {
  109. // std::cout << " huli test :::: " << " t_error = " << t_error << std::endl;
  110. }
  111. // std::cout << " huli test :::: " << " mp_cloud_collection->size() = " << mp_cloud_collection->size() << std::endl;
  112. //huli
  113. }
  114. }
  115. LOG(INFO) << " Wanji_manager::collect_cloud_thread_fun() end "<< this;
  116. return;
  117. }