ivdecision.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. #include "ivdecision.h"
  2. #include "gnss_coordinate_convert.h"
  3. #include "decition_type.h"
  4. #include "decition.pb.h"
  5. namespace iv {
  6. ivdecision::ivdecision()
  7. {
  8. }
  9. void ivdecision::modulerun()
  10. {
  11. ModuleFun fungps = std::bind(&iv::ivdecision::UpdateGPS,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4,std::placeholders::_5);
  12. mpagpsmsg = iv::modulecomm::RegisterRecvPlus(mstrgpsmsgname.data(),fungps);
  13. ModuleFun funlidar = std::bind(&iv::ivdecision::UpdateLIDAR,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4,std::placeholders::_5);
  14. mpalidarmsg = iv::modulecomm::RegisterRecvPlus(mstrlidarmsgname.data(),funlidar);
  15. ModuleFun funradar = std::bind(&iv::ivdecision::UpdateRADAR,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4,std::placeholders::_5);
  16. mparadarmsg = iv::modulecomm::RegisterRecvPlus(mstrradarmsgname.data(),funradar);
  17. ModuleFun funmap = std::bind(&iv::ivdecision::UpdateMAP,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4,std::placeholders::_5);
  18. mpamapmsg = iv::modulecomm::RegisterRecvPlus(mstrmapmsgname.data(),funmap);
  19. ModuleFun funmobileye = std::bind(&iv::ivdecision::UpdateMobileye,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4,std::placeholders::_5);
  20. mpamobileyemsg = iv::modulecomm::RegisterRecvPlus(mstrmobileyemsgname.data(),funmobileye);
  21. ModuleFun funhmi = std::bind(&iv::ivdecision::UpdateHMI,this,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3,std::placeholders::_4,std::placeholders::_5);
  22. mpahmimsg = iv::modulecomm::RegisterRecvPlus(mstrhmimsgname.data(),funhmi);
  23. mpapadmsg = iv::modulecomm::RegisterRecvPlus(mstrpadmsgname.data(),funhmi);
  24. mpaDecition = iv::modulecomm::RegisterSend(mstrdecisionmsgname.data(),10*sizeof(iv::decition::DecitionBasic),1);
  25. mpaVechicleState = iv::modulecomm::RegisterSend(mstrbrainstatemsgname.data(),10000,10);
  26. iv::brain::brainstate xbs;
  27. iv::brain::decition xdecition;
  28. qint64 nSpace = 10;
  29. qint64 nLastDecTime = 0;
  30. while(mbrun)
  31. {
  32. if((QDateTime::currentMSecsSinceEpoch() - nLastDecTime)<nSpace)
  33. {
  34. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  35. continue;
  36. }
  37. nLastDecTime = QDateTime::currentMSecsSinceEpoch();
  38. std::cout<<"running."<<std::endl;
  39. getdecision(xdecition,xbs);
  40. sharemsg(xdecition,xbs);
  41. //RunDecision
  42. //ShareMsg
  43. }
  44. iv::modulecomm::Unregister(mpapadmsg);
  45. iv::modulecomm::Unregister(mpahmimsg);
  46. iv::modulecomm::Unregister(mpamobileyemsg);
  47. iv::modulecomm::Unregister(mpamapmsg);
  48. iv::modulecomm::Unregister(mparadarmsg);
  49. iv::modulecomm::Unregister(mpalidarmsg);
  50. iv::modulecomm::Unregister(mpagpsmsg);
  51. }
  52. void ivdecision::sharemsg(brain::decition &xdecition, brain::brainstate &xbs)
  53. {
  54. int nsize = xdecition.ByteSize();
  55. char * str = new char[nsize];
  56. std::shared_ptr<char> pstr;
  57. pstr.reset(str);
  58. if(xdecition.SerializeToArray(str,nsize))
  59. {
  60. // if(ServiceCarStatus.mbRunPause == false)
  61. // {
  62. iv::modulecomm::ModuleSendMsg(mpaDecition,str,nsize);
  63. // }
  64. }
  65. else
  66. {
  67. std::cout<<"iv::decition::BrainDecition::ShareDecition serialize error."<<std::endl;
  68. }
  69. nsize = xbs.ByteSize();
  70. str = new char[nsize];
  71. // std::shared_ptr<char> pstr;
  72. pstr.reset(str);
  73. if(xbs.SerializeToArray(str,nsize))
  74. {
  75. iv::modulecomm::ModuleSendMsg(mpaVechicleState,str,nsize);
  76. }
  77. else
  78. {
  79. std::cout<<"iv::decition::BrainDecition::ShareBrainstate serialize error."<<std::endl;
  80. }
  81. }
  82. void ivdecision::UpdateGPS(const char *strdata, const unsigned int nSize, const unsigned int index, const QDateTime *dt, const char *strmemname)
  83. {
  84. (void )&index;
  85. (void )&dt;
  86. (void )strmemname;
  87. if(nSize<1)return;
  88. iv::gps::gpsimu xgpsimu;
  89. if(!xgpsimu.ParseFromArray(strdata,nSize))
  90. {
  91. std::cout<<"ivdecision::UpdateGPSMsg error."<<std::endl;
  92. return;
  93. }
  94. iv::GPSData data(new iv::GPS_INS);
  95. data->ins_status = xgpsimu.ins_state();
  96. data->rtk_status = xgpsimu.rtk_state();
  97. data->accel_x = xgpsimu.acce_x();
  98. data->accel_y = xgpsimu.acce_y();
  99. data->accel_z = xgpsimu.acce_z();
  100. data->ang_rate_x = xgpsimu.gyro_x();
  101. data->ang_rate_y = xgpsimu.gyro_y();
  102. data->ang_rate_z = xgpsimu.gyro_z();
  103. data->gps_lat = xgpsimu.lat();
  104. data->gps_lng = xgpsimu.lon();
  105. data->gps_z = xgpsimu.height();
  106. data->ins_heading_angle = xgpsimu.heading();
  107. data->ins_pitch_angle = xgpsimu.pitch();
  108. data->ins_roll_angle = xgpsimu.roll();
  109. data->speed = sqrt(pow(xgpsimu.ve(),2)+pow(xgpsimu.vn(),2)) * 3.6;
  110. GaussProjCal(data->gps_lng, data->gps_lat, &data->gps_x, &data->gps_y);
  111. mMutexGPS.lock();
  112. iv::gps::gpsimu * pgpsimu = new iv::gps::gpsimu;
  113. pgpsimu->CopyFrom(xgpsimu);
  114. mGPSIMUptr.reset(pgpsimu);
  115. mNowGPS = data;
  116. mnGPSUpdateTime = QDateTime::currentMSecsSinceEpoch();
  117. mMutexGPS.unlock();
  118. }
  119. bool ivdecision::GetGPS(GPSData &xGPSData)
  120. {
  121. if((QDateTime::currentMSecsSinceEpoch() - mnGPSUpdateTime)>mnNewThresh)
  122. {
  123. xGPSData = NULL;
  124. return false;
  125. }
  126. xGPSData = mNowGPS;
  127. return true;
  128. }
  129. void ivdecision::UpdateRADAR(const char *strdata, const unsigned int nSize, const unsigned int index, const QDateTime *dt, const char *strmemname)
  130. {
  131. (void )&index;
  132. (void )&dt;
  133. (void )strmemname;
  134. if(nSize<1)return;
  135. iv::radar::radarobjectarray * pradar = new iv::radar::radarobjectarray;
  136. if(!pradar->ParseFromArray(strdata,nSize))
  137. {
  138. std::cout<<"ivdecision::UpdateRADARMsg Parse Error"<<std::endl;
  139. return;
  140. }
  141. mMutexRADAR.lock();
  142. mRADAR.reset(pradar);
  143. mnRADARUpdateTime = QDateTime::currentMSecsSinceEpoch();
  144. mMutexRADAR.unlock();
  145. }
  146. bool ivdecision::GetRADAR(std::shared_ptr<radar::radarobjectarray> &xRADAR)
  147. {
  148. if((QDateTime::currentMSecsSinceEpoch() - mnRADARUpdateTime)>mnNewThresh)
  149. {
  150. xRADAR = NULL;
  151. return false;
  152. }
  153. xRADAR = mRADAR;
  154. return true;
  155. }
  156. void ivdecision::UpdateLIDAR(const char *strdata, const unsigned int nSize, const unsigned int index, const QDateTime *dt, const char *strmemname)
  157. {
  158. (void )&index;
  159. (void )&dt;
  160. (void )strmemname;
  161. if(nSize<1)return;
  162. unsigned int nCount = nSize/sizeof(iv::ObstacleBasic);
  163. std::shared_ptr<std::vector<iv::ObstacleBasic>> lidar_obs = std::shared_ptr<std::vector<iv::ObstacleBasic>>(new std::vector<iv::ObstacleBasic>);
  164. lidar_obs->resize(nCount);
  165. memcpy(lidar_obs->data(),strdata,nCount*sizeof(iv::ObstacleBasic));
  166. mMutexLIDAR.lock();
  167. mlidar_obs = lidar_obs;
  168. mnLIDARUpdateTime = QDateTime::currentMSecsSinceEpoch();
  169. mMutexLIDAR.unlock();
  170. }
  171. bool ivdecision::GetLIDARGrid(LidarGridPtr &lidargridptr)
  172. {
  173. if((QDateTime::currentMSecsSinceEpoch() - mnLIDARUpdateTime)>mnNewThresh)
  174. {
  175. lidargridptr = NULL;
  176. return false;
  177. }
  178. LidarGridPtr ptr = (Obs_grid *)malloc(sizeof(Obs_grid[iv::grx][iv::gry]));
  179. memset(ptr, 0, sizeof(Obs_grid[iv::grx][iv::gry]));
  180. for (int i = 0; i <iv::grx; i++)//复制到指针数组
  181. {
  182. for (int j = 0; j <iv::gry; j++)
  183. {
  184. // ptr[i * (iv::gry) + j].high = obs_grid[i][j].high;
  185. // ptr[i * (iv::gry) + j].low = obs_grid[i][j].low;
  186. ptr[i * (iv::gry) + j].ob = 0;
  187. // ptr[i * (iv::gry) + j].obshight = obs_grid[i][j].obshight;
  188. // ptr[i * (iv::gry) + j].pointcount = obs_grid[i][j].pointcount;
  189. }
  190. }
  191. mMutexLIDAR.lock();
  192. for(unsigned int i=0;i<mlidar_obs->size();i++)
  193. {
  194. iv::ObstacleBasic xobs = mlidar_obs->at(i);
  195. int dx,dy;
  196. dx = (xobs.nomal_x + gridwide * (double)centerx)/gridwide;
  197. dy = (xobs.nomal_y + gridwide * (double)centery)/gridwide;
  198. if((dx>=0)&&(dx<iv::grx)&&(dy>=0)&&(dy<iv::gry))
  199. {
  200. ptr[dx*(iv::gry) +dy].high = xobs.high;
  201. ptr[dx*(iv::gry) +dy].low = xobs.low;
  202. ptr[dx*(iv::gry) + dy].ob = 2;
  203. ptr[dx*(iv::gry) + dy].obshight = xobs.nomal_z;
  204. ptr[dx*(iv::gry) + dy].pointcount = 5;
  205. }
  206. }
  207. lidargridptr = ptr;
  208. mMutexLIDAR.unlock();
  209. return true;
  210. }
  211. bool ivdecision::GetLIDARObs(std::shared_ptr<std::vector<ObstacleBasic> > &xlidar_obs)
  212. {
  213. if((QDateTime::currentMSecsSinceEpoch() - mnLIDARUpdateTime)>mnNewThresh)
  214. {
  215. xlidar_obs = NULL;
  216. return false;
  217. }
  218. mMutexLIDAR.lock();
  219. xlidar_obs = mlidar_obs;
  220. mMutexLIDAR.unlock();
  221. return true;
  222. }
  223. void ivdecision::UpdateMAP(const char *strdata, const unsigned int nSize, const unsigned int index, const QDateTime *dt, const char *strmemname)
  224. {
  225. (void )&index;
  226. (void )&dt;
  227. (void )strmemname;
  228. if(nSize<1)return;
  229. // std::cout<<"update map "<<std::endl;
  230. int gpsunitsize = sizeof(iv::GPS_INS);
  231. int nMapSize = nSize/gpsunitsize;
  232. // std::cout<<"map size is "<<nMapSize<<std::endl;
  233. if(nMapSize < 1)return;
  234. bool bUpdate = true;
  235. // if(nMapSize != mnavigation_data.size())
  236. // {
  237. // bUpdate = true;
  238. // }
  239. // else
  240. // {
  241. // iv::GPS_INS * p = (iv::GPS_INS *)mapdata;
  242. // if((p->gps_lat == navigation_data.at(0)->gps_lat)&&(p->ins_heading_angle == navigation_data.at(0)->ins_heading_angle))
  243. // {
  244. // // qDebug("same map");
  245. // bUpdate = false;
  246. // }
  247. // else
  248. // {
  249. // bUpdate = true;
  250. // }
  251. // }
  252. if(bUpdate)
  253. {
  254. mMutexMAP.lock();
  255. mnavigation_data.clear();
  256. mnavigation_data.resize(nMapSize);
  257. memcpy(mnavigation_data.data(),strdata,nMapSize * sizeof(iv::GPS_INS));
  258. mnMAPUpdateTime = QDateTime::currentMSecsSinceEpoch();
  259. mMutexMAP.unlock();
  260. }
  261. else
  262. {
  263. }
  264. }
  265. bool ivdecision::IsMAPUpdate(qint64 &nLastUpdateTime)
  266. {
  267. if(mnavigation_data.size()<1)
  268. {
  269. return false;
  270. }
  271. if(nLastUpdateTime != mnMAPUpdateTime)
  272. {
  273. return true;
  274. }
  275. return false;
  276. }
  277. bool ivdecision::GetMAP(std::vector<GPSData> &navigation_data, qint64 &nLastUpdateTime)
  278. {
  279. if(mnavigation_data.size()<1)
  280. {
  281. return false;
  282. }
  283. mMutexMAP.unlock();
  284. nLastUpdateTime = mnMAPUpdateTime;
  285. navigation_data = mnavigation_data;
  286. mMutexMAP.unlock();
  287. return true;
  288. }
  289. void ivdecision::UpdateMobileye(const char *strdata, const unsigned int nSize, const unsigned int index, const QDateTime *dt, const char *strmemname)
  290. {
  291. (void )&index;
  292. (void )&dt;
  293. (void )strmemname;
  294. if(nSize<1)return;
  295. std::shared_ptr<iv::mobileye::mobileye> xmobileye = std::shared_ptr<iv::mobileye::mobileye>(new iv::mobileye::mobileye);
  296. if(!xmobileye->ParseFromArray(strdata,nSize))
  297. {
  298. std::cout<<"ivdecision::UpdateMobileye Parse Error."<<std::endl;
  299. return;
  300. }
  301. mMutexmobileye.lock();
  302. mmobileye = xmobileye;
  303. mnmobileyeUpdateTime = QDateTime::currentMSecsSinceEpoch();
  304. mMutexmobileye.unlock();
  305. }
  306. bool ivdecision::GetMobileye(std::shared_ptr<mobileye::mobileye> &xmobileye)
  307. {
  308. if((QDateTime::currentMSecsSinceEpoch() - mnmobileyeUpdateTime)>mnNewThresh)
  309. {
  310. xmobileye = NULL;
  311. return false;
  312. }
  313. mMutexmobileye.lock();
  314. xmobileye = mmobileye;
  315. mMutexmobileye.unlock();
  316. return true;
  317. }
  318. void ivdecision::UpdateHMI(const char *strdata, const unsigned int nSize, const unsigned int index, const QDateTime *dt, const char *strmemname)
  319. {
  320. (void )&index;
  321. (void )&dt;
  322. (void )strmemname;
  323. if(nSize<1)return;
  324. std::shared_ptr<iv::hmi::hmimsg> xhmiptr = std::shared_ptr<iv::hmi::hmimsg>(new iv::hmi::hmimsg);
  325. if(!xhmiptr->ParseFromArray(strdata,nSize))
  326. {
  327. std::cout<<"ivdecision::UpdateHMI Parse Error."<<std::endl;
  328. return;
  329. }
  330. mMutexHMI.lock();
  331. mHMImsg = xhmiptr;
  332. mnHMIUpdateTime = QDateTime::currentMSecsSinceEpoch();
  333. mMutexHMI.unlock();
  334. }
  335. bool ivdecision::GetHMImsg(std::shared_ptr<hmi::hmimsg> &xhmimsg)
  336. {
  337. if((QDateTime::currentMSecsSinceEpoch() - mnHMIUpdateTime)>mnNewThresh)
  338. {
  339. xhmimsg = NULL;
  340. return false;
  341. }
  342. mMutexHMI.lock();
  343. xhmimsg = mHMImsg;
  344. mMutexHMI.unlock();
  345. return true;
  346. }
  347. void ivdecision::UpdateVbox(const char *strdata, const unsigned int nSize, const unsigned int index, const QDateTime *dt, const char *strmemname)
  348. {
  349. (void )&index;
  350. (void )&dt;
  351. (void )strmemname;
  352. std::shared_ptr<iv::vbox::vbox> vbox_ptr = std::shared_ptr<iv::vbox::vbox>(new iv::vbox::vbox);
  353. if(!vbox_ptr->ParseFromArray(strdata,nSize))
  354. {
  355. std::cout<<"vdecision::UpdateVbox Parse Error"<<std::endl;
  356. return;
  357. }
  358. mMutexvbox.lock();
  359. mvboxmsg = vbox_ptr;
  360. mnvboxUpdateTime = QDateTime::currentMSecsSinceEpoch();
  361. mMutexvbox.unlock();
  362. }
  363. bool ivdecision::Getvboxmsg(std::shared_ptr<vbox::vbox> &xvboxmsg)
  364. {
  365. if((QDateTime::currentMSecsSinceEpoch() - mnvboxUpdateTime)>mnNewThresh)
  366. {
  367. xvboxmsg = NULL;
  368. return false;
  369. }
  370. mMutexvbox.lock();
  371. xvboxmsg = mvboxmsg;
  372. mMutexvbox.unlock();
  373. return true;
  374. }
  375. void ivdecision::Updatev2x(const char *strdata, const unsigned int nSize, const unsigned int index, const QDateTime *dt, const char *strmemname)
  376. {
  377. (void )&index;
  378. (void )&dt;
  379. (void )strmemname;
  380. std::shared_ptr<iv::v2x::v2x> xv2x_ptr = std::shared_ptr<iv::v2x::v2x>(new iv::v2x::v2x);
  381. if(!xv2x_ptr->ParseFromArray(strdata,nSize))
  382. {
  383. std::cout<<"ivdecision::Updatev2x Parse Error."<<std::endl;
  384. return;
  385. }
  386. mMutexv2x.lock();
  387. mv2xmsg = xv2x_ptr;
  388. mnv2xUpdateTime = QDateTime::currentMSecsSinceEpoch();
  389. mMutexv2x.unlock();
  390. }
  391. bool ivdecision::Getv2xmsg(std::shared_ptr<v2x::v2x> &xv2xmsg)
  392. {
  393. if((QDateTime::currentMSecsSinceEpoch() - mnv2xUpdateTime)>mnNewThresh)
  394. {
  395. xv2xmsg = NULL;
  396. return false;
  397. }
  398. mMutexv2x.lock();
  399. xv2xmsg = mv2xmsg;
  400. mMutexv2x.unlock();
  401. return true;
  402. }
  403. void ivdecision::Updateultrasonic(const char *strdata, const unsigned int nSize, const unsigned int index, const QDateTime *dt, const char *strmemname)
  404. {
  405. (void )&index;
  406. (void )&dt;
  407. (void )strmemname;
  408. std::shared_ptr<iv::ultrasonic::ultrasonic> xultrasonic_ptr
  409. = std::shared_ptr<iv::ultrasonic::ultrasonic>(new iv::ultrasonic::ultrasonic);
  410. if(!xultrasonic_ptr->ParseFromArray(strdata,nSize))
  411. {
  412. std::cout<<"ivdecision::Updateultrasonic Parse Error."<<std::endl;
  413. return;
  414. }
  415. mMutexultrasonic.lock();
  416. multrasonicmsg = xultrasonic_ptr;
  417. mnultrasonicUpdateTime = QDateTime::currentMSecsSinceEpoch();
  418. mMutexultrasonic.unlock();
  419. }
  420. bool ivdecision::Getultrasonic(std::shared_ptr<ultrasonic::ultrasonic> &xultramsg)
  421. {
  422. if((QDateTime::currentMSecsSinceEpoch() - mnultrasonicUpdateTime)>mnNewThresh)
  423. {
  424. xultramsg = NULL;
  425. return false;
  426. }
  427. mMutexultrasonic.lock();
  428. xultramsg = multrasonicmsg;
  429. mMutexultrasonic.unlock();
  430. return true;
  431. }
  432. }