device_tof3d.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #pragma once
  2. #include <thread>
  3. #include <boost/chrono/duration.hpp>
  4. #include <opencv2/opencv.hpp>
  5. #include <pcl/point_types.h>
  6. #include <pcl/point_cloud.h>
  7. #include "error_code/error_code.hpp"
  8. #include "VzenseNebula_api.h"
  9. #include "log/log.h"
  10. #include "thread/thread_condition.h"
  11. #include "tof3d_config.pb.h"
  12. #include "detect/wheel-detector.h"
  13. class DeviceTof3D {
  14. public:
  15. struct tof3dVzenseInfo {
  16. VzDeviceInfo info;
  17. tof3dVzenseEtc etc;
  18. VzDeviceHandle handle = nullptr;
  19. bool is_connect = false;
  20. bool is_start_stream = false;
  21. };
  22. using VzEtcMap = std::map<std::string, tof3dVzenseEtc>;
  23. using tof3dVzenseInfoMap = std::map<std::string, tof3dVzenseInfo>;
  24. public:
  25. static DeviceTof3D* iter() {
  26. static DeviceTof3D* instance = nullptr;
  27. if (instance == nullptr) {
  28. instance = new DeviceTof3D();
  29. }
  30. return instance;
  31. }
  32. ~DeviceTof3D() = default;
  33. // TODO
  34. void run();
  35. Error_manager Init(const VzEtcMap &tp_tof3d, bool search= true);
  36. Error_manager reInit(const VzEtcMap &etc);
  37. Error_manager SearchDevice(const double &time);
  38. Error_manager ConnectDevice(const std::string &ip, bool open_stream=true);
  39. Error_manager ConnectAllDevices(bool open_stream=true);
  40. Error_manager ConnectAllEtcDevices(bool open_stream=true);
  41. Error_manager DisConnectDevice(const std::string &ip);
  42. Error_manager DisConnectAllEtcDevices();
  43. Error_manager DisConnectAllDevices();
  44. Error_manager getDepthFrame(const std::string &ip, VzFrame &depthFrame);
  45. Error_manager getIrFrame(const std::string &ip, VzFrame &irFrame);
  46. Error_manager getDepthAndIrPicture(const std::string &ip, VzFrame &depthFrame, VzFrame &irFrame);
  47. Error_manager getDepthPointCloud(const std::string &ip, pcl::PointCloud<pcl::PointXYZ>::Ptr &cloud);
  48. Error_manager DepthFrame2PointCloud(const std::string &ip, VzFrame &depthFrame, pcl::PointCloud<pcl::PointXYZ>::Ptr &cloud);
  49. Error_manager updateTof3dEtc();
  50. Error_manager setTof3dParams(const std::string &ip);
  51. Error_manager getTof3dParams(const std::string &ip);
  52. static Error_manager Frame2Mat(VzFrame &frame, cv::Mat &mat);
  53. protected:
  54. DeviceTof3D() = default;
  55. static Error_manager DepthFrame2Mat(VzFrame &frame, cv::Mat &mat);
  56. static Error_manager IrFrame2Mat(VzFrame &frame, cv::Mat &mat);
  57. void receiveFrameThread(const std::string &ip);
  58. void detectThread(const std::string &ip);
  59. static void HotPlugStateCallback(const VzDeviceInfo *pInfo, int status, void *contex);
  60. bool drawBox(cv::Mat &mat, cv::Rect &box, cv::Scalar &color, std::string className, float confidence);
  61. Error_manager loadEtc(const VzEtcMap &etc);
  62. void stopWorking();
  63. // 传入深度图,得到车轮在mat中的坐标
  64. Error_manager segmentTensorRT(cv::Mat &depth_mat, std::vector<cv::Point> &wheel_cv_cloud);
  65. // 根据传入坐标,分离depth frame的数据点
  66. Error_manager segFrame2CarAndWheel(VzFrame &depth_frame, std::vector<cv::Point> &wheel_cv_cloud, pcl::PointCloud<pcl::PointXYZ>::Ptr &car_cloud, pcl::PointCloud<pcl::PointXYZ>::Ptr &wheel_cloud);
  67. // 车轮点云优化
  68. Error_manager WheelCloudOptimize(pcl::PointCloud<pcl::PointXYZ>::Ptr &wheel_cloud);
  69. // 车身点云优化
  70. Error_manager CarPoseOptimize(pcl::PointCloud<pcl::PointXYZ>::Ptr &car_cloud);
  71. private:
  72. struct ThreadInfo {
  73. std::thread * t;
  74. Thread_condition * condit;
  75. ThreadInfo(std::thread *m_t, Thread_condition * m_condit): t(m_t), condit(m_condit) {}
  76. };
  77. VzReturnStatus m_vz_status = VzRetOthers;
  78. tof3dVzenseInfoMap mp_device_info;
  79. std::map<std::string, ThreadInfo> mp_thread_info;
  80. VzFrame lf_frame;
  81. std::mutex lf_frame_mutex; // 测量结果互斥锁
  82. VzFrame rf_frame;
  83. std::mutex rf_frame_mutex; // 测量结果互斥锁
  84. VzFrame lr_frame;
  85. std::mutex lr_frame_mutex; // 测量结果互斥锁
  86. VzFrame rr_frame;
  87. std::mutex rr_frame_mutex; // 测量结果互斥锁
  88. };