lidar_16z.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "lidar_16z.h"
  2. static float V_theta[16] = {-15,-13,-11,-9,-7,-5,-3,-1,1,3,5,7,9,11,13,15};
  3. static float H_Beta[16] = {-2.5,-2.5 ,-2.5 ,-2.5,-2.5,-2.5 ,-2.5 ,-2.5,
  4. 2.5,-2.5 ,-2.5 ,-2.5, -2.5,-2.5 ,-2.5 ,-2.5};
  5. lidar_16z::lidar_16z() {
  6. pcl::PointCloud<pcl::PointXYZI>::Ptr point_cloud(
  7. new pcl::PointCloud<pcl::PointXYZI>());
  8. point_cloud->header.frame_id = "velodyne";
  9. point_cloud->height = 1;
  10. point_cloud->header.stamp = static_cast<uint64_t>(std::chrono::system_clock::now().time_since_epoch().count()/1000);
  11. point_cloud->width = 0;
  12. point_cloud->header.seq =0;
  13. mpoint_cloud_temp = point_cloud;
  14. mpdriver = new lidar_bkdriver();
  15. mpTheadDecode = new std::thread(&lidar_16z::ThreadDecode,this);
  16. }
  17. lidar_16z::~lidar_16z(){
  18. mbRun = false;
  19. mpTheadDecode->join();
  20. delete mpdriver;
  21. }
  22. void lidar_16z::ThreadDecode()
  23. {
  24. static double azutotal = 0.0;
  25. static int g_seq = 0;
  26. while(mbRun)
  27. {
  28. std::shared_ptr<unsigned char> pdata_ptr;
  29. int64_t nrecvtime;
  30. int ndatasize = 0;
  31. int nread = mpdriver->GetRecvData(pdata_ptr,nrecvtime,ndatasize);
  32. if(nread == 1)
  33. {
  34. std::cout<<"read "<<ndatasize<<std::endl;
  35. if(ndatasize % 1184 != 0)
  36. {
  37. std::cout<<" data not complete. datasize: "<<ndatasize<<std::endl;
  38. }
  39. else
  40. {
  41. unsigned char * p = pdata_ptr.get();
  42. unsigned short * pang_start = (unsigned short * )(p+12);
  43. unsigned short * pang_end = (unsigned short *)(p+14);
  44. (void)pang_end;
  45. double ang_space = 0.2;
  46. int i;
  47. int nheadlen = 32;
  48. double fAzuAng = static_cast<double>(*pang_start) * 0.01;
  49. for(i=0;i<12;i++)
  50. {
  51. int j;
  52. for(j=0;j<16;j++)
  53. {
  54. unsigned char * pecho = (unsigned char *)(p +nheadlen + i*96 + j*6);
  55. unsigned short * prange;
  56. unsigned char * pchch;
  57. double frange;
  58. unsigned char nch;
  59. unsigned char intensity;
  60. pchch = pecho;
  61. prange = (unsigned short *)(pecho +3);
  62. intensity = *(pecho + 5);
  63. nch = * pchch;
  64. frange = static_cast<double>(*prange);
  65. frange = frange * 0.004;
  66. if(nch<=16){
  67. double fAng = (-fAzuAng - H_Beta[nch] - mfRollAng) * M_PI/180.0;
  68. double vtheta = V_theta[nch] * M_PI/180.0;
  69. double x = frange * cos(fAng)* cos(vtheta);
  70. double y = frange * sin(fAng)* cos(vtheta);
  71. double z = frange * sin(vtheta);
  72. pcl::PointXYZI point;
  73. point.x = x;
  74. point.y = y;
  75. point.z = z;
  76. point.intensity = intensity;
  77. mpoint_cloud_temp->points.push_back(point);
  78. ++mpoint_cloud_temp->width;
  79. }
  80. else
  81. {
  82. std::cout<<" channel error."<<std::endl;
  83. }
  84. }
  85. fAzuAng = fAzuAng + ang_space;
  86. azutotal = azutotal + ang_space;
  87. if(azutotal>=360.0)
  88. {
  89. //share pointcloud
  90. mmutexpc.lock();
  91. mpoint_cloud = mpoint_cloud_temp;
  92. mbpcupdate = true;
  93. mmutexpc.unlock();
  94. mcv.notify_all();
  95. pcl::PointCloud<pcl::PointXYZI>::Ptr point_cloud(
  96. new pcl::PointCloud<pcl::PointXYZI>());
  97. point_cloud->header.frame_id = "velodyne";
  98. point_cloud->height = 1;
  99. point_cloud->header.stamp = static_cast<uint64_t>(std::chrono::system_clock::now().time_since_epoch().count()/1000);
  100. point_cloud->width = 0;
  101. point_cloud->header.seq =g_seq;
  102. g_seq++;
  103. mpoint_cloud_temp = point_cloud;
  104. azutotal = 0;
  105. }
  106. }
  107. }
  108. }
  109. }
  110. }
  111. int lidar_16z::GetPointCloud(pcl::PointCloud<pcl::PointXYZI>::Ptr & point_cloud)
  112. {
  113. return 1;
  114. }