ivdriver_lidar.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include "ivdriver_lidar.h"
  2. #include <iostream>
  3. namespace iv {
  4. ivdriver_lidar::ivdriver_lidar()
  5. {
  6. strncpy(mstr_hostip,"0.0.0.0",256);
  7. strncpy(mstr_port,"6699",256);
  8. strncpy(mstr_memname,"lidar_pc",256);
  9. }
  10. void ivdriver_lidar::RegisterPointMsg(const char * strmemname,iv::modulecomm::ModuleComm_TYPE xtype)
  11. {
  12. mpa = iv::modulecomm::RegisterSend(strmemname,10000000,1,xtype);
  13. }
  14. void ivdriver_lidar::SharePointCloud(pcl::PointCloud<pcl::PointXYZI>::Ptr point_cloud)
  15. {
  16. char * strOut = new char[4+sizeof(point_cloud->header.frame_id.size()) + 4+8+point_cloud->width * sizeof(pcl::PointXYZI)];
  17. int * pHeadSize = (int *)strOut;
  18. *pHeadSize = 4 + point_cloud->header.frame_id.size()+4+8;
  19. memcpy(strOut+4,point_cloud->header.frame_id.c_str(),point_cloud->header.frame_id.size());
  20. pcl::uint32_t * pu32 = (pcl::uint32_t *)(strOut+4+sizeof(point_cloud->header.frame_id.size()));
  21. *pu32 = point_cloud->header.seq;
  22. memcpy(strOut+4+sizeof(point_cloud->header.frame_id.size()) + 4,&point_cloud->header.stamp,8);
  23. pcl::PointXYZI * p;
  24. p = (pcl::PointXYZI *)(strOut +4+sizeof(point_cloud->header.frame_id.size()) + 4+8 );
  25. memcpy(p,point_cloud->points.data(),point_cloud->width * sizeof(pcl::PointXYZI));
  26. iv::modulecomm::ModuleSendMsg(mpa,strOut,4+sizeof(point_cloud->header.frame_id.size()) + 4+8+point_cloud->width * sizeof(pcl::PointXYZI));
  27. delete strOut;
  28. }
  29. void ivdriver_lidar::modulerun()
  30. {
  31. if(mfinclinationang_xaxis != 0.0)mbinclix = true;
  32. if(mfinclinationang_yaxis != 0.0)mbincliy = true;
  33. mcos_finclinationang_xaxis= cos(mfinclinationang_xaxis);
  34. msin_finclinationang_xaxis = sin(mfinclinationang_xaxis);
  35. mcos_finclinationang_yaxis = cos(mfinclinationang_yaxis);
  36. msin_finclinationang_yaxis = sin(mfinclinationang_yaxis);
  37. RegisterPointMsg(mstr_memname,getdefefaultcommtype());
  38. mbrunudpthread = true;
  39. std::thread * xudpthread = new std::thread(&ivdriver_lidar::threadudp,this);
  40. QDateTime dt = QDateTime::currentDateTime();
  41. pcl::PointCloud<pcl::PointXYZI>::Ptr point_cloud(
  42. new pcl::PointCloud<pcl::PointXYZI>());
  43. point_cloud->header.frame_id = "velodyne";
  44. point_cloud->height = 1;
  45. point_cloud->header.stamp = dt.currentMSecsSinceEpoch();
  46. point_cloud->width = 0;
  47. point_cloud->header.seq =m_seq;
  48. m_seq++;
  49. while(mbrun)
  50. {
  51. if(mvectorba.size()>0)
  52. {
  53. mMutexba.lock();
  54. QByteArray ba = mvectorba[0];
  55. mvectorba.erase(mvectorba.begin());
  56. mMutexba.unlock();
  57. if(processudpData(ba,point_cloud) == 1)
  58. {
  59. SharePointCloud(point_cloud);
  60. point_cloud->clear();
  61. point_cloud->height = 1;
  62. point_cloud->header.stamp = dt.currentMSecsSinceEpoch();
  63. point_cloud->width = 0;
  64. point_cloud->header.seq =m_seq;
  65. m_seq++;
  66. }
  67. }
  68. else
  69. {
  70. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  71. }
  72. }
  73. mbrunudpthread = false;
  74. xudpthread->join();
  75. }
  76. void ivdriver_lidar::threadudp()
  77. {
  78. QUdpSocket * udpSocket = new QUdpSocket( );
  79. bool bbind = udpSocket->bind(QHostAddress(mstr_hostip), atoi(mstr_port));
  80. if(bbind == false)
  81. {
  82. qDebug("bind socket %s:%s error.",mstr_hostip,mstr_port);
  83. return;
  84. }
  85. int nnotrecv = 0;
  86. while(mbrunudpthread)
  87. {
  88. if(udpSocket->hasPendingDatagrams())
  89. {
  90. if(nnotrecv > 10000)
  91. {
  92. std::cout<<" Recv Data."<<std::endl;
  93. }
  94. QNetworkDatagram datagram = udpSocket->receiveDatagram();
  95. mMutexba.lock();
  96. if(mvectorba.size()<1000)
  97. {
  98. mvectorba.push_back(datagram.data());
  99. }
  100. else
  101. {
  102. std::cout<<" proc buffer is full."<<std::endl;
  103. }
  104. mMutexba.unlock();
  105. datagram.clear();
  106. nnotrecv = 0;
  107. }
  108. else
  109. {
  110. nnotrecv++;
  111. // std::cout<<"running."<<std::endl;
  112. std::this_thread::sleep_for(std::chrono::microseconds(100));
  113. if(nnotrecv == 10000)
  114. {
  115. std::cout<<"not recv datagram."<<std::endl;
  116. }
  117. }
  118. }
  119. }
  120. }