device_tof3d.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #include "tof3d_transformation.hpp"
  22. class DeviceTof3D {
  23. public:
  24. struct DeviceTof3DSaveInfo {
  25. cv::Mat irMat; // 灰度图
  26. cv::Mat pointMat; // 点云坐标信息
  27. DeviceTof3DSaveInfo() {
  28. irMat = cv::Mat::zeros(480, 640, CV_8UC1);
  29. pointMat = cv::Mat::zeros(480, 640, CV_32FC3);
  30. }
  31. DeviceTof3DSaveInfo& operator=(const DeviceTof3DSaveInfo &p) {
  32. this->pointMat = p.pointMat.clone();
  33. this->irMat = p.irMat.clone();
  34. return *this;
  35. }
  36. };
  37. public:
  38. static DeviceTof3D *iter() {
  39. static DeviceTof3D *instance = nullptr;
  40. if (instance == nullptr) {
  41. instance = new DeviceTof3D();
  42. }
  43. return instance;
  44. }
  45. ~DeviceTof3D() = default;
  46. Error_manager Init(const google::protobuf::RepeatedPtrField<tof3dVzenseEtc> &tof_devices);
  47. Error_manager Release();
  48. Error_manager Stop();
  49. Error_manager Continue();
  50. Error_manager updateTrans(const DeviceAzimuth &azimuth, const CoordinateTransformation3D &trans);
  51. cv::Mat getDeviceIrMat();
  52. cv::Mat getDevicePointsMat(const DeviceAzimuth &azimuth);
  53. DeviceTof3DSaveInfo getDeviceSaveInfo(const DeviceAzimuth &azimuth);
  54. Error_manager getDeviceStatus();
  55. protected:
  56. // bool
  57. Error_manager SearchDevice(const double &time = 10);
  58. bool isAllDevicesFound(uint32_t deviceCount);
  59. Error_manager setTof3dParams(VzDeviceHandle handle, const Tof3dVzenseBuiltInParams &params);
  60. Error_manager getTof3dParams(VzDeviceHandle handle, Tof3dVzenseBuiltInParams &params);
  61. Error_manager ConnectDevice(const DeviceAzimuth &azimuth);
  62. Error_manager StartStreamDevice(VzDeviceHandle handle);
  63. Error_manager DisConnectDevice(VzDeviceHandle handle);
  64. Error_manager CloseStreamDevice(VzDeviceHandle handle);
  65. Error_manager getDepthAndIrPicture(VzDeviceHandle handle, VzFrame &depthFrame, VzFrame &irFrame);
  66. static Error_manager DepthFrame2Mat(VzFrame &frame, cv::Mat &mat);
  67. static Error_manager IrFrame2Mat(VzFrame &frame, cv::Mat &mat);
  68. static Error_manager Frame2Mat(VzFrame &frame, cv::Mat &mat);
  69. static void HotPlugStateCallback(const VzDeviceInfo *pInfo, int status, void *contex);
  70. void run(const DeviceAzimuth &azimuth);
  71. void stop();
  72. private:
  73. struct DeviceTof3DStatusInfo {
  74. bool connected = false;
  75. bool found = false;
  76. };
  77. struct DeviceTof3DParamsInfo {
  78. DeviceTof3DStatusInfo status;
  79. std::thread *t = nullptr;
  80. VzDeviceHandle handle = nullptr;
  81. Thread_condition *condit = nullptr;
  82. const tof3dVzenseEtc *etc = nullptr;
  83. std::mutex *handle_mutex = nullptr;
  84. void Release() {
  85. condit->kill_all();
  86. t->join();
  87. delete t;
  88. t = nullptr;
  89. delete condit;
  90. condit = nullptr;
  91. delete etc;
  92. etc = nullptr;
  93. }
  94. };
  95. private:
  96. TimedLockData<DeviceTof3DSaveInfo> m_device_mat[DeviceAzimuth::DEVICE_AZIMUTH_MAX];
  97. std::vector<DeviceTof3DParamsInfo> m_devices_list;
  98. TimedLockData<int> m_devices_status[DeviceAzimuth::DEVICE_AZIMUTH_MAX];
  99. std::mutex rotation_mutex;
  100. std::mutex all_device_mutex;
  101. Eigen::Matrix3f rotation_matrix3[4];
  102. Eigen::Vector3f move[4];
  103. };