6 bool PSPlanner::collide()
8 std::vector<std::tuple<double, double>> bc;
9 bc.push_back(std::make_tuple(this->cc().lfx(), this->cc().lfy()));
10 bc.push_back(std::make_tuple(this->cc().lrx(), this->cc().lry()));
11 bc.push_back(std::make_tuple(this->cc().rrx(), this->cc().rry()));
12 bc.push_back(std::make_tuple(this->cc().rfx(), this->cc().rfy()));
13 bc.push_back(std::make_tuple(this->cc().lfx(), this->cc().lfy()));
14 std::vector<std::tuple<double, double>> ps;
15 ps.push_back(std::make_tuple(this->ps().x1(), this->ps().y1()));
16 ps.push_back(std::make_tuple(this->ps().x2(), this->ps().y2()));
17 ps.push_back(std::make_tuple(this->ps().x3(), this->ps().y3()));
18 ps.push_back(std::make_tuple(this->ps().x4(), this->ps().y4()));
19 return std::get<0>(::collide(bc, ps));
22 bool PSPlanner::forward()
24 #if FORWARD_PARKING > 0
31 void PSPlanner::gc_to_4()
33 double angl_slot = atan2(
34 this->ps().y3() - this->ps().y4(),
35 this->ps().x3() - this->ps().x4()
37 double angl_delta = M_PI / 2;
38 if (this->ps().right())
39 angl_delta = -M_PI / 2;
40 double x = this->ps().x4();
41 double y = this->ps().y4();
42 x += (this->gc().dr() + 0.01) * cos(angl_slot);
43 y += (this->gc().dr() + 0.01) * sin(angl_slot);
44 x += (this->gc().w() / 2 + 0.01) * cos(angl_slot + angl_delta);
45 y += (this->gc().w() / 2 + 0.01) * sin(angl_slot + angl_delta);
48 this->gc().h(angl_slot);
51 std::tuple<double, double, double, double> circle_line_intersection(
52 double cx, double cy, double r,
57 double t = (y2 - y1) / (x2 - x1);
58 //double a = 1 + pow(t, 2);
59 //double b = - 2 * cx - 2 * pow(t, 2) * x1 + 2 * t * y1 - 2 * t * cy;
60 //double c = pow(cx, 2) + pow(t, 2) * pow(x1, 2) - 2 * t * y1 * x1
61 // + pow(y1, 2) + 2 * t * cy * x1 - 2 * y1 * cy + pow(cy, 2)
63 double a = 1 + pow(t, 2);
64 double b = - 2 * cx + 2 * t * (-t * x1 + y1) - 2 * cy * t;
65 double c = pow(cx, 2) + pow(cy, 2) - pow(r, 2);
66 c += pow(-t * x1 + y1, 2);
67 c += 2 * cy * t * x1 - 2 * cy * y1;
68 double D = pow(b, 2) - 4 * a * c;
70 return std::make_tuple(cx, cy, cx, cy);
71 double res_x1 = (-b + sqrt(D)) / (2 * a);
72 double res_y1 = t * (res_x1 - x1) + y1;
73 double res_x2 = (-b - sqrt(D)) / (2 * a);
74 double res_y2 = t * (res_x2 - x1) + y1;
75 return std::make_tuple(res_x1, res_y1, res_x2, res_y2);
78 double edist(double x1, double y1, double x2, double y2)
80 return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
83 void PSPlanner::guess_gc()
85 double x = this->ps().x1();
86 double y = this->ps().y1();
87 double h = this->ps().heading();
88 double dts = + M_PI / 2; // direction to slot
89 if (this->ps().right())
91 if (this->ps().parallel()) {
92 x += (this->gc().w() / 2 + 0.01) * cos(h + dts);
93 x += (this->gc().dr() + 0.01) * cos(h);
94 y += (this->gc().w() / 2 + 0.01) * sin(h + dts);
95 y += (this->gc().dr() + 0.01) * sin(h);
97 #if FORWARD_PARKING > 0
99 double entry_width = edist(
100 this->ps().x1(), this->ps().y1(),
101 this->ps().x4(), this->ps().y4()
103 x += entry_width / 2 * cos(h);
104 y += entry_width / 2 * sin(h);
106 this->ps().y2() - this->ps().y1(),
107 this->ps().x2() - this->ps().x1()
109 while (h < 0) h += 2 * M_PI;
110 x += this->gc().dr() * cos(h + M_PI);
111 y += this->gc().dr() * sin(h + M_PI);
114 double entry_width = edist(
115 this->ps().x1(), this->ps().y1(),
116 this->ps().x4(), this->ps().y4()
118 x += entry_width / 2 * cos(h);
119 y += entry_width / 2 * sin(h);
121 this->ps().y1() - this->ps().y2(),
122 this->ps().x1() - this->ps().x2()
124 while (h < 0) h += 2 * M_PI;
125 x += this->gc().df() * cos(h + M_PI);
126 y += this->gc().df() * sin(h + M_PI);
138 std::vector<BicycleCar> PSPlanner::last_maneuver()
140 std::vector<BicycleCar> lm;
141 if (this->ps().parallel()) {
142 // zig-zag out from the slot
143 this->cc() = BicycleCar(this->gc());
145 while (!this->left()) {
146 while (!this->collide() && !this->left()) {
148 lm.push_back(BicycleCar(this->cc()));
150 if (this->left() && !this->collide()) {
154 this->cc().sp(this->cc().sp() * -1);
156 this->cc().st(this->cc().st() * -1);
158 lm.push_back(BicycleCar(this->cc()));
161 if (this->cc().st() < 0) {
163 lm.push_back(BicycleCar(this->cc()));
168 BicycleCar orig_cc(this->cc());
169 for (unsigned int i = 0; i < 10; i++) {
171 lm.push_back(BicycleCar(this->cc()));
173 this->cc() = BicycleCar(orig_cc);
178 bool PSPlanner::left()
180 double lfx = this->cc().lfx();
181 double lfy = this->cc().lfy();
182 double lrx = this->cc().lrx();
183 double lry = this->cc().lry();
184 double rrx = this->cc().rrx();
185 double rry = this->cc().rry();
186 double rfx = this->cc().rfx();
187 double rfy = this->cc().rfy();
189 (lfx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
190 - (lfy - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
193 (lrx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
194 - (lry - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
197 (rrx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
198 - (rry - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
201 (rfx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
202 - (rfy - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
204 if (this->ps().parallel())
205 return lfs == rfs && (lfs != lrs || lfs != rrs);
206 else if (!this->forward())
207 return lfs == rfs && (lfs != lrs || lfs != rrs);
209 return lrs == rrs && (lrs != lfs || lrs != rfs);
212 bool PSPlanner::parked()
214 std::vector<std::tuple<double, double>> slot;
215 slot.push_back(std::make_tuple(this->ps().x1(), this->ps().y1()));
216 slot.push_back(std::make_tuple(this->ps().x2(), this->ps().y2()));
217 slot.push_back(std::make_tuple(this->ps().x3(), this->ps().y3()));
218 slot.push_back(std::make_tuple(this->ps().x4(), this->ps().y4()));
219 return inside(this->gc().lfx(), this->gc().lfy(), slot)
220 && inside(this->gc().lrx(), this->gc().lry(), slot)
221 && inside(this->gc().rrx(), this->gc().rry(), slot)
222 && inside(this->gc().rfx(), this->gc().rfy(), slot);
225 std::vector<BicycleCar> PSPlanner::possible_goals(
230 std::vector<BicycleCar> pi;
231 if (this->ps().parallel())
235 this->cc().sp(this->cc().sp() * dist);
236 BicycleCar orig_cc(this->cc());
237 for (unsigned int i = 0; i < cnt; i++) {
239 pi.push_back(BicycleCar(this->cc()));
241 this->cc() = BicycleCar(orig_cc);
242 if (this->ps().parallel()) {
244 for (unsigned int i = 0; i < cnt; i++) {
246 pi.push_back(BicycleCar(this->cc()));
248 this->cc() = BicycleCar(orig_cc);
250 if (!this->ps().right()) {
251 this->cc().set_max_steer();
252 for (unsigned int i = 0; i < cnt; i++) {
254 pi.push_back(BicycleCar(this->cc()));
257 this->cc().set_max_steer();
258 this->cc().st(this->cc().st() * -1);
259 for (unsigned int i = 0; i < cnt; i++) {
261 pi.push_back(BicycleCar(this->cc()));
264 this->cc() = BicycleCar(orig_cc);
269 void PSPlanner::shrink_to_perfect_len()
271 if (!this->ps().parallel())
273 double perfect_len = this->gc().perfect_parking_slot_len();
275 this->ps().x1(), this->ps().y1(),
276 this->ps().x4(), this->ps().y4()
279 double h = this->ps().heading();
281 while (h < 0) h += 2 * M_PI;
282 double ch = perfect_len * cos(h);
283 double sh = perfect_len * sin(h);
285 this->ps().x4() + ch, this->ps().y4() + sh,
286 this->ps().x3() + ch, this->ps().y3() + sh,
287 this->ps().x3(), this->ps().y3(),
288 this->ps().x4(), this->ps().y4()
296 if (this->ps().parallel()) {
297 return this->fe_parallel();
300 this->cc() = BicycleCar(this->gc());
301 //this->cc().set_max_steer();
302 //if (this->ps().right())
303 // this->cc().st(this->cc().st() * -1);
308 double angle_between_closer_point(
309 double sx, double sy, // common start point
310 double cx, double cy, // common middle point
311 double x1, double y1, // first ending point
312 double x2, double y2 // second ending point
314 if (edist(sx, sy, x1, y1) < edist(sx, sy, x2, y2))
315 return ::angle_between_three_points(sx, sy, cx, cy, x1, y1);
317 return ::angle_between_three_points(sx, sy, cx, cy, x2, y2);
320 void PSPlanner::fe_parallel()
322 this->shrink_to_perfect_len();
323 BicycleCar bco = BicycleCar(this->gc());
324 this->cc() = BicycleCar();
325 this->cc().sp(-0.01);
326 this->cc().set_max_steer();
327 if (!this->ps().right())
328 this->cc().st(this->cc().st() * -1);
329 this->cc().h(this->ps().heading());
330 double angl_in_slot = this->ps().heading() - M_PI / 4;
331 if (!this->ps().right())
332 angl_in_slot += M_PI / 2;
335 + this->cc().w()/2 * cos(
337 + (this->ps().right() ? + M_PI / 2 : - M_PI / 2)
339 + (this->cc().df() + 0.01) * cos(
340 this->ps().heading() + M_PI
345 + this->cc().w()/2 * sin(
347 + (this->ps().right() ? + M_PI / 2 : - M_PI / 2)
349 + (this->cc().df() + 0.01) * sin(
350 this->ps().heading() + M_PI
354 std::queue<BicycleCar, std::list<BicycleCar>> q;
355 while (!this->collide()) {
359 this->ps().y4() - 0.01,
360 ((this->ps().right()) ? 0.001 : -0.001)
363 // BFS - find entry current car `cc` and corresponding goal car `gc`
364 unsigned int iter_cntr = 0;
365 while (!q.empty() && iter_cntr < 30) {
366 this->cc() = BicycleCar(q.front());
368 if (this->ps().right() && this->cc().sp() < 0) {
369 double cclx = this->cc().ccl().x();
370 double ccly = this->cc().ccl().y();
371 double ccl_lr = edist(
373 this->cc().lrx(), this->cc().lry()
375 double ccl_rr = edist(
377 this->cc().rrx(), this->cc().rry()
379 double ccl_p1 = edist(
381 this->ps().x1(), this->ps().y1()
383 if (ccl_rr < ccl_p1) {
386 } else if (ccl_rr >= ccl_p1 && ccl_lr < ccl_p1) {
387 // partially out of parking slot
388 auto cli1 = ::intersect(
390 this->cc().lrx(), this->cc().lry(),
391 this->cc().rrx(), this->cc().rry()
393 double a1 = ::angle_between_closer_point(
394 this->ps().x1(), this->ps().y1(),
396 std::get<1>(cli1), std::get<2>(cli1),
397 std::get<3>(cli1), std::get<4>(cli1)
399 auto cli2 = ::intersect(
401 this->ps().x2(), this->ps().y2(),
402 this->ps().x3(), this->ps().y3()
404 double a2 = angle_between_closer_point(
405 this->cc().rrx(), this->cc().rry(),
407 std::get<1>(cli2), std::get<2>(cli2),
408 std::get<3>(cli2), std::get<4>(cli2)
410 if (std::get<0>(cli1) && (
414 this->cc().rotate(cclx, ccly, -a1);
415 if (::right_side_of_line(
416 this->cc().x(), this->cc().y(),
419 + cos(this->ps().heading()),
421 + sin(this->ps().heading()),
424 + cos(this->cc().h()),
426 + sin(this->cc().h())
430 } else if (std::get<0>(cli2) && (
434 this->cc().rotate(cclx, ccly, -a2);
438 } else if (ccl_lr >= ccl_p1) {
440 auto cli1 = ::intersect(
442 this->ps().x1(), this->ps().y1(),
443 this->ps().x2(), this->ps().y2()
445 double a1 = angle_between_closer_point(
446 this->cc().lrx(), this->cc().lry(),
448 std::get<1>(cli1), std::get<2>(cli1),
449 std::get<3>(cli1), std::get<4>(cli1)
451 auto cli2 = ::intersect(
453 this->ps().x2(), this->ps().y2(),
454 this->ps().x3(), this->ps().y3()
456 double a2 = angle_between_closer_point(
457 this->cc().rrx(), this->cc().rry(),
459 std::get<1>(cli2), std::get<2>(cli2),
460 std::get<3>(cli2), std::get<4>(cli2)
462 if (std::get<0>(cli1) && (
466 this->cc().rotate(cclx, ccly, -a1);
467 if (::right_side_of_line(
468 this->cc().x(), this->cc().y(),
471 + cos(this->ps().heading()),
473 + sin(this->ps().heading()),
476 + cos(this->cc().h()),
478 + sin(this->cc().h())
482 } else if (std::get<0>(cli2) && (
486 this->cc().rotate(cclx, ccly, -a2);
491 } else if (this->ps().right() && this->cc().sp() > 0) {
492 double ccrx = this->cc().ccr().x();
493 double ccry = this->cc().ccr().y();
494 double ccr_lf = edist(
496 this->cc().lfx(), this->cc().lfy()
498 double ccr_rf = edist(
500 this->cc().rfx(), this->cc().rfy()
503 double af = std::abs(
507 auto tmp_cc = BicycleCar(this->cc());
508 this->cc().rotate(ccrx, ccry, -af);
509 this->gc() = BicycleCar(this->cc());
514 this->cc().sp(this->cc().sp() * -1);
515 this->gc() = BicycleCar(this->cc());
518 this->cc() = BicycleCar(tmp_cc);
521 auto cli1 = ::intersect(
523 this->ps().x3(), this->ps().y3(),
524 this->ps().x4(), this->ps().y4()
526 double a1 = angle_between_closer_point(
527 this->cc().rfx(), this->cc().rfy(),
529 std::get<1>(cli1), std::get<2>(cli1),
530 std::get<3>(cli1), std::get<4>(cli1)
532 auto cli2 = ::intersect(
534 this->ps().x2(), this->ps().y2(),
535 this->ps().x3(), this->ps().y3()
537 double a2 = angle_between_closer_point(
538 this->cc().rfx(), this->cc().rfy(),
540 std::get<1>(cli2), std::get<2>(cli2),
541 std::get<3>(cli2), std::get<4>(cli2)
543 auto cli3 = ::intersect(
545 this->ps().x3(), this->ps().y3(),
546 this->ps().x4(), this->ps().y4()
548 double a3 = angle_between_closer_point(
549 this->cc().lfx(), this->cc().lfy(),
551 std::get<1>(cli3), std::get<2>(cli3),
552 std::get<3>(cli3), std::get<4>(cli3)
554 if (std::get<0>(cli1) && (
555 (!std::get<0>(cli2) && !std::get<0>(cli3))
556 || (a1 < a2 && !std::get<0>(cli3))
557 || (a1 < a3 && !std::get<0>(cli2))
558 || (a1 < a2 && a1 < a3)
560 this->cc().rotate(ccrx, ccry, -a1);
561 } else if (std::get<0>(cli2) && (
562 (!std::get<0>(cli1) && !std::get<0>(cli3))
563 || (a2 < a1 && !std::get<0>(cli3))
564 || (a2 < a3 && !std::get<0>(cli1))
565 || (a2 < a1 && a2 < a3)
567 this->cc().rotate(ccrx, ccry, -a2);
568 } else if (std::get<0>(cli3) && (
569 (!std::get<0>(cli1) && !std::get<0>(cli2))
570 || (a3 < a1 && !std::get<0>(cli2))
571 || (a3 < a2 && !std::get<0>(cli1))
572 || (a3 < a1 && a3 < a2)
574 this->cc().rotate(ccrx, ccry, -a3);
578 } else if (!this->ps().right() && this->cc().sp() < 0) {
579 double ccrx = this->cc().ccr().x();
580 double ccry = this->cc().ccr().y();
581 double ccr_rr = edist(
583 this->cc().rrx(), this->cc().rry()
585 double ccr_lr = edist(
587 this->cc().lrx(), this->cc().lry()
589 double ccr_p1 = edist(
591 this->ps().x1(), this->ps().y1()
593 if (ccr_lr < ccr_p1) {
596 } else if (ccr_lr >= ccr_p1 && ccr_rr < ccr_p1) {
597 // partially out of parking slot
598 auto cli1 = ::intersect(
600 this->cc().lrx(), this->cc().lry(),
601 this->cc().rrx(), this->cc().rry()
603 double a1 = ::angle_between_closer_point(
604 this->ps().x1(), this->ps().y1(),
606 std::get<1>(cli1), std::get<2>(cli1),
607 std::get<3>(cli1), std::get<4>(cli1)
609 auto cli2 = ::intersect(
611 this->ps().x2(), this->ps().y2(),
612 this->ps().x3(), this->ps().y3()
614 double a2 = angle_between_closer_point(
615 this->cc().lrx(), this->cc().lry(),
617 std::get<1>(cli2), std::get<2>(cli2),
618 std::get<3>(cli2), std::get<4>(cli2)
620 if (std::get<0>(cli1) && (
624 this->cc().rotate(ccrx, ccry, a1);
625 if (!::right_side_of_line(
626 this->cc().x(), this->cc().y(),
629 + cos(this->ps().heading()),
631 + sin(this->ps().heading()),
634 + cos(this->cc().h()),
636 + sin(this->cc().h())
640 } else if (std::get<0>(cli2) && (
644 this->cc().rotate(ccrx, ccry, a2);
648 } else if (ccr_rr >= ccr_p1) {
650 auto cli1 = ::intersect(
652 this->ps().x1(), this->ps().y1(),
653 this->ps().x2(), this->ps().y2()
655 double a1 = angle_between_closer_point(
656 this->cc().rrx(), this->cc().rry(),
658 std::get<1>(cli1), std::get<2>(cli1),
659 std::get<3>(cli1), std::get<4>(cli1)
661 auto cli2 = ::intersect(
663 this->ps().x2(), this->ps().y2(),
664 this->ps().x3(), this->ps().y3()
666 double a2 = angle_between_closer_point(
667 this->cc().lrx(), this->cc().lry(),
669 std::get<1>(cli2), std::get<2>(cli2),
670 std::get<3>(cli2), std::get<4>(cli2)
672 if (std::get<0>(cli1) && (
676 this->cc().rotate(ccrx, ccry, a1);
677 if (!::right_side_of_line(
678 this->cc().x(), this->cc().y(),
681 + cos(this->ps().heading()),
683 + sin(this->ps().heading()),
686 + cos(this->cc().h()),
688 + sin(this->cc().h())
692 } else if (std::get<0>(cli2) && (
696 this->cc().rotate(ccrx, ccry, a2);
701 } else if (!this->ps().right() && this->cc().sp() > 0) {
702 double cclx = this->cc().ccl().x();
703 double ccly = this->cc().ccl().y();
704 double ccl_rf = edist(
706 this->cc().rfx(), this->cc().rfy()
708 double ccl_lf = edist(
710 this->cc().lfx(), this->cc().lfy()
713 double af = std::abs(
717 auto tmp_cc = BicycleCar(this->cc());
718 this->cc().rotate(cclx, ccly, af);
719 this->gc() = BicycleCar(this->cc());
724 this->cc().sp(this->cc().sp() * -1);
725 this->gc() = BicycleCar(this->cc());
728 this->cc() = BicycleCar(tmp_cc);
731 auto cli1 = ::intersect(
733 this->ps().x3(), this->ps().y3(),
734 this->ps().x4(), this->ps().y4()
736 double a1 = angle_between_closer_point(
737 this->cc().lfx(), this->cc().lfy(),
739 std::get<1>(cli1), std::get<2>(cli1),
740 std::get<3>(cli1), std::get<4>(cli1)
742 auto cli2 = ::intersect(
744 this->ps().x2(), this->ps().y2(),
745 this->ps().x3(), this->ps().y3()
747 double a2 = angle_between_closer_point(
748 this->cc().lfx(), this->cc().lfy(),
750 std::get<1>(cli2), std::get<2>(cli2),
751 std::get<3>(cli2), std::get<4>(cli2)
753 auto cli3 = ::intersect(
755 this->ps().x3(), this->ps().y3(),
756 this->ps().x4(), this->ps().y4()
758 double a3 = angle_between_closer_point(
759 this->cc().rfx(), this->cc().rfy(),
761 std::get<1>(cli3), std::get<2>(cli3),
762 std::get<3>(cli3), std::get<4>(cli3)
764 if (std::get<0>(cli1) && (
765 (!std::get<0>(cli2) && !std::get<0>(cli3))
766 || (a1 < a2 && !std::get<0>(cli3))
767 || (a1 < a3 && !std::get<0>(cli2))
768 || (a1 < a2 && a1 < a3)
770 this->cc().rotate(cclx, ccly, a1);
771 } else if (std::get<0>(cli2) && (
772 (!std::get<0>(cli1) && !std::get<0>(cli3))
773 || (a2 < a1 && !std::get<0>(cli3))
774 || (a2 < a3 && !std::get<0>(cli1))
775 || (a2 < a1 && a2 < a3)
777 this->cc().rotate(cclx, ccly, a2);
778 } else if (std::get<0>(cli3) && (
779 (!std::get<0>(cli1) && !std::get<0>(cli2))
780 || (a3 < a1 && !std::get<0>(cli2))
781 || (a3 < a2 && !std::get<0>(cli1))
782 || (a3 < a1 && a3 < a2)
784 this->cc().rotate(cclx, ccly, a3);
789 // TODO left parking slot (both forward, backward)
791 this->cc().sp(this->cc().sp() * -1);
793 this->gc() = BicycleCar(this->cc());
796 this->cc().st(this->cc().st() * -1);
797 q.push(BicycleCar(this->cc()));
798 if (sgn(this->cc().st()) == sgn(q.front().st()))
802 this->gc() = BicycleCar(bco);
804 return this->fer_parallel();
807 void PSPlanner::fe_perpendicular()
809 // TODO Try multiple angles when going from parking slot.
811 // Do not use just the maximum steer angle. Test angles
812 // until the whole current car `cc` is out of the parking
815 // Another approach could be testing angles from the
816 // beginning of the escape parkig slot maneuver.
818 this->cc().sp(-0.01);
821 while (!this->left())
826 void PSPlanner::fer()
829 if (this->ps().parallel()) {
831 this->cc() = BicycleCar(this->gc());
832 this->cc().set_max_steer();
833 if (!this->ps().right())
834 this->cc().st(this->cc().st() * -1);
836 return this->fer_parallel();
838 return this->fer_perpendicular();
842 void PSPlanner::fer_parallel()
844 this->cusps_.clear();
845 while (!this->left()) {
846 while (!this->collide() && !this->left())
848 if (this->left() && !this->collide()) {
851 this->cc().sp(this->cc().sp() * -1);
853 this->cc().st(this->cc().st() * -1);
855 this->cusps_.push_back(this->cc());
858 if (this->cc().st() < 0) {
860 this->cusps_.push_back(this->cc());
864 void PSPlanner::fer_perpendicular()
866 bool delta_use[] = {true, true, true};
867 double cc_h = this->cc().h();
870 // check inner radius
871 if (this->forward()) {
880 if (this->ps().right()) {
881 x1 = this->cc().ccr().x();
882 y1 = this->cc().ccr().y();
884 x1 = this->cc().ccl().x();
885 y1 = this->cc().ccl().y();
887 double IR = this->cc().iradi();
891 b = (x - x1) * 2 * cos(cc_h) + (y - y1) * 2 * sin(cc_h);
893 b = (x1 - x) * 2 * cos(cc_h) + (y1 - y) * 2 * sin(cc_h);
894 double c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
895 double D = pow(b, 2) - 4 * a * c;
897 delta = -b - sqrt(D);
899 double delta_1 = delta;
901 delta_use[0] = false;
902 // check outer radius
903 if (this->forward()) {
910 IR = this->cc().ofradi();
913 b = (x - x1) * 2 * cos(cc_h) + (y - y1) * 2 * sin(cc_h);
915 b = (x1 - x) * 2 * cos(cc_h) + (y1 - y) * 2 * sin(cc_h);
916 c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
917 D = pow(b, 2) - 4 * a * c;
918 if (this->forward()) {
919 delta = -b + sqrt(D);
922 double delta_2 = delta;
924 delta_use[1] = false;
925 delta = -b - sqrt(D);
927 double delta_3 = delta;
929 delta_use[2] = false;
930 if (delta_use[0] && delta_use[1] && delta_use[2])
931 delta = std::max(delta_1, std::max(delta_2, delta_3));
932 else if (delta_use[0] && delta_use[1])
933 delta = std::max(delta_1, delta_2);
934 else if (delta_use[0] && delta_use[2])
935 delta = std::max(delta_1, delta_3);
936 else if (delta_use[1] && delta_use[2])
937 delta = std::max(delta_2, delta_3);
938 else if (delta_use[0])
940 else if (delta_use[1])
942 else if (delta_use[2])
946 // current car `cc` can get out of slot with max steer
947 this->cc().x(this->cc().x() + delta * cos(cc_h));
948 this->cc().y(this->cc().y() + delta * sin(cc_h));
950 // get current car `cc` out of slot
952 this->cc().sp(-0.01);
955 this->cc().set_max_steer();
956 if (this->ps().right())
957 this->cc().st(this->cc().st() * -1);
958 while (!this->left()) {
959 while (!this->collide() && !this->left())
961 if (this->left() && !this->collide()) {
964 this->cc().sp(this->cc().sp() * -1);
966 this->cc().st(this->cc().st() * -1);
971 PSPlanner::PSPlanner()