lidar_driver_leishen16.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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_driver_leishen16.h"
  13. #include "lidar_leishen16_rawdata.h"
  14. #include "ivexit.h"
  15. #include "ivfault.h"
  16. #include "ivlog.h"
  17. #ifdef VV7_1
  18. int vv7;
  19. #endif
  20. #define Lidar_Pi 3.1415926535897932384626433832795
  21. #define Lidar32 (unsigned long)3405883584//192.168.1.203
  22. #define Lidar_roll_ang 90*Lidar_Pi/180.0
  23. static std::thread * g_pleishen16Thread;
  24. static std::thread * g_pleishen16ProcThread;
  25. //float gV_theta[16] = {-15,1,-13,3,-11,5,-9,7,-7,9,-5,11,-3,13,-1,15}; //16 Angles
  26. static float gV_theta[16] = {-15,1,-13,3,-11,5,-9,7,-7,9,-5,11,-3,13,-1,15}; //16 Angles
  27. static float gV_theta_cos[16],gV_theta_sin[16];
  28. //static void * g_leishen16_raw;
  29. static void * g_lidar_pc;
  30. static bool g_bleishen16_run = false;
  31. static bool g_bleishen16_running = false;
  32. static bool g_bleishen16_Proc_running = false;
  33. static int g_seq = 0;
  34. static unsigned short glidar_port=2368;
  35. extern char gstr_memname[256];
  36. extern char gstr_rollang[256];
  37. extern char gstr_inclinationang_yaxis[256]; //from y axis
  38. extern char gstr_inclinationang_xaxis[256]; //from x axis
  39. //char gstr_hostip[256];
  40. extern char gstr_port[256];
  41. extern char gstr_yaml[256];
  42. extern iv::Ivfault * gIvfault;
  43. extern iv::Ivlog * gIvlog;
  44. /**
  45. * @brief The leishen16_Buf class
  46. * Use for Lidar UDP DATA Save
  47. */
  48. class leishen16_Buf
  49. {
  50. private:
  51. char * mstrdata;
  52. int mnSize; //Data SizeUse
  53. QMutex mMutex;
  54. int mIndex;
  55. public:
  56. leishen16_Buf()
  57. {
  58. mstrdata = new char[BK32_DATA_BUFSIZE];
  59. mIndex = 0;
  60. mnSize = 0;
  61. }
  62. ~leishen16_Buf()
  63. {
  64. delete mstrdata;
  65. }
  66. void WriteData(const char * strdata,const int nSize)
  67. {
  68. mMutex.lock();
  69. memcpy(mstrdata,strdata,nSize);
  70. mnSize = nSize;
  71. mIndex++;
  72. mMutex.unlock();
  73. }
  74. int ReadData(char * strdata,const int nRead,int * pnIndex)
  75. {
  76. int nRtn = 0;
  77. if(mnSize <= 0)return 0;
  78. mMutex.lock();
  79. nRtn = mnSize;
  80. if(nRtn >nRead)
  81. {
  82. nRtn = nRead;
  83. std::cout<<"lidar_leishen16 leishen16_Buf ReadData data nRead = "<<nRead<<" is small"<<std::endl;
  84. }
  85. memcpy(strdata,mstrdata,nRtn);
  86. mnSize = 0;
  87. if(pnIndex != 0)*pnIndex = mIndex;
  88. mMutex.unlock();
  89. return nRtn;
  90. }
  91. };
  92. static leishen16_Buf * g_leishen16_Buf;
  93. static char * g_RawData_Buf; //Buffer UDP Data
  94. static int gnRawPos = 0;
  95. static float gAngle_Old = 0;
  96. static float gAngle_Total = 0;
  97. static unsigned short gold = 0;
  98. static int gnPac = 0;
  99. #include <QTime>
  100. static QTime gTime;
  101. /**
  102. * @brief processleishen16_Data
  103. * @param ba UDP Buffer
  104. * 1.UDP ByteArray Length is 1206.
  105. * 2.if Angle is More than 360. Tell Another thread process.
  106. */
  107. static void processleishen16_Data(QByteArray ba)
  108. {
  109. gnPac++;
  110. unsigned short * pAng;
  111. float fAng;
  112. char * pdata;
  113. pdata = ba.data();
  114. // std::cout<<"len is "<<ba.length()<<std::endl;
  115. if(ba.length() == 1206)
  116. {
  117. pAng = (unsigned short *)(pdata+2);
  118. fAng = *pAng;fAng = fAng*0.01;
  119. // std::cout<<"ang is "<<*pAng<<std::endl;
  120. if(fabs(fAng-gAngle_Old)>300)
  121. {
  122. gAngle_Total = gAngle_Total + fabs(fabs(fAng-gAngle_Old)-360);
  123. }
  124. else
  125. {
  126. gAngle_Total = gAngle_Total + fabs(fabs(fAng-gAngle_Old));
  127. }
  128. gAngle_Old = fAng;
  129. if(gAngle_Total > 360)
  130. {
  131. g_leishen16_Buf->WriteData(g_RawData_Buf,gnRawPos);
  132. lidar_leishen16_raw * p = new lidar_leishen16_raw();
  133. p->mnLen = gnRawPos;
  134. memcpy(p->mstrdata,g_RawData_Buf,gnRawPos);
  135. // iv::modulecomm::ModuleSendMsg(g_leishen16_raw,(char *)p,sizeof(lidar_leishen16_raw));
  136. delete p;
  137. memcpy(g_RawData_Buf,pdata,1206);
  138. gnRawPos = 1206;
  139. // std::cout<<"index = "<<gnPac<<" time ="<<gTime.elapsed()<<" a cycle"<<std::endl;
  140. gAngle_Total = 0;
  141. }
  142. else
  143. {
  144. if((gnRawPos+1206)<= BK32_DATA_BUFSIZE)
  145. {
  146. memcpy(g_RawData_Buf+gnRawPos,pdata,1206);
  147. gnRawPos= gnRawPos+1206;
  148. }
  149. else
  150. {
  151. std::cout<<"lidar_leishen16 processleishen16_Data data is very big gnRawPos = "<<gnRawPos<<std::endl;
  152. }
  153. }
  154. // std::cout<<*pAng<<std::endl;
  155. // gold = *pAng;
  156. if(gnRawPos == 0)
  157. {
  158. gAngle_Total = 0;
  159. gAngle_Old = *pAng;
  160. gAngle_Old = gAngle_Old*0.01;
  161. memcpy(g_RawData_Buf,pdata,1206);
  162. gnRawPos = gnRawPos+1206;
  163. }
  164. }
  165. else
  166. {
  167. std::cout<<"lidar_leishen16 processleishen16_Data receive data packet len is not 1206 "<<std::endl;
  168. }
  169. }
  170. /**
  171. * @brief leishen16_Func thread for receive udp socket
  172. * @param n
  173. */
  174. static void leishen16_Func(int n)
  175. {
  176. gTime.start();
  177. std::cout<<"Enter leishen16_Func."<<std::endl;
  178. QUdpSocket * udpSocket = new QUdpSocket( );
  179. udpSocket->bind(QHostAddress::Any, glidar_port);
  180. unsigned int ndatamisstime = 0;
  181. int nstate = 0;
  182. int nlaststate = 0;
  183. while(g_bleishen16_run)
  184. {
  185. if(udpSocket->hasPendingDatagrams())
  186. {
  187. ndatamisstime = 0;
  188. // std::cout<<"have data."<<std::endl;
  189. QByteArray datagram;
  190. datagram.resize(udpSocket->pendingDatagramSize());
  191. QHostAddress sender;
  192. quint16 senderPort;
  193. udpSocket->readDatagram(datagram.data(), datagram.size(),
  194. &sender, &senderPort);
  195. // processTheDatagram(datagram);
  196. // std::cout<<"have data."<<std::endl;
  197. // QNetworkDatagram datagram = udpSocket->receiveDatagram();
  198. processleishen16_Data(datagram);
  199. datagram.clear();
  200. }
  201. else
  202. {
  203. // std::cout<<"running."<<std::endl;
  204. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  205. if(ndatamisstime < 1000000) ndatamisstime++;
  206. }
  207. if(ndatamisstime > 1000)
  208. {
  209. nstate = 1;
  210. }
  211. if(ndatamisstime > 60000)
  212. {
  213. nstate = 2;
  214. }
  215. if(ndatamisstime < 100)
  216. {
  217. nstate = 0;
  218. }
  219. if(nlaststate != nstate)
  220. {
  221. nlaststate = nstate;
  222. switch (nstate) {
  223. case 0:
  224. gIvfault->SetFaultState(0,0,"OK");
  225. gIvlog->info("received udp data.device is ok.");
  226. break;
  227. case 1:
  228. gIvfault->SetFaultState(1,1,"No data");
  229. gIvlog->warn("more than 1 second no data. warning.");
  230. break;
  231. case 2:
  232. gIvfault->SetFaultState(2,2,"No data,Please Check Device or setting.");
  233. gIvlog->error("more than 60 seconds no data. error. Please check device or setting.");
  234. break;
  235. default:
  236. break;
  237. }
  238. }
  239. }
  240. udpSocket->close();
  241. delete udpSocket;
  242. g_bleishen16_running = false;
  243. std::cout<<"leishen16_Func Exit."<<std::endl;
  244. }
  245. /**
  246. * @brief process_leishen16obs Make PointCloud And Share
  247. * @param strdata pointer to data
  248. * @param nLen data length
  249. */
  250. static void process_leishen16obs(char * strdata,int nLen)
  251. {
  252. double frollang = atof(gstr_rollang) *M_PI/180.0; //Roll Angle
  253. double finclinationang_xaxis = atof(gstr_inclinationang_xaxis)*M_PI/180.0; //Inclination from x axis
  254. double finclinationang_yaxis = atof(gstr_inclinationang_yaxis)*M_PI/180.0; //Inclination from y axis
  255. bool binclix = false;
  256. bool bincliy = false;
  257. if(finclinationang_xaxis != 0.0)binclix = true;
  258. if(finclinationang_yaxis != 0.0)bincliy = true;
  259. double cos_finclinationang_xaxis = cos(finclinationang_xaxis);
  260. double sin_finclinationang_xaxis = sin(finclinationang_xaxis);
  261. double cos_finclinationang_yaxis = cos(finclinationang_yaxis);
  262. double sin_finclinationang_yaxis = sin(finclinationang_yaxis);
  263. QDateTime dt = QDateTime::currentDateTime();
  264. pcl::PointCloud<pcl::PointXYZI>::Ptr point_cloud(
  265. new pcl::PointCloud<pcl::PointXYZI>());
  266. point_cloud->header.frame_id = "velodyne";
  267. point_cloud->height = 1;
  268. point_cloud->header.stamp = dt.currentMSecsSinceEpoch();
  269. point_cloud->width = 0;
  270. point_cloud->header.seq =g_seq;
  271. g_seq++;
  272. unsigned char * pstr = (unsigned char *)strdata;
  273. // std::cout<<"enter obs."<<std::endl;
  274. // float w = 0.0036;
  275. float Ang = 0;
  276. float Range = 0;
  277. int bag = 0;
  278. int Group = 0;
  279. int pointi = 0;
  280. float wt = 0;
  281. int wt1 = 0;
  282. int buf1len = nLen/1206;
  283. unsigned short * pAng;
  284. double ang1,ang2;
  285. pAng = (unsigned short *)(strdata+2+0*100);
  286. ang1 = *pAng;ang1 = ang1/100.0;
  287. pAng = (unsigned short *)(strdata+2+1*100);
  288. ang2 = *pAng;ang2 = ang2/100.0;
  289. double angdiff = ang2 - ang1;
  290. if(angdiff<0)
  291. angdiff = angdiff + 360.0;
  292. angdiff = angdiff/2.0;
  293. for (bag = 0; bag < buf1len; bag++)
  294. {
  295. for (Group = 0; Group <= 11; Group++)
  296. {
  297. wt1 = ((pstr[bag*1206 +3 + Group * 100] *256) + pstr[bag*1206 + 2 + Group * 100]) ;
  298. wt = wt1/ 100.0;
  299. // std::cout<<"wt1 is "<<wt1<<std::endl;
  300. for (pointi = 0; pointi <16; pointi++)
  301. {
  302. Ang = (0 - wt) / 180.0 * Lidar_Pi;
  303. Range = ((pstr[bag*1206 + Group * 100 + 5 + 3 * pointi] << 8) + pstr[bag*1206+Group * 100 + 4 + 3 * pointi]);
  304. unsigned char intensity = pstr[bag*1206 + Group * 100 + 6 + 3 * pointi];
  305. Range=Range* 0.0025;
  306. pcl::PointXYZI point;
  307. point.x = Range* gV_theta_cos[pointi]*cos(Ang + frollang);
  308. point.y = Range* gV_theta_cos[pointi] *sin(Ang + frollang);
  309. point.z = Range* gV_theta_sin[pointi];
  310. point.intensity = intensity;
  311. if(binclix)
  312. {
  313. double y,z;
  314. y = point.y;z = point.z;
  315. point.y = y*cos_finclinationang_xaxis +z*sin_finclinationang_xaxis;
  316. point.z = z*cos_finclinationang_xaxis - y*sin_finclinationang_xaxis;
  317. }
  318. if(bincliy)
  319. {
  320. double z,x;
  321. z = point.z;x = point.x;
  322. point.z = z*cos_finclinationang_yaxis + x*sin_finclinationang_yaxis;
  323. point.x = x*cos_finclinationang_yaxis - z*sin_finclinationang_yaxis;
  324. }
  325. point_cloud->points.push_back(point);
  326. ++point_cloud->width;
  327. }
  328. wt = wt + 0.2; //扫描帧频10Hz时的水平角分辨率是0.2°,
  329. for (pointi = 0; pointi < 16; pointi++)
  330. {
  331. Ang = (0 - wt) / 180.0 * Lidar_Pi;
  332. Range = ((pstr[bag*1206 + Group * 100 + 53 + 3 * pointi] << 8) + pstr[bag*1206+Group * 100 + 52 + 3 * pointi]);
  333. unsigned char intensity = pstr[bag*1206 + Group * 100 + 54 + 3 * pointi];
  334. Range=Range* 0.0025;
  335. pcl::PointXYZI point;
  336. point.x = Range* gV_theta_cos[pointi] *cos(Ang + frollang);
  337. point.y = Range* gV_theta_cos[pointi] *sin(Ang + frollang);
  338. point.z = Range* gV_theta_sin[pointi] ;
  339. point.intensity = intensity;
  340. if(binclix)
  341. {
  342. double y,z;
  343. y = point.y;z = point.z;
  344. point.y = y*cos_finclinationang_xaxis +z*sin_finclinationang_xaxis;
  345. point.z = z*cos_finclinationang_xaxis - y*sin_finclinationang_xaxis;
  346. }
  347. if(bincliy)
  348. {
  349. double z,x;
  350. z = point.z;x = point.x;
  351. point.z = z*cos_finclinationang_yaxis + x*sin_finclinationang_yaxis;
  352. point.x = x*cos_finclinationang_yaxis - z*sin_finclinationang_yaxis;
  353. }
  354. point_cloud->points.push_back(point);
  355. ++point_cloud->width;
  356. }
  357. }
  358. }
  359. char * strOut = new char[4+sizeof(point_cloud->header.frame_id.size()) + 4+8+point_cloud->width * sizeof(pcl::PointXYZI)];
  360. int * pHeadSize = (int *)strOut;
  361. *pHeadSize = 4 + point_cloud->header.frame_id.size()+4+8;
  362. memcpy(strOut+4,point_cloud->header.frame_id.c_str(),point_cloud->header.frame_id.size());
  363. pcl::uint32_t * pu32 = (pcl::uint32_t *)(strOut+4+sizeof(point_cloud->header.frame_id.size()));
  364. *pu32 = point_cloud->header.seq;
  365. memcpy(strOut+4+sizeof(point_cloud->header.frame_id.size()) + 4,&point_cloud->header.stamp,8);
  366. pcl::PointXYZI * p;
  367. p = (pcl::PointXYZI *)(strOut +4+sizeof(point_cloud->header.frame_id.size()) + 4+8 );
  368. memcpy(p,point_cloud->points.data(),point_cloud->width * sizeof(pcl::PointXYZI));
  369. iv::modulecomm::ModuleSendMsg(g_lidar_pc,strOut,4+sizeof(point_cloud->header.frame_id.size()) + 4+8+point_cloud->width * sizeof(pcl::PointXYZI));
  370. delete strOut;
  371. // std::cout<<"point cloud width = "<<point_cloud->width<<" size = "<<point_cloud->size()<<std::endl;
  372. }
  373. /**
  374. * @brief leishen16_Proc_Func
  375. * thread for process data to PointCloud
  376. * @param n
  377. */
  378. static void leishen16_Proc_Func(int n)
  379. {
  380. std::cout<<"Enter leishen16_Proc_Func"<<std::endl;
  381. char * strdata = new char[BK32_DATA_BUFSIZE];
  382. int nIndex;
  383. int nRead;
  384. while(g_bleishen16_run)
  385. {
  386. if((nRead = g_leishen16_Buf->ReadData(strdata,BK32_DATA_BUFSIZE,&nIndex))>0)
  387. {
  388. //process data.
  389. process_leishen16obs(strdata,nRead);
  390. }
  391. else
  392. {
  393. // std::cout<<"running."<<std::endl;
  394. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  395. }
  396. }
  397. g_bleishen16_Proc_running = false;
  398. std::cout<<"Exit leishen16_Proc_Func"<<std::endl;
  399. }
  400. void exitfunc()
  401. {
  402. StopLidar_leishen16();
  403. }
  404. /**
  405. * @brief StartLidar_leishen16x Start
  406. * @return
  407. */
  408. int LIDAR_DRIVER_LEISHEN16SHARED_EXPORT StartLidar_leishen16()
  409. {
  410. iv::ivexit::RegIVExitCall(exitfunc);
  411. std::cout<<"Now Start leishen16 Listen."<<std::endl;
  412. g_RawData_Buf = new char[BK32_DATA_BUFSIZE];
  413. int i;
  414. for(i=0;i<16;i++)
  415. {
  416. gV_theta[i] = gV_theta[i]*M_PI/180.0;
  417. gV_theta_cos[i] = cos(gV_theta[i]);
  418. gV_theta_sin[i] = sin(gV_theta[i]);
  419. }
  420. g_leishen16_Buf = new leishen16_Buf();
  421. g_bleishen16_run = true;
  422. g_bleishen16_running = true;
  423. g_bleishen16_Proc_running = true;
  424. glidar_port = atoi(gstr_port);
  425. // g_leishen16_raw = iv::modulecomm::RegisterSend(strmemnameraw,10*sizeof(lidar_leishen16_raw),10);
  426. g_lidar_pc = iv::modulecomm::RegisterSend(gstr_memname,20000000,1);
  427. g_pleishen16Thread = new std::thread(leishen16_Func,0);
  428. g_pleishen16ProcThread = new std::thread(leishen16_Proc_Func,0);
  429. return 0;
  430. }
  431. /**
  432. * @brief StopLidar_leishen16 Stop
  433. */
  434. void LIDAR_DRIVER_LEISHEN16SHARED_EXPORT StopLidar_leishen16()
  435. {
  436. std::cout<<"Now Close leishen16. "<<std::endl;
  437. g_bleishen16_run = false;
  438. g_pleishen16Thread->join();
  439. g_pleishen16ProcThread->join();
  440. iv::modulecomm::Unregister(g_lidar_pc);
  441. delete g_pleishen16ProcThread;
  442. delete g_pleishen16Thread;
  443. delete g_leishen16_Buf;
  444. delete g_RawData_Buf;
  445. }