lidar_hesai40line.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. #include <thread>
  2. #include <iostream>
  3. #include <QUdpSocket>
  4. #include <QNetworkDatagram>
  5. #include <iostream>
  6. #include <QMutex>
  7. #include <QDateTime>
  8. #include <pcl/conversions.h>
  9. #include <pcl/point_cloud.h>
  10. #include <pcl/point_types.h>
  11. #include "modulecomm.h"
  12. #include "lidar_hesai40line.h".h"
  13. #include "lidar_hesai40line_rawdata.h"
  14. #ifdef VV7_1
  15. int vv7;
  16. #endif
  17. #define Lidar_Pi 3.1415926535897932384626433832795
  18. #define Lidar32 (unsigned long)3405883584//192.168.1.203
  19. #define Lidar_roll_ang (270*Lidar_Pi/180.0)
  20. std::thread * g_phesai40lineThread;
  21. std::thread * g_phesai40lineProcThread;
  22. extern char gstr_memname[256];
  23. extern char gstr_rollang[256];
  24. extern char gstr_inclinationang_yaxis[256]; //from y axis
  25. extern char gstr_inclinationang_xaxis[256]; //from x axis
  26. //char gstr_hostip[256];
  27. extern char gstr_port[256];
  28. extern char gstr_yaml[256];
  29. void * g_hesai40line_raw;
  30. void * g_lidar_pc;
  31. bool g_bhesai40line_run = false;
  32. bool g_bhesai40line_running = false;
  33. bool g_bhesai40line_Proc_running = false;
  34. int g_seq = 0;
  35. double groll_ang;
  36. class hesai40line_Buf
  37. {
  38. private:
  39. char * mstrdata;
  40. int mnSize; //Data SizeUse
  41. QMutex mMutex;
  42. int mIndex;
  43. public:
  44. hesai40line_Buf()
  45. {
  46. mstrdata = new char[BK32_DATA_BUFSIZE];
  47. mIndex = 0;
  48. mnSize = 0;
  49. }
  50. ~hesai40line_Buf()
  51. {
  52. delete mstrdata;
  53. }
  54. void WriteData(const char * strdata,const int nSize)
  55. {
  56. mMutex.lock();
  57. memcpy(mstrdata,strdata,nSize);
  58. mnSize = nSize;
  59. mIndex++;
  60. mMutex.unlock();
  61. }
  62. int ReadData(char * strdata,const int nRead,int * pnIndex)
  63. {
  64. int nRtn = 0;
  65. if(mnSize <= 0)return 0;
  66. mMutex.lock();
  67. nRtn = mnSize;
  68. if(nRtn >nRead)
  69. {
  70. nRtn = nRead;
  71. std::cout<<"lidar_hesai40line hesai40line_Buf ReadData data nRead = "<<nRead<<" is small"<<std::endl;
  72. }
  73. memcpy(strdata,mstrdata,nRtn);
  74. mnSize = 0;
  75. if(pnIndex != 0)*pnIndex = mIndex;
  76. mMutex.unlock();
  77. return nRtn;
  78. }
  79. };
  80. hesai40line_Buf * g_hesai40line_Buf;
  81. char * g_RawData_Buf;
  82. int gnRawPos = 0;
  83. float gAngle_Old = 0;
  84. float gAngle_Total = 0;
  85. unsigned short gold = 0;
  86. int gnPac = 0;
  87. #include <QTime>
  88. QTime gTime;
  89. void processhesai40line_Data(QByteArray ba)
  90. {
  91. gnPac++;
  92. unsigned short * pAng;
  93. float fAng;
  94. char * pdata;
  95. pdata = ba.data();
  96. // qDebug("size is %d",ba.length());
  97. if(ba.length() == 1240)
  98. {
  99. pAng = (unsigned short *)(pdata+2);
  100. fAng = *pAng;fAng = fAng*0.01;
  101. if(fabs(fAng-gAngle_Old)>300)
  102. {
  103. gAngle_Total = gAngle_Total + fabs(fabs(fAng-gAngle_Old)-360);
  104. }
  105. else
  106. {
  107. gAngle_Total = gAngle_Total + fabs(fabs(fAng-gAngle_Old));
  108. }
  109. gAngle_Old = fAng;
  110. if(gAngle_Total > 360)
  111. {
  112. g_hesai40line_Buf->WriteData(g_RawData_Buf,gnRawPos);
  113. lidar_hesai40line_raw * p = new lidar_hesai40line_raw();
  114. p->mnLen = gnRawPos;
  115. memcpy(p->mstrdata,g_RawData_Buf,gnRawPos);
  116. // g_hesai40line_raw->writemsg((char *)p,sizeof(lidar_hesai40line_raw));
  117. delete p;
  118. memcpy(g_RawData_Buf,pdata,1240);
  119. gnRawPos = 1240;
  120. // std::cout<<"index = "<<gnPac<<" time ="<<gTime.elapsed()<<" a cycle"<<std::endl;
  121. gAngle_Total = 0;
  122. }
  123. else
  124. {
  125. if((gnRawPos+1240)<= BK32_DATA_BUFSIZE)
  126. {
  127. memcpy(g_RawData_Buf+gnRawPos,pdata,1240);
  128. gnRawPos= gnRawPos+1240;
  129. }
  130. else
  131. {
  132. std::cout<<"lidar_hesai40line processhesai40line_Data data is very big gnRawPos = "<<gnRawPos<<std::endl;
  133. }
  134. }
  135. // std::cout<<*pAng<<std::endl;
  136. // gold = *pAng;
  137. if(gnRawPos == 0)
  138. {
  139. gAngle_Total = 0;
  140. gAngle_Old = *pAng;
  141. gAngle_Old = gAngle_Old*0.01;
  142. memcpy(g_RawData_Buf,pdata,1240);
  143. gnRawPos = gnRawPos+1240;
  144. }
  145. }
  146. else
  147. {
  148. std::cout<<"lidar_hesai40line processhesai40line_Data receive data packet len is not 1240 "<<std::endl;
  149. }
  150. }
  151. void hesai40line_Func(int n)
  152. {
  153. gTime.start();
  154. std::cout<<"Enter hesai40line_Func."<<std::endl;
  155. QUdpSocket * udpSocket = new QUdpSocket( );
  156. udpSocket->bind(QHostAddress::Any, 8080);
  157. while(g_bhesai40line_run)
  158. {
  159. if(udpSocket->hasPendingDatagrams())
  160. {
  161. // std::cout<<"have data."<<std::endl;
  162. QNetworkDatagram datagram = udpSocket->receiveDatagram();
  163. processhesai40line_Data(datagram.data());
  164. datagram.clear();
  165. }
  166. else
  167. {
  168. // std::cout<<"running."<<std::endl;
  169. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  170. }
  171. }
  172. udpSocket->close();
  173. delete udpSocket;
  174. g_bhesai40line_running = false;
  175. std::cout<<"hesai40line_Func Exit."<<std::endl;
  176. }
  177. void process_obs(char * strdata,int nLen)
  178. {
  179. QDateTime dt = QDateTime::currentDateTime();
  180. pcl::PointCloud<pcl::PointXYZI>::Ptr point_cloud(
  181. new pcl::PointCloud<pcl::PointXYZI>());
  182. // point_cloud->header.stamp =
  183. // pcl_conversions::toPCL(sweep_data->header).stamp;
  184. point_cloud->header.frame_id = "velodyne";
  185. point_cloud->height = 1;
  186. point_cloud->header.stamp = dt.currentMSecsSinceEpoch();
  187. point_cloud->width = 0;
  188. point_cloud->header.seq =g_seq;
  189. g_seq++;
  190. unsigned char * pstr = (unsigned char *)strdata;
  191. std::cout<<"enter obs."<<std::endl;
  192. // float w = 0.0036;
  193. float w = 0;
  194. float Ang = 0;
  195. float Range = 0;
  196. int bag = 0;
  197. int Group = 0;
  198. int pointi = 0;
  199. float wt = 0;
  200. int wt1 = 0;
  201. // int dx;
  202. // int dy;
  203. // float lidar_32_xdistance = 0.3; //32线激光雷达X轴补偿
  204. // float lidar_32_ydistance = -2.3; //32线激光雷达Y轴补偿
  205. float H_BETA[40] = {
  206. 0.005, 0.006, 0.006, 0.006,
  207. -2.479, -2.479, 2.491, -4.953,
  208. -2.479, 2.492, -4.953, -2.479,
  209. 2.492, -4.953, 0.007, 2.491,
  210. -4.953, 0.006, 4.961, -2.479,
  211. 0.006, 4.96, -2.478, 0.006,
  212. 4.958, -2.478, 2.488, 4.956,
  213. -2.477, 2.487, 2.485, 2.483,
  214. 0.004, 0.004, 0.003, 0.003,
  215. -2.466, -2.463, -2.46, -2.457};
  216. float V_theta[40] = {
  217. 6.96, 5.976, 4.988, 3.996,
  218. 2.999, 2.001, 1.667, 1.333,
  219. 1.001, 0.667, 0.333, 0,
  220. -0.334, -0.667, -1.001, -1.334,
  221. -1.667, -2.001, -2.331, -2.667,
  222. -3, -3.327, -3.663, -3.996,
  223. -4.321, -4.657, -4.986, -5.311,
  224. -5.647, -5.974, -6.957, -7.934,
  225. -8.908, -9.871, -10.826, -11.772,
  226. -12.705, -13.63, -14.543, -15.444};
  227. float T[32] = { 0, 3.125, 1.5625, 4.6875, 6.25, 9.375, 7.8125, 10.9375,
  228. 12.5, 15.625, 14.0625, 17.1875, 18.75, 21.875, 20.3125, 23.4375,
  229. 25, 28.125, 26.5625, 29.6875, 31.25, 34.375, 32.8125, 35.9375,
  230. 37.5, 40.625, 39.0625, 42.1875, 43.75, 46.875, 45.3125, 48.4375};
  231. int buf1len = nLen/1240;
  232. for (bag = 0; bag < buf1len; bag++)
  233. {
  234. for (Group = 0; Group < 6; Group++)
  235. {
  236. wt1 = ((pstr[bag*1240 +3 + Group * 204] *256) + pstr[bag*1240 + 2 + Group * 204]) ;
  237. wt = wt1/ 100.0;
  238. for (pointi = 0; pointi <= 39; pointi++)
  239. {
  240. // Ang = (0 - wt - w * T[pointi] - H_BETA[pointi]+213) / 180.0 * Lidar_Pi; //shuiping 213
  241. Ang = (0-wt - H_BETA[pointi]) / 180.0 * Lidar_Pi;
  242. Range = ((pstr[bag*1240 + Group * 204 + 6 + 5 * pointi] << 16) + (pstr[bag*1240 + Group * 204 + 5 + 5 * pointi] << 8) + pstr[bag*1240+Group * 204 + 4 + 5 * pointi]);
  243. unsigned short intensity =(pstr[bag*1240 + Group * 204 + 8 + 5 * pointi] << 8) + pstr[bag*1240 + Group * 204 + 7 + 5 * pointi];
  244. Range=Range* 2.0/1000.0;
  245. pcl::PointXYZI point;
  246. point.x = Range*cos(V_theta[pointi] / 180 * Lidar_Pi)*cos(Ang + groll_ang);
  247. point.y = Range*cos(V_theta[pointi] / 180 * Lidar_Pi)*sin(Ang + groll_ang);
  248. point.z = Range*sin(V_theta[pointi] / 180 * Lidar_Pi);
  249. point.intensity = intensity;
  250. point_cloud->points.push_back(point);
  251. ++point_cloud->width;
  252. }
  253. }
  254. }
  255. char * strOut = new char[4+sizeof(point_cloud->header.frame_id.size()) + 4+8+point_cloud->width * sizeof(pcl::PointXYZI)];
  256. int * pHeadSize = (int *)strOut;
  257. *pHeadSize = 4 + point_cloud->header.frame_id.size()+4+8;
  258. memcpy(strOut+4,point_cloud->header.frame_id.c_str(),point_cloud->header.frame_id.size());
  259. pcl::uint32_t * pu32 = (pcl::uint32_t *)(strOut+4+sizeof(point_cloud->header.frame_id.size()));
  260. *pu32 = point_cloud->header.seq;
  261. memcpy(strOut+4+sizeof(point_cloud->header.frame_id.size()) + 4,&point_cloud->header.stamp,8);
  262. pcl::PointXYZI * p;
  263. p = (pcl::PointXYZI *)(strOut +4+sizeof(point_cloud->header.frame_id.size()) + 4+8 );
  264. memcpy(p,point_cloud->points.data(),point_cloud->width * sizeof(pcl::PointXYZI));
  265. iv::modulecomm::ModuleSendMsg(g_lidar_pc,strOut,4+sizeof(point_cloud->header.frame_id.size()) + 4+8+point_cloud->width * sizeof(pcl::PointXYZI));
  266. delete strOut;
  267. std::cout<<"point cloud width = "<<point_cloud->width<<" size = "<<point_cloud->size()<<std::endl;
  268. }
  269. void hesai40line_Proc_Func(int n)
  270. {
  271. std::cout<<"Enter hesai40line_Proc_Func"<<std::endl;
  272. char * strdata = new char[BK32_DATA_BUFSIZE];
  273. int nIndex;
  274. int nRead;
  275. while(g_bhesai40line_run)
  276. {
  277. if((nRead = g_hesai40line_Buf->ReadData(strdata,BK32_DATA_BUFSIZE,&nIndex))>0)
  278. {
  279. //process data.
  280. process_obs(strdata,nRead);
  281. }
  282. else
  283. {
  284. // std::cout<<"running."<<std::endl;
  285. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  286. }
  287. }
  288. g_bhesai40line_Proc_running = false;
  289. std::cout<<"Exit hesai40line_Proc_Func"<<std::endl;
  290. }
  291. int LIDAR_HESAI40LINESHARED_EXPORT StartLidar_hesai40line()
  292. {
  293. std::cout<<"Now Start hesai40line Listen."<<std::endl;
  294. g_RawData_Buf = new char[BK32_DATA_BUFSIZE];
  295. g_hesai40line_Buf = new hesai40line_Buf();
  296. g_bhesai40line_run = true;
  297. g_bhesai40line_running = true;
  298. g_bhesai40line_Proc_running = true;
  299. groll_ang = atof(gstr_rollang);
  300. std::cout<<"roll ang is "<<groll_ang;
  301. groll_ang = groll_ang * M_PI/180.0;
  302. g_hesai40line_raw = iv::modulecomm::RegisterSend("hesai40line_Raw",10*sizeof(lidar_hesai40line_raw),10);
  303. g_lidar_pc = iv::modulecomm::RegisterSend(gstr_memname,20000000,1);
  304. g_phesai40lineThread = new std::thread(hesai40line_Func,0);
  305. g_phesai40lineProcThread = new std::thread(hesai40line_Proc_Func,0);
  306. return 0;
  307. }
  308. void LIDAR_HESAI40LINESHARED_EXPORT StopLidar_hesai40line()
  309. {
  310. std::cout<<"Now Close hesai40line. "<<std::endl;
  311. g_bhesai40line_run = false;
  312. g_phesai40lineThread->join();
  313. g_phesai40lineProcThread->join();
  314. delete g_phesai40lineProcThread;
  315. delete g_phesai40lineThread;
  316. delete g_hesai40line_Buf;
  317. delete g_RawData_Buf;
  318. }