ivpark_simple.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. 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)
  111. {
  112. if(bbocheMode == false)
  113. {
  114. return 0; //Not in boche mode
  115. }
  116. static int64_t nstoptiming_ms = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  117. int64_t nstoptime_ms = 1000; //every stop time is 3
  118. if(xvehstate!=dRever && xvehstate!=dRever0 && xvehstate!=dRever1 && xvehstate!=dRever2
  119. && xvehstate!=dRever3 && xvehstate!=dRever4 && xvehstate!=reverseArr
  120. && xvehstate!=reverseCar && xvehstate!=reversing && xvehstate!=reverseCircle && xvehstate!=reverseDirect)
  121. {
  122. if(fSpeed>0.3)
  123. {
  124. fdSpeed = 0;fdSecSpeed = fdSpeed/3.6;
  125. fAcc = -0.5;
  126. fWheel = 0.0;
  127. nstoptiming_ms = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  128. }
  129. else
  130. {
  131. fdSpeed = 0;fdSecSpeed = fdSpeed/3.6;
  132. fAcc = -0.5;
  133. fWheel = 0.0;
  134. nshift = 2; //rear
  135. int64_t nnow = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  136. if(abs(nnow - nstoptiming_ms) >= nstoptime_ms)
  137. {
  138. if(iv::decition::Compute00().nParkType == 0)
  139. {
  140. xvehstate = reversing;
  141. mlastvehstate = xvehstate;
  142. }
  143. else
  144. {
  145. xvehstate = dRever;
  146. mlastvehstate = xvehstate;
  147. }
  148. }
  149. }
  150. return 1;
  151. }
  152. iv::GPS_INS nowgps;
  153. nowgps.gps_lat = fLat;
  154. nowgps.gps_lng = fLon;
  155. GaussProjCal(nowgps.gps_lng,nowgps.gps_lat,&nowgps.gps_x,&nowgps.gps_y);
  156. switch (xvehstate) {
  157. case reversing:
  158. reversingcarFun(nowgps,fSpeed,fAcc,fWheel,nshift,fdSpeed,fdSecSpeed,xvehstate);
  159. break;
  160. case reverseCircle:
  161. reverseCircleFun(nowgps,fSpeed,fAcc,fWheel,nshift,fdSpeed,fdSecSpeed,xvehstate);
  162. break;
  163. case reverseDirect:
  164. reverseDirectFun(nowgps,fSpeed,fAcc,fWheel,nshift,fdSpeed,fdSecSpeed,xvehstate);
  165. break;
  166. case reverseArr:
  167. reverseArrFun(nowgps,fSpeed,fAcc,fWheel,nshift,fdSpeed,fdSecSpeed,xvehstate);
  168. break;
  169. default:
  170. return 0;
  171. }
  172. }
  173. void ivpark_simple::reversingcarFun(iv::GPS_INS nowgps,double fSpeed,double & fAcc,double & fWheel,int & nshift,double & fdSpeed,double & fdSecSpeed,VehState & xvehstate)
  174. {
  175. mlastvehstate = xvehstate;
  176. 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));
  177. Point2D pt = iv::decition::Coordinate_Transfer(nowgps.gps_x,nowgps.gps_y, maimgps);
  178. Point2D ptnear = iv::decition::Coordinate_Transfer(iv::decition::Compute00().nearTpoint.gps_x,iv::decition::Compute00().nearTpoint.gps_y, maimgps);
  179. fdistonear = fabs(pt.x - ptnear.x);
  180. nshift = 2;
  181. if(fdistonear>1.0)
  182. {
  183. fAcc = 0.0; //acc calcutale by pid
  184. fWheel = 0.0;
  185. fdSpeed = 2.0; fdSecSpeed = fdSpeed/3.6;
  186. }
  187. else
  188. {
  189. if((fSpeed>0.3)&&(fdistonear>0.3))
  190. {
  191. fAcc = (-1.0)*pow(fSpeed/3.6,2)/(2.0*fdistonear);
  192. }
  193. else
  194. {
  195. fAcc = -0.5;
  196. xvehstate = reverseCircle;
  197. }
  198. }
  199. return;
  200. }
  201. void ivpark_simple::reverseCircleFun(iv::GPS_INS nowgps,double fSpeed,double & fAcc,double & fWheel,int & nshift,double & fdSpeed,double & fdSecSpeed,VehState & xvehstate)
  202. {
  203. static int64_t nstoptiming_ms = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  204. int64_t nstoptime_ms = 1000; //every stop time is 3
  205. if(mlastvehstate != xvehstate)
  206. {
  207. nstoptiming_ms = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  208. }
  209. mlastvehstate = xvehstate;
  210. int64_t nnow = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  211. nshift = 2;
  212. if(abs(nnow - nstoptiming_ms) < nstoptime_ms) //stop 3s, change wheel when stop
  213. {
  214. fdSpeed = 0;
  215. fdSecSpeed = 0;
  216. fAcc = -0.5;
  217. fWheel = iv::decition::Compute00().bocheAngle*16.5 *(-1.05);
  218. mCircleWheel = fWheel;
  219. return;
  220. }
  221. fWheel = mCircleWheel;
  222. Point2D pt = iv::decition::Coordinate_Transfer(nowgps.gps_x,nowgps.gps_y, maimgps);
  223. double angdis =fabs(nowgps.ins_heading_angle - maimgps.ins_heading_angle);
  224. if((fabs(pt.x)<2.0)&&(((angdis<5)||(angdis>355))))
  225. {
  226. xvehstate = reverseDirect;
  227. fAcc = -0.5;
  228. fdSpeed = 0.0;
  229. fWheel = 0.0;
  230. fdSecSpeed = 0.0;
  231. }
  232. else
  233. {
  234. fAcc = 0.0;
  235. fdSpeed = 2;
  236. fdSecSpeed = fdSecSpeed / 3.6;
  237. }
  238. return;
  239. }
  240. void ivpark_simple::reverseDirectFun(iv::GPS_INS nowgps,double fSpeed,double & fAcc,double & fWheel,int & nshift,double & fdSpeed,double & fdSecSpeed,VehState & xvehstate)
  241. {
  242. static int64_t nstoptiming_ms = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  243. int64_t nstoptime_ms = 1000; //every stop time is 3
  244. if(mlastvehstate != xvehstate)
  245. {
  246. nstoptiming_ms = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  247. }
  248. mlastvehstate = xvehstate;
  249. int64_t nnow = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  250. nshift = 2;
  251. if(abs(nnow - nstoptiming_ms) < nstoptime_ms) //stop 3s, change wheel when stop
  252. {
  253. fdSpeed = 0;
  254. fdSecSpeed = 0;
  255. fAcc = -0.5;
  256. fWheel = 0.0;
  257. return;
  258. }
  259. Point2D pt = iv::decition::Coordinate_Transfer(nowgps.gps_x,nowgps.gps_y, maimgps);
  260. if(pt.y<0.5)
  261. {
  262. xvehstate = reverseArr;
  263. fAcc = -0.5;
  264. fdSpeed = 0.0;
  265. fWheel = 0.0;
  266. fdSecSpeed = 0.0;
  267. }
  268. else
  269. {
  270. fAcc = 0.0;
  271. fdSpeed = 2;
  272. fdSecSpeed = fdSecSpeed / 3.6;
  273. fWheel = 0.0;
  274. }
  275. return;
  276. }
  277. void ivpark_simple::reverseArrFun(iv::GPS_INS nowgps,double fSpeed,double & fAcc,double & fWheel,int & nshift,double & fdSpeed,double & fdSecSpeed,VehState & xvehstate)
  278. {
  279. static int64_t nstoptiming_ms = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  280. int64_t nstoptime_ms = 1000; //every stop time is 3
  281. if(mlastvehstate != xvehstate)
  282. {
  283. nstoptiming_ms = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  284. }
  285. mlastvehstate = xvehstate;
  286. int64_t nnow = std::chrono::system_clock::now().time_since_epoch().count()/1000000;
  287. nshift = 2;
  288. if(abs(nnow - nstoptiming_ms) < nstoptime_ms) //stop 3s, change wheel when stop
  289. {
  290. fdSpeed = 0;
  291. fdSecSpeed = 0;
  292. fAcc = -0.5;
  293. fWheel = 0.0;
  294. return;
  295. }
  296. nshift = 1; //P shift
  297. fdSpeed = 0;
  298. fdSecSpeed = 0;
  299. fAcc = -0.5;
  300. fWheel = 0.0;
  301. xvehstate = reverseComplete;
  302. }