main.cpp 25 KB

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