main.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #include <QCoreApplication>
  2. #include <QDateTime>
  3. #include <pcl/io/io.h>
  4. #include <pcl/io/pcd_io.h>
  5. #include <iostream>
  6. #include <fstream>
  7. #include <yaml-cpp/yaml.h>
  8. #include <QMutex>
  9. #include <vector>
  10. #include <thread>
  11. #include "lidarmerge.h"
  12. #include "modulecomm.h"
  13. #include "ivversion.h"
  14. QMutex gMutex;
  15. static qint64 glasttime = 0;
  16. static std::vector<iv::lidar_data> gvectorlidar;
  17. static char gstroutmemname[255];
  18. static void * gpaout;
  19. void dec_yaml(const char * stryamlpath)
  20. {
  21. YAML::Node config;
  22. try
  23. {
  24. config = YAML::LoadFile(stryamlpath);
  25. }
  26. catch(YAML::BadFile e)
  27. {
  28. qDebug("load error.");
  29. return;
  30. }
  31. int i;
  32. int nlidarsize;
  33. std::vector<std::string> veclidarname;
  34. if(config["lidar"])
  35. {
  36. qDebug("have lidar size is %d",config["lidar"].size());
  37. nlidarsize = config["lidar"].size();
  38. for(i=0;i<nlidarsize;i++)
  39. {
  40. std::string strname = config["lidar"][i].as<std::string>();
  41. veclidarname.push_back(strname);
  42. }
  43. }
  44. else
  45. {
  46. return;
  47. }
  48. if(nlidarsize <1)return;
  49. std::string strmemname;
  50. std::string stroffset_x;
  51. std::string stroffset_y;
  52. std::string stroffset_z;
  53. for(i=0;i<nlidarsize;i++)
  54. {
  55. if(config[veclidarname[i].data()])
  56. {
  57. if((config[veclidarname[i].data()]["memname"])&&(config[veclidarname[i].data()]["offset"]["x"])&&(config[veclidarname[i].data()]["offset"]["y"])&&(config[veclidarname[i].data()]["offset"]["z"]))
  58. {
  59. strmemname = config[veclidarname[i].data()]["memname"].as<std::string>();
  60. stroffset_x = config[veclidarname[i].data()]["offset"]["x"].as<std::string>();
  61. stroffset_y = config[veclidarname[i].data()]["offset"]["y"].as<std::string>();
  62. stroffset_z = config[veclidarname[i].data()]["offset"]["z"].as<std::string>();
  63. iv::lidar_data xlidardata;
  64. xlidardata.foff_x = atof(stroffset_x.data());
  65. xlidardata.foff_y = atof(stroffset_y.data());
  66. xlidardata.foff_z = atof(stroffset_z.data());
  67. strncpy(xlidardata.strmemname,strmemname.data(),255);
  68. gvectorlidar.push_back(xlidardata);
  69. qDebug("name is %s ",strmemname.data());
  70. }
  71. }
  72. }
  73. if(config["output"])
  74. {
  75. strncpy(gstroutmemname,config["output"].as<std::string>().data(),255);
  76. }
  77. else
  78. {
  79. strncpy(gstroutmemname,"lidar_pc",255);
  80. }
  81. return;
  82. }
  83. void ListenPointCloud(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  84. {
  85. if(nSize <=16)return;
  86. unsigned int * pHeadSize = (unsigned int *)strdata;
  87. if(*pHeadSize > nSize)
  88. {
  89. std::cout<<"ListenPointCloud data is small headsize ="<<*pHeadSize<<" data size is"<<nSize<<std::endl;
  90. }
  91. pcl::PointCloud<pcl::PointXYZI>::Ptr point_cloud(
  92. new pcl::PointCloud<pcl::PointXYZI>());
  93. int nNameSize;
  94. nNameSize = *pHeadSize - 4-4-8;
  95. char * strName = new char[nNameSize+1];strName[nNameSize] = 0;
  96. memcpy(strName,(char *)((char *)strdata +4),nNameSize);
  97. point_cloud->header.frame_id = strName;
  98. memcpy(&point_cloud->header.seq,(char *)strdata+4+nNameSize,4);
  99. memcpy(&point_cloud->header.stamp,(char *)strdata+4+nNameSize+4,8);
  100. int nPCount = (nSize - *pHeadSize)/sizeof(pcl::PointXYZI);
  101. int i;
  102. pcl::PointXYZI * p;
  103. p = (pcl::PointXYZI *)((char *)strdata + *pHeadSize);
  104. for(i=0;i<nPCount;i++)
  105. {
  106. pcl::PointXYZI xp;
  107. xp.x = p->x;
  108. xp.y = p->y;
  109. xp.z = p->z;
  110. xp.intensity = p->intensity;
  111. point_cloud->push_back(xp);
  112. p++;
  113. // std::cout<<" x is "<<xp.x<<" y is "<<xp.y<<std::endl;
  114. }
  115. for(i=0;i<gvectorlidar.size();i++)
  116. {
  117. if(strncmp(strmemname,gvectorlidar[i].strmemname,255) == 0)
  118. {
  119. gMutex.lock();
  120. gvectorlidar[i].mpoint_cloud = point_cloud;
  121. gvectorlidar[i].bUpdate = true;
  122. gMutex.unlock();
  123. }
  124. }
  125. }
  126. void sharepointcloud(pcl::PointCloud<pcl::PointXYZI>::Ptr point_cloud,void * pa)
  127. {
  128. char * strOut = new char[4+sizeof(point_cloud->header.frame_id.size()) + 4+8+point_cloud->width * sizeof(pcl::PointXYZI)];
  129. int * pHeadSize = (int *)strOut;
  130. *pHeadSize = 4 + point_cloud->header.frame_id.size()+4+8;
  131. memcpy(strOut+4,point_cloud->header.frame_id.c_str(),point_cloud->header.frame_id.size());
  132. pcl::uint32_t * pu32 = (pcl::uint32_t *)(strOut+4+sizeof(point_cloud->header.frame_id.size()));
  133. *pu32 = point_cloud->header.seq;
  134. memcpy(strOut+4+sizeof(point_cloud->header.frame_id.size()) + 4,&point_cloud->header.stamp,8);
  135. pcl::PointXYZI * p;
  136. p = (pcl::PointXYZI *)(strOut +4+sizeof(point_cloud->header.frame_id.size()) + 4+8 );
  137. memcpy(p,point_cloud->points.data(),point_cloud->width * sizeof(pcl::PointXYZI));
  138. iv::modulecomm::ModuleSendMsg(pa,strOut,4+sizeof(point_cloud->header.frame_id.size()) + 4+8+point_cloud->width * sizeof(pcl::PointXYZI));
  139. delete strOut;
  140. }
  141. void merge()
  142. {
  143. int i;
  144. std::vector<iv::lidar_data> xvectorlidar;
  145. pcl::PointCloud<pcl::PointXYZI>::Ptr point_cloud(
  146. new pcl::PointCloud<pcl::PointXYZI>());
  147. gMutex.lock();
  148. xvectorlidar = gvectorlidar;
  149. for(i=0;i<gvectorlidar.size();i++)
  150. {
  151. gvectorlidar[i].bUpdate = false;
  152. }
  153. gMutex.unlock();
  154. point_cloud = mergefunc(xvectorlidar);
  155. sharepointcloud(point_cloud,gpaout);
  156. }
  157. void mergethread()
  158. {
  159. int i;
  160. while(1)
  161. {
  162. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  163. bool bNOtAllOK = false;
  164. for(i=0;i<gvectorlidar.size();i++)
  165. {
  166. if(!gvectorlidar[i].bUpdate)
  167. {
  168. bNOtAllOK = true;
  169. break;
  170. }
  171. }
  172. if(((QDateTime::currentMSecsSinceEpoch() - glasttime)>150) ||(bNOtAllOK == false))
  173. {
  174. merge();
  175. qDebug("time is %ld",glasttime);
  176. glasttime = QDateTime::currentMSecsSinceEpoch();
  177. }
  178. }
  179. }
  180. int main(int argc, char *argv[])
  181. {
  182. showversion("driver_lidar_merge");
  183. QCoreApplication a(argc, argv);
  184. dec_yaml("driver_lidar_merge.yaml");
  185. void * pa;
  186. int i;
  187. for(i=0;i<gvectorlidar.size();i++)
  188. {
  189. iv::modulecomm::RegisterRecv(gvectorlidar[i].strmemname,ListenPointCloud);
  190. }
  191. gpaout = iv::modulecomm::RegisterSend(gstroutmemname,20000000,1);
  192. std::thread xthread(mergethread);
  193. return a.exec();
  194. }