ivpark_simple.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. #include "ivpark_simple.h"
  2. #include "adc_tools/compute_00.h"
  3. #include "adc_tools/transfer.h"
  4. #include <chrono>
  5. using namespace iv;
  6. ivpark_simple::ivpark_simple()
  7. {
  8. }
  9. bool ivpark_simple::IsBocheEnable(double fLon, double fLat, double fHeading)
  10. {
  11. GPS_INS nowgps,aimgps;
  12. std::vector<std::vector<GPS_INS>> xvectordTPoint;
  13. std::vector<double> xvectorRearDis;
  14. std::vector<double> xvectorAngle;
  15. std::vector<int> xvectortype;
  16. std::vector<GPS_INS> xvectoraimgps;
  17. std::vector<iv::simpleparkspace> xvectorsimpleparkspace = GetParkSpace();
  18. xvectordTPoint.clear();
  19. xvectorRearDis.clear();
  20. xvectorAngle.clear();
  21. unsigned int i;
  22. unsigned int nsize = static_cast<unsigned int >(xvectorsimpleparkspace.size());
  23. nowgps.gps_lat = fLat;
  24. nowgps.gps_lng = fLon;
  25. nowgps.ins_heading_angle = fHeading;
  26. for(i=0;i<nsize;i++)
  27. {
  28. iv::simpleparkspace xpark = xvectorsimpleparkspace[i];
  29. iv::GPS_INS aimgps;
  30. aimgps.gps_lat = xpark.mfLat;
  31. aimgps.gps_lng = xpark.mfLon;
  32. aimgps.ins_heading_angle = xpark.mfHeading;
  33. std::vector<GPS_INS> TPoints;
  34. double fRearDis,fAngle;
  35. if(xpark.mnParkType == 1) //side park
  36. {
  37. if(iv::decition::Compute00().bocheDirectCompute(nowgps,aimgps,TPoints,fAngle,fRearDis) == 1)
  38. {
  39. xvectordTPoint.push_back(TPoints);
  40. xvectorAngle.push_back(fAngle);
  41. xvectorRearDis.push_back(fRearDis);
  42. xvectoraimgps.push_back(aimgps);
  43. xvectortype.push_back(1);
  44. }
  45. }
  46. if(xpark.mnParkType == 0)
  47. {
  48. if(iv::decition::Compute00().bocheCompute(nowgps,aimgps,TPoints,fAngle,fRearDis) == 1)
  49. {
  50. xvectordTPoint.push_back(TPoints);
  51. xvectorAngle.push_back(fAngle);
  52. xvectorRearDis.push_back(fRearDis);
  53. xvectoraimgps.push_back(aimgps);
  54. xvectortype.push_back(0);
  55. }
  56. }
  57. }
  58. nsize = static_cast<unsigned int >(xvectordTPoint.size());
  59. if(nsize<1)
  60. {
  61. return false;
  62. }
  63. unsigned int nsel = 0;
  64. //Select rear dis >1.0 and small
  65. for(i =1;i<nsize;i++)
  66. {
  67. if((xvectorRearDis[i] < xvectorRearDis[nsel])&&(xvectorRearDis[i]>1.0))
  68. {
  69. nsel = i;
  70. }
  71. else
  72. {
  73. if(xvectorRearDis[i]<1.0)
  74. {
  75. if(xvectorRearDis[nsel]>5.0)
  76. {
  77. nsel = i;
  78. }
  79. }
  80. else
  81. {
  82. if((xvectorRearDis[i]<3.0)&&(xvectorRearDis[nsel]<0.5))
  83. {
  84. nsel = i;
  85. }
  86. }
  87. }
  88. }
  89. if(xvectortype[nsel] == 0)
  90. {
  91. iv::decition::Compute00().nearTpoint = xvectordTPoint[nsel].at(0);
  92. iv::decition::Compute00().farTpoint = xvectordTPoint[nsel].at(1);
  93. iv::decition::Compute00().bocheAngle = xvectorAngle[nsel];
  94. iv::decition::Compute00().nParkType = 0;
  95. maimgps = xvectoraimgps[nsel];
  96. }
  97. if(xvectortype[nsel] == 1)
  98. {
  99. iv::decition::Compute00().dTpoint0 = xvectordTPoint[nsel].at(0);
  100. iv::decition::Compute00().dTpoint1 = xvectordTPoint[nsel].at(0);
  101. iv::decition::Compute00().dTpoint2 = xvectordTPoint[nsel].at(0);
  102. iv::decition::Compute00().dTpoint3 = xvectordTPoint[nsel].at(0);
  103. iv::decition::Compute00().bocheAngle = xvectorAngle[nsel];
  104. iv::decition::Compute00().nParkType = 1;
  105. maimgps = xvectoraimgps[nsel];
  106. }
  107. GaussProjCal(maimgps.gps_lng,maimgps.gps_lat,&maimgps.gps_x,&maimgps.gps_y);
  108. return true;
  109. }
  110. double ivpark_simple::GetDistance(iv::GPS_INS p1,iv::GPS_INS p2)
  111. {
  112. return sqrt(pow(p1.gps_x - p2.gps_x,2)+pow(p1.gps_y-p2.gps_y,2));
  113. }
  114. int ivpark_simple::GetBocheDecision(double fLon,double fLat,double fHeading,double fSpeed,double & fAcc,double & fWheel,int & nshift,double & fdSpeed,double & fdSecSpeed,VehState & xvehstate,bool bbocheMode)
  115. {
  116. (void)fHeading;
  117. if(bbocheMode == false)
  118. {
  119. return 0; //Not in boche mode
  120. }
  121. static int64_t nstoptiming_ms = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  122. int64_t nstoptime_ms = 1000; //every stop time is 3
  123. if(xvehstate!=dRever && xvehstate!=dRever0 && xvehstate!=dRever1 && xvehstate!=dRever2
  124. && xvehstate!=dRever3 && xvehstate!=dRever4 && xvehstate!=reverseArr
  125. && xvehstate!=reverseCar && xvehstate!=reversing && xvehstate!=reverseCircle && xvehstate!=reverseDirect)
  126. {
  127. if(fSpeed>0.3)
  128. {
  129. fdSpeed = 0;fdSecSpeed = fdSpeed/3.6;
  130. fAcc = -0.5;
  131. fWheel = 0.0;
  132. nstoptiming_ms = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  133. }
  134. else
  135. {
  136. fdSpeed = 0;fdSecSpeed = fdSpeed/3.6;
  137. fAcc = -0.5;
  138. fWheel = 0.0;
  139. nshift = 2; //rear
  140. int64_t nnow = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  141. if(abs(nnow - nstoptiming_ms) >= nstoptime_ms)
  142. {
  143. if(iv::decition::Compute00().nParkType == 0)
  144. {
  145. xvehstate = reversing;
  146. mlastvehstate = xvehstate;
  147. }
  148. else
  149. {
  150. xvehstate = dRever;
  151. mlastvehstate = xvehstate;
  152. }
  153. }
  154. }
  155. return 1;
  156. }
  157. iv::GPS_INS nowgps;
  158. nowgps.gps_lat = fLat;
  159. nowgps.gps_lng = fLon;
  160. GaussProjCal(nowgps.gps_lng,nowgps.gps_lat,&nowgps.gps_x,&nowgps.gps_y);
  161. switch (xvehstate) {
  162. case reversing:
  163. reversingcarFun(nowgps,fSpeed,fAcc,fWheel,nshift,fdSpeed,fdSecSpeed,xvehstate);
  164. break;
  165. case reverseCircle:
  166. reverseCircleFun(nowgps,fSpeed,fAcc,fWheel,nshift,fdSpeed,fdSecSpeed,xvehstate);
  167. break;
  168. case reverseDirect:
  169. reverseDirectFun(nowgps,fSpeed,fAcc,fWheel,nshift,fdSpeed,fdSecSpeed,xvehstate);
  170. break;
  171. case reverseArr:
  172. reverseArrFun(nowgps,fSpeed,fAcc,fWheel,nshift,fdSpeed,fdSecSpeed,xvehstate);
  173. break;
  174. default:
  175. return 0;
  176. }
  177. return 0;
  178. }
  179. void ivpark_simple::reversingcarFun(iv::GPS_INS nowgps,double fSpeed,double & fAcc,double & fWheel,int & nshift,double & fdSpeed,double & fdSecSpeed,VehState & xvehstate)
  180. {
  181. mlastvehstate = xvehstate;
  182. double fdistonear = sqrt(pow(nowgps.gps_x - iv::decition::Compute00().nearTpoint.gps_x,2)+pow(nowgps.gps_y - iv::decition::Compute00().nearTpoint.gps_y,2));
  183. Point2D pt = iv::decition::Coordinate_Transfer(nowgps.gps_x,nowgps.gps_y, maimgps);
  184. Point2D ptnear = iv::decition::Coordinate_Transfer(iv::decition::Compute00().nearTpoint.gps_x,iv::decition::Compute00().nearTpoint.gps_y, maimgps);
  185. fdistonear = fabs(pt.x - ptnear.x);
  186. nshift = 2;
  187. if(fdistonear>1.0)
  188. {
  189. fAcc = 0.0; //acc calcutale by pid
  190. fWheel = 0.0;
  191. fdSpeed = 2.0; fdSecSpeed = fdSpeed/3.6;
  192. }
  193. else
  194. {
  195. if((fSpeed>0.3)&&(fdistonear>0.3))
  196. {
  197. fAcc = (-1.0)*pow(fSpeed/3.6,2)/(2.0*fdistonear);
  198. }
  199. else
  200. {
  201. fAcc = -0.5;
  202. xvehstate = reverseCircle;
  203. }
  204. }
  205. return;
  206. }
  207. void ivpark_simple::reverseCircleFun(iv::GPS_INS nowgps,double fSpeed,double & fAcc,double & fWheel,int & nshift,double & fdSpeed,double & fdSecSpeed,VehState & xvehstate)
  208. {
  209. (void)fSpeed;
  210. static int64_t nstoptiming_ms = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  211. int64_t nstoptime_ms = 3000; //every stop time is 3
  212. if(mlastvehstate != xvehstate)
  213. {
  214. nstoptiming_ms = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  215. }
  216. mlastvehstate = xvehstate;
  217. int64_t nnow = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  218. nshift = 2;
  219. if(abs(nnow - nstoptiming_ms) < nstoptime_ms) //stop 3s, change wheel when stop
  220. {
  221. fdSpeed = 0;
  222. fdSecSpeed = 0;
  223. fAcc = -0.5;
  224. fWheel = iv::decition::Compute00().bocheAngle*16.5 *(-1.05);
  225. mCircleWheel = fWheel;
  226. return;
  227. }
  228. fWheel = mCircleWheel;
  229. Point2D pt = iv::decition::Coordinate_Transfer(nowgps.gps_x,nowgps.gps_y, maimgps);
  230. double angdis =fabs(nowgps.ins_heading_angle - maimgps.ins_heading_angle);
  231. if((fabs(pt.x)<2.0)&&(((angdis<5)||(angdis>355))))
  232. {
  233. xvehstate = reverseDirect;
  234. fAcc = -0.5;
  235. fdSpeed = 0.0;
  236. fWheel = 0.0;
  237. fdSecSpeed = 0.0;
  238. }
  239. else
  240. {
  241. fAcc = 0.0;
  242. fdSpeed = 2;
  243. fdSecSpeed = fdSecSpeed / 3.6;
  244. }
  245. return;
  246. }
  247. void ivpark_simple::reverseDirectFun(iv::GPS_INS nowgps,double fSpeed,double & fAcc,double & fWheel,int & nshift,double & fdSpeed,double & fdSecSpeed,VehState & xvehstate)
  248. {
  249. (void)fSpeed;
  250. static int64_t nstoptiming_ms = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  251. int64_t nstoptime_ms = 3000; //every stop time is 3
  252. if(mlastvehstate != xvehstate)
  253. {
  254. nstoptiming_ms = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  255. }
  256. mlastvehstate = xvehstate;
  257. int64_t nnow = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  258. nshift = 2;
  259. if(abs(nnow - nstoptiming_ms) < nstoptime_ms) //stop 3s, change wheel when stop
  260. {
  261. fdSpeed = 0;
  262. fdSecSpeed = 0;
  263. fAcc = -0.5;
  264. fWheel = 0.0;
  265. return;
  266. }
  267. Point2D pt = iv::decition::Coordinate_Transfer(nowgps.gps_x,nowgps.gps_y, maimgps);
  268. if(pt.y<0.5)
  269. {
  270. xvehstate = reverseArr;
  271. fAcc = -0.5;
  272. fdSpeed = 0.0;
  273. fWheel = 0.0;
  274. fdSecSpeed = 0.0;
  275. }
  276. else
  277. {
  278. fAcc = 0.0;
  279. fdSpeed = 2;
  280. fdSecSpeed = fdSecSpeed / 3.6;
  281. fWheel = 0.0;
  282. }
  283. return;
  284. }
  285. void ivpark_simple::reverseArrFun(iv::GPS_INS nowgps,double fSpeed,double & fAcc,double & fWheel,int & nshift,double & fdSpeed,double & fdSecSpeed,VehState & xvehstate)
  286. {
  287. (void)nowgps;
  288. (void)fSpeed;
  289. static int64_t nstoptiming_ms = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  290. int64_t nstoptime_ms = 3000; //every stop time is 3
  291. if(mlastvehstate != xvehstate)
  292. {
  293. nstoptiming_ms = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  294. }
  295. mlastvehstate = xvehstate;
  296. int64_t nnow = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  297. nshift = 2;
  298. if(abs(nnow - nstoptiming_ms) < nstoptime_ms) //stop 3s, change wheel when stop
  299. {
  300. fdSpeed = 0;
  301. fdSecSpeed = 0;
  302. fAcc = -0.5;
  303. fWheel = 0.0;
  304. return;
  305. }
  306. nshift = 1; //P shift
  307. fdSpeed = 0;
  308. fdSecSpeed = 0;
  309. fAcc = -0.5;
  310. fWheel = 0.0;
  311. xvehstate = normalRun;
  312. }
  313. void ivpark_simple::dRever0Fun(iv::GPS_INS nowgps,double fSpeed,double & fAcc,double & fWheel,int & nshift,double & fdSpeed,double & fdSecSpeed,VehState & xvehstate)
  314. {
  315. mlastvehstate = xvehstate;
  316. Point2D pt = iv::decition::Coordinate_Transfer(nowgps.gps_x,nowgps.gps_y, maimgps);
  317. Point2D ptnear = iv::decition::Coordinate_Transfer(iv::decition::Compute00().dTpoint1.gps_x,iv::decition::Compute00().dTpoint1.gps_y, maimgps);
  318. double fdistonear = fabs(pt.y - ptnear.y);
  319. nshift = 2;
  320. if(fdistonear>1.0)
  321. {
  322. fAcc = 0.0; //acc calcutale by pid
  323. fWheel = 0.0;
  324. fdSpeed = 2.0; fdSecSpeed = fdSpeed/3.6;
  325. }
  326. else
  327. {
  328. if((fSpeed>0.3)&&(fdistonear>0.3))
  329. {
  330. fAcc = (-1.0)*pow(fSpeed/3.6,2)/(2.0*fdistonear);
  331. fdSpeed = 0.0;
  332. fdSecSpeed = fdSpeed/3.6;
  333. }
  334. else
  335. {
  336. fAcc = -0.5;
  337. xvehstate = dRever2;
  338. fdSpeed = 0.0;
  339. fdSecSpeed = fdSpeed/3.6;
  340. }
  341. }
  342. return;
  343. }
  344. void ivpark_simple::dRever1Fun(iv::GPS_INS nowgps,double fSpeed,double & fAcc,double & fWheel,int & nshift,double & fdSpeed,double & fdSecSpeed,VehState & xvehstate)
  345. {
  346. (void)fSpeed;
  347. static int64_t nstoptiming_ms = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  348. int64_t nstoptime_ms = 3000; //every stop time is 3
  349. if(mlastvehstate != xvehstate)
  350. {
  351. nstoptiming_ms = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  352. }
  353. mlastvehstate = xvehstate;
  354. int64_t nnow = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  355. nshift = 2;
  356. if(abs(nnow - nstoptiming_ms) < nstoptime_ms) //stop 3s, change wheel when stop
  357. {
  358. fdSpeed = 0;
  359. fdSecSpeed = 0;
  360. fAcc = -0.5;
  361. fWheel = iv::decition::Compute00().bocheAngle*16.5 *(-1.0);
  362. mCircleWheel = fWheel;
  363. return;
  364. }
  365. Point2D pt1 = iv::decition::Coordinate_Transfer(nowgps.gps_x,nowgps.gps_y, maimgps);
  366. Point2D pt2 = iv::decition::Coordinate_Transfer(iv::decition::Compute00().dTpoint1.gps_x,iv::decition::Compute00().dTpoint1.gps_y, maimgps);
  367. double fdis = fabs(pt1.x - pt2.x);
  368. if(fdis>1.0)
  369. {
  370. fAcc = 0.0; //acc calcutale by pid
  371. fdSpeed = 2.0; fdSecSpeed = fdSpeed/3.6;
  372. }
  373. else
  374. {
  375. if((fSpeed>0.3)&&(fdis>0.3))
  376. {
  377. fAcc = (-1.0)*pow(fSpeed/3.6,2)/(2.0*fdis);
  378. fdSpeed = 0.0;
  379. fdSecSpeed = fdSpeed/3.6;
  380. }
  381. else
  382. {
  383. fAcc = -0.5;
  384. fdSpeed = 0.0;
  385. fdSecSpeed = fdSpeed/3.6;
  386. xvehstate = dRever2;
  387. }
  388. }
  389. fWheel = mCircleWheel;
  390. }
  391. void ivpark_simple::dRever2Fun(iv::GPS_INS nowgps,double fSpeed,double & fAcc,double & fWheel,int & nshift,double & fdSpeed,double & fdSecSpeed,VehState & xvehstate)
  392. {
  393. (void)fSpeed;
  394. static int64_t nstoptiming_ms = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  395. int64_t nstoptime_ms = 3000; //every stop time is 3
  396. if(mlastvehstate != xvehstate)
  397. {
  398. nstoptiming_ms = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  399. }
  400. mlastvehstate = xvehstate;
  401. int64_t nnow = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  402. nshift = 2;
  403. if(abs(nnow - nstoptiming_ms) < nstoptime_ms) //stop 3s, change wheel when stop
  404. {
  405. fdSpeed = 0;
  406. fdSecSpeed = 0;
  407. fAcc = -0.5;
  408. fWheel = 0;
  409. return;
  410. }
  411. Point2D pt1 = iv::decition::Coordinate_Transfer(nowgps.gps_x,nowgps.gps_y, maimgps);
  412. Point2D pt2 = iv::decition::Coordinate_Transfer(iv::decition::Compute00().dTpoint2.gps_x,iv::decition::Compute00().dTpoint2.gps_y, maimgps);
  413. double fdis = pt1.x - pt2.x;
  414. if(fdis>-1.0)
  415. {
  416. fAcc = 0.0; //acc calcutale by pid
  417. fdSpeed = 2.0; fdSecSpeed = fdSpeed/3.6;
  418. }
  419. else
  420. {
  421. if((fSpeed>0.3)&&(fabs(fdis)>0.3))
  422. {
  423. fAcc = (-1.0)*pow(fSpeed/3.6,2)/(2.0*fabs(fdis));
  424. fdSpeed = 0.0;
  425. fdSecSpeed = fdSpeed/3.6;
  426. }
  427. else
  428. {
  429. fAcc = -0.5;
  430. fdSpeed = 0.0;
  431. fdSecSpeed = fdSpeed/3.6;
  432. xvehstate = dRever3;
  433. }
  434. }
  435. fWheel = 0;
  436. }
  437. void ivpark_simple::dRever3Fun(iv::GPS_INS nowgps,double fSpeed,double & fAcc,double & fWheel,int & nshift,double & fdSpeed,double & fdSecSpeed,VehState & xvehstate)
  438. {
  439. (void)fSpeed;
  440. static int64_t nstoptiming_ms = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  441. int64_t nstoptime_ms = 3000; //every stop time is 3
  442. if(mlastvehstate != xvehstate)
  443. {
  444. nstoptiming_ms = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  445. }
  446. mlastvehstate = xvehstate;
  447. int64_t nnow = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  448. nshift = 2;
  449. if(abs(nnow - nstoptiming_ms) < nstoptime_ms) //stop 3s, change wheel when stop
  450. {
  451. fdSpeed = 0;
  452. fdSecSpeed = 0;
  453. fAcc = -0.5;
  454. fWheel = iv::decition::Compute00().bocheAngle*16.5 *1.0;
  455. mCircleWheel = fWheel;
  456. return;
  457. }
  458. Point2D pt1 = iv::decition::Coordinate_Transfer(nowgps.gps_x,nowgps.gps_y, maimgps);
  459. Point2D pt2 = iv::decition::Coordinate_Transfer(iv::decition::Compute00().dTpoint3.gps_x,iv::decition::Compute00().dTpoint3.gps_y, maimgps);
  460. double fdis = fabs(pt1.x - pt2.x);
  461. if(fdis>1.0)
  462. {
  463. fAcc = 0.0; //acc calcutale by pid
  464. fdSpeed = 2.0; fdSecSpeed = fdSpeed/3.6;
  465. }
  466. else
  467. {
  468. if((fSpeed>0.3)&&(fabs(fdis)>0.3))
  469. {
  470. fAcc = (-1.0)*pow(fSpeed/3.6,2)/(2.0*fabs(fdis));
  471. fdSpeed = 0.0;
  472. fdSecSpeed = fdSpeed/3.6;
  473. }
  474. else
  475. {
  476. fAcc = -0.5;
  477. fdSpeed = 0.0;
  478. fdSecSpeed = fdSpeed/3.6;
  479. xvehstate = reverseDirect;
  480. }
  481. }
  482. fWheel = mCircleWheel;
  483. }