compute_00.cpp 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843
  1. #include <decition/compute_00.h>
  2. #include <decition/decide_gps_00.h>
  3. #include <decition/gps_distance.h>
  4. #include <decition/decition_type.h>
  5. #include <decition/transfer.h>
  6. #include <common/constants.h>
  7. #include <math.h>
  8. #include <iostream>
  9. #include <fstream>
  10. //#include <control/radar_exam.h>
  11. #include <control/can.h>
  12. using namespace std;
  13. #define PI (3.1415926535897932384626433832795)
  14. iv::decition::Compute00::Compute00() {
  15. }
  16. iv::decition::Compute00::~Compute00() {
  17. }
  18. double iv::decition::Compute00::angleLimit = 700;
  19. double iv::decition::Compute00::lastEA = 0;
  20. double iv::decition::Compute00::lastEP = 0;
  21. double iv::decition::Compute00::decision_Angle = 0;
  22. double iv::decition::Compute00::lastAng = 0;
  23. int iv::decition::Compute00::lastEsrID = -1;
  24. int iv::decition::Compute00::lastEsrCount = 0;
  25. int iv::decition::Compute00::lastEsrIDAvoid = -1;
  26. int iv::decition::Compute00::lastEsrCountAvoid = 0;
  27. iv::GPS_INS iv::decition::Compute00::nearTpoint;
  28. iv::GPS_INS iv::decition::Compute00::farTpoint;
  29. double iv::decition::Compute00::bocheAngle;
  30. iv::GPS_INS iv::decition::Compute00::dTpoint0;
  31. iv::GPS_INS iv::decition::Compute00::dTpoint1;
  32. iv::GPS_INS iv::decition::Compute00::dTpoint2;
  33. iv::GPS_INS iv::decition::Compute00::dTpoint3;
  34. double iv::decition::Compute00::dBocheAngle;
  35. std::vector<int> lastEsrIdVector;
  36. std::vector<int> lastEsrCountVector;
  37. int iv::decition::Compute00::getNearestPointIndex(GPS_INS rp, const std::vector<GPSData> gpsMap, int lastIndex, double mindis, double maxAngle)
  38. {
  39. int index = -1;
  40. // DecideGps00().minDis = iv::MaxValue;
  41. DecideGps00().minDis = 20;
  42. DecideGps00().maxAngle = iv::MaxValue;
  43. int startIndex = max((lastIndex - 500), 0); // startIndex = 0 则每一次都是遍历整条地图路线
  44. int endIndex = min((gpsMap.size() - 1), (size_t)(lastIndex + 2000 + 100 * (gpsMissCount + 1)));
  45. for (int j = startIndex; j < endIndex; j++)
  46. {
  47. int i = (j + gpsMap.size()) % gpsMap.size();
  48. double tmpdis = GetDistance(rp, (*gpsMap[i]));
  49. if (tmpdis < DecideGps00().minDis && (abs(rp.ins_heading_angle - (*gpsMap[i]).ins_heading_angle) < 80
  50. || abs(rp.ins_heading_angle - (*gpsMap[i]).ins_heading_angle - 360) < 80
  51. || abs(rp.ins_heading_angle - (*gpsMap[i]).ins_heading_angle + 360) < 80)
  52. )
  53. {
  54. index = i;
  55. DecideGps00().minDis = tmpdis;
  56. DecideGps00().maxAngle = min(abs(rp.ins_heading_angle - (*gpsMap[i]).ins_heading_angle), abs(rp.ins_heading_angle - (*gpsMap[i]).ins_heading_angle - 360));
  57. DecideGps00().maxAngle = min(DecideGps00().maxAngle, abs(rp.ins_heading_angle - (*gpsMap[i]).ins_heading_angle + 360));
  58. }
  59. }
  60. /*if (index == -1) {
  61. index = 0;
  62. }*/
  63. return index;
  64. }
  65. //首次找点
  66. int iv::decition::Compute00::getFirstNearestPointIndex(GPS_INS rp, const std::vector<GPSData> gpsMap, int lastIndex, double mindis, double maxAngle)
  67. {
  68. int index = -1;
  69. // DecideGps00().minDis = iv::MaxValue;
  70. DecideGps00().minDis = 40;
  71. DecideGps00().maxAngle = iv::MaxValue;
  72. int startIndex = 0; // startIndex = 0 则每一次都是遍历整条地图路线
  73. int endIndex = gpsMap.size() - 1;
  74. for (int j = startIndex; j < endIndex; j++)
  75. {
  76. int i = (j + gpsMap.size()) % gpsMap.size();
  77. double tmpdis = GetDistance(rp, (*gpsMap[i]));
  78. if (tmpdis < DecideGps00().minDis && (abs(rp.ins_heading_angle - (*gpsMap[i]).ins_heading_angle) < 80
  79. || abs(rp.ins_heading_angle - (*gpsMap[i]).ins_heading_angle - 360) < 80
  80. || abs(rp.ins_heading_angle - (*gpsMap[i]).ins_heading_angle + 360) < 80)
  81. )
  82. {
  83. index = i;
  84. DecideGps00().minDis = tmpdis;
  85. DecideGps00().maxAngle = min(abs(rp.ins_heading_angle - (*gpsMap[i]).ins_heading_angle), abs(rp.ins_heading_angle - (*gpsMap[i]).ins_heading_angle - 360));
  86. DecideGps00().maxAngle = min(DecideGps00().maxAngle, abs(rp.ins_heading_angle - (*gpsMap[i]).ins_heading_angle + 360));
  87. }
  88. }
  89. /*if (index == -1) {
  90. index = 0;
  91. }*/
  92. return index;
  93. }
  94. double iv::decition::Compute00::getAveDef(std::vector<Point2D> farTrace)
  95. {
  96. double sum_x = 0;
  97. double sum_y = 0;
  98. for (int i = 0; i < min(5, (int)farTrace.size()); i++)
  99. {
  100. sum_x += farTrace[i].x;
  101. sum_y += abs(farTrace[i].y);
  102. }
  103. double average_y = sum_y / min(5, (int)farTrace.size());
  104. double average_x = sum_x / min(5, (int)farTrace.size());
  105. return atan(average_x / average_y) / PI * 180;
  106. }
  107. double iv::decition::Compute00::getAvoidAveDef(std::vector<Point2D> farTrace, double avoidX)
  108. {
  109. double sum_x = 0;
  110. double sum_y = 0;
  111. for (int i = 0; i < min(5, (int)farTrace.size()); i++)
  112. {
  113. sum_x += farTrace[i].x;
  114. sum_y += abs(farTrace[i].y);
  115. }
  116. double average_y = sum_y / min(5, (int)farTrace.size());
  117. double average_x = sum_x / min(5, (int)farTrace.size());
  118. return atan(average_x + avoidX / average_y) / PI * 180;
  119. }
  120. double iv::decition::Compute00::getDecideAngle(std::vector<Point2D> gpsTrace, double realSpeed) {
  121. double ang = 0;
  122. double EPos = 0, EAng = 0;
  123. // double KEang = 14, KEPos = 10, DEang = 3, DEPos = 1; // double KEang = 14, KEPos = 10, DEang = 10, DEPos = 10;
  124. double KEang = 14, KEPos = 10, DEang = 0, DEPos = 0;
  125. if(transferPieriod&& !transferPieriod2){
  126. DEang = 200;
  127. DEPos = 150;
  128. }
  129. // double PreviewDistance = max(6.0, realSpeed / 3.6 * 1.8);//预瞄距离
  130. double PreviewDistance;//预瞄距离
  131. realSpeed > 40 ? PreviewDistance = max(6.0, realSpeed *0.6) : PreviewDistance = max(6.0, realSpeed *0.5);
  132. if(changeRoad ||transferPieriod){
  133. PreviewDistance=PreviewDistance+avoidX;
  134. }
  135. if(realSpeed <15){
  136. PreviewDistance = max(4.0, realSpeed *0.4) ;
  137. }
  138. if (gpsTrace[0].v1 == 1)
  139. {
  140. KEang = 14; KEPos = 10;
  141. }
  142. else if (gpsTrace[0].v1 == 2 || gpsTrace[0].v1 == 3)
  143. {
  144. KEang = 14; KEPos = 10;
  145. }
  146. else if (gpsTrace[0].v1 == 4 || gpsTrace[0].v1 == 5)
  147. {
  148. KEang = 14; KEPos = 10;
  149. }
  150. else if (gpsTrace[0].v1 == 7 && (gpsTrace[0].v2 == 23 || gpsTrace[0].v2 == 24))
  151. {
  152. KEang = 18; KEPos = 50; PreviewDistance = 3;
  153. }
  154. else if (gpsTrace[0].v1 == 7)
  155. {
  156. KEang = 20; KEPos = 50; PreviewDistance = 4;
  157. }
  158. if (realSpeed > 40) KEang = 10; KEPos = 8;
  159. if (realSpeed > 50) KEang = 5;
  160. double sumdis = 0;
  161. int gpsIndex = 0;
  162. std::vector<Point2D> farTrace;
  163. for (int i = 1; i < gpsTrace.size() - 1; i++)
  164. {
  165. sumdis += GetDistance(gpsTrace[i - 1], gpsTrace[i]);
  166. if (sumdis > PreviewDistance)
  167. {
  168. gpsIndex = i;
  169. break;
  170. }
  171. }
  172. EPos = gpsTrace[gpsIndex].x;
  173. for (unsigned int i = max(0, gpsIndex - 3); i < min((size_t)(gpsIndex + 3), gpsTrace.size()); i++) {
  174. farTrace.push_back(gpsTrace[gpsIndex]);
  175. }
  176. if (farTrace.size() == 0) {
  177. EAng = 0;
  178. }
  179. else {
  180. EAng = getAveDef(farTrace);
  181. }
  182. ang = KEang * EAng + KEPos * EPos + DEang * (EAng - lastEA) + DEPos * (EPos - lastEP);
  183. lastEA = EAng;
  184. lastEP = EPos;
  185. if (ang > angleLimit) {
  186. ang = angleLimit;
  187. }
  188. else if (ang < -angleLimit) {
  189. ang = -angleLimit;
  190. }
  191. if (lastAng != iv::MaxValue) {
  192. ang = 0.2 * lastAng + 0.8 * ang;
  193. //ODS("lastAng:%d\n", lastAng);
  194. }
  195. lastAng = ang;
  196. return ang;
  197. }
  198. int iv::decition::Compute00::getSpeedPointIndex(std::vector<Point2D> gpsTrace, double realSpeed)
  199. {
  200. int index = 1;
  201. double sumdis = 0;
  202. while (index < gpsTrace.size() && sumdis < realSpeed)
  203. sumdis += GetDistance(gpsTrace[index - 1], gpsTrace[index++]);
  204. if (index == gpsTrace.size())
  205. return index - 1;
  206. if (abs(sumdis - realSpeed) > abs(sumdis - GetDistance(gpsTrace[index - 1], gpsTrace[index]) - realSpeed))
  207. index--;
  208. return index;
  209. }
  210. iv::Point2D iv::decition::Compute00::getLidarObsPoint(std::vector<Point2D> gpsTrace, iv::LidarGridPtr lidarGridPtr) {
  211. iv::Point2D obsPoint(-1, -1);
  212. vector<Point2D> gpsTraceLeft;
  213. vector<Point2D> gpsTraceRight;
  214. for (int j = 0; j < gpsTrace.size(); j++)
  215. {
  216. double sumx1 = 0, sumy1 = 0, count1 = 0;
  217. double sumx2 = 0, sumy2 = 0, count2 = 0;
  218. for (int k = max(0, j - 4); k <= j; k++)
  219. {
  220. count1 = count1 + 1;
  221. sumx1 += gpsTrace[k].x;
  222. sumy1 += gpsTrace[k].y;
  223. }
  224. for (unsigned int k = j; k <= min(gpsTrace.size() - 1, (size_t)(j + 4)); k++)
  225. {
  226. count2 = count2 + 1;
  227. sumx2 += gpsTrace[k].x;
  228. sumy2 += gpsTrace[k].y;
  229. }
  230. sumx1 /= count1; sumy1 /= count1;
  231. sumx2 /= count2; sumy2 /= count2;
  232. double anglevalue = atan2(sumy2 - sumy1, sumx2 - sumx1);
  233. double carFrontx = gpsTrace[j].x;// -Form1.CarRear * Math.Cos(anglevalue);
  234. double carFronty = gpsTrace[j].y;// -Form1.CarRear * Math.Sin(anglevalue);
  235. Point2D ptLeft(carFrontx + Veh_Width / 2 * cos(anglevalue + PI / 2),
  236. carFronty + Veh_Width / 2 * sin(anglevalue + PI / 2));
  237. Point2D ptRight(carFrontx + Veh_Width / 2 * cos(anglevalue - PI / 2),
  238. carFronty + Veh_Width / 2 * sin(anglevalue - PI / 2));
  239. gpsTraceLeft.push_back(ptLeft);
  240. gpsTraceRight.push_back(ptRight);
  241. }
  242. bool isRemove = false;
  243. for (int j = 1; j < gpsTrace.size() - 1 && !isRemove; j++)
  244. {
  245. if (!isRemove && gpsTrace[j].y>2.5 )
  246. {
  247. int count = 0;
  248. for (double length = 0; length <= Veh_Width; length += 0.4)
  249. {
  250. double ptx = gpsTraceLeft[j].x + (gpsTraceRight[j].x - gpsTraceLeft[j].x) / Veh_Width * length;
  251. double pty = gpsTraceLeft[j].y + (gpsTraceRight[j].y - gpsTraceLeft[j].y) / Veh_Width * length;
  252. // int dx = (int)(ptx / gridwide * 2 + (ptx / abs(ptx))) / 2 + centerx; //+(ptx / abs(ptx))) / 2左右多出一半的车宽(1米)
  253. // int dy = (int)(pty / gridwide * 2 + (pty / abs(pty))) / 2 + centery;
  254. int dx = (ptx + gridwide*(double)centerx)/gridwide;
  255. int dy = (pty + gridwide*(double)centery)/gridwide;
  256. if (dx >= 0 && dx <grx && dy >= 0 && dy < gry)
  257. {
  258. // if (lidarGridPtr[dx * (iv::gry + 1) + dy].ob != 0)
  259. if (lidarGridPtr[dx * (iv::gry) + dy].ob != 0)
  260. {
  261. count++; obsPoint.x = ptx; obsPoint.y = pty;
  262. }
  263. }
  264. }
  265. j++;
  266. for (double length = 0; length <= Veh_Width; length += 0.4)
  267. {
  268. double ptx = gpsTraceLeft[j].x + (gpsTraceRight[j].x - gpsTraceLeft[j].x) / Veh_Width * length;
  269. double pty = gpsTraceLeft[j].y + (gpsTraceRight[j].y - gpsTraceLeft[j].y) / Veh_Width * length;
  270. // int dx = (int)(ptx / gridwide * 2 + (ptx / abs(ptx))) / 2 + centerx;
  271. // int dy = (int)(pty / gridwide * 2 + (pty / abs(pty))) / 2 + centery;
  272. int dx = (ptx + gridwide*(double)centerx)/gridwide;
  273. int dy = (pty + gridwide*(double)centery)/gridwide;
  274. if (dx >= 0 && dx <grx && dy >= 0 && dy < gry)
  275. {
  276. // if (lidarGridPtr[dx * (iv::gry + 1) + dy].ob != 0)
  277. if (lidarGridPtr[dx * (iv::gry) + dy].ob != 0)
  278. {
  279. count++; obsPoint.x = ptx; obsPoint.y = pty;
  280. }
  281. }
  282. }
  283. if (count >= 2)
  284. {
  285. obsPoint.x = gpsTrace[j].x;
  286. obsPoint.y = gpsTrace[j].y;
  287. isRemove = true;
  288. // DecideGps00().lidarDistance = obsPoint.y;
  289. return obsPoint;
  290. }
  291. }
  292. }
  293. // DecideGps00().lidarDistance = obsPoint.y;
  294. return obsPoint;
  295. }
  296. //1220
  297. iv::Point2D iv::decition::Compute00::getLidarRearObsPoint(std::vector<Point2D> gpsTrace, iv::LidarGridPtr lidarGridPtr) {
  298. iv::Point2D obsPoint(-1, -1);
  299. vector<Point2D> gpsTraceLeft;
  300. vector<Point2D> gpsTraceRight;
  301. for (int j = 0; j < gpsTrace.size(); j++)
  302. {
  303. double sumx1 = 0, sumy1 = 0, count1 = 0;
  304. double sumx2 = 0, sumy2 = 0, count2 = 0;
  305. for (int k = max(0, j - 4); k <= j; k++)
  306. {
  307. count1 = count1 + 1;
  308. sumx1 += gpsTrace[k].x;
  309. sumy1 += gpsTrace[k].y;
  310. }
  311. for (unsigned int k = j; k <= min(gpsTrace.size() - 1, (size_t)(j + 4)); k++)
  312. {
  313. count2 = count2 + 1;
  314. sumx2 += gpsTrace[k].x;
  315. sumy2 += gpsTrace[k].y;
  316. }
  317. sumx1 /= count1; sumy1 /= count1;
  318. sumx2 /= count2; sumy2 /= count2;
  319. double anglevalue = atan2(sumy2 - sumy1, sumx2 - sumx1);
  320. double carFrontx = gpsTrace[j].x;// -Form1.CarRear * Math.Cos(anglevalue);
  321. double carFronty = gpsTrace[j].y;// -Form1.CarRear * Math.Sin(anglevalue);
  322. Point2D ptLeft(carFrontx + (Veh_Width-0.3) / 2 * cos(anglevalue + PI / 2),
  323. carFronty + (Veh_Width-0.3) / 2 * sin(anglevalue + PI / 2));
  324. Point2D ptRight(carFrontx + (Veh_Width-0.3) / 2 * cos(anglevalue - PI / 2),
  325. carFronty + (Veh_Width-0.3) / 2 * sin(anglevalue - PI / 2));
  326. gpsTraceLeft.push_back(ptLeft);
  327. gpsTraceRight.push_back(ptRight);
  328. }
  329. bool isRemove = false;
  330. for (int j = 1; j < gpsTrace.size() - 1 && !isRemove; j++)
  331. {
  332. if (!isRemove && gpsTrace[j].y<-1.0 )
  333. {
  334. int count = 0;
  335. for (double length = 0; length <= Veh_Width; length += 0.4)
  336. {
  337. double ptx = gpsTraceLeft[j].x + (gpsTraceRight[j].x - gpsTraceLeft[j].x) / Veh_Width * length;
  338. double pty = gpsTraceLeft[j].y + (gpsTraceRight[j].y - gpsTraceLeft[j].y) / Veh_Width * length;
  339. // int dx = (int)(ptx / gridwide * 2 + (ptx / abs(ptx))) / 2 + centerx; //+(ptx / abs(ptx))) / 2左右多出一半的车宽(1米)
  340. // int dy = (int)(pty / gridwide * 2 + (pty / abs(pty))) / 2 + centery;
  341. int dx = (ptx + gridwide*(double)centerx)/gridwide;
  342. dx=grx-dx;//1227
  343. int dy = (pty + gridwide*(double)centery)/gridwide;
  344. if (dx >= 0 && dx <grx && dy >= 0 && dy < gry)
  345. {
  346. // if (lidarGridPtr[dx * (iv::gry + 1) + dy].ob != 0)
  347. if (lidarGridPtr[dx * (iv::gry) + dy].ob != 0)
  348. {
  349. count++; obsPoint.x = ptx; obsPoint.y = pty;
  350. }
  351. }
  352. }
  353. j++;
  354. for (double length = 0; length <= Veh_Width; length += 0.4)
  355. {
  356. double ptx = gpsTraceLeft[j].x + (gpsTraceRight[j].x - gpsTraceLeft[j].x) / Veh_Width * length;
  357. double pty = gpsTraceLeft[j].y + (gpsTraceRight[j].y - gpsTraceLeft[j].y) / Veh_Width * length;
  358. // int dx = (int)(ptx / gridwide * 2 + (ptx / abs(ptx))) / 2 + centerx;
  359. // int dy = (int)(pty / gridwide * 2 + (pty / abs(pty))) / 2 + centery;
  360. int dx = (ptx + gridwide*(double)centerx)/gridwide;
  361. dx=grx-dx;//1227
  362. int dy = (pty + gridwide*(double)centery)/gridwide;
  363. if (dx >= 0 && dx <grx && dy >= 0 && dy < gry)
  364. {
  365. // if (lidarGridPtr[dx * (iv::gry + 1) + dy].ob != 0)
  366. if (lidarGridPtr[dx * (iv::gry) + dy].ob != 0)
  367. {
  368. count++; obsPoint.x = ptx; obsPoint.y = pty;
  369. }
  370. }
  371. }
  372. if (count >= 2)
  373. {
  374. obsPoint.x = gpsTrace[j].x;
  375. obsPoint.y = gpsTrace[j].y;
  376. isRemove = true;
  377. // DecideGps00().lidarDistance = obsPoint.y;
  378. return obsPoint;
  379. }
  380. }
  381. }
  382. // DecideGps00().lidarDistance = obsPoint.y;
  383. return obsPoint;
  384. }
  385. iv::Point2D iv::decition::Compute00::getLidarObsPointAvoid(std::vector<Point2D> gpsTrace, iv::LidarGridPtr lidarGridPtr) {
  386. iv::Point2D obsPoint(-1, -1);
  387. vector<Point2D> gpsTraceLeft;
  388. vector<Point2D> gpsTraceRight;
  389. for (int j = 0; j < gpsTrace.size(); j++)
  390. {
  391. double sumx1 = 0, sumy1 = 0, count1 = 0;
  392. double sumx2 = 0, sumy2 = 0, count2 = 0;
  393. for (int k = max(0, j - 4); k <= j; k++)
  394. {
  395. count1 = count1 + 1;
  396. sumx1 += gpsTrace[k].x;
  397. sumy1 += gpsTrace[k].y;
  398. }
  399. for (unsigned int k = j; k <= min(gpsTrace.size() - 1, (size_t)(j + 4)); k++)
  400. {
  401. count2 = count2 + 1;
  402. sumx2 += gpsTrace[k].x;
  403. sumy2 += gpsTrace[k].y;
  404. }
  405. sumx1 /= count1; sumy1 /= count1;
  406. sumx2 /= count2; sumy2 /= count2;
  407. double anglevalue = atan2(sumy2 - sumy1, sumx2 - sumx1);
  408. double carFrontx = gpsTrace[j].x;// -Form1.CarRear * Math.Cos(anglevalue);
  409. double carFronty = gpsTrace[j].y;// -Form1.CarRear * Math.Sin(anglevalue);
  410. //1127 fanwei xiuzheng
  411. float buchang=0;
  412. Point2D ptLeft(carFrontx + (Veh_Width+(buchang)*2) / 2 * cos(anglevalue + PI / 2),
  413. carFronty + (Veh_Width+(buchang)*2) / 2 * sin(anglevalue + PI / 2));
  414. Point2D ptRight(carFrontx + (Veh_Width+(buchang)*2) / 2 * cos(anglevalue - PI / 2),
  415. carFronty + (Veh_Width+(buchang)*2) / 2 * sin(anglevalue - PI / 2));
  416. gpsTraceLeft.push_back(ptLeft);
  417. gpsTraceRight.push_back(ptRight);
  418. }
  419. bool isRemove = false;
  420. for (int j = 1; j < gpsTrace.size() - 1 && !isRemove; j++)
  421. {
  422. if (!isRemove && gpsTrace[j].y>2.5 && gpsTraceLeft[j].y>2.5 && gpsTraceRight[j].y>2.5)
  423. {
  424. int count = 0;
  425. for (double length = 0; length <= Veh_Width; length += 0.4)
  426. {
  427. double ptx = gpsTraceLeft[j].x + (gpsTraceRight[j].x - gpsTraceLeft[j].x) / Veh_Width * length;
  428. double pty = gpsTraceLeft[j].y + (gpsTraceRight[j].y - gpsTraceLeft[j].y) / Veh_Width * length;
  429. int dx = (int)(ptx / gridwide * 2 + (ptx / abs(ptx))) / 2 + centerx; //*2左右多出一半的车宽(1米)
  430. int dy = (int)(pty / gridwide * 2 + (pty / abs(pty))) / 2 + centery;
  431. if (dx >= 0 && dx <grx && dy >= 0 && dy < gry)
  432. {
  433. if (lidarGridPtr[dx * (iv::gry + 1) + dy].ob != 0)
  434. {
  435. count++; obsPoint.x = ptx; obsPoint.y = pty;
  436. }
  437. }
  438. }
  439. j++;
  440. for (double length = 0; length <= Veh_Width; length += 0.4)
  441. {
  442. double ptx = gpsTraceLeft[j].x + (gpsTraceRight[j].x - gpsTraceLeft[j].x) / Veh_Width * length;
  443. double pty = gpsTraceLeft[j].y + (gpsTraceRight[j].y - gpsTraceLeft[j].y) / Veh_Width * length;
  444. int dx = (int)(ptx / gridwide * 2 + (ptx / abs(ptx))) / 2 + centerx;
  445. int dy = (int)(pty / gridwide * 2 + (pty / abs(pty))) / 2 + centery;
  446. if (dx >= 0 && dx <grx && dy >= 0 && dy < gry)
  447. {
  448. if (lidarGridPtr[dx * (iv::gry + 1) + dy].ob != 0)
  449. {
  450. count++; obsPoint.x = ptx; obsPoint.y = pty;
  451. }
  452. }
  453. }
  454. if (count >= 2)
  455. {
  456. obsPoint.x = gpsTrace[j].x;
  457. obsPoint.y = gpsTrace[j].y;
  458. isRemove = true;
  459. DecideGps00().lidarDistanceAvoid = obsPoint.y;
  460. return obsPoint;
  461. }
  462. }
  463. }
  464. // DecideGps00().lidarDistanceAvoid = obsPoint.y;
  465. return obsPoint;
  466. }
  467. //int iv::decition::Compute00::getEsrIndex(std::vector<Point2D> gpsTrace, std::vector<ObstacleBasic> esrRadars) {
  468. // bool isRemove = false;
  469. //
  470. // for (int j = 1; j < gpsTrace.size() - 1 && !isRemove; j++)
  471. // {
  472. //
  473. // for (int i = 0; i < esrRadars.size(); i++)
  474. // if ((esrRadars[i].nomal_y) != 0)
  475. // {
  476. // double xxx = esrRadars[i].nomal_x + Esr_Offset;
  477. // double yyy = esrRadars[i].nomal_y;
  478. //
  479. // if (abs(xxx - gpsTrace[j].x) <= 3.0*Veh_Width / 4.0 && abs(yyy - (gpsTrace[j].y)) <= 1)
  480. // {
  481. //
  482. // if (lastEsrID == (esrRadars[i]).esr_ID)
  483. // {
  484. // lastEsrCount++;
  485. // }
  486. // else
  487. // {
  488. // lastEsrCount = 0;
  489. // }
  490. //
  491. // if (lastEsrCount >= 3)
  492. // {
  493. // return i;
  494. // }
  495. //
  496. // lastEsrID = (esrRadars[i]).esr_ID;
  497. // }
  498. // }
  499. // }
  500. // return -1;
  501. //}
  502. int iv::decition::Compute00::getEsrIndex(std::vector<Point2D> gpsTrace,int roadNum,int *esrPathpoint) {
  503. bool isRemove = false;
  504. for (int j = 1; j < gpsTrace.size() - 1 && !isRemove; j++)
  505. {
  506. for (int i = 0; i < 64; i++)
  507. if ((ServiceCarStatus.obs_radar[i].nomal_y) != 0 && (ServiceCarStatus.obs_radar[i].valid))
  508. {
  509. double xxx = ServiceCarStatus.obs_radar[i].nomal_x + Esr_Offset;
  510. double yyy = ServiceCarStatus.obs_radar[i].nomal_y+ Esr_Y_Offset;
  511. /*ODS("\nESR毫米波检测物体X距离:%f\n", xxx);
  512. ODS("\nESR毫米波检测物体Y距离:%f\n", yyy);*/
  513. //优化
  514. // if(sqrt((xxx - gpsTrace[j].x)*(xxx - gpsTrace[j].x) + (yyy - gpsTrace[j].y)*(yyy - gpsTrace[j].y)) < (1.0*Veh_Width / 2.0+DecideGps00().xiuzhengCs)){
  515. // *esrPathpoint = j;
  516. // return i;
  517. // }
  518. if (abs(xxx - gpsTrace[j].x) <= (3.0*Veh_Width / 4.0+DecideGps00().xiuzhengCs) && abs(yyy - (gpsTrace[j].y)) <= 1)
  519. {
  520. if (lastEsrID == i)
  521. {
  522. lastEsrCount++;
  523. }
  524. else
  525. {
  526. lastEsrCount = 0;
  527. }
  528. if(yyy>50 ){
  529. if (lastEsrCount >=200)
  530. {
  531. return i;
  532. }
  533. }
  534. else if (lastEsrCount >= 1)
  535. {
  536. return i;
  537. }
  538. lastEsrID = i;
  539. }
  540. }
  541. }
  542. return -1;
  543. }
  544. //int iv::decition::Compute00::getEsrIndex(std::vector<Point2D> gpsTrace,int roadNum) {
  545. // bool isRemove = false;
  546. // for (int j = 1; j < gpsTrace.size() - 1 && !isRemove; j++)
  547. // {
  548. // for (int i = 0; i < 64; i++)
  549. // if ((examed_obs_radar[i].nomal_y) != 0 && (examed_obs_radar[i].valid))
  550. // {
  551. // double xxx = examed_obs_radar[i].nomal_x + Esr_Offset;
  552. // double yyy = examed_obs_radar[i].nomal_y+ Esr_Y_Offset;
  553. // /*ODS("\nESR毫米波检测物体X距离:%f\n", xxx);
  554. // ODS("\nESR毫米波检测物体Y距离:%f\n", yyy);*/
  555. // if (abs(xxx - gpsTrace[j].x) <= 3.0*Veh_Width / 4.0 && abs(yyy - (gpsTrace[j].y)) <= 1)
  556. // {
  557. // if (lastEsrID == i)
  558. // {
  559. // lastEsrCount++;
  560. // }
  561. // else
  562. // {
  563. // lastEsrCount = 0;
  564. // }
  565. // if(yyy>50 ){
  566. // if (lastEsrCount >=200)
  567. // {
  568. // return i;
  569. // }
  570. // }
  571. // else if (lastEsrCount >= 3)
  572. // {
  573. // return i;
  574. // }
  575. // lastEsrID = i;
  576. // }
  577. // }
  578. // }
  579. // return -1;
  580. //}
  581. int iv::decition::Compute00::getEsrIndexAvoid(std::vector<Point2D> gpsTrace) {
  582. bool isRemove = false;
  583. for (int j = 1; j < gpsTrace.size() - 1 && !isRemove; j++)
  584. {
  585. for (int i = 0; i < 64; i++)
  586. if ((ServiceCarStatus.obs_radar[i].nomal_y) != 0 && (ServiceCarStatus.obs_radar[i].valid))
  587. {
  588. double xxx = ServiceCarStatus.obs_radar[i].nomal_x + Esr_Offset;
  589. double yyy = ServiceCarStatus.obs_radar[i].nomal_y;
  590. if (abs(xxx - gpsTrace[j].x) <= 3.0*Veh_Width / 4.0 && abs(yyy - (gpsTrace[j].y)) <= 1)
  591. {
  592. if (lastEsrIDAvoid == i)
  593. {
  594. lastEsrCountAvoid++;
  595. }
  596. else
  597. {
  598. lastEsrCountAvoid = 0;
  599. }
  600. if (lastEsrCountAvoid >= 6)
  601. {
  602. return i;
  603. }
  604. lastEsrIDAvoid = i;
  605. }
  606. }
  607. }
  608. return -1;
  609. }
  610. //double iv::decition::Compute00::getObsSpeed(Point2D obsPoint, std::vector<ObstacleBasic> esrRadars,double realSecSpeed) {
  611. // double obsSpeed = 0 - realSecSpeed;
  612. // double minDis = iv::MaxValue;
  613. // for (int i = 0; i < esrRadars.size(); i++)
  614. // if ((esrRadars[i].nomal_y) != 0)
  615. // {
  616. // double xxx = esrRadars[i].nomal_x + Esr_Offset;
  617. // double yyy = esrRadars[i].nomal_y;
  618. //
  619. // if (abs(xxx - obsPoint.x) < 4 && abs(yyy - obsPoint.y) < 2)
  620. // {
  621. // double tmpDis =sqrt((xxx - obsPoint.x) * (xxx - obsPoint.x) + (yyy - obsPoint.y) * (yyy - obsPoint.y));
  622. // if (tmpDis < minDis)
  623. // {
  624. // minDis = tmpDis;
  625. // obsSpeed = esrRadars[i].speed_y;
  626. // }
  627. // }
  628. // }
  629. //
  630. // return obsSpeed;
  631. //
  632. //
  633. //}
  634. double iv::decition::Compute00::getObsSpeed(Point2D obsPoint, double realSecSpeed) {
  635. double obsSpeed = 0 - realSecSpeed;
  636. double minDis = iv::MaxValue;
  637. for (int i = 0; i < 64; i++)
  638. if ((ServiceCarStatus.obs_radar[i].nomal_y) != 0 && ServiceCarStatus.obs_radar[i].valid)
  639. {
  640. double xxx = ServiceCarStatus.obs_radar[i].nomal_x + Esr_Offset;
  641. double yyy = ServiceCarStatus.obs_radar[i].nomal_y + Esr_Y_Offset;
  642. if (abs(xxx - obsPoint.x) < 4 && abs(yyy - obsPoint.y) < 2)
  643. {
  644. double tmpDis = sqrt((xxx - obsPoint.x) * (xxx - obsPoint.x) + (yyy - obsPoint.y) * (yyy - obsPoint.y));
  645. if (tmpDis < minDis)
  646. {
  647. minDis = tmpDis;
  648. obsSpeed = ServiceCarStatus.obs_radar[i].speed_y;
  649. }
  650. }
  651. }
  652. return obsSpeed;
  653. }
  654. double iv::decition::Compute00::getDecideAvoidAngle(std::vector<Point2D> gpsTrace, double realSpeed, float avoidX) {
  655. double ang = 0;
  656. double EPos = 0, EAng = 0;
  657. double KEang = 14, KEPos = 10, DEang = 0, DEPos = 0;
  658. double PreviewDistance = max(6.0, realSpeed / 3.6 * 1.8);//预瞄距离
  659. if (gpsTrace[0].v1 == 1)
  660. {
  661. KEang = 10; KEPos = 8;
  662. if (realSpeed > 60) KEang = 5;
  663. }
  664. else if (gpsTrace[0].v1 == 2 || gpsTrace[0].v1 == 3)
  665. {
  666. KEang = 14; KEPos = 10;
  667. }
  668. else if (gpsTrace[0].v1 == 4 || gpsTrace[0].v1 == 5)
  669. {
  670. KEang = 14; KEPos = 10;
  671. }
  672. else if (gpsTrace[0].v1 == 7 && (gpsTrace[0].v2 == 23 || gpsTrace[0].v2 == 24))
  673. {
  674. KEang = 18; KEPos = 50; PreviewDistance = 3;
  675. }
  676. else if (gpsTrace[0].v1 == 7)
  677. {
  678. KEang = 20; KEPos = 50; PreviewDistance = 4;
  679. }
  680. double sumdis = 0;
  681. int gpsIndex = 0;
  682. std::vector<Point2D> farTrace;
  683. for (int i = 1; i < gpsTrace.size() - 1; i++)
  684. {
  685. sumdis += GetDistance(gpsTrace[i - 1], gpsTrace[i]);
  686. if (sumdis > PreviewDistance)
  687. {
  688. gpsIndex = i;
  689. break;
  690. }
  691. }
  692. if ((DecideGps00().readyParkMode) && (gpsIndex + 10>DecideGps00().gpsLineParkIndex))
  693. {
  694. gpsIndex = DecideGps00().gpsLineParkIndex;
  695. }
  696. EPos = gpsTrace[gpsIndex].x + avoidX;
  697. for (unsigned int i = max(0, gpsIndex - 3); i < min((size_t)(gpsIndex + 3), gpsTrace.size()); i++) {
  698. farTrace.push_back(gpsTrace[gpsIndex]);
  699. }
  700. if (farTrace.size() == 0) {
  701. EAng = 0;
  702. }
  703. else {
  704. EAng = getAvoidAveDef(farTrace, avoidX);
  705. }
  706. ang = KEang * EAng + KEPos * EPos + DEang * (EAng - lastEA) + DEPos * (EPos - lastEP);
  707. lastEA = EAng;
  708. lastEP = EPos;
  709. if (ang > angleLimit) {
  710. ang = angleLimit;
  711. }
  712. else if (ang < -angleLimit) {
  713. ang = -angleLimit;
  714. }
  715. if (lastAng != iv::MaxValue) {
  716. ang = 0.2 * lastAng + 0.8 * ang;
  717. //ODS("lastAng:%d\n", lastAng);
  718. }
  719. lastAng = ang;
  720. return ang;
  721. }
  722. std::vector<iv::GPSData> iv::decition::Compute00::getBesideGpsMapLine(iv::GPS_INS now_gps_ins, vector<iv::GPSData>gpsMapLine, float avoidX) {
  723. vector<vector<iv::GPSData>> maps;
  724. vector<iv::GPSData> gpsMapLineBeside;
  725. int sizeN = gpsMapLine.size();
  726. for (int i = 1; i < sizeN; i++)
  727. {
  728. iv::GPSData gpsData(new GPS_INS);
  729. double xx = gpsMapLine[i]->gps_x - now_gps_ins.gps_x;
  730. double yy = gpsMapLine[i]->gps_y - now_gps_ins.gps_y;
  731. double lng = ServiceCarStatus.location->ins_heading_angle;
  732. double x0 = xx * cos(lng * PI / 180) - yy * sin(lng * PI / 180);
  733. double y0 = xx * sin(lng * PI / 180) + yy * cos(lng * PI / 180);
  734. double k1 = sin((90 + (gpsMapLine[i]->ins_heading_angle - lng)) * PI / 180);
  735. double k2 = cos((90 + (gpsMapLine[i]->ins_heading_angle - lng)) * PI / 180);
  736. // memcpy(&gpsData, &gpsMapLine[i], sizeof(gpsData));
  737. gpsData->speed_mode = gpsMapLine[i]->speed_mode;
  738. gpsData->gps_x = x0 + k1 * avoidX;
  739. gpsData->gps_y = y0 + k2 * avoidX;
  740. gpsMapLineBeside.push_back(gpsData);
  741. }
  742. return gpsMapLineBeside;
  743. }
  744. //double iv::decition::Compute00::getDecideAngleByLane(double realSpeed) {
  745. // double ang = 0;
  746. // double EPos = 0, EAng = 0;
  747. // // double KEang = 14, KEpos = 10, DEang = 0, DEpos = 0;
  748. // double KEang = 5, KEPos = 30, DEang = 0, DEPos = 0;
  749. // // double PreviewDistance = max(6.0, realSpeed / 3.6 * 1.8);//预瞄距离
  750. // double PreviewDistance;//预瞄距离
  751. // realSpeed > 40 ? PreviewDistance = max(6.0, realSpeed *0.6) : PreviewDistance = max(6.0, realSpeed *0.5);
  752. //// if (realSpeed > 40) KEang = 10; KEpos = 8;
  753. //// if (realSpeed > 50) KEang = 5;
  754. //double c1 = ServiceCarStatus.aftermarketLane.dist_to_lane_l;
  755. //double c2 = ServiceCarStatus.aftermarketLane.dist_to_lane_r;
  756. //double a = ServiceCarStatus.Lane.curvature;
  757. //double b = ServiceCarStatus.Lane.heading;
  758. //double c = (c1+c2)*0.5;
  759. //double x= PreviewDistance;
  760. //double y;
  761. //y=a*x*x+b*x+c;
  762. // // EPos = y;
  763. //EPos=c;
  764. // // EAng=atan(2*a*x+b) / PI * 180;
  765. // EAng=ServiceCarStatus.Lane.yaw;
  766. // ang = KEang * EAng + KEPos * EPos + DEang * (EAng - lastEA) + DEPos * (EPos - lastEP);
  767. // lastEA = EAng;
  768. // lastEP = EPos;
  769. // std::cout << "\nEPos:%f\n" << EPos << std::endl;
  770. // std::cout << "\nEAng:%f\n" << EAng << std::endl;
  771. // if (ang > angleLimit) {
  772. // ang = angleLimit;
  773. // }
  774. // else if (ang < -angleLimit) {
  775. // ang = -angleLimit;
  776. // }
  777. // if (lastAng != iv::MaxValue) {
  778. // ang = 0.2 * lastAng + 0.8 * ang;
  779. // //ODS("lastAng:%d\n", lastAng);
  780. // }
  781. // lastAng = ang;
  782. // return ang;
  783. // }
  784. double IEPos = 0, IEang = 0;
  785. double iv::decition::Compute00::getDecideAngleByLanePID(double realSpeed) {
  786. double ang = 0;
  787. double EPos = 0, EAng = 0;
  788. double Curve=0;
  789. double KEang = 14, KEPos = 10, DEang = 0, DEPos = 0;
  790. double KCurve=120;
  791. double KIEPos = 0, KIEang = 0;
  792. // double PreviewDistance = max(6.0, realSpeed / 3.6 * 1.8);//预瞄距离
  793. double PreviewDistance;//预瞄距离
  794. int confL=ServiceCarStatus.aftermarketLane.lane_conf_left;
  795. int confR=ServiceCarStatus.aftermarketLane.lane_conf_right;
  796. int conf =min(confL,confR);
  797. realSpeed > 40 ? PreviewDistance = max(6.0, realSpeed *0.6) : PreviewDistance = max(6.0, realSpeed *0.5);
  798. if (realSpeed > 40) KEang = 10; KEPos = 8;
  799. if (realSpeed > 50) KEang = 5;
  800. KEPos = 20;
  801. KEang = 200;
  802. //KEang = 15;
  803. double c1 = ServiceCarStatus.aftermarketLane.dist_to_lane_l;
  804. double c2 = ServiceCarStatus.aftermarketLane.dist_to_lane_r;
  805. double a = ServiceCarStatus.Lane.curvature;
  806. double b = ServiceCarStatus.Lane.heading;
  807. double c = (c1+c2)*0.5;
  808. double yaw= ServiceCarStatus.Lane.yaw;
  809. double x= PreviewDistance;
  810. double y;
  811. y=c-(a*x*x+b*x);
  812. double difa=0-(atan(2*a*x+b) / PI * 180);
  813. Curve=0-a;
  814. //EAng=difa;
  815. //EPos=y;
  816. EAng= 0-b;
  817. EPos = c;
  818. DEang = 10;
  819. DEPos = 20;
  820. //DEang = 20;
  821. //DEPos = 10;
  822. IEang = EAng+0.7*IEang;
  823. IEPos = EPos+0.7*IEPos;
  824. KIEang = 0;
  825. //KIEang = 0.5;
  826. KIEPos =2;
  827. if(abs(confL)>=2&&abs(confR)>=2){
  828. //ang = KEang * EAng + KEPos * EPos + DEang * (EAng - lastEA) + DEPos * (EPos - lastEP)+ KIEang * IEang + KIEPos * IEPos;
  829. ang = KEang * EAng + KEPos * EPos +KCurve*Curve+ DEang * (EAng - lastEA) + DEPos * (EPos - lastEP)+ KIEang * IEang + KIEPos * IEPos;
  830. }else{
  831. ang=lastAng;
  832. }
  833. //if(lastAng!=0&&abs(ang-lastAng)>20)ang=lastAng;
  834. lastEA = EAng;
  835. lastEP = EPos;
  836. if (ang > angleLimit) {
  837. ang = angleLimit;
  838. }
  839. else if (ang < -angleLimit) {
  840. ang = -angleLimit;
  841. }
  842. if (lastAng != iv::MaxValue) {
  843. ang = 0.2 * lastAng + 0.8 * ang;
  844. //ODS("lastAng:%d\n", lastAng);
  845. }
  846. lastAng = ang;
  847. return ang;
  848. }
  849. double iv::decition::Compute00::bocheCompute(GPS_INS nowGps, GPS_INS aimGps) {
  850. GaussProjCal(aimGps.gps_lng, aimGps.gps_lat, &aimGps.gps_x, &aimGps.gps_y);
  851. Point2D pt = Coordinate_Transfer(nowGps.gps_x, nowGps.gps_y, aimGps);
  852. double x_1 = pt.x;
  853. double y_1 = pt.y;
  854. double angle_1 = getQieXianAngle(nowGps,aimGps);
  855. double x_2 = 0.0, y_2 = 0.0;
  856. double steering_angle;
  857. double l = 2.950;
  858. double r =6;
  859. double x_o, y_o, x_o_1, y_o_1, x_o_2, y_o_2, x_3, y_3;
  860. double x_t_n, y_t_n, x_t_f, y_t_f;//近切点和远切点
  861. double x_t_1, y_t_1, x_t_2, y_t_2;//圆形1的切点
  862. double x_t_3, y_t_3, x_t_4, y_t_4;//圆形2的切点
  863. double g_1 = tan(angle_1);
  864. double car_pos[3] = { x_1,y_1,g_1 };
  865. double parking_pos[2] = { x_2,y_2 };
  866. double g_3;
  867. double t[4][2];
  868. double p[4];
  869. double s1, s2; //切点与车起始位置的距离
  870. double min;
  871. int min_i;
  872. //g_3 = 0 - 0.5775;
  873. g_3 = pingfenxian_xielv(x_1, y_1, x_2, y_2, angle_1);
  874. //交点
  875. x_3 = 0.0;//(y_1 - y_2 + g_2*x_2 - g_1*x_1) / (g_2 - g_1);
  876. y_3 = y_1 - g_1 * x_1;
  877. //圆心1
  878. x_o_1 = r;
  879. y_o_1 = g_3 * r + y_3;
  880. //圆形1的切点1
  881. x_t_1 = 0.0;
  882. y_t_1 = g_3 * r + y_3;
  883. //圆形1的切点2
  884. if (g_1 == 0)
  885. {
  886. x_t_2 = r;
  887. y_t_2 = y_1 - g_1 * x_1;
  888. }
  889. else
  890. {
  891. y_t_2 = (y_1 + g_1 * x_o_1 + y_o_1 * g_1*g_1 - g_1 * x_1) / (1 + g_1 * g_1);
  892. x_t_2 = (y_t_2 + g_1 * x_1 - y_1) / g_1;
  893. }
  894. //圆心2
  895. x_o_2 = 0 - r;
  896. y_o_2 = y_3 - g_3 * r;
  897. //圆形2的切点1
  898. x_t_3 = 0;
  899. y_t_3 = y_3 - g_3 * r;
  900. //圆形2的切点2
  901. if (g_1 == 0)
  902. {
  903. x_t_4 = 0 - r;
  904. y_t_4 = y_1 - g_1 * x_1;
  905. }
  906. else
  907. {
  908. y_t_4 = (y_1 + g_1 * x_o_2 + y_o_2 * g_1*g_1 - g_1 * x_1) / (1 + g_1 * g_1);
  909. x_t_4 = (y_t_4 + g_1 * x_1 - y_1) / g_1;
  910. }
  911. t[0][0] = x_t_1;
  912. t[0][1] = y_t_1;
  913. t[1][0] = x_t_2;
  914. t[1][1] = y_t_2;
  915. t[2][0] = x_t_3;
  916. t[2][1] = y_t_3;
  917. t[3][0] = x_t_4;
  918. t[3][1] = y_t_4;
  919. for (int i = 0; i < 4; i++)
  920. {
  921. p[i] = (t[i][0] - parking_pos[0])*(t[i][0] - parking_pos[0]) + (t[i][1] - parking_pos[1])*(t[i][1] - parking_pos[1]);
  922. }
  923. min = p[0];
  924. min_i = 0;
  925. for (int i = 1; i < 4; i++)
  926. {
  927. if (p[i] < min)
  928. {
  929. min = p[i]; min_i = i;
  930. }
  931. }
  932. if (min_i < 2)
  933. {
  934. x_o = x_o_1;
  935. y_o = y_o_1;
  936. s1 = (x_t_1 - x_1)*(x_t_1 - x_1) + (y_t_1 - y_1)*(y_t_1 - y_1);
  937. s2 = (x_t_2 - x_1)*(x_t_2 - x_1) + (y_t_2 - y_1)*(y_t_2 - y_1);
  938. if (s1 < s2)
  939. {
  940. x_t_n = x_t_1;
  941. y_t_n = y_t_1;
  942. x_t_f = x_t_2;
  943. y_t_f = y_t_2;
  944. }
  945. else
  946. {
  947. x_t_n = x_t_2;
  948. y_t_n = y_t_2;
  949. x_t_f = x_t_1;
  950. y_t_f = y_t_1;
  951. }
  952. }
  953. else
  954. {
  955. x_o = x_o_2;
  956. y_o = y_o_2;
  957. s1 = (x_t_3 - x_1)*(x_t_3 - x_1) + (y_t_3 - y_1)*(y_t_3 - y_1);
  958. s2 = (x_t_4 - x_1)*(x_t_4 - x_1) + (y_t_4 - y_1)*(y_t_4 - y_1);
  959. if (s1 < s2)
  960. {
  961. x_t_n = x_t_3;
  962. y_t_n = y_t_3;
  963. x_t_f = x_t_4;
  964. y_t_f = y_t_4;
  965. }
  966. else
  967. {
  968. x_t_n = x_t_4;
  969. y_t_n = y_t_4;
  970. x_t_f = x_t_3;
  971. y_t_f = y_t_3;
  972. }
  973. }
  974. steering_angle = atan2(l, r);
  975. if (x_t_n < 0)
  976. {
  977. steering_angle = 0 - steering_angle;
  978. }
  979. nearTpoint=Coordinate_UnTransfer(x_t_n, y_t_n, aimGps);
  980. farTpoint = Coordinate_UnTransfer(x_t_f, y_t_f, aimGps);
  981. bocheAngle = steering_angle*180/PI;
  982. cout << "近切点:x_t_n=" << x_t_n << endl;
  983. cout << "近切点:y_t_n=" << y_t_n << endl;
  984. cout << "远切点:x_t_f=" << x_t_f << endl;
  985. cout << "远切点:y_t_f=" << y_t_f << endl;
  986. cout << "航向角:" << steering_angle << endl;
  987. // if (x_1 < 0 && y_1 > 0 && x_1 < x_t_n &&y_t_f > 0.1) {
  988. // return 1;
  989. // }
  990. Point2D ptN = Coordinate_Transfer(nearTpoint.gps_x, nearTpoint.gps_y, nowGps);
  991. double disA = GetDistance(aimGps,nowGps);
  992. if(y_t_n>0 && ptN.y<0 && y_t_f>0.1 && disA<40){
  993. return 1;
  994. }
  995. return 0;
  996. }
  997. //返回垂直平分线的斜率
  998. double iv::decition::Compute00::pingfenxian_xielv(double x_1, double y_1, double x_2, double y_2, double angle_1) {
  999. double angl, x_3, angle_3;
  1000. if (tan(angle_1 == 0))
  1001. {
  1002. if ((x_1 - x_2) > 0 && ((y_1 - y_2) > 0))
  1003. {
  1004. angle_3 = 0 - 1;
  1005. }
  1006. else
  1007. {
  1008. angle_3 = 1;
  1009. }
  1010. }
  1011. else
  1012. {
  1013. x_3 = (tan(angle_1)*x_1 - y_1) / tan(angle_1);//车所在直线与x轴交点
  1014. angl = tan(angle_1);//车所在直线的斜率
  1015. if ((x_1 - x_2)>0 && ((y_1 - y_2)>0))//第一象限
  1016. {
  1017. if ((angl *x_3)<0)//车斜率与车直线的x轴交点异号
  1018. {
  1019. if (angl < 0)
  1020. {
  1021. angle_3 = tan(PI*0.5 + (PI*0.5 - atan(fabs(angl))) *0.5);//垂直平分线斜率
  1022. }
  1023. else
  1024. {
  1025. angle_3 = tan(PI*0.5 + (PI*0.5 + atan(fabs(angl))) *0.5);//垂直平分线斜率
  1026. }
  1027. }
  1028. }
  1029. else//第二象限
  1030. {
  1031. if ((angl*x_3)<0)//车斜率与车直线的x轴交点异号
  1032. {
  1033. if (angl < 0)
  1034. {
  1035. angle_3 = tan(PI*0.5 - (PI*0.5 + atan(fabs(angl))) *0.5);//垂直平分线斜率
  1036. }
  1037. else
  1038. {
  1039. angle_3 = tan(atan(fabs(angl)) + (PI*0.5 - atan(fabs(angl))) *0.5);//垂直平分线斜率
  1040. }
  1041. }
  1042. }
  1043. }
  1044. return angle_3;
  1045. }
  1046. double iv::decition::Compute00::getQieXianAngle(GPS_INS nowGps, GPS_INS aimGps) {
  1047. double heading = nowGps.ins_heading_angle *PI/180;
  1048. double x1 = nowGps.gps_x;
  1049. double y1 = nowGps.gps_y;
  1050. if (heading<=PI*0.5)
  1051. {
  1052. heading = 0.5*PI - heading;
  1053. }
  1054. else if (heading>PI*0.5 && heading<=PI*1.5) {
  1055. heading = 1.5*PI - heading;
  1056. }
  1057. else if (heading>PI*1.5) {
  1058. heading = 2.5*PI - heading;
  1059. }
  1060. double k1 = tan(heading);
  1061. double x = x1+10;
  1062. double y = k1 * x + y1 - (k1 * x1);
  1063. Point2D pt1 = Coordinate_Transfer(nowGps.gps_x, nowGps.gps_y, aimGps);
  1064. Point2D pt2 = Coordinate_Transfer(x, y, aimGps);
  1065. double xielv = (pt1.y - pt2.y) / (pt1.x - pt2.x);
  1066. double angle = atan(abs(xielv));
  1067. if (xielv<0)
  1068. {
  1069. angle = PI - angle;
  1070. }
  1071. return angle;
  1072. }
  1073. /*
  1074. chuizhicheweiboche
  1075. */
  1076. int iv::decition::Compute00::bocheDirectCompute(GPS_INS nowGps, GPS_INS aimGps)
  1077. {
  1078. double l=2.95;//轴距
  1079. double x_0 = 0, y_0 = 0.5;
  1080. double x_1, y_1;//车起点坐标
  1081. double ange1;//车航向角弧度
  1082. double x_2, y_2;//另一条与车直线在angle2和R_M 固定情况下过坐标点,第二个近切点
  1083. double real_rad;;//另一条直线的航向角弧度
  1084. double angle_3;//垂直平分线弧度
  1085. double x_3, y_3;//垂直平分线交点
  1086. double x_4, y_4;//另一条直线的远切点坐标,第二个远切点,已知
  1087. double x_o_1, y_o_1;//圆形1坐标
  1088. double x_o_2, y_o_2;//圆形2坐标
  1089. double x_t_n, y_t_n, x_t_f, y_t_f;//近切点和远切点
  1090. double min_rad;
  1091. double R_M; //后轴中点的转弯半径
  1092. double steering_angle;
  1093. GaussProjCal(aimGps.gps_lng, aimGps.gps_lat, &aimGps.gps_x, &aimGps.gps_y);
  1094. Point2D pt = Coordinate_Transfer(nowGps.gps_x, nowGps.gps_y, aimGps);
  1095. x_1=pt.x;
  1096. y_1=pt.y;
  1097. ange1=getQieXianAngle(nowGps,aimGps);
  1098. min_rad_zhuanxiang(&R_M , &min_rad);
  1099. qiedian_n(x_1,y_1,R_M,min_rad,&x_2 , &y_2, &real_rad);//计算另一条与车直线在angle2和R_M 固定情况下近切点:x_2, y_2
  1100. liangzhixian_jiaodian( x_1, y_1, x_2, y_2,ange1,real_rad,&x_3 , &y_3);
  1101. chuizhipingfenxian_xielv( x_1, y_1, ange1, real_rad, min_rad,&angle_3);
  1102. yuanxin( x_2, y_2, x_3, y_3, real_rad, angle_3, R_M,&x_o_1,&y_o_1,&x_o_2,&y_o_2);
  1103. yuanxin_qiedian( ange1, x_o_1, y_o_1, x_o_2, y_o_2,
  1104. x_1, y_1, x_2, y_2, x_3, y_3, real_rad, angle_3, R_M,&x_t_n,&y_t_n,&x_t_f, &y_t_f);
  1105. steering_angle = atan2(l, R_M);
  1106. x_4 = 0.5;
  1107. y_4 = 0;
  1108. //for (int i = 0; i < 4; i++)
  1109. //{
  1110. //for (int j = 0; j < 2; j++)
  1111. //{
  1112. // cout << t[i][j] << endl;
  1113. //}
  1114. //}
  1115. //cout << "min_rad:" << min_rad<< endl;
  1116. //cout << "jiaodian:x=" << x_3 << endl;
  1117. //cout << "jiaodian:y=" << y_3 << endl;
  1118. // cout << "R-M:" << R_M << endl;
  1119. cout << "x_0:" << x_0 << endl;
  1120. cout << "y_0:" << y_0 << endl;
  1121. cout << "x_2:" << x_2 << endl;
  1122. cout << "y_2:" << y_2 << endl;
  1123. cout << "近切点:x_t_n="<< x_t_n << endl;
  1124. cout << "近切点:y_t_n=" << y_t_n << endl;
  1125. cout << "远切点:x_t_f=" << x_t_f << endl;
  1126. cout << "远切点:y_t_f=" << y_t_f << endl;
  1127. //cout << "航向角:" << steering_angle << endl;
  1128. //cout << "圆心1横坐标=" << x_o_1 << endl;
  1129. //cout << "圆心1纵坐标=" << y_o_1 << endl;
  1130. //cout << "圆心2横坐标=" << x_o_2 << endl;
  1131. //cout << "圆心2纵坐标=" << y_o_2 << endl;
  1132. //cout << "平分线弧度=" << angle_3 << endl;
  1133. //cout << " min_rad=" << min_rad << endl;
  1134. //cout << " real_rad=" << real_rad << endl;
  1135. // system("PAUSE");
  1136. dTpoint0=Coordinate_UnTransfer(x_t_n, y_t_n, aimGps);
  1137. dTpoint1 = Coordinate_UnTransfer(x_t_f, y_t_f, aimGps);
  1138. dTpoint2 = Coordinate_UnTransfer(x_2, y_2, aimGps);
  1139. dTpoint3 = Coordinate_UnTransfer(x_0, y_0, aimGps);
  1140. dBocheAngle = steering_angle*180/PI;
  1141. double disA = GetDistance(aimGps,nowGps);
  1142. if(pt.y>y_t_n && x_t_f<x_2 && y_t_f>y_2&&disA<40){
  1143. return 1;
  1144. }
  1145. return 0;
  1146. }
  1147. double iv::decition::Compute00::min_rad_zhuanxiang(double *R_M, double *min_rad) {
  1148. double L_c = 4.749;//车长
  1149. double rad_1;
  1150. double rad_2;
  1151. double L_k = 1.931;//车宽
  1152. double L = 2.95;//轴距
  1153. double L_f =1.2 ;//前悬
  1154. double L_r =0.7 ;//后悬
  1155. double R_min =6.5 ;//最小转弯半径
  1156. *R_M = fabs(sqrt(R_min*R_min - (L + L_f)*(L + L_f))) - L_k*0.5;//double R_M ;//后轴中点的转弯半径
  1157. //rad_1 = atan2(sqrt(R_min*R_min - (R_M - L_k*0.5)*(R_M - L_k*0.5)), R_M - L_k*0.5);
  1158. //rad_2 = atan2(L + L_f, R_M + L_k*0.5);
  1159. *min_rad = 45 * PI / 180;// rad_1 - rad_2;
  1160. return 0;
  1161. }
  1162. double iv::decition::Compute00::qiedian_n(double x_1, double y_1, double R_M,double min_rad, double *x_2, double *y_2, double *real_rad ) {
  1163. if (x_1 > 0 && y_1 > 0)
  1164. {
  1165. *real_rad = PI*0.5 - min_rad;
  1166. *x_2 = R_M - R_M*cos(min_rad);
  1167. *y_2 = R_M*sin(min_rad) + 0.5;
  1168. }
  1169. else
  1170. {
  1171. *real_rad = PI*0.5 + min_rad;
  1172. *x_2 = R_M*cos(min_rad) - R_M;
  1173. *y_2 = R_M*sin(min_rad) + 0.5;
  1174. }
  1175. return 0;
  1176. }
  1177. double iv::decition::Compute00::liangzhixian_jiaodian(double x_1,double y_1,double x_2,double y_2,double ange1,double real_rad,double *x_3,double *y_3) {
  1178. double b1, b2;
  1179. double k1, k2;
  1180. if (ange1!=(PI*0.5))
  1181. {
  1182. k1 = tan(ange1);
  1183. b1 = y_1 - k1*x_1;
  1184. k2 = tan(real_rad);
  1185. b2 = y_2 - k2*x_2;
  1186. *x_3 = (b2 - b1) / (k1 - k2);
  1187. *y_3 = k2*(*x_3) + b2;
  1188. }
  1189. else
  1190. {
  1191. k2 = tan(real_rad);
  1192. b2 = y_2 - k2*x_2;
  1193. *x_3 = x_1;
  1194. *y_3 = k2*(*x_3) + b2;
  1195. }
  1196. return 0;
  1197. }
  1198. double iv::decition::Compute00::chuizhipingfenxian_xielv(double x_1,double y_1,double ange1,double real_rad,double min_rad,double *angle_3) {
  1199. double k1, k2;
  1200. double angle_j;
  1201. k2 = tan(real_rad);
  1202. if (ange1 != (PI*0.5))
  1203. {
  1204. k1 = tan(ange1);
  1205. angle_j = atan(fabs((k2 - k1) / (1 + k2*k1)));//两直线夹角
  1206. if (x_1 > 0 && y_1 > 0)
  1207. {
  1208. *angle_3 = angle_j*0.5 - min_rad + PI;
  1209. }
  1210. else
  1211. {
  1212. *angle_3 = min_rad - angle_j*0.5;
  1213. }
  1214. }
  1215. else
  1216. {
  1217. angle_j = min_rad;//两直线夹角
  1218. if (x_1 > 0 && y_1 > 0)
  1219. {
  1220. *angle_3 = angle_j*0.5 - min_rad + PI;
  1221. }
  1222. else
  1223. {
  1224. *angle_3 = min_rad - angle_j*0.5;
  1225. }
  1226. }
  1227. return 0;
  1228. }
  1229. double iv::decition::Compute00::yuanxin(double x_2,double y_2,double x_3,double y_3,double real_rad,double angle_3,double R_M,
  1230. double *x_o_1,double *y_o_1,double *x_o_2,double *y_o_2) {
  1231. double b2, b3, k2, k3;
  1232. b2 = y_2 - tan(real_rad)*x_2;
  1233. b3 = y_3 - tan(angle_3)*x_3;
  1234. k2 = tan(real_rad);
  1235. k3 = tan(angle_3);
  1236. *x_o_1 = (sqrt(k2*k2 + 1)*R_M + b3 - b2) / (k2 - k3);
  1237. *y_o_1 = k3*(*x_o_1) + b3;
  1238. *x_o_2 = (b3 - b2 - (sqrt(k2*k2 + 1)*R_M)) / (k2 - k3);
  1239. *y_o_2 = k3*(*x_o_2) + b3;
  1240. return 0;
  1241. }
  1242. double iv::decition::Compute00::yuanxin_qiedian(double ange1,double x_o_1,double y_o_1, double x_o_2,double y_o_2,
  1243. double x_1,double y_1,double x_2,double y_2,double x_3,double y_3,double real_rad,double angle_3,double R_M,
  1244. double *x_t_n, double *y_t_n, double *x_t_f, double *y_t_f)
  1245. {
  1246. double x_o, y_o;
  1247. double b2, b3, k1, k2, k3;
  1248. //double car_pos[3] = { x_1,y_1,k1 };
  1249. double parking_pos[2] = { x_2,y_2 };
  1250. //double t[4][2];
  1251. double p[4];
  1252. double s1, s2; //切点与车起始位置的距离
  1253. double min;
  1254. int min_i;
  1255. double x_t_1, y_t_1, x_t_2, y_t_2;//圆形1的切点
  1256. double x_t_3, y_t_3, x_t_4, y_t_4;//圆形2的切点
  1257. double t[4][2];
  1258. k1 = tan(ange1);
  1259. b2 = y_2 - tan(real_rad)*x_2;
  1260. b3 = y_3 - tan(real_rad)*x_3;
  1261. k2 = tan(real_rad);//另一条直线斜率
  1262. k3 = tan(angle_3);//垂直平分线斜率
  1263. //圆心1和2切点*********************************************
  1264. if (x_1 > 0 && y_1 > 0)//第一象限
  1265. {
  1266. if (ange1 == (PI*0.5))
  1267. {
  1268. x_t_1 = x_1;
  1269. y_t_1 = y_o_1;
  1270. y_t_2 = (y_2 + k2 *x_o_1 + y_o_1*k2*k2 - k2*x_2) / (1 + k2*k2);
  1271. x_t_2 = (y_t_2 + k2*x_2 - y_2) / k2;
  1272. x_t_3 = x_1;
  1273. y_t_3 = y_o_2;
  1274. y_t_4 = (y_2 + k2 *x_o_2 + y_o_2*k2*k2 - k2*x_2) / (1 + k2*k2);
  1275. x_t_4 = (y_t_4 + k2*x_2 - y_2) / k2;
  1276. }
  1277. else
  1278. {
  1279. y_t_1 = (y_1 + k1 *x_o_1 + y_o_1*k1*k1 - k1*x_1) / (1 + k1*k1);
  1280. x_t_1 = (y_t_1 + k1*x_1 - y_1) / k1;
  1281. y_t_2 = (y_2 + k2 *x_o_1 + y_o_1*k2*k2 - k2*x_2) / (1 + k2*k2);
  1282. x_t_2 = (y_t_2 + k2*x_2 - y_2) / k2;
  1283. y_t_3 = (y_1 + k1 *x_o_2 + y_o_2*k1*k1 - k1*x_1) / (1 + k1*k1);
  1284. x_t_3 = (y_t_3 + k1*x_1 - y_1) / k1;
  1285. y_t_4 = (y_2 + k2 *x_o_2 + y_o_2*k2*k2 - k2*x_2) / (1 + k2*k2);
  1286. x_t_4 = (y_t_4 + k2*x_2 - y_2) / k2;
  1287. }
  1288. }
  1289. else
  1290. {
  1291. if (ange1 == 0)
  1292. {
  1293. x_t_1 = 0 - x_1;
  1294. y_t_1 = y_o_1;
  1295. y_t_2 = (y_2 + k2 *x_o_1 + y_o_1*k2*k2 - k2*x_2) / (1 + k2*k2);
  1296. x_t_2 = (y_t_2 + k2*x_2 - y_2) / k2;
  1297. x_t_3 = 0 - x_1;
  1298. y_t_3 = y_o_2;
  1299. y_t_4 = (y_2 + k2 *x_o_2 + y_o_2*k2*k2 - k2*x_2) / (1 + k2*k2);
  1300. x_t_4 = (y_t_4 + k2*x_2 - y_2) / k2;
  1301. }
  1302. else
  1303. {
  1304. y_t_1 = (y_1 + k1 *x_o_1 + y_o_1*k1*k1 - k1*x_1) / (1 + k1*k1);
  1305. x_t_1 = (y_t_1 + k1*x_1 - y_1) / k1;
  1306. y_t_2 = (y_2 + k2 *x_o_1 + y_o_1*k2*k2 - k2*x_2) / (1 + k2*k2);
  1307. x_t_2 = (y_t_2 + k2*x_2 - y_2) / k2;
  1308. y_t_3 = (y_1 + k1 *x_o_2 + y_o_2*k1*k1 - k1*x_1) / (1 + k1*k1);
  1309. x_t_3 = (y_t_3 + k1*x_1 - y_1) / k1;
  1310. y_t_4 = (y_2 + k2 *x_o_2 + y_o_2*k2*k2 - k2*x_2) / (1 + k2*k2);
  1311. x_t_4 = (y_t_4 + k2*x_2 - y_2) / k2;
  1312. }
  1313. }
  1314. //圆心1和2切点*********************************************
  1315. t[0][0] = x_t_1;
  1316. t[0][1] = y_t_1;
  1317. t[1][0] = x_t_2;
  1318. t[1][1] = y_t_2;
  1319. t[2][0] = x_t_3;
  1320. t[2][1] = y_t_3;
  1321. t[3][0] = x_t_4;
  1322. t[3][1] = y_t_4;
  1323. for (int i = 0; i < 4; i++)
  1324. {
  1325. p[i] = (t[i][0] - parking_pos[0])*(t[i][0] - parking_pos[0]) + (t[i][1] - parking_pos[1])*(t[i][1] - parking_pos[1]);
  1326. }
  1327. min = p[0];
  1328. min_i = 0;
  1329. for (int i = 1; i < 4; i++)
  1330. {
  1331. if (p[i] < min)
  1332. {
  1333. min = p[i]; min_i = i;
  1334. }
  1335. }
  1336. if (min_i < 2)
  1337. {
  1338. x_o = x_o_1;
  1339. y_o = y_o_1;
  1340. s1 = (x_t_1 - x_1)*(x_t_1 - x_1) + (y_t_1 - y_1)*(y_t_1 - y_1);
  1341. s2 = (x_t_2 - x_1)*(x_t_2 - x_1) + (y_t_2 - y_1)*(y_t_2 - y_1);
  1342. if (s1 < s2)
  1343. {
  1344. *x_t_n = x_t_1;
  1345. *y_t_n = y_t_1;
  1346. *x_t_f = x_t_2;
  1347. *y_t_f = y_t_2;
  1348. }
  1349. else
  1350. {
  1351. *x_t_n = x_t_2;
  1352. *y_t_n = y_t_2;
  1353. *x_t_f = x_t_1;
  1354. *y_t_f = y_t_1;
  1355. }
  1356. }
  1357. else
  1358. {
  1359. x_o = x_o_2;
  1360. y_o = y_o_2;
  1361. s1 = (x_t_3 - x_1)*(x_t_3 - x_1) + (y_t_3 - y_1)*(y_t_3 - y_1);
  1362. s2 = (x_t_4 - x_1)*(x_t_4 - x_1) + (y_t_4 - y_1)*(y_t_4 - y_1);
  1363. if (s1 < s2)
  1364. {
  1365. *x_t_n = x_t_3;
  1366. *y_t_n = y_t_3;
  1367. *x_t_f = x_t_4;
  1368. *y_t_f = y_t_4;
  1369. }
  1370. else
  1371. {
  1372. *x_t_n = x_t_4;
  1373. *y_t_n = y_t_4;
  1374. *x_t_f = x_t_3;
  1375. *y_t_f = y_t_3;
  1376. }
  1377. }
  1378. return 0;
  1379. }
  1380. int iv::decition::Compute00::getNoAngleNearestPointIndex(GPS_INS rp, const std::vector<GPSData> gpsMap)
  1381. {
  1382. int index = -1;
  1383. int startIndex = 0; // startIndex = 0 则每一次都是遍历整条地图路线
  1384. int endIndex = gpsMap.size() - 1;
  1385. float minDis=20;
  1386. for (int j = startIndex; j < endIndex; j++)
  1387. {
  1388. int i = (j + gpsMap.size()) % gpsMap.size();
  1389. double tmpdis = GetDistance(rp, (*gpsMap[i]));
  1390. if (tmpdis < minDis)
  1391. {
  1392. index = i;
  1393. minDis=tmpdis;
  1394. }
  1395. }
  1396. return index;
  1397. }
  1398. double iv::decition::Compute00::getObsSpeedByFrenet(Point2D obsPoint, double realSecSpeed,const vector<Point2D>& gpsTrace, const std::vector<iv::GPSData>& gpsMap,int pathpoint,GPS_INS nowGps) {
  1399. double obsSpeed = 0 - realSecSpeed;
  1400. double minDis = iv::MaxValue;
  1401. FrenetPoint esr_obs_F_point;
  1402. for (int i = 0; i < 64; i++)
  1403. if ((ServiceCarStatus.obs_radar[i].nomal_y) != 0 && ServiceCarStatus.obs_radar[i].valid)
  1404. {
  1405. double xxx = ServiceCarStatus.obs_radar[i].nomal_x + Esr_Offset;
  1406. double yyy = ServiceCarStatus.obs_radar[i].nomal_y + Esr_Y_Offset;
  1407. if (abs(xxx - obsPoint.x) < 4 && abs(yyy - obsPoint.y) < 2)
  1408. {
  1409. double tmpDis = sqrt((xxx - obsPoint.x) * (xxx - obsPoint.x) + (yyy - obsPoint.y) * (yyy - obsPoint.y));
  1410. if (tmpDis < minDis)
  1411. {
  1412. minDis = tmpDis;
  1413. // esr_obs_F_point = iv::decition::FrenetPlanner::XY2Frenet(xxx, yyy, gpsTrace);
  1414. esr_obs_F_point = iv::decition::FrenetPlanner::getFrenetfromXY(xxx, yyy, gpsTrace,gpsMap,pathpoint,nowGps);
  1415. // obsSpeed = ServiceCarStatus.obs_radar[i].speed_y;
  1416. double speedx=ServiceCarStatus.obs_radar[i].speed_x; //障碍物相对于车辆x轴的速度
  1417. double speedy=ServiceCarStatus.obs_radar[i].speed_y; //障碍物相对于车辆y轴的速度
  1418. double speed_combine = sqrt(speedx*speedx+speedy*speedy); //将x、y轴两个方向的速度求矢量和
  1419. //障碍物的速度方向与道路方向的夹角。用于将速度分解到s方向和d方向。
  1420. //所谓道路方向是指,道路上离障碍物的最近的点沿道路弧线的切线方向。
  1421. double Etheta = esr_obs_F_point.tangent_Ang - atan2(speedy,speedx);
  1422. obsSpeed = speed_combine*cos(Etheta); //由speed_combine分解的s轴方向上的速度
  1423. }
  1424. }
  1425. }
  1426. return obsSpeed;
  1427. }
  1428. int iv::decition::Compute00::getEsrIndexByFrenet(const std::vector<Point2D>& gpsTrace, FrenetPoint &esrObsPoint, const std::vector<iv::GPSData>& gpsMap,int pathpoint,GPS_INS nowGps){
  1429. double minDistance = numeric_limits<double>::max();
  1430. int minDis_index=-1;
  1431. for(int i=0; i<64; ++i){
  1432. if ((ServiceCarStatus.obs_radar[i].nomal_y) != 0 && (ServiceCarStatus.obs_radar[i].valid)){
  1433. //毫米波在车头,故要加上毫米波与惯导的相对距离。(xxx,yyy)才是障碍物在 车辆坐标系下的坐标。
  1434. double xxx = ServiceCarStatus.obs_radar[i].nomal_x + Esr_Offset;
  1435. double yyy = ServiceCarStatus.obs_radar[i].nomal_y+ Esr_Y_Offset;
  1436. //将毫米波障碍物位置转换到frenet坐标系下
  1437. // esrObsPoint = iv::decition::FrenetPlanner::XY2Frenet(xxx,yyy,gpsTrace);
  1438. esrObsPoint = iv::decition::FrenetPlanner::getFrenetfromXY(xxx,yyy,gpsTrace,gpsMap,pathpoint,nowGps);
  1439. //如果障碍物与道路的横向距离d<=3.0*Veh_Width / 4.0,则认为道路上有障碍物。
  1440. //s则可理解为障碍物距离。为障碍物与车辆沿着道路方向的距离,而不是空间上的x或y坐标或者直线距离。
  1441. //minDistance、minDis_index用来统计最近的障碍物信息。
  1442. if(abs(esrObsPoint.d)<=(3.0*Veh_Width / 4.0+DecideGps00().xiuzhengCs)){
  1443. if(esrObsPoint.s<minDistance){
  1444. minDistance = esrObsPoint.s;
  1445. minDis_index = i;
  1446. }
  1447. }
  1448. }
  1449. }
  1450. return minDis_index;
  1451. }
  1452. std::vector<std::vector<iv::GPSData>> gmapsL;
  1453. std::vector<std::vector<iv::GPSData>> gmapsR;