device_tof3d.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #pragma once
  2. #include <thread>
  3. #include <boost/chrono/duration.hpp>
  4. #include <opencv2/opencv.hpp>
  5. #include <Eigen/Core>
  6. #include <Eigen/Geometry>
  7. #include <pcl/point_types.h>
  8. #include <pcl/point_cloud.h>
  9. #include <pcl/filters/statistical_outlier_removal.h>
  10. #include <pcl/filters/approximate_voxel_grid.h>
  11. #include <pcl/filters/voxel_grid.h>
  12. #include <pcl/visualization/pcl_visualizer.h>
  13. #include "log/log.h"
  14. #include "error_code/error_code.hpp"
  15. #include "VzenseNebula_api.h"
  16. #include "thread/thread_condition.h"
  17. #include "tool/timedlockdata.hpp"
  18. #include "pcl/point3D_tool.h"
  19. #include "proto/vzense.pb.h"
  20. #include "communication/transitData.h"
  21. class DeviceTof3D {
  22. public:
  23. struct DeviceTof3DSaveInfo {
  24. cv::Mat irMat; // 灰度图
  25. cv::Mat pointMat; // 点云坐标信息
  26. DeviceTof3DSaveInfo() {
  27. irMat = cv::Mat::zeros(480, 640, CV_8UC1);
  28. pointMat = cv::Mat::zeros(480, 640, CV_32FC4);
  29. }
  30. DeviceTof3DSaveInfo& operator=(const DeviceTof3DSaveInfo &p) {
  31. this->pointMat = p.pointMat.clone();
  32. this->irMat = p.irMat.clone();
  33. return *this;
  34. }
  35. };
  36. public:
  37. static DeviceTof3D *iter() {
  38. static DeviceTof3D *instance = nullptr;
  39. if (instance == nullptr) {
  40. instance = new DeviceTof3D();
  41. }
  42. return instance;
  43. }
  44. ~DeviceTof3D() = default;
  45. Error_manager Init(const google::protobuf::RepeatedPtrField<tof3dVzenseEtc> &tof_devices);
  46. Error_manager Release();
  47. Error_manager updateTrans(const DeviceAzimuth &azimuth, const CoordinateTransformation3D &trans);
  48. cv::Mat getDeviceIrMat();
  49. cv::Mat getDevicePointsMat(const DeviceAzimuth &azimuth);
  50. DeviceTof3DSaveInfo getDeviceSaveInfo(const DeviceAzimuth &azimuth);
  51. Error_manager getDeviceStatus();
  52. protected:
  53. // bool
  54. Error_manager SearchDevice(const double &time = 10);
  55. bool isAllDevicesFound(uint32_t deviceCount);
  56. Error_manager setTof3dParams(VzDeviceHandle handle, const Tof3dVzenseBuiltInParams &params);
  57. Error_manager getTof3dParams(VzDeviceHandle handle, Tof3dVzenseBuiltInParams &params);
  58. Error_manager ConnectDevice(const DeviceAzimuth &azimuth);
  59. Error_manager DisConnectDevice(VzDeviceHandle handle);
  60. Error_manager getDepthAndIrPicture(VzDeviceHandle handle, VzFrame &depthFrame, VzFrame &irFrame);
  61. static Error_manager DepthFrame2Mat(VzFrame &frame, cv::Mat &mat);
  62. static Error_manager IrFrame2Mat(VzFrame &frame, cv::Mat &mat);
  63. static Error_manager Frame2Mat(VzFrame &frame, cv::Mat &mat);
  64. static void HotPlugStateCallback(const VzDeviceInfo *pInfo, int status, void *contex);
  65. void run(const DeviceAzimuth &azimuth);
  66. void stop();
  67. private:
  68. struct DeviceTof3DStatusInfo {
  69. bool connected = false;
  70. bool found = false;
  71. };
  72. struct DeviceTof3DParamsInfo {
  73. DeviceTof3DStatusInfo status;
  74. std::thread *t = nullptr;
  75. VzDeviceHandle handle = nullptr;
  76. Thread_condition *condit = nullptr;
  77. const tof3dVzenseEtc *etc = nullptr;
  78. std::mutex *handle_mutex = nullptr;
  79. void Release() {
  80. condit->kill_all();
  81. t->join();
  82. delete t;
  83. t = nullptr;
  84. delete condit;
  85. condit = nullptr;
  86. if (handle != nullptr) {
  87. delete handle;
  88. handle = nullptr;
  89. }
  90. delete etc;
  91. etc = nullptr;
  92. }
  93. };
  94. private:
  95. TimedLockData<DeviceTof3DSaveInfo> m_device_mat[DeviceAzimuth::DEVICE_AZIMUTH_MAX];
  96. std::vector<DeviceTof3DParamsInfo> m_devices_list;
  97. TimedLockData<int> m_devices_status[DeviceAzimuth::DEVICE_AZIMUTH_MAX];
  98. std::mutex rotation_mutex;
  99. std::mutex all_device_mutex;
  100. Eigen::Matrix3f rotation_matrix3[4];
  101. Eigen::Vector3f move[4];
  102. };