perception_lidar_vv7.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. #include <QCoreApplication>
  2. #include "perception_lidar_vv7.h"
  3. #include <getopt.h>
  4. #include <thread>
  5. #include <vector>
  6. #include <list>
  7. #include <iostream>
  8. #include <QDateTime>
  9. #include <QMutex>
  10. #include <pcl/conversions.h>
  11. #include <pcl/point_cloud.h>
  12. #include <pcl/point_types.h>
  13. #include "modulecomm.h"
  14. #include "ivversion.h"
  15. #include "ivbacktrace.h"
  16. #include "perceptionoutput.h"
  17. #include <iostream>
  18. #include <fstream>
  19. #include <yaml-cpp/yaml.h>
  20. #include "ivfault.h"
  21. #include "ivlog.h"
  22. static void * glidar_pc;
  23. static void * glidar_obs;
  24. static void * g_lidar_pc_floor;
  25. static void * g_lidar_pc_nofloor;
  26. static void * glidar_pc_p;
  27. static void * glidar_per;
  28. static void * gpafusion;
  29. iv::Ivfault *gfault = nullptr;
  30. iv::Ivlog *givlog = nullptr;
  31. static char gstr_sensorheight[256];
  32. static char gstr_vehicleheight[256];
  33. static char gstr_inputmemname[256];
  34. static char gstr_outputmemname[256];
  35. static char gstr_skipxmin[256];
  36. static char gstr_skipxmax[256];
  37. static char gstr_skipymin[256];
  38. static char gstr_skipymax[256];
  39. static int g_robosys = 10;
  40. //#define OUT_GROUND
  41. namespace iv {
  42. const int grx = 200, gry = 550, centerx = 100, centery = 50;
  43. const double gridwide = 0.2;
  44. struct ObstacleBasic
  45. {
  46. bool valid;
  47. float nomal_x;
  48. float nomal_y;
  49. float nomal_z;
  50. float speed_relative;
  51. float speed_x;
  52. float speed_y;
  53. float speed_z;
  54. float high;
  55. float low;
  56. int esr_ID;
  57. };
  58. // typedef boost::shared_ptr<std::vector<iv::ObstacleBasic>> ObsLiDAR;
  59. // typedef boost::shared_ptr<std::vector<iv::ObstacleBasic>> ObsRadar;
  60. // typedef boost::shared_ptr<std::vector<iv::ObstacleBasic>> ObsCamera;
  61. // typedef boost::shared_ptr<std::vector<iv::ObstacleBasic>>ObsRadarPointer;
  62. struct Obs_grid
  63. {
  64. double high;
  65. double low;
  66. double obshight;
  67. double groundheight;
  68. int pointcount;
  69. int ob;//??????0?? ?1???2???
  70. };
  71. typedef Obs_grid LidarGrid;
  72. typedef Obs_grid* LidarGridPtr;
  73. // int xxxx;
  74. }
  75. static void writepctosm(pcl::PointCloud<pcl::PointXYZI>::Ptr & point_cloud,void * pasm)
  76. {
  77. char * strOut = new char[4+sizeof(point_cloud->header.frame_id.size()) + 4+8+point_cloud->width * sizeof(pcl::PointXYZI)];
  78. int * pHeadSize = (int *)strOut;
  79. *pHeadSize = 4 + point_cloud->header.frame_id.size()+4+8;
  80. memcpy(strOut+4,point_cloud->header.frame_id.c_str(),point_cloud->header.frame_id.size());
  81. // pcl::uint32_t * pu32 = (pcl::uint32_t *)(strOut+4+sizeof(point_cloud->header.frame_id.size()));
  82. unsigned int * pu32 = (unsigned int *)(strOut+4+sizeof(point_cloud->header.frame_id.size()));
  83. *pu32 = point_cloud->header.seq;
  84. memcpy(strOut+4+sizeof(point_cloud->header.frame_id.size()) + 4,&point_cloud->header.stamp,8);
  85. pcl::PointXYZI * p;
  86. p = (pcl::PointXYZI *)(strOut +4+sizeof(point_cloud->header.frame_id.size()) + 4+8 );
  87. memcpy(p,point_cloud->points.data(),point_cloud->width * sizeof(pcl::PointXYZI));
  88. // std::cout<<"width = "<<point_cloud->width<<std::endl;
  89. iv::modulecomm::ModuleSendMsg(pasm,strOut,4+sizeof(point_cloud->header.frame_id.size()) + 4+8+point_cloud->size() * sizeof(pcl::PointXYZI));
  90. delete strOut;
  91. }
  92. static float SENSOR_HEIGHT = 1.9;
  93. static float VEHICLE_HEIGHT = 2.2;
  94. //static float local_max_slope_ = 10;
  95. //static float general_max_slope_ = 3;
  96. //static float concentric_divider_distance_ = 1.5;
  97. //static float min_height_threshold_ = 0.05;
  98. //static float reclass_distance_threshold_ = 10.0;
  99. static float GRID_SIZE = 2.0;
  100. static float SMALLGRID_SIZE = 0.2;
  101. static float Height_Thesh_Ratio = 0.1;
  102. static float General_Thresh_Ratio = 0.1;
  103. static float VEC_LEN = 200.0;
  104. static float HOR_LEN = 200.0;
  105. static float VEC_MIN = -100.0;
  106. static float HOR_MIN = -100.0;
  107. static int g_seq = 0;
  108. static float skip_xmin = -0.9;
  109. static float skip_ymin = -2.3;
  110. static float skip_xmax = 0.9;
  111. static float skip_ymax = 2.3;
  112. static void GridGetObs(pcl::PointCloud<pcl::PointXYZI>::Ptr & point_cloud,std::shared_ptr<std::vector<iv::ObstacleBasic>> lidar_obs)
  113. {
  114. // std::cout<<"enter gird obs"<<std::endl;
  115. int nVecSize = VEC_LEN/GRID_SIZE;
  116. int nHorSize = HOR_LEN/GRID_SIZE;
  117. bool * floor_find = new bool[(nVecSize+2)*(nHorSize+2)];
  118. float * floor_h_raw = new float[(nVecSize+2)*(nHorSize+2)];
  119. float * floor_h = new float[nVecSize*nHorSize];
  120. pcl::PointCloud<pcl::PointXYZI>::Ptr point_cloud_nofloor(
  121. new pcl::PointCloud<pcl::PointXYZI>());
  122. pcl::PointCloud<pcl::PointXYZI>::Ptr point_cloud_floor(
  123. new pcl::PointCloud<pcl::PointXYZI>());
  124. QDateTime dt = QDateTime::currentDateTime();
  125. point_cloud_floor->header.frame_id = "velodyne";
  126. point_cloud_floor->height = 1;
  127. point_cloud_floor->header.stamp = dt.currentMSecsSinceEpoch();
  128. point_cloud_floor->width = 0;
  129. point_cloud_floor->header.seq =g_seq;
  130. point_cloud_nofloor->header.frame_id = "velodyne";
  131. point_cloud_nofloor->height = 1;
  132. point_cloud_nofloor->header.stamp = dt.currentMSecsSinceEpoch();
  133. point_cloud_nofloor->width = 0;
  134. point_cloud_nofloor->header.seq =g_seq;
  135. g_seq++;
  136. int i,j;
  137. for(i=0;i<(nVecSize+2);i++)
  138. {
  139. for(j=0;j<(nHorSize+2);j++)
  140. {
  141. floor_find[i*(nHorSize+2) +j] = false;
  142. }
  143. }
  144. for(i=0;i<nVecSize;i++)
  145. {
  146. for(j=0;j<nHorSize;j++)
  147. {
  148. floor_h[i*nHorSize+j] = -SENSOR_HEIGHT;
  149. }
  150. }
  151. for(i=0;i<point_cloud->size();i++)
  152. {
  153. float x = point_cloud->at(i).x;
  154. float y = point_cloud->at(i).y;
  155. float z = point_cloud->at(i).z;
  156. if((x>=skip_xmin)&&(y>=skip_ymin)&&(x<=skip_xmax)&&(y<=skip_ymax))
  157. {
  158. continue;
  159. }
  160. float dis = sqrt(x*x + y*y);
  161. // if((dis>0.9)&&(z>(-SENSOR_HEIGHT-dis*General_Thresh_Ratio))&&(z<(-SENSOR_HEIGHT+dis*General_Thresh_Ratio)))
  162. if((z>(-SENSOR_HEIGHT-dis*General_Thresh_Ratio))&&(z<(-SENSOR_HEIGHT+dis*General_Thresh_Ratio)))
  163. {
  164. int Rpos,Cpos;
  165. Rpos = (y-VEC_MIN)/GRID_SIZE;
  166. Cpos = (x-HOR_MIN)/GRID_SIZE;
  167. if((Rpos>=0)&&(Rpos<nVecSize)&&(Cpos>=0)&&(Cpos<nHorSize))
  168. {
  169. int xPos = Rpos*(nHorSize+2) + Cpos+1;
  170. if(floor_find[xPos] == true)
  171. {
  172. if(z<floor_h_raw[xPos])
  173. {
  174. floor_h_raw[xPos] = z;
  175. }
  176. }
  177. else
  178. {
  179. floor_find[xPos]= true;
  180. floor_h_raw[xPos] = z;
  181. }
  182. }
  183. }
  184. }
  185. for(i=1;i<(nVecSize+1);i++)
  186. {
  187. for(j=1;j<(nHorSize+1);j++)
  188. {
  189. int m,n;
  190. bool bHave = false;
  191. float min;
  192. for(m=-1;m<=1;m++)
  193. {
  194. for(n=-1;n<=1;n++)
  195. {
  196. int xpos = (i+m)*(nHorSize+2) + j+n;
  197. if(floor_find[xpos])
  198. {
  199. if(bHave)
  200. {
  201. if(min<floor_h_raw[xpos])min = floor_h_raw[xpos];
  202. }
  203. else
  204. {
  205. min = floor_h_raw[xpos];
  206. bHave = true;
  207. }
  208. }
  209. }
  210. }
  211. if(bHave)floor_h[(i-1)*nHorSize +j] = min;
  212. }
  213. }
  214. //#ifdef OUT_GROUND
  215. for(i=0;i<point_cloud->size();i++)
  216. {
  217. pcl::PointXYZI po = point_cloud->at(i);
  218. float x = point_cloud->at(i).x;
  219. float y = point_cloud->at(i).y;
  220. float z = point_cloud->at(i).z;
  221. if((x>=skip_xmin)&&(y>=skip_ymin)&&(x<=skip_xmax)&&(y<=skip_ymax))
  222. {
  223. continue;
  224. }
  225. // float dis = sqrt(x*x + y*y);
  226. int Rpos,Cpos;
  227. Rpos = (y-VEC_MIN)/GRID_SIZE;
  228. Cpos = (x-HOR_MIN)/GRID_SIZE;
  229. if((Rpos>=0)&&(Rpos<nVecSize)&&(Cpos>=0)&&(Cpos<nHorSize))
  230. {
  231. if((z-floor_h[Rpos*nHorSize+Cpos])>0.15)
  232. {
  233. point_cloud_nofloor->push_back(po);
  234. ++point_cloud_nofloor->width;
  235. }
  236. else
  237. {
  238. point_cloud_floor->push_back(po);
  239. ++point_cloud_floor->width;
  240. }
  241. }
  242. }
  243. //#endif
  244. // for(i=0;i<nVecSize;i++)
  245. // {
  246. // char strOut[3000];
  247. // char strTem[30];
  248. // snprintf(strOut,3000,"i = %d",i);
  249. // for(j=0;j<nHorSize;j++)
  250. // {
  251. // // floor_h[i*nHorSize+j] = -SENSOR_HEIGHT;
  252. // snprintf(strTem,30,"%6.3f ",floor_h[i*nHorSize+j]);
  253. // strncat(strOut,strTem,3000);
  254. // }
  255. // qDebug(strOut);
  256. // }
  257. #ifdef OUT_GROUND
  258. writepctosm(point_cloud_floor,g_lidar_pc_floor);
  259. writepctosm(point_cloud_nofloor,g_lidar_pc_nofloor);
  260. #endif
  261. int NYC = VEC_LEN/SMALLGRID_SIZE;
  262. int NXC = HOR_LEN/SMALLGRID_SIZE;
  263. iv::LidarGrid * pobsgrid = new iv::LidarGrid[NYC*NXC];//from 50m back 50m left 25m right 25m
  264. // int i,j;
  265. for(i=0;i<NYC;i++)
  266. {
  267. for(j=0;j<NXC;j++)
  268. {
  269. iv::LidarGrid * p = pobsgrid + i*NXC+j;
  270. p->pointcount = 0;
  271. int Rpos,Cpos;
  272. Rpos = (i*SMALLGRID_SIZE)/GRID_SIZE;
  273. Cpos = (j*SMALLGRID_SIZE)/GRID_SIZE;
  274. if((Rpos>=0)&&(Rpos<nVecSize)&&(Cpos>=0)&&(Cpos<nHorSize))
  275. {
  276. int xPos = Rpos*nHorSize + Cpos;
  277. p->groundheight = floor_h[xPos];
  278. }
  279. else
  280. {
  281. p->groundheight = -SENSOR_HEIGHT+1000;
  282. }
  283. p->low = -SENSOR_HEIGHT+1000;
  284. p->high = -SENSOR_HEIGHT - 1000;
  285. p->obshight = 0;
  286. p->ob = 0;
  287. }
  288. }
  289. for(i=0;i<point_cloud_nofloor->size();i++)
  290. {
  291. pcl::PointXYZI pxyzi = point_cloud_nofloor->at(i);
  292. int x,y;
  293. x = (int)((pxyzi.y-VEC_MIN)/SMALLGRID_SIZE);
  294. y = (int)((pxyzi.x-HOR_MIN)/SMALLGRID_SIZE);
  295. if((x>=0)&&(x<NXC)&&(y<NYC)&&(y>=0))
  296. {
  297. iv::LidarGrid * p = pobsgrid + x*NXC+y;
  298. p->ob++;
  299. if(pxyzi.z<p->low)p->low = pxyzi.z;
  300. if(pxyzi.z>p->high)p->high = pxyzi.z;
  301. }
  302. }
  303. for(i=0;i<NYC;i++)
  304. {
  305. for(j=0;j<NXC;j++)
  306. {
  307. iv::LidarGrid * p = pobsgrid + i*NXC+j;
  308. if(p->ob>=3)
  309. {
  310. if(p->low>(p->groundheight+VEHICLE_HEIGHT))
  311. {
  312. // std::cout<<"low high"<<std::endl;
  313. }
  314. else
  315. {
  316. // temp.nomal_x = (j-125)*0.2+0.1;
  317. // temp.nomal_y = (i-250)*0.2+0.1;
  318. iv::ObstacleBasic temp;
  319. temp.low = p->low;
  320. temp.high = p->high;
  321. temp.nomal_z = p->high-p->low;
  322. temp.nomal_x = j*SMALLGRID_SIZE + 0.1+HOR_MIN;
  323. temp.nomal_y = i*SMALLGRID_SIZE +0.1 +VEC_MIN;
  324. lidar_obs->push_back(temp);
  325. }
  326. }
  327. }
  328. }
  329. delete pobsgrid;
  330. delete floor_find;
  331. delete floor_h_raw;
  332. delete floor_h;
  333. }
  334. #include <QTime>
  335. #include "fusionobjectarray.pb.h"
  336. iv::fusion::fusionobjectarray xfusion;
  337. void ShareFusionObject(void * pa ,std::shared_ptr<std::vector<iv::ObstacleBasic>> & lidar_obs)
  338. {
  339. double timestamp = std::chrono::system_clock::now().time_since_epoch().count();
  340. timestamp = timestamp/1000000.0;
  341. xfusion.clear_obj();
  342. xfusion.set_timestamp(timestamp);
  343. iv::fusion::fusionobject * pobj = xfusion.add_obj();
  344. pobj->set_id(0);
  345. pobj->set_type(0);
  346. iv::fusion::VelXY *pxvel = new iv::fusion::VelXY;
  347. pxvel->set_x(0);
  348. pxvel->set_y(0);
  349. pobj->set_allocated_vel_abs(pxvel);
  350. iv::fusion::Dimension * pxdim = new iv::fusion::Dimension;
  351. pxdim->set_x(0.2);
  352. pxdim->set_y(0.2);
  353. pxdim->set_z(2.0);
  354. pobj->set_allocated_dimensions(pxdim);
  355. int i;
  356. int nsize = lidar_obs->size();
  357. for(i=0;i<nsize;i++)
  358. {
  359. iv::fusion::NomalXYZ * pcen = pobj->add_nomal_centroid();
  360. pcen->set_nomal_x(lidar_obs->at(i).nomal_x);
  361. pcen->set_nomal_y(lidar_obs->at(i).nomal_y);
  362. pcen->set_nomal_z(lidar_obs->at(i).nomal_z);
  363. }
  364. int nbytesize = xfusion.ByteSize();
  365. std::shared_ptr<char> pstr_ptr = std::shared_ptr<char>(new char[nbytesize]);
  366. if(xfusion.SerializeToArray(pstr_ptr.get(),nbytesize))
  367. {
  368. iv::modulecomm::ModuleSendMsg(pa,pstr_ptr.get(),nbytesize);
  369. }
  370. else
  371. {
  372. std::cout<<" serialzie fusion fail."<<std::endl;
  373. }
  374. }
  375. static void ListenPointCloud(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  376. {
  377. // std::cout<<"enter listen."<<std::endl;
  378. if(nSize <=16)return;
  379. int xin = index;
  380. xin++;
  381. unsigned int * pHeadSize = (unsigned int *)strdata;
  382. if(*pHeadSize > nSize)
  383. {
  384. std::cout<<"ListenPointCloud data is small headsize ="<<*pHeadSize<<" data size is"<<nSize<<std::endl;
  385. }
  386. pcl::PointCloud<pcl::PointXYZI>::Ptr point_cloud(
  387. new pcl::PointCloud<pcl::PointXYZI>());
  388. int nNameSize;
  389. nNameSize = *pHeadSize - 4-4-8;
  390. char * strName = new char[nNameSize+1];strName[nNameSize] = 0;
  391. memcpy(strName,(char *)((char*)strdata +4),nNameSize);
  392. point_cloud->header.frame_id = strName;
  393. memcpy(&point_cloud->header.seq,((char*)strdata)+4+nNameSize,4);
  394. memcpy(&point_cloud->header.stamp,(char *)strdata+4+nNameSize+4,8);
  395. int nPCount = (nSize - *pHeadSize)/sizeof(pcl::PointXYZI);
  396. int i;
  397. pcl::PointXYZI * p;
  398. p = (pcl::PointXYZI *)((char*)strdata + *pHeadSize);
  399. for(i=0;i<nPCount;i++)
  400. {
  401. pcl::PointXYZI xp;
  402. xp.x = p->x;
  403. xp.y = p->y;
  404. xp.z = p->z;
  405. xp.intensity = p->intensity;
  406. point_cloud->push_back(xp);
  407. p++;
  408. }
  409. std::shared_ptr<std::vector<iv::ObstacleBasic>> lidar_obs(new std::vector<iv::ObstacleBasic>);
  410. GridGetObs(point_cloud,lidar_obs);
  411. // perception_lidar_vv7_ProcObs(point_cloud,lidar_obs);
  412. // std::cout<<"prc."<<std::endl;
  413. ShareFusionObject(gpafusion,lidar_obs);
  414. if(g_robosys == 0)
  415. {
  416. if(lidar_obs->size() == 0)
  417. {
  418. iv::ObstacleBasic temp;
  419. temp.low = 0;
  420. temp.high = 3.0;
  421. temp.nomal_z = 3.0;
  422. temp.nomal_x = 1000;
  423. temp.nomal_y = 1000;
  424. lidar_obs->push_back(temp);
  425. }
  426. if(lidar_obs->size() >0)iv::modulecomm::ModuleSendMsg(glidar_obs,(char *)(lidar_obs->data()),lidar_obs->size()*sizeof(iv::ObstacleBasic));
  427. }
  428. else
  429. {
  430. g_robosys--;
  431. if(g_robosys<0)
  432. {
  433. g_robosys = 0;
  434. }
  435. }
  436. gfault->SetFaultState(0, 0, "ok");
  437. // return;
  438. // gw->UpdatePointCloud(point_cloud);
  439. }
  440. void PERCEPTION_LIDAR_VV7SHARED_EXPORT StartPerception_lidar_vv7()
  441. {
  442. std::cout<<"HHINow Start LIDAR Perception"<<std::endl;
  443. glidar_obs = iv::modulecomm::RegisterSend(gstr_outputmemname,20000000,3);
  444. gpafusion = iv::modulecomm::RegisterSend("li_ra_fusion",10000000,1);
  445. #ifdef OUT_GROUND
  446. g_lidar_pc_floor = iv::modulecomm::RegisterSend("lidar_pc_floor",20000000,3);
  447. g_lidar_pc_nofloor = iv::modulecomm::RegisterSend("lidar_pc_nofloor",20000000,3);
  448. #endif
  449. glidar_pc = iv::modulecomm::RegisterRecv(gstr_inputmemname,ListenPointCloud);
  450. }
  451. void PERCEPTION_LIDAR_VV7SHARED_EXPORT StopPerception_lidar_vv7()
  452. {
  453. }
  454. void decodeyaml(const char * stryaml)
  455. {
  456. YAML::Node config;
  457. try
  458. {
  459. config = YAML::LoadFile(stryaml);
  460. }
  461. catch(YAML::BadFile e)
  462. {
  463. givlog->verbose("load yaml error.");
  464. return;
  465. }
  466. if(config["SensorHeight"])
  467. {
  468. strncpy(gstr_sensorheight,config["SensorHeight"].as<std::string>().data(),255);
  469. }
  470. if(config["VehicleHeight"])
  471. {
  472. strncpy(gstr_vehicleheight,config["VehicleHeight"].as<std::string>().data(),255);
  473. }
  474. if(config["inputmemname"])
  475. {
  476. strncpy(gstr_inputmemname,config["inputmemname"].as<std::string>().data(),255);
  477. }
  478. if(config["outputmemname"])
  479. {
  480. strncpy(gstr_outputmemname,config["outputmemname"].as<std::string>().data(),255);
  481. }
  482. if(config["skip_xmin"])
  483. {
  484. strncpy(gstr_skipxmin,config["skip_xmin"].as<std::string>().data(),255);
  485. }
  486. if(config["skip_xmax"])
  487. {
  488. strncpy(gstr_skipxmax,config["skip_xmax"].as<std::string>().data(),255);
  489. }
  490. if(config["skip_ymin"])
  491. {
  492. strncpy(gstr_skipymin,config["skip_ymin"].as<std::string>().data(),255);
  493. }
  494. if(config["skip_ymax"])
  495. {
  496. strncpy(gstr_skipymax,config["skip_ymax"].as<std::string>().data(),255);
  497. }
  498. // std::cout<<gstr_memname<<std::endl;
  499. // std::cout<<gstr_rollang<<std::endl;
  500. // std::cout<<gstr_inclinationang_xaxis<<std::endl;
  501. // std::cout<<gstr_inclinationang_yaxis<<std::endl;
  502. // std::cout<<gstr_hostip<<std::endl;
  503. // std::cout<<gstr_port<<std::endl;
  504. }
  505. char gstr_yaml[256];
  506. void print_useage()
  507. {
  508. std::cout<<" -s --setyaml $yaml : port . eq. -s rs1.yaml"<<std::endl;
  509. std::cout<<" -h --help print help"<<std::endl;
  510. }
  511. int GetOptLong(int argc, char *argv[]) {
  512. int nRtn = 0;
  513. int opt; // getopt_long() 的返回值
  514. int digit_optind = 0; // 设置短参数类型及是否需要参数
  515. // 如果option_index非空,它指向的变量将记录当前找到参数符合long_opts里的
  516. // 第几个元素的描述,即是long_opts的下标值
  517. int option_index = 0;
  518. // 设置短参数类型及是否需要参数
  519. const char *optstring = "s:h";
  520. // 设置长参数类型及其简写,比如 --reqarg <==>-r
  521. /*
  522. struct option {
  523. const char * name; // 参数的名称
  524. int has_arg; // 是否带参数值,有三种:no_argument, required_argument,optional_argument
  525. int * flag; // 为空时,函数直接将 val 的数值从getopt_long的返回值返回出去,
  526. // 当非空时,val的值会被赋到 flag 指向的整型数中,而函数返回值为0
  527. int val; // 用于指定函数找到该选项时的返回值,或者当flag非空时指定flag指向的数据的值
  528. };
  529. 其中:
  530. no_argument(即0),表明这个长参数不带参数(即不带数值,如:--name)
  531. required_argument(即1),表明这个长参数必须带参数(即必须带数值,如:--name Bob)
  532. optional_argument(即2),表明这个长参数后面带的参数是可选的,(即--name和--name Bob均可)
  533. */
  534. static struct option long_options[] = {
  535. {"setyaml", required_argument, NULL, 's'},
  536. {"help", no_argument, NULL, 'h'},
  537. // {"optarg", optional_argument, NULL, 'o'},
  538. {0, 0, 0, 0} // 添加 {0, 0, 0, 0} 是为了防止输入空值
  539. };
  540. while ( (opt = getopt_long(argc,
  541. argv,
  542. optstring,
  543. long_options,
  544. &option_index)) != -1) {
  545. // printf("opt = %c\n", opt); // 命令参数,亦即 -a -b -n -r
  546. // printf("optarg = %s\n", optarg); // 参数内容
  547. // printf("optind = %d\n", optind); // 下一个被处理的下标值
  548. // printf("argv[optind - 1] = %s\n", argv[optind - 1]); // 参数内容
  549. // printf("option_index = %d\n", option_index); // 当前打印参数的下标值
  550. // printf("\n");
  551. switch(opt)
  552. {
  553. case 's':
  554. strncpy(gstr_yaml,optarg,255);
  555. break;
  556. case 'h':
  557. print_useage();
  558. nRtn = 1; //because use -h
  559. break;
  560. default:
  561. break;
  562. }
  563. }
  564. return nRtn;
  565. }
  566. int main(int argc, char *argv[])
  567. {
  568. RegisterIVBackTrace();
  569. showversion("detection_lidar_grid");
  570. QCoreApplication a(argc, argv);
  571. gfault = new iv::Ivfault("lidar_grid");
  572. givlog = new iv::Ivlog("lidar_grid");
  573. snprintf(gstr_yaml,255,"");
  574. int nRtn = GetOptLong(argc,argv);
  575. if(nRtn == 1) //show help,so exit.
  576. {
  577. return 0;
  578. }
  579. snprintf(gstr_sensorheight,255,"2.0");
  580. snprintf(gstr_vehicleheight,255,"2.3");
  581. snprintf(gstr_inputmemname,255,"lidar_pc");
  582. snprintf(gstr_outputmemname,255,"lidar_obs");
  583. snprintf(gstr_skipxmin,255,"-0.9");
  584. snprintf(gstr_skipxmax,255,"0.9");
  585. snprintf(gstr_skipymin,255,"-2.3");
  586. snprintf(gstr_skipymax,255,"2.3");
  587. givlog->verbose("yaml is %s ",gstr_yaml);
  588. if(strnlen(gstr_yaml,255)>0)
  589. {
  590. decodeyaml(gstr_yaml);
  591. }
  592. SENSOR_HEIGHT = atof(gstr_sensorheight);
  593. VEHICLE_HEIGHT = atof(gstr_vehicleheight);
  594. skip_xmin = atof(gstr_skipxmin);
  595. skip_xmax = atof(gstr_skipxmax);
  596. skip_ymin = atof(gstr_skipymin);
  597. skip_ymax = atof(gstr_skipymax);
  598. StartPerception_lidar_vv7();
  599. return a.exec();
  600. }