region_worker.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // Created by zx on 2019/12/9.
  3. //
  4. #ifndef REGION_WORKER_H
  5. #define REGION_WORKER_H
  6. #include "region_detect.h"
  7. //#include "../tool/StdCondition.h"
  8. #include <thread>
  9. #include <mutex>
  10. #include <iostream>
  11. #include <atomic>
  12. #include <chrono>
  13. #include "../error_code/error_code.h"
  14. #include "../verify/Verify_result.h"
  15. #include "../tool/thread_condition.h"
  16. #include "../tool/common_data.h"
  17. /**
  18. * 区域功能类,负责自动检测区域状态并更新到plc
  19. * */
  20. class Region_worker
  21. {
  22. public:
  23. #define REGION_WORKER_RESULT_DEFAULT 0x0000
  24. //#define REGION_WORKER_VERIFY_OK 0x0001
  25. //#define REGION_WORKER_EMPTY_SPACE 0x0020
  26. //#define REGION_WORKER_CLUSTER_SIZE_ERROR 0x0040
  27. //#define REGION_WORKER_OTHER_ERROR 0x0080
  28. //#define REGION_WORKER_NULL_POINTER 0x0100
  29. #define REGION_WORKER_EMPTY_SPACE 1
  30. #define REGION_WORKER_HAS_CAR 2
  31. #define REGION_WORKER_DETECT_ERROR 3
  32. //区域功能类 的预测算法的时间周期, 一般为100ms, 我们这里设一个较大值200ms
  33. #define REGION_WORKER_DETECT_CYCLE_MS 200
  34. //万集区域功能的状态
  35. enum Region_worker_status
  36. {
  37. E_UNKNOWN = 0, //未知
  38. E_READY = 1, //准备,待机
  39. E_BUSY = 2, //工作正忙
  40. E_FAULT = 10, //故障
  41. };
  42. public:
  43. Region_worker();
  44. ~Region_worker();
  45. Error_manager init(Point2D_tool::Point2D_box point2D_box, std::mutex* p_cloud_collection_mutex,
  46. pcl::PointCloud<pcl::PointXYZ>::Ptr p_cloud_collection);
  47. // 初始化传入编号,仅普爱使用
  48. Error_manager init(Point2D_tool::Point2D_box point2D_box, std::mutex* p_cloud_collection_mutex,
  49. pcl::PointCloud<pcl::PointXYZ>::Ptr p_cloud_collection, int index);
  50. //开始自动预测的算法线程
  51. Error_manager start_auto_detect();
  52. //关闭自动预测的算法线程
  53. Error_manager stop_auto_detect();
  54. // 获得中心点、角度等测量数据
  55. Error_manager detect_wheel_result(std::mutex* p_cloud_mutex, pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_in,
  56. Common_data::Car_wheel_information& car_wheel_information);
  57. // 获得中心点、角度等测量数据, 新算法, 可以测量前轮旋转
  58. Error_manager detect_wheel_result_ex(std::mutex* p_cloud_mutex, pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_in,
  59. Common_data::Car_wheel_information& car_wheel_information);
  60. //外部调用获取新的车轮定位信息, 获取指令时间之后的车轮定位信息, 如果没有就会等待车轮定位信息刷新, 直到超时, (内置while)
  61. Error_manager get_new_wheel_information_and_wait(Common_data::Car_wheel_information* p_car_wheel_information,
  62. std::chrono::system_clock::time_point command_time, int timeout_ms=1500);
  63. //外部调用获取当前车轮定位信息, 获取指令时间之后的车轮定位信息, 如果没有就会报错, 不会等待
  64. Error_manager get_current_wheel_information(Common_data::Car_wheel_information* p_car_wheel_information,
  65. std::chrono::system_clock::time_point command_time);
  66. //外部调用获取最新的车轮定位信息, 获取指令时间往前一个周期内的车轮定位信息, 如果没有就会报错, 不会等待
  67. Error_manager get_last_wheel_information(Common_data::Car_wheel_information* p_car_wheel_information,
  68. std::chrono::system_clock::time_point command_time);
  69. public:
  70. Region_worker_status get_status();
  71. std::chrono::system_clock::time_point get_detect_updata_time();
  72. protected:
  73. static cv::RotatedRect create_rotate_rect(float length,float width,float angle,float x,float y);
  74. //自动预测的线程函数
  75. void auto_detect_thread_fun();
  76. protected:
  77. int m_index;// 区域编号,仅普爱使用
  78. //状态
  79. Region_worker_status m_region_worker_status; //万集区域功能的状态
  80. Region_detector m_detector; // 区域检测算法实例
  81. //万集雷达的自动功能, 定时收集所有点云, 然后通知detect去计算车轮信息.
  82. std::mutex* mp_cloud_collection_mutex; // 点云更新互斥锁, 内存由上级管理
  83. pcl::PointCloud<pcl::PointXYZ>::Ptr mp_cloud_collection; //扫描点的点云集合, 内存由上级管理
  84. Common_data::Car_wheel_information m_car_wheel_information; //车轮的定位结果,
  85. std::chrono::system_clock::time_point m_detect_updata_time; //定位结果的更新时间.
  86. std::thread* mp_auto_detect_thread; //自动预测的线程指针,内存由本类管理
  87. Thread_condition m_auto_detect_condition; //自动预测的条件变量
  88. // pcl::PointCloud<pcl::PointXYZ>::Ptr m_cloud; // 自动区域检测用点云
  89. // std::atomic<bool> mb_cloud_updated; // 点云更新指标
  90. ////
  91. //// std::chrono::system_clock::time_point m_update_plc_time; // 更新plc状态时刻
  92. //// int m_last_sent_code; // 上次写入plc的状态值
  93. //// int m_last_read_code; // 上次检查获得的状态值
  94. //// int m_last_border_status; // 上次超界提示
  95. //// std::atomic<int> m_read_code_count; // 检查后重复获取相同状态值次数
  96. //
  97. // Thread_condition m_cond_exit; // 系统退出标志
  98. // std::thread *m_detect_thread; // 实时检测线程
  99. // std::mutex m_mutex; // 点云互斥锁
  100. //
  101. // Verify_result* mp_verify_handle; // 边缘检测
  102. };
  103. #endif //REGION_WORKER_H