main.cpp 24 KB

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