main.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. #include <QCoreApplication>
  2. #include <math.h>
  3. #include <string>
  4. #include <thread>
  5. #include <QFile>
  6. #include "OpenDrive/OpenDrive.h"
  7. #include "OpenDrive/OpenDriveXmlParser.h"
  8. #include "globalplan.h"
  9. #include "gpsimu.pb.h"
  10. #include "v2x.pb.h"
  11. #include "hmi.pb.h"
  12. #include "modulecomm.h"
  13. #include "xmlparam.h"
  14. #include "gps_type.h"
  15. #include "xodrdijkstra.h"
  16. #include "gnss_coordinate_convert.h"
  17. #include "ivexit.h"
  18. #include "ivversion.h"
  19. #include "ivbacktrace.h"
  20. #include <signal.h>
  21. OpenDrive mxodr;
  22. xodrdijkstra * gpxd;
  23. bool gmapload = false;
  24. double glat0,glon0,ghead0;
  25. double gvehiclewidth = 2.0;
  26. bool gbExtendMap = true;
  27. void * gpa;
  28. void * gpasrc;
  29. void * gpmap;
  30. void * gpagps;
  31. void * gpasimple;
  32. void * gpav2x;
  33. void * gpaHmi;
  34. iv::Ivfault *gfault = nullptr;
  35. iv::Ivlog *givlog = nullptr;
  36. static QCoreApplication * gApp;
  37. namespace iv {
  38. struct simpletrace
  39. {
  40. double gps_lat = 0;//纬度
  41. double gps_lng = 0;//经度
  42. double gps_x = 0;
  43. double gps_y = 0;
  44. double gps_z = 0;
  45. double ins_heading_angle = 0; //航向角
  46. };
  47. }
  48. /**
  49. *
  50. *
  51. *
  52. *
  53. * */
  54. bool LoadXODR(std::string strpath)
  55. {
  56. OpenDriveXmlParser xp(&mxodr);
  57. xp.ReadFile(strpath);
  58. std::cout<<"road cout is "<<mxodr.GetRoadCount()<<std::endl;
  59. if(mxodr.GetRoadCount() < 1)
  60. {
  61. gmapload = true;
  62. return false;
  63. }
  64. xodrdijkstra * pxd = new xodrdijkstra(&mxodr);
  65. gpxd = pxd;
  66. // std::vector<int> xpath = pxd->getpath(10001,2,30012,2);
  67. // pxd->getgpspoint(10001,2,30012,2,xpath);
  68. int i;
  69. double nlenth = 0;
  70. int nroadsize = mxodr.GetRoadCount();
  71. for(i=0;i<nroadsize;i++)
  72. {
  73. Road * px = mxodr.GetRoad(i);
  74. nlenth = nlenth + px->GetRoadLength();
  75. int bloksize = px->GetGeometryBlockCount();
  76. if(px->GetGeometryBlock(0)->GetGeometryAt(0)->GetGeomType() == 4)
  77. {
  78. GeometryParamPoly3 * p = (GeometryParamPoly3 *) px->GetGeometryBlock(0)->GetGeometryAt(0);
  79. double a = p->GetuA();
  80. a = p->GetuB();
  81. a = p->GetuC();
  82. a = p->GetuD();
  83. a = p->GetvA();
  84. }
  85. }
  86. // void Header::GetAllParams(unsigned short int &revMajor, unsigned short int &revMinor, string &name, float &version, string &date,
  87. // double &north, double &south, double &east,double &west,double &lat0,double &lon0, double & hdg0)
  88. unsigned short int revMajor,revMinor;
  89. std::string name,date;
  90. float version;
  91. double north,south,east,west,lat0,lon0,hdg0;
  92. if(mxodr.GetHeader() != 0)
  93. {
  94. mxodr.GetHeader()->GetAllParams(revMajor,revMinor,name,version,date,north,south,east,west,lat0,lon0,hdg0);
  95. glat0 = lat0;
  96. glon0 = lon0;
  97. }
  98. Road * proad1 = mxodr.GetRoad(0);
  99. givlog->info("road name is %s",proad1->GetRoadName().c_str());
  100. std::cout<<" road name is "<<proad1->GetRoadName()<<std::endl;
  101. }
  102. class roadx
  103. {
  104. public:
  105. roadx * para;
  106. std::vector<roadx> child;
  107. int nlen;
  108. };
  109. #define EARTH_RADIUS 6370856.0
  110. //从1到2的距离和方向
  111. int CalcDisAngle(double lat1,double lon1,double lat2,double lon2,double * pLatDis,double * pLonDis,double * pDis,double * pangle)
  112. {
  113. double a,b;
  114. double LonDis,LatDis;
  115. double LonRadius;
  116. double Dis;
  117. double angle;
  118. if((lat1 == lat2)&&(lon1 == lon2))return -1;
  119. LonRadius = EARTH_RADIUS * cos(lat2/(180.0/M_PI));
  120. a = (EARTH_RADIUS * M_PI*2.0/360.0)/(100000.0);
  121. b = lat2 - lat1; b = b*100000.0;
  122. LatDis = a*b;
  123. a = (LonRadius * M_PI*2.0/360.0)/100000.0;
  124. b = lon2 - lon1; b = b*100000.0;
  125. LonDis = a*b;
  126. Dis = sqrt(LatDis*LatDis + LonDis *LonDis);
  127. angle = acos(fabs(LonDis)/Dis);
  128. angle = angle * 180.0/M_PI;
  129. if(LonDis > 0)
  130. {
  131. if(LatDis > 0)angle = 90.0 - angle;
  132. else angle= 90.0+angle;
  133. }
  134. else
  135. {
  136. if(LatDis > 0)angle = 270.0+angle;
  137. else angle = 270.0-angle;
  138. }
  139. if(pLatDis != 0)*pLatDis = LatDis;
  140. if(pLonDis != 0)*pLonDis = LonDis;
  141. if(pDis != 0)*pDis = Dis;
  142. if(pangle != 0)*pangle = angle;
  143. }
  144. //==========================================================
  145. //高斯投影由经纬度(Unit:DD)反算大地坐标(含带号,Unit:Metres)
  146. //void GaussProjCal(double longitude, double latitude, double *X, double *Y)
  147. //{
  148. // int ProjNo = 0; int ZoneWide; ////带宽
  149. // double longitude1, latitude1, longitude0, latitude0, X0, Y0, xval, yval;
  150. // double a, f, e2, ee, NN, T, C, A, M, iPI;
  151. // iPI = 0.0174532925199433; ////3.1415926535898/180.0;
  152. // ZoneWide = 6; ////6度带宽
  153. // a = 6378245.0; f = 1.0 / 298.3; //54年北京坐标系参数
  154. // ////a=6378140.0; f=1/298.257; //80年西安坐标系参数
  155. // ProjNo = (int)(longitude / ZoneWide);
  156. // longitude0 = ProjNo * ZoneWide + ZoneWide / 2;
  157. // longitude0 = longitude0 * iPI;
  158. // latitude0 = 0;
  159. // longitude1 = longitude * iPI; //经度转换为弧度
  160. // latitude1 = latitude * iPI; //纬度转换为弧度
  161. // e2 = 2 * f - f * f;
  162. // ee = e2 * (1.0 - e2);
  163. // NN = a / sqrt(1.0 - e2 * sin(latitude1)*sin(latitude1));
  164. // T = tan(latitude1)*tan(latitude1);
  165. // C = ee * cos(latitude1)*cos(latitude1);
  166. // A = (longitude1 - longitude0)*cos(latitude1);
  167. // M = a * ((1 - e2 / 4 - 3 * e2*e2 / 64 - 5 * e2*e2*e2 / 256)*latitude1 - (3 * e2 / 8 + 3 * e2*e2 / 32 + 45 * e2*e2
  168. // *e2 / 1024)*sin(2 * latitude1)
  169. // + (15 * e2*e2 / 256 + 45 * e2*e2*e2 / 1024)*sin(4 * latitude1) - (35 * e2*e2*e2 / 3072)*sin(6 * latitude1));
  170. // xval = NN * (A + (1 - T + C)*A*A*A / 6 + (5 - 18 * T + T * T + 72 * C - 58 * ee)*A*A*A*A*A / 120);
  171. // yval = M + NN * tan(latitude1)*(A*A / 2 + (5 - T + 9 * C + 4 * C*C)*A*A*A*A / 24
  172. // + (61 - 58 * T + T * T + 600 * C - 330 * ee)*A*A*A*A*A*A / 720);
  173. // X0 = 1000000L * (ProjNo + 1) + 500000L;
  174. // Y0 = 0;
  175. // xval = xval + X0; yval = yval + Y0;
  176. // *X = xval;
  177. // *Y = yval;
  178. //}
  179. #include <math.h>
  180. static void OffLngLat(double fLat0,double fLng0,double & fLat,double & fLng,double fHeading,double x,double y)
  181. {
  182. double fxdiff,fydiff;
  183. double xoff = y*(-1);
  184. double yoff = x;
  185. fxdiff = 0+xoff * cos(fHeading*M_PI/180.0)+ yoff*sin(fHeading*M_PI/180.0); //East
  186. fydiff = 0-xoff * sin(fHeading*M_PI/180.0)+ yoff*cos(fHeading*M_PI/180.0); //South
  187. double fEarthRadius = 6378245.0;
  188. double ns1d = fEarthRadius*2*M_PI/360.0;
  189. double fewRadius = fEarthRadius * cos(fLat0*M_PI/180.0);
  190. double ew1d = fewRadius * 2*M_PI/360.0;
  191. fLat = fLat0 + fydiff/ns1d;
  192. fLng = fLng0 + fxdiff/ew1d;
  193. }
  194. void CalcXY(const double lat0,const double lon0,const double hdg0,
  195. const double lat,const double lon,
  196. double & x,double & y)
  197. {
  198. double x0,y0;
  199. GaussProjCal(lon0,lat0,&x0,&y0);
  200. GaussProjCal(lon,lat,&x,&y);
  201. x = x - x0;
  202. y = y- y0;
  203. // double ang,dis;
  204. // CalcDisAngle(lat0,lon0,lat,lon,0,0,&dis,&ang);
  205. // double xang = hdg0 - ang;
  206. // while(xang<0)xang = xang + 360.0;
  207. // x = dis * cos(xang * M_PI/180.0);
  208. // y = dis * sin(xang * M_PI/180.0);
  209. }
  210. //void CalcLatLon(const double lat0,const double lon0,const double hdg0,
  211. // const double x,const double y,const double xyhdg,
  212. // double &lat,double & lon, double & hdg)
  213. //{
  214. // OffLngLat(lat0,lon0,lat,lon,hdg0,x,y);
  215. // hdg = hdg0 - xyhdg * 180.0/M_PI;
  216. // while(hdg < 0)hdg = hdg + 360;
  217. // while(hdg >= 360)hdg = hdg - 360;
  218. //}
  219. void CalcLatLon(const double lat0,const double lon0,
  220. const double x,const double y,
  221. double &lat,double & lon)
  222. {
  223. double x0,y0;
  224. GaussProjCal(lon0,lat0,&x0,&y0);
  225. double x_gps,y_gps;
  226. x_gps = x0+x;
  227. y_gps = y0+y;
  228. GaussProjInvCal(x_gps,y_gps,&lon,&lat);
  229. }
  230. void CalcLatLon(const double lat0,const double lon0,const double hdg0,
  231. const double x,const double y,const double xyhdg,
  232. double &lat,double & lon, double & hdg)
  233. {
  234. double x0,y0;
  235. GaussProjCal(lon0,lat0,&x0,&y0);
  236. double x_gps,y_gps;
  237. x_gps = x0+x;
  238. y_gps = y0+y;
  239. GaussProjInvCal(x_gps,y_gps,&lon,&lat);
  240. // hdg = hdg0 -xyhdg * 270/M_PI;
  241. hdg = 90 - xyhdg* 180.0/M_PI;
  242. // OffLngLat(lat0,lon0,lat,lon,hdg0,x,y);
  243. // hdg = hdg0 - xyhdg * 180.0/M_PI;
  244. while(hdg < 0)hdg = hdg + 360;
  245. while(hdg >= 360)hdg = hdg - 360;
  246. }
  247. class xodrobj
  248. {
  249. public:
  250. double flatsrc;
  251. double flonsrc;
  252. double fhgdsrc;
  253. double flat;
  254. double flon;
  255. int lane;
  256. };
  257. xodrobj gsrc;
  258. void ShareMap(std::vector<iv::GPSData> navigation_data)
  259. {
  260. if(navigation_data.size()<1)return;
  261. iv::GPS_INS x;
  262. x = *(navigation_data.at(0));
  263. char * data = new char[sizeof(iv::GPS_INS)*navigation_data.size()];
  264. int gpssize = sizeof(iv::GPS_INS);
  265. int i;
  266. for(i=0;i<navigation_data.size();i++)
  267. {
  268. x = *(navigation_data.at(i));
  269. memcpy(data+i*gpssize,&x,gpssize);
  270. }
  271. iv::modulecomm::ModuleSendMsg(gpmap,data,navigation_data.size()*gpssize);
  272. int nsize = 100;
  273. int nstep = 1;
  274. if(navigation_data.size() < 100)
  275. {
  276. nsize = navigation_data.size();
  277. }
  278. else
  279. {
  280. nstep = navigation_data.size()/100;
  281. }
  282. iv::simpletrace psim[100];
  283. for(i=0;i<nsize;i++)
  284. {
  285. x = *(navigation_data.at(i*nstep));
  286. psim[i].gps_lat = x.gps_lat;
  287. psim[i].gps_lng = x.gps_lng;
  288. psim[i].gps_z = x.gps_z;
  289. psim[i].gps_x = x.gps_x;
  290. psim[i].gps_y = x.gps_y;
  291. psim[i].ins_heading_angle = x.ins_heading_angle;
  292. }
  293. if(navigation_data.size()>100)
  294. {
  295. int nlast = 99;
  296. x = *(navigation_data.at(navigation_data.size()-1));
  297. psim[nlast].gps_lat = x.gps_lat;
  298. psim[nlast].gps_lng = x.gps_lng;
  299. psim[nlast].gps_z = x.gps_z;
  300. psim[nlast].gps_x = x.gps_x;
  301. psim[nlast].gps_y = x.gps_y;
  302. psim[nlast].ins_heading_angle = x.ins_heading_angle;
  303. }
  304. iv::modulecomm::ModuleSendMsg(gpasimple,(char *)psim,nsize * sizeof(iv::simpletrace));
  305. delete data;
  306. }
  307. static void ToGPSTrace(std::vector<PlanPoint> xPlan)
  308. {
  309. // double x_src,y_src,x_dst,y_dst;
  310. // x_src = -26;y_src = 10;
  311. // x_dst = -50;y_dst = -220;
  312. int i;
  313. int nSize = xPlan.size();
  314. std::vector<iv::GPSData> mapdata;
  315. QFile xfile;
  316. QString strpath;
  317. strpath = getenv("HOME");
  318. strpath = strpath + "/map/maptrace.txt";
  319. xfile.setFileName(strpath);
  320. bool bFileOpen = xfile.open(QIODevice::ReadWrite);
  321. for(i=0;i<nSize;i++)
  322. {
  323. iv::GPSData data(new iv::GPS_INS);
  324. CalcLatLon(glat0,glon0,ghead0,xPlan[i].x,xPlan[i].y,xPlan[i].hdg,data->gps_lat,
  325. data->gps_lng,data->ins_heading_angle);
  326. data->index = i;
  327. data->speed = xPlan[i].speed;
  328. GaussProjCal(data->gps_lng,data->gps_lat,&data->gps_x,&data->gps_y);
  329. givlog->debug(" %d %11.7f %11.7f %11.3f ",i,data->gps_lat,
  330. data->gps_lng,data->ins_heading_angle);
  331. data->roadSum = xPlan[i].mnLaneTotal;
  332. data->roadMode = 0;
  333. data->roadOri = xPlan[i].mnLaneori;
  334. data->mfDisToLaneLeft = xPlan[i].mfDisToLaneLeft;
  335. data->mfDisToRoadLeft = xPlan[i].mfDisToRoadLeft;
  336. data->mfLaneWidth = xPlan[i].mWidth;
  337. data->mfRoadWidth = xPlan[i].mfRoadWidth;
  338. data->mnLaneChangeMark = xPlan[i].lanmp;
  339. if(xPlan[i].lanmp == -1)data->roadMode = 15;
  340. if(xPlan[i].lanmp == 1)data->roadMode = 14;
  341. mapdata.push_back(data);
  342. char strline[255];
  343. snprintf(strline,255,"%d\t%11.7f\t%11.7f\t%d\t%d\t%11.3f\t%d\t%d\t%d\t%d\n",
  344. i,data->gps_lng,data->gps_lat,0,0,data->ins_heading_angle,0,0,0,0);
  345. if(bFileOpen) xfile.write(strline);
  346. // fout << gps_index << "\t" << data->gps_lng << "\t" << data->gps_lat << "\t" << ServiceCarStatus.location->speed_mode << "\t" << ServiceCarStatus.location->mode2 << "\t" << data->ins_heading_angle << "\t" << obs_modes << "\t" << speed_modes << "\t" << lane_num << "\t" << lane_status <<"\t" <<road_width <<std::endl;
  347. }
  348. if(bFileOpen)xfile.close();
  349. ShareMap(mapdata);
  350. }
  351. int avoidroadid[] = {10002,10019,10003,10098,10099,10063,10099,10100,10104,10110,10111};
  352. inline bool isboringroad(int nroadid)
  353. {
  354. int i;
  355. bool brtn = false;
  356. for(i=0;i<11;i++)
  357. {
  358. if(avoidroadid[i] == nroadid)
  359. {
  360. brtn = true;
  361. break;
  362. }
  363. }
  364. return brtn;
  365. }
  366. void SetPlan(xodrobj xo)
  367. {
  368. double x_src,y_src,x_dst,y_dst;
  369. CalcXY(glat0,glon0,ghead0,xo.flatsrc,xo.flonsrc,x_src,y_src);
  370. CalcXY(glat0,glon0,ghead0,xo.flat,xo.flon,x_dst,y_dst);
  371. std::vector<PlanPoint> xPlan;
  372. double s;
  373. // x_src = -5;y_src = 6;
  374. // x_dst = -60;y_src = -150;
  375. int nRtn = MakePlan(gpxd,&mxodr,x_src,y_src,(90 - xo.fhgdsrc)*M_PI/180.0,x_dst,y_dst,s,30.0,100.0,xo.lane,xPlan);
  376. if(nRtn < 0)
  377. {
  378. qDebug("plan fail.");
  379. return;
  380. }
  381. int i;
  382. int nSize = xPlan.size();
  383. if(nSize<1)
  384. {
  385. qDebug("plan fail.");
  386. return;
  387. }
  388. PlanPoint xLastPoint = xPlan[nSize -1];
  389. for(i=0;i<600;i++)
  390. {
  391. PlanPoint pp = xLastPoint;
  392. double fdis = 0.1*(i+1);
  393. pp.mS = pp.mS + i*0.1;
  394. pp.x = pp.x + fdis * cos(pp.hdg);
  395. pp.y = pp.y + fdis * sin(pp.hdg);
  396. pp.nSignal = 23;
  397. if(gbExtendMap)
  398. {
  399. xPlan.push_back(pp);
  400. }
  401. }
  402. nSize = xPlan.size();
  403. std::vector<iv::GPSData> mapdata;
  404. QFile xfile;
  405. QString strpath;
  406. strpath = getenv("HOME");
  407. strpath = strpath + "/map/maptrace.txt";
  408. xfile.setFileName(strpath);
  409. xfile.open(QIODevice::ReadWrite);
  410. for(i=0;i<nSize;i++)
  411. {
  412. iv::GPSData data(new iv::GPS_INS);
  413. CalcLatLon(glat0,glon0,ghead0,xPlan[i].x,xPlan[i].y,xPlan[i].hdg,data->gps_lat,
  414. data->gps_lng,data->ins_heading_angle);
  415. CalcLatLon(glat0,glon0,xPlan[i].mx_left,xPlan[i].my_left,
  416. data->gps_lat_avoidleft,data->gps_lng_avoidleft);
  417. CalcLatLon(glat0,glon0,xPlan[i].mx_right,xPlan[i].my_right,
  418. data->gps_lat_avoidright,data->gps_lng_avoidright);
  419. data->index = i;
  420. data->speed = xPlan[i].speed;
  421. // ZBGaussProjCal(data->gps_lng,data->gps_lat,&data->gps_x,&data->gps_y);
  422. GaussProjCal(data->gps_lng,data->gps_lat,&data->gps_x,&data->gps_y);
  423. GaussProjCal(data->gps_lng_avoidleft,data->gps_lat_avoidleft,&data->gps_x_avoidleft,&data->gps_y_avoidleft);
  424. GaussProjCal(data->gps_lng_avoidright,data->gps_lat_avoidright,&data->gps_x_avoidright,&data->gps_y_avoidright);
  425. givlog->verbose(" %d %11.7f %11.7f %11.3f ",i,data->gps_lat,
  426. data->gps_lng,data->ins_heading_angle);
  427. data->roadOri = xPlan[i].mnLaneori;
  428. data->roadSum = xPlan[i].mnLaneTotal;
  429. data->mfDisToLaneLeft = xPlan[i].mfDisToLaneLeft;
  430. data->mfDisToRoadLeft = xPlan[i].mfDisToRoadLeft;
  431. data->mfLaneWidth = xPlan[i].mWidth;
  432. data->mfRoadWidth = xPlan[i].mfRoadWidth;
  433. data->mbInLaneAvoid = xPlan[i].bInlaneAvoid;
  434. if(xPlan[i].mbBoringRoad)
  435. {
  436. data->roadOri = 0;
  437. data->roadSum = 2;
  438. }
  439. data->mbnoavoid = xPlan[i].mbNoavoid;
  440. if(data->mbnoavoid)
  441. {
  442. qDebug("no avoid i = %d",i);
  443. }
  444. data->mfCurvature = xPlan[i].mfCurvature;
  445. data->mode2 = xPlan[i].nSignal;
  446. if(data->mode2 == 3000)
  447. {
  448. int k;
  449. for(k=(mapdata.size()-1);k>(mapdata.size()-150);k--)
  450. {
  451. if(k<0)break;
  452. mapdata.at(k)->mode2 = 3000;
  453. }
  454. }
  455. #ifdef BOAVOID
  456. if(isboringroad(xPlan[i].nRoadID))
  457. {
  458. const int nrangeavoid = 300;
  459. if((i+(nrangeavoid + 10))<nSize)
  460. {
  461. double fhdg1 = xPlan[i].hdg;
  462. bool bavoid = true;
  463. // int k;
  464. // for(k=0;k<=10;k++)
  465. // {
  466. // double fhdg5 = xPlan[i+nrangeavoid*k/10].hdg;
  467. // double fhdgdiff1 = fhdg5 - fhdg1;
  468. // while(fhdgdiff1<0)fhdgdiff1 = fhdgdiff1 + 2.0*M_PI;
  469. // if((fhdgdiff1>(M_PI/3.0))&&(fhdgdiff1<(5.0*M_PI/3.0)))
  470. // {
  471. // bavoid = false;
  472. // break;
  473. // }
  474. // }
  475. if(bavoid)
  476. {
  477. data->roadSum = 2;
  478. data->roadOri = 0;
  479. }
  480. }
  481. else
  482. {
  483. int a = 1;
  484. a++;
  485. }
  486. }
  487. #endif
  488. // data->roadSum = 1;
  489. // data->roadMode = 0;
  490. // data->roadOri = 0;
  491. // if(xPlan[i].lanmp == -1)data->roadMode = 15;
  492. // if(xPlan[i].lanmp == 1)data->roadMode = 14;
  493. mapdata.push_back(data);
  494. char strline[255];
  495. // snprintf(strline,255,"%d\t%11.7f\t%11.7f\t%d\t%11.3f\t%d\t%d\t%d\t%d\t%d\t%d\t%11.7f\n",
  496. // i,data->gps_lng,data->gps_lat,0,data->ins_heading_angle,0,0,0,data->roadOri,data->roadSum,data->mode2,data->mfCurvature);
  497. snprintf(strline,255,"%d\t%11.7f\t%11.7f\t%d\t%d\t%11.3f\t%d\t%d\t%d\t%d\n",
  498. i,data->gps_lng,data->gps_lat,0,0,data->ins_heading_angle,0,0,data->roadSum,data->roadOri);
  499. xfile.write(strline);
  500. }
  501. xfile.close();
  502. ShareMap(mapdata);
  503. s = 1;
  504. }
  505. void MultiStationPlan(iv::v2x::v2x * pxv2x,double fsrclat,double fsrclon,double fsrcheading,int nlane)
  506. {
  507. std::vector<PlanPoint> xPlan;
  508. int i;
  509. double fLastHdg = 0;
  510. int ndeflane =nlane;
  511. for(i=0;i<pxv2x->stgps_size();i++)
  512. {
  513. double x_src,y_src,x_dst,y_dst;
  514. if(i==0)
  515. {
  516. CalcXY(glat0,glon0,ghead0,fsrclat,fsrclon,x_src,y_src);
  517. }
  518. else
  519. {
  520. CalcXY(glat0,glon0,ghead0,pxv2x->stgps(i-1).lat(),pxv2x->stgps(i-1).lon(),x_src,y_src);
  521. }
  522. CalcXY(glat0,glon0,ghead0,pxv2x->stgps(i).lat(),pxv2x->stgps(i).lon(),x_dst,y_dst);
  523. std::vector<PlanPoint> xPlanPart;
  524. double s;
  525. // x_src = -5;y_src = 6;
  526. // x_dst = -60;y_src = -150;
  527. int nRtn = -1;
  528. if(i==0)
  529. {
  530. nRtn = MakePlan(gpxd,&mxodr,x_src,y_src,(90 - fsrcheading)*M_PI/180.0,x_dst,y_dst,s,30.0,100.0,ndeflane,xPlanPart);
  531. }
  532. else
  533. {
  534. nRtn = MakePlan(gpxd,&mxodr,x_src,y_src,fLastHdg,x_dst,y_dst,s,30.0,100.0,ndeflane,xPlanPart);
  535. }
  536. if(nRtn < 0)
  537. {
  538. qDebug("plan fail.");
  539. return;
  540. }
  541. int j;
  542. for(j=0;j<xPlanPart.size();j++)xPlan.push_back(xPlanPart.at(j));
  543. fLastHdg = xPlanPart.at(xPlanPart.size()-1).hdg;
  544. }
  545. int nSize = xPlan.size();
  546. std::vector<iv::GPSData> mapdata;
  547. QFile xfile;
  548. QString strpath;
  549. strpath = getenv("HOME");
  550. strpath = strpath + "/map/maptrace.txt";
  551. xfile.setFileName(strpath);
  552. xfile.open(QIODevice::ReadWrite);
  553. for(i=0;i<nSize;i++)
  554. {
  555. iv::GPSData data(new iv::GPS_INS);
  556. CalcLatLon(glat0,glon0,ghead0,xPlan[i].x,xPlan[i].y,xPlan[i].hdg,data->gps_lat,
  557. data->gps_lng,data->ins_heading_angle);
  558. data->index = i;
  559. data->speed = xPlan[i].speed;
  560. // ZBGaussProjCal(data->gps_lng,data->gps_lat,&data->gps_x,&data->gps_y);
  561. GaussProjCal(data->gps_lng,data->gps_lat,&data->gps_x,&data->gps_y);
  562. givlog->verbose(" %d %11.7f %11.7f %11.3f ",i,data->gps_lat,
  563. data->gps_lng,data->ins_heading_angle);
  564. // data->roadSum = 1;
  565. // data->roadMode = 0;
  566. // data->roadOri = 0;
  567. // if(xPlan[i].lanmp == -1)data->roadMode = 15;
  568. // if(xPlan[i].lanmp == 1)data->roadMode = 14;
  569. data->roadOri = xPlan[i].mnLaneori;
  570. data->roadSum = xPlan[i].mnLaneTotal;
  571. data->mfDisToLaneLeft = xPlan[i].mfDisToLaneLeft;
  572. data->mfDisToRoadLeft = xPlan[i].mfDisToRoadLeft;
  573. data->mfLaneWidth = xPlan[i].mWidth;
  574. data->mfRoadWidth = xPlan[i].mfRoadWidth;
  575. data->mode2 = xPlan[i].nSignal;
  576. if(data->mode2 == 3000)
  577. {
  578. int k;
  579. for(k=(mapdata.size()-1);k>(mapdata.size()-150);k--)
  580. {
  581. if(k<0)break;
  582. mapdata.at(k)->mode2 = 3000;
  583. }
  584. }
  585. mapdata.push_back(data);
  586. char strline[255];
  587. snprintf(strline,255,"%d\t%11.7f\t%11.7f\t%d\t%d\t%11.3f\t%d\t%d\t%d\t%d\n",
  588. i,data->gps_lng,data->gps_lat,0,data->ins_heading_angle,0,0,0,0,0);
  589. xfile.write(strline);
  590. }
  591. xfile.close();
  592. ShareMap(mapdata);
  593. }
  594. void ListenCmd(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  595. {
  596. if(nSize<sizeof(xodrobj))
  597. {
  598. std::cout<<"ListenCmd small."<<std::endl;
  599. return;
  600. }
  601. xodrobj xo;
  602. memcpy(&xo,strdata,sizeof(xodrobj));
  603. givlog->debug("mapLoad","lat is %f, lon is %f", xo.flat, xo.flon);
  604. xo.fhgdsrc = gsrc.fhgdsrc;
  605. xo.flatsrc = gsrc.flatsrc;
  606. xo.flonsrc = gsrc.flonsrc;
  607. SetPlan(xo);
  608. }
  609. void ListenV2X(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  610. {
  611. iv::v2x::v2x xv2x;
  612. if(!xv2x.ParseFromArray(strdata,nSize))
  613. {
  614. givlog->warn("ListernV2X Parse Error.");
  615. std::cout<<"ListenV2X Parse Error."<<std::endl;
  616. return;
  617. }
  618. if(xv2x.stgps_size()<1)
  619. {
  620. givlog->debug("ListenV2X no gps station.");
  621. std::cout<<"ListenV2X no gps station."<<std::endl;
  622. return;
  623. }
  624. MultiStationPlan(&xv2x,gsrc.flatsrc,gsrc.flonsrc,gsrc.fhgdsrc,1);
  625. }
  626. void ListenHmi(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  627. {
  628. iv::hmi::hmimsg xhmimsg;
  629. if(!xhmimsg.ParseFromArray(strdata,nSize))
  630. {
  631. givlog->warn("<map lodar> ListenHmi Parse Error.");
  632. std::cout<<"<map lodar> ListenHmi Parse Error."<<std::endl;
  633. return;
  634. }
  635. if(xhmimsg.has_mapname())
  636. {
  637. if(LoadXODR(xhmimsg.mapname()))
  638. givlog->info("mapLoad","load map success: %s",xhmimsg.mapname().c_str());
  639. else
  640. givlog->error("mapLoad","load map fail: %s",xhmimsg.mapname().c_str());
  641. }
  642. }
  643. void ListenSrc(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  644. {
  645. if(nSize<sizeof(xodrobj))
  646. {
  647. givlog->warn("ListenSrc small");
  648. std::cout<<"ListenSrc small."<<std::endl;
  649. return;
  650. }
  651. memcpy(&gsrc,strdata,sizeof(xodrobj));
  652. // givlog->debug("src hdg is %f", gsrc.fhgdsrc);
  653. // std::cout<<"src hdg is "<<gsrc.fhgdsrc<<std::endl;
  654. }
  655. void UpdateGPSIMU(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname)
  656. {
  657. iv::gps::gpsimu xgpsimu;
  658. if(!xgpsimu.ParseFromArray(strdata,nSize))
  659. {
  660. givlog->warn("ADCIntelligentVehicle::UpdateGPSIMU parse error. nSize is %d",nSize);
  661. return;
  662. }
  663. xodrobj xo;
  664. xo.fhgdsrc = xgpsimu.heading();
  665. xo.flatsrc = xgpsimu.lat();
  666. xo.flonsrc = xgpsimu.lon();
  667. gsrc = xo;
  668. // givlog->debug("src hdg is %f", gsrc.fhgdsrc);
  669. // std::cout<<"src hdg is "<<gsrc.fhgdsrc<<std::endl;
  670. }
  671. void ExitFunc()
  672. {
  673. // gApp->quit();
  674. iv::modulecomm::Unregister(gpasimple);
  675. iv::modulecomm::Unregister(gpav2x);
  676. iv::modulecomm::Unregister(gpagps);
  677. iv::modulecomm::Unregister(gpasrc);
  678. iv::modulecomm::Unregister(gpmap);
  679. iv::modulecomm::Unregister(gpa);
  680. std::cout<<"driver_map_xodrload exit."<<std::endl;
  681. gApp->quit();
  682. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  683. // std::this_thread::sleep_for(std::chrono::milliseconds(900));
  684. }
  685. void signal_handler(int sig)
  686. {
  687. if(sig == SIGINT)
  688. {
  689. ExitFunc();
  690. }
  691. }
  692. int main(int argc, char *argv[])
  693. {
  694. showversion("driver_map_xodrload");
  695. QCoreApplication a(argc, argv);
  696. gApp = &a;
  697. RegisterIVBackTrace();
  698. gfault = new iv::Ivfault("driver_map_xodrload");
  699. givlog = new iv::Ivlog("driver_map_xodrload");
  700. std::string strmapth,strparapath;
  701. if(argc<3)
  702. {
  703. // strmapth = "./map.xodr";
  704. strmapth = getenv("HOME");
  705. strmapth = strmapth + "/map/map.xodr";
  706. // strmapth = "/home/yuchuli/1226.xodr";
  707. strparapath = "./driver_map_xodrload.xml";
  708. }
  709. else
  710. {
  711. strmapth = argv[1];
  712. strparapath = argv[2];
  713. }
  714. iv::xmlparam::Xmlparam xp(strparapath);
  715. xp.GetParam(std::string("he"),std::string("1"));
  716. std::string strlat0 = xp.GetParam("lat0","39");
  717. std::string strlon0 = xp.GetParam("lon0","117.0");
  718. std::string strhdg0 = xp.GetParam("hdg0","360.0");
  719. std::string strgpsmsg = xp.GetParam("gpsmsg","ins550d_gpsimu");
  720. std::string strv2xmsg = xp.GetParam("v2xmsg","v2x");
  721. std::string strvehiclewidth = xp.GetParam("vehiclewidth","2.0");
  722. std::string strextendmap = xp.GetParam("extendmap","false");
  723. glat0 = atof(strlat0.data());
  724. glon0 = atof(strlon0.data());
  725. ghead0 = atof(strhdg0.data());
  726. gvehiclewidth = atof(strvehiclewidth.data());
  727. if(strextendmap == "true")
  728. {
  729. gbExtendMap = true;
  730. }
  731. else
  732. {
  733. gbExtendMap = false;
  734. }
  735. // LoadXODR(strmapth);
  736. gpmap = iv::modulecomm::RegisterSend("tracemap",20000000,1);
  737. gpa = iv::modulecomm::RegisterRecv("xodrreq",ListenCmd);
  738. gpasrc =iv::modulecomm::RegisterRecv("xodrsrc",ListenSrc);
  739. gpagps = iv::modulecomm::RegisterRecv(strgpsmsg.data(),UpdateGPSIMU);
  740. gpav2x = iv::modulecomm::RegisterRecv(strv2xmsg.data(),ListenV2X);
  741. gpaHmi = iv::modulecomm::RegisterRecv("hmi",ListenHmi);
  742. gpasimple = iv::modulecomm::RegisterSend("simpletrace",100000,1);
  743. double x_src,y_src,x_dst,y_dst;
  744. x_src = -26;y_src = 10;
  745. x_dst = -50;y_dst = -220;
  746. x_src = 0;y_src = 0;
  747. x_dst = -23;y_dst = -18;
  748. x_dst = 21;y_dst =-21;
  749. x_dst =5;y_dst = 0;
  750. x_src = -20; y_src = -1000;
  751. x_dst = 900; y_dst = -630;
  752. // x_dst = 450; y_dst = -640;
  753. // x_dst = -190; y_dst = -690;
  754. // x_src = 900; y_src = -610;
  755. // x_dst = -100; y_dst = -680;
  756. std::vector<PlanPoint> xPlan;
  757. double s;
  758. // int nRtn = MakePlan(gpxd,&mxodr,x_src,y_src,3.14,x_dst,y_dst,s,30.0,100.0,1,xPlan);
  759. // ToGPSTrace(xPlan);
  760. // double lat = 39.1443880;
  761. // double lon = 117.0812543;
  762. // xodrobj xo;
  763. // xo.fhgdsrc = 340;
  764. // xo.flatsrc = lat; xo.flonsrc = lon;
  765. // xo.flat = 39.1490196;
  766. // xo.flon = 117.0806979;
  767. // xo.lane = 1;
  768. // SetPlan(xo);
  769. void * pivexit = iv::ivexit::RegIVExitCall(ExitFunc);
  770. signal(SIGINT,signal_handler);
  771. int nrc = a.exec();
  772. std::cout<<"driver_map_xodr app exit. code :"<<nrc<<std::endl;
  773. return nrc;
  774. }