device_tof3d.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 "VzenseNebula_api.h"
  14. #include "thread/thread_condition.h"
  15. #include "proto/vzense.pb.h"
  16. #include "communication/transitData.h"
  17. #include "tool/log.hpp"
  18. #include "tool/error_code.hpp"
  19. #include "tool/timedlockdata.hpp"
  20. #include "tool/point3D_tool.hpp"
  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_32FC3);
  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 Stop();
  48. Error_manager Continue();
  49. Error_manager updateTrans(const DeviceAzimuth &azimuth, const CoordinateTransformation3D &trans);
  50. cv::Mat getDeviceIrMat();
  51. cv::Mat getDevicePointsMat(const DeviceAzimuth &azimuth);
  52. DeviceTof3DSaveInfo getDeviceSaveInfo(const DeviceAzimuth &azimuth);
  53. Error_manager getDeviceStatus();
  54. protected:
  55. // bool
  56. Error_manager SearchDevice(const double &time = 10);
  57. bool isAllDevicesFound(uint32_t deviceCount);
  58. Error_manager setTof3dParams(VzDeviceHandle handle, const Tof3dVzenseBuiltInParams &params);
  59. Error_manager getTof3dParams(VzDeviceHandle handle, Tof3dVzenseBuiltInParams &params);
  60. Error_manager ConnectDevice(const DeviceAzimuth &azimuth);
  61. Error_manager StartStreamDevice(VzDeviceHandle handle);
  62. Error_manager DisConnectDevice(VzDeviceHandle handle);
  63. Error_manager CloseStreamDevice(VzDeviceHandle handle);
  64. Error_manager getDepthAndIrPicture(VzDeviceHandle handle, VzFrame &depthFrame, VzFrame &irFrame);
  65. static Error_manager DepthFrame2Mat(VzFrame &frame, cv::Mat &mat);
  66. static Error_manager IrFrame2Mat(VzFrame &frame, cv::Mat &mat);
  67. static Error_manager Frame2Mat(VzFrame &frame, cv::Mat &mat);
  68. static void HotPlugStateCallback(const VzDeviceInfo *pInfo, int status, void *contex);
  69. void run(const DeviceAzimuth &azimuth);
  70. void stop();
  71. private:
  72. struct DeviceTof3DStatusInfo {
  73. bool connected = false;
  74. bool found = false;
  75. };
  76. struct DeviceTof3DParamsInfo {
  77. DeviceTof3DStatusInfo status;
  78. std::thread *t = nullptr;
  79. VzDeviceHandle handle = nullptr;
  80. Thread_condition *condit = nullptr;
  81. const tof3dVzenseEtc *etc = nullptr;
  82. std::mutex *handle_mutex = nullptr;
  83. void Release() {
  84. condit->kill_all();
  85. t->join();
  86. delete t;
  87. t = nullptr;
  88. delete condit;
  89. condit = nullptr;
  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. };