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 += 2 * this->gc().dr() * cos(h);
111 y += 2 * this->gc().dr() * sin(h);
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 lm.push_back(BicycleCar(this->cc()));
146 while (!this->left()) {
147 while (!this->collide() && !this->left()) {
149 lm.push_back(BicycleCar(this->cc()));
151 if (this->left() && !this->collide()) {
155 this->cc().sp(this->cc().sp() * -1);
157 this->cc().st(this->cc().st() * -1);
159 lm.push_back(BicycleCar(this->cc()));
162 if (this->cc().st() < 0) {
164 lm.push_back(BicycleCar(this->cc()));
169 BicycleCar orig_cc(this->cc());
170 for (unsigned int i = 0; i < 10; i++) {
172 lm.push_back(BicycleCar(this->cc()));
174 this->cc() = BicycleCar(orig_cc);
179 bool PSPlanner::left()
181 double lfx = this->cc().lfx();
182 double lfy = this->cc().lfy();
183 double lrx = this->cc().lrx();
184 double lry = this->cc().lry();
185 double rrx = this->cc().rrx();
186 double rry = this->cc().rry();
187 double rfx = this->cc().rfx();
188 double rfy = this->cc().rfy();
190 (lfx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
191 - (lfy - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
194 (lrx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
195 - (lry - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
198 (rrx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
199 - (rry - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
202 (rfx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
203 - (rfy - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
205 if (this->ps().parallel())
206 return lfs == rfs && (lfs != lrs || lfs != rrs);
207 else if (!this->forward())
208 return lfs == rfs && (lfs != lrs || lfs != rrs);
210 return lrs == rrs && (lrs != lfs || lrs != rfs);
213 bool PSPlanner::parked()
215 std::vector<std::tuple<double, double>> slot;
216 slot.push_back(std::make_tuple(this->ps().x1(), this->ps().y1()));
217 slot.push_back(std::make_tuple(this->ps().x2(), this->ps().y2()));
218 slot.push_back(std::make_tuple(this->ps().x3(), this->ps().y3()));
219 slot.push_back(std::make_tuple(this->ps().x4(), this->ps().y4()));
220 return inside(this->gc().lfx(), this->gc().lfy(), slot)
221 && inside(this->gc().lrx(), this->gc().lry(), slot)
222 && inside(this->gc().rrx(), this->gc().rry(), slot)
223 && inside(this->gc().rfx(), this->gc().rfy(), slot);
226 std::vector<BicycleCar> PSPlanner::possible_goals(
231 std::vector<BicycleCar> pi;
232 if (this->ps().parallel())
236 this->cc().sp(this->cc().sp() * dist);
237 BicycleCar orig_cc(this->cc());
238 for (unsigned int i = 0; i < cnt; i++) {
240 pi.push_back(BicycleCar(this->cc()));
242 this->cc() = BicycleCar(orig_cc);
243 if (this->ps().parallel()) {
245 for (unsigned int i = 0; i < cnt; i++) {
247 pi.push_back(BicycleCar(this->cc()));
249 this->cc() = BicycleCar(orig_cc);
251 if (!this->ps().right()) {
252 this->cc().set_max_steer();
253 for (unsigned int i = 0; i < cnt; i++) {
255 pi.push_back(BicycleCar(this->cc()));
258 this->cc().set_max_steer();
259 this->cc().st(this->cc().st() * -1);
260 for (unsigned int i = 0; i < cnt; i++) {
262 pi.push_back(BicycleCar(this->cc()));
265 this->cc() = BicycleCar(orig_cc);
270 void PSPlanner::shrink_to_perfect_len()
272 if (!this->ps().parallel())
274 double perfect_len = this->gc().perfect_parking_slot_len();
276 this->ps().x1(), this->ps().y1(),
277 this->ps().x4(), this->ps().y4()
280 double h = this->ps().heading();
282 while (h < 0) h += 2 * M_PI;
283 double ch = perfect_len * cos(h);
284 double sh = perfect_len * sin(h);
286 this->ps().x4() + ch, this->ps().y4() + sh,
287 this->ps().x3() + ch, this->ps().y3() + sh,
288 this->ps().x3(), this->ps().y3(),
289 this->ps().x4(), this->ps().y4()
297 if (this->ps().parallel()) {
298 return this->fe_parallel();
301 this->cc() = BicycleCar(this->gc());
302 //this->cc().set_max_steer();
303 //if (this->ps().right())
304 // this->cc().st(this->cc().st() * -1);
309 double angle_between_closer_point(
310 double sx, double sy, // common start point
311 double cx, double cy, // common middle point
312 double x1, double y1, // first ending point
313 double x2, double y2 // second ending point
315 if (edist(sx, sy, x1, y1) < edist(sx, sy, x2, y2))
316 return ::angle_between_three_points(sx, sy, cx, cy, x1, y1);
318 return ::angle_between_three_points(sx, sy, cx, cy, x2, y2);
321 void PSPlanner::fe_parallel()
323 this->shrink_to_perfect_len();
324 BicycleCar bco = BicycleCar(this->gc());
325 this->cc() = BicycleCar();
326 this->cc().sp(-0.01);
327 this->cc().set_max_steer();
328 if (!this->ps().right())
329 this->cc().st(this->cc().st() * -1);
330 this->cc().h(this->ps().heading());
331 double angl_in_slot = this->ps().heading() - M_PI / 4;
332 if (!this->ps().right())
333 angl_in_slot += M_PI / 2;
336 + this->cc().w()/2 * cos(
338 + (this->ps().right() ? + M_PI / 2 : - M_PI / 2)
340 + (this->cc().df() + 0.01) * cos(
341 this->ps().heading() + M_PI
346 + this->cc().w()/2 * sin(
348 + (this->ps().right() ? + M_PI / 2 : - M_PI / 2)
350 + (this->cc().df() + 0.01) * sin(
351 this->ps().heading() + M_PI
355 std::queue<BicycleCar, std::list<BicycleCar>> q;
356 while (!this->collide()) {
360 this->ps().y4() - 0.01,
361 ((this->ps().right()) ? 0.001 : -0.001)
364 // BFS - find entry current car `cc` and corresponding goal car `gc`
365 unsigned int iter_cntr = 0;
366 while (!q.empty() && iter_cntr < 30) {
367 this->cc() = BicycleCar(q.front());
369 if (this->ps().right() && this->cc().sp() < 0) {
370 double cclx = this->cc().ccl().x();
371 double ccly = this->cc().ccl().y();
372 double ccl_lr = edist(
374 this->cc().lrx(), this->cc().lry()
376 double ccl_rr = edist(
378 this->cc().rrx(), this->cc().rry()
380 double ccl_p1 = edist(
382 this->ps().x1(), this->ps().y1()
384 if (ccl_rr < ccl_p1) {
387 } else if (ccl_rr >= ccl_p1 && ccl_lr < ccl_p1) {
388 // partially out of parking slot
389 auto cli1 = ::intersect(
391 this->cc().lrx(), this->cc().lry(),
392 this->cc().rrx(), this->cc().rry()
394 double a1 = ::angle_between_closer_point(
395 this->ps().x1(), this->ps().y1(),
397 std::get<1>(cli1), std::get<2>(cli1),
398 std::get<3>(cli1), std::get<4>(cli1)
400 auto cli2 = ::intersect(
402 this->ps().x2(), this->ps().y2(),
403 this->ps().x3(), this->ps().y3()
405 double a2 = angle_between_closer_point(
406 this->cc().rrx(), this->cc().rry(),
408 std::get<1>(cli2), std::get<2>(cli2),
409 std::get<3>(cli2), std::get<4>(cli2)
411 if (std::get<0>(cli1) && (
415 this->cc().rotate(cclx, ccly, -a1);
416 if (::right_side_of_line(
417 this->cc().x(), this->cc().y(),
420 + cos(this->ps().heading()),
422 + sin(this->ps().heading()),
425 + cos(this->cc().h()),
427 + sin(this->cc().h())
431 } else if (std::get<0>(cli2) && (
435 this->cc().rotate(cclx, ccly, -a2);
439 } else if (ccl_lr >= ccl_p1) {
441 auto cli1 = ::intersect(
443 this->ps().x1(), this->ps().y1(),
444 this->ps().x2(), this->ps().y2()
446 double a1 = angle_between_closer_point(
447 this->cc().lrx(), this->cc().lry(),
449 std::get<1>(cli1), std::get<2>(cli1),
450 std::get<3>(cli1), std::get<4>(cli1)
452 auto cli2 = ::intersect(
454 this->ps().x2(), this->ps().y2(),
455 this->ps().x3(), this->ps().y3()
457 double a2 = angle_between_closer_point(
458 this->cc().rrx(), this->cc().rry(),
460 std::get<1>(cli2), std::get<2>(cli2),
461 std::get<3>(cli2), std::get<4>(cli2)
463 if (std::get<0>(cli1) && (
467 this->cc().rotate(cclx, ccly, -a1);
468 if (::right_side_of_line(
469 this->cc().x(), this->cc().y(),
472 + cos(this->ps().heading()),
474 + sin(this->ps().heading()),
477 + cos(this->cc().h()),
479 + sin(this->cc().h())
483 } else if (std::get<0>(cli2) && (
487 this->cc().rotate(cclx, ccly, -a2);
492 } else if (this->ps().right() && this->cc().sp() > 0) {
493 double ccrx = this->cc().ccr().x();
494 double ccry = this->cc().ccr().y();
495 double ccr_lf = edist(
497 this->cc().lfx(), this->cc().lfy()
499 double ccr_rf = edist(
501 this->cc().rfx(), this->cc().rfy()
504 double af = std::abs(
508 auto tmp_cc = BicycleCar(this->cc());
509 this->cc().rotate(ccrx, ccry, -af);
510 this->gc() = BicycleCar(this->cc());
515 this->cc().sp(this->cc().sp() * -1);
516 this->gc() = BicycleCar(this->cc());
519 this->cc() = BicycleCar(tmp_cc);
522 auto cli1 = ::intersect(
524 this->ps().x3(), this->ps().y3(),
525 this->ps().x4(), this->ps().y4()
527 double a1 = angle_between_closer_point(
528 this->cc().rfx(), this->cc().rfy(),
530 std::get<1>(cli1), std::get<2>(cli1),
531 std::get<3>(cli1), std::get<4>(cli1)
533 auto cli2 = ::intersect(
535 this->ps().x2(), this->ps().y2(),
536 this->ps().x3(), this->ps().y3()
538 double a2 = angle_between_closer_point(
539 this->cc().rfx(), this->cc().rfy(),
541 std::get<1>(cli2), std::get<2>(cli2),
542 std::get<3>(cli2), std::get<4>(cli2)
544 auto cli3 = ::intersect(
546 this->ps().x3(), this->ps().y3(),
547 this->ps().x4(), this->ps().y4()
549 double a3 = angle_between_closer_point(
550 this->cc().lfx(), this->cc().lfy(),
552 std::get<1>(cli3), std::get<2>(cli3),
553 std::get<3>(cli3), std::get<4>(cli3)
555 if (std::get<0>(cli1) && (
556 (!std::get<0>(cli2) && !std::get<0>(cli3))
557 || (a1 < a2 && !std::get<0>(cli3))
558 || (a1 < a3 && !std::get<0>(cli2))
559 || (a1 < a2 && a1 < a3)
561 this->cc().rotate(ccrx, ccry, -a1);
562 } else if (std::get<0>(cli2) && (
563 (!std::get<0>(cli1) && !std::get<0>(cli3))
564 || (a2 < a1 && !std::get<0>(cli3))
565 || (a2 < a3 && !std::get<0>(cli1))
566 || (a2 < a1 && a2 < a3)
568 this->cc().rotate(ccrx, ccry, -a2);
569 } else if (std::get<0>(cli3) && (
570 (!std::get<0>(cli1) && !std::get<0>(cli2))
571 || (a3 < a1 && !std::get<0>(cli2))
572 || (a3 < a2 && !std::get<0>(cli1))
573 || (a3 < a1 && a3 < a2)
575 this->cc().rotate(ccrx, ccry, -a3);
579 } else if (!this->ps().right() && this->cc().sp() < 0) {
580 double ccrx = this->cc().ccr().x();
581 double ccry = this->cc().ccr().y();
582 double ccr_rr = edist(
584 this->cc().rrx(), this->cc().rry()
586 double ccr_lr = edist(
588 this->cc().lrx(), this->cc().lry()
590 double ccr_p1 = edist(
592 this->ps().x1(), this->ps().y1()
594 if (ccr_lr < ccr_p1) {
597 } else if (ccr_lr >= ccr_p1 && ccr_rr < ccr_p1) {
598 // partially out of parking slot
599 auto cli1 = ::intersect(
601 this->cc().lrx(), this->cc().lry(),
602 this->cc().rrx(), this->cc().rry()
604 double a1 = ::angle_between_closer_point(
605 this->ps().x1(), this->ps().y1(),
607 std::get<1>(cli1), std::get<2>(cli1),
608 std::get<3>(cli1), std::get<4>(cli1)
610 auto cli2 = ::intersect(
612 this->ps().x2(), this->ps().y2(),
613 this->ps().x3(), this->ps().y3()
615 double a2 = angle_between_closer_point(
616 this->cc().lrx(), this->cc().lry(),
618 std::get<1>(cli2), std::get<2>(cli2),
619 std::get<3>(cli2), std::get<4>(cli2)
621 if (std::get<0>(cli1) && (
625 this->cc().rotate(ccrx, ccry, a1);
626 if (!::right_side_of_line(
627 this->cc().x(), this->cc().y(),
630 + cos(this->ps().heading()),
632 + sin(this->ps().heading()),
635 + cos(this->cc().h()),
637 + sin(this->cc().h())
641 } else if (std::get<0>(cli2) && (
645 this->cc().rotate(ccrx, ccry, a2);
649 } else if (ccr_rr >= ccr_p1) {
651 auto cli1 = ::intersect(
653 this->ps().x1(), this->ps().y1(),
654 this->ps().x2(), this->ps().y2()
656 double a1 = angle_between_closer_point(
657 this->cc().rrx(), this->cc().rry(),
659 std::get<1>(cli1), std::get<2>(cli1),
660 std::get<3>(cli1), std::get<4>(cli1)
662 auto cli2 = ::intersect(
664 this->ps().x2(), this->ps().y2(),
665 this->ps().x3(), this->ps().y3()
667 double a2 = angle_between_closer_point(
668 this->cc().lrx(), this->cc().lry(),
670 std::get<1>(cli2), std::get<2>(cli2),
671 std::get<3>(cli2), std::get<4>(cli2)
673 if (std::get<0>(cli1) && (
677 this->cc().rotate(ccrx, ccry, a1);
678 if (!::right_side_of_line(
679 this->cc().x(), this->cc().y(),
682 + cos(this->ps().heading()),
684 + sin(this->ps().heading()),
687 + cos(this->cc().h()),
689 + sin(this->cc().h())
693 } else if (std::get<0>(cli2) && (
697 this->cc().rotate(ccrx, ccry, a2);
702 } else if (!this->ps().right() && this->cc().sp() > 0) {
703 double cclx = this->cc().ccl().x();
704 double ccly = this->cc().ccl().y();
705 double ccl_rf = edist(
707 this->cc().rfx(), this->cc().rfy()
709 double ccl_lf = edist(
711 this->cc().lfx(), this->cc().lfy()
714 double af = std::abs(
718 auto tmp_cc = BicycleCar(this->cc());
719 this->cc().rotate(cclx, ccly, af);
720 this->gc() = BicycleCar(this->cc());
725 this->cc().sp(this->cc().sp() * -1);
726 this->gc() = BicycleCar(this->cc());
729 this->cc() = BicycleCar(tmp_cc);
732 auto cli1 = ::intersect(
734 this->ps().x3(), this->ps().y3(),
735 this->ps().x4(), this->ps().y4()
737 double a1 = angle_between_closer_point(
738 this->cc().lfx(), this->cc().lfy(),
740 std::get<1>(cli1), std::get<2>(cli1),
741 std::get<3>(cli1), std::get<4>(cli1)
743 auto cli2 = ::intersect(
745 this->ps().x2(), this->ps().y2(),
746 this->ps().x3(), this->ps().y3()
748 double a2 = angle_between_closer_point(
749 this->cc().lfx(), this->cc().lfy(),
751 std::get<1>(cli2), std::get<2>(cli2),
752 std::get<3>(cli2), std::get<4>(cli2)
754 auto cli3 = ::intersect(
756 this->ps().x3(), this->ps().y3(),
757 this->ps().x4(), this->ps().y4()
759 double a3 = angle_between_closer_point(
760 this->cc().rfx(), this->cc().rfy(),
762 std::get<1>(cli3), std::get<2>(cli3),
763 std::get<3>(cli3), std::get<4>(cli3)
765 if (std::get<0>(cli1) && (
766 (!std::get<0>(cli2) && !std::get<0>(cli3))
767 || (a1 < a2 && !std::get<0>(cli3))
768 || (a1 < a3 && !std::get<0>(cli2))
769 || (a1 < a2 && a1 < a3)
771 this->cc().rotate(cclx, ccly, a1);
772 } else if (std::get<0>(cli2) && (
773 (!std::get<0>(cli1) && !std::get<0>(cli3))
774 || (a2 < a1 && !std::get<0>(cli3))
775 || (a2 < a3 && !std::get<0>(cli1))
776 || (a2 < a1 && a2 < a3)
778 this->cc().rotate(cclx, ccly, a2);
779 } else if (std::get<0>(cli3) && (
780 (!std::get<0>(cli1) && !std::get<0>(cli2))
781 || (a3 < a1 && !std::get<0>(cli2))
782 || (a3 < a2 && !std::get<0>(cli1))
783 || (a3 < a1 && a3 < a2)
785 this->cc().rotate(cclx, ccly, a3);
790 // TODO left parking slot (both forward, backward)
792 this->cc().sp(this->cc().sp() * -1);
794 this->gc() = BicycleCar(this->cc());
797 this->cc().st(this->cc().st() * -1);
798 q.push(BicycleCar(this->cc()));
799 if (sgn(this->cc().st()) == sgn(q.front().st()))
803 this->gc() = BicycleCar(bco);
805 return this->fer_parallel();
808 void PSPlanner::fe_perpendicular()
810 // TODO Try multiple angles when going from parking slot.
812 // Do not use just the maximum steer angle. Test angles
813 // until the whole current car `cc` is out of the parking
816 // Another approach could be testing angles from the
817 // beginning of the escape parkig slot maneuver.
819 this->cc().sp(-0.01);
822 while (!this->left())
827 void PSPlanner::fer()
830 if (this->ps().parallel()) {
832 this->cc() = BicycleCar(this->gc());
833 this->cc().set_max_steer();
834 if (!this->ps().right())
835 this->cc().st(this->cc().st() * -1);
837 return this->fer_parallel();
839 return this->fer_perpendicular();
843 void PSPlanner::fer_parallel()
845 this->cusps_.clear();
846 while (!this->left()) {
847 while (!this->collide() && !this->left())
849 if (this->left() && !this->collide()) {
852 this->cc().sp(this->cc().sp() * -1);
854 this->cc().st(this->cc().st() * -1);
856 this->cusps_.push_back(this->cc());
859 if (this->cc().st() < 0) {
861 this->cusps_.push_back(this->cc());
865 void PSPlanner::fer_perpendicular()
867 bool delta_use[] = {true, true, true};
868 double cc_h = this->cc().h();
871 // check inner radius
872 if (this->forward()) {
881 if (this->ps().right()) {
882 x1 = this->cc().ccr().x();
883 y1 = this->cc().ccr().y();
885 x1 = this->cc().ccl().x();
886 y1 = this->cc().ccl().y();
888 double IR = this->cc().iradi();
892 b = (x - x1) * 2 * cos(cc_h) + (y - y1) * 2 * sin(cc_h);
894 b = (x1 - x) * 2 * cos(cc_h) + (y1 - y) * 2 * sin(cc_h);
895 double c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
896 double D = pow(b, 2) - 4 * a * c;
898 delta = -b - sqrt(D);
900 double delta_1 = delta;
902 delta_use[0] = false;
903 // check outer radius
904 if (this->forward()) {
911 IR = this->cc().ofradi();
914 b = (x - x1) * 2 * cos(cc_h) + (y - y1) * 2 * sin(cc_h);
916 b = (x1 - x) * 2 * cos(cc_h) + (y1 - y) * 2 * sin(cc_h);
917 c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
918 D = pow(b, 2) - 4 * a * c;
919 if (this->forward()) {
920 delta = -b + sqrt(D);
923 double delta_2 = delta;
925 delta_use[1] = false;
926 delta = -b - sqrt(D);
928 double delta_3 = delta;
930 delta_use[2] = false;
931 if (delta_use[0] && delta_use[1] && delta_use[2])
932 delta = std::max(delta_1, std::max(delta_2, delta_3));
933 else if (delta_use[0] && delta_use[1])
934 delta = std::max(delta_1, delta_2);
935 else if (delta_use[0] && delta_use[2])
936 delta = std::max(delta_1, delta_3);
937 else if (delta_use[1] && delta_use[2])
938 delta = std::max(delta_2, delta_3);
939 else if (delta_use[0])
941 else if (delta_use[1])
943 else if (delta_use[2])
947 // current car `cc` can get out of slot with max steer
948 this->cc().x(this->cc().x() + delta * cos(cc_h));
949 this->cc().y(this->cc().y() + delta * sin(cc_h));
951 // get current car `cc` out of slot
953 this->cc().sp(-0.01);
956 this->cc().set_max_steer();
957 if (this->ps().right())
958 this->cc().st(this->cc().st() * -1);
959 while (!this->left()) {
960 while (!this->collide() && !this->left())
962 if (this->left() && !this->collide()) {
965 this->cc().sp(this->cc().sp() * -1);
967 this->cc().st(this->cc().st() * -1);
972 PSPlanner::PSPlanner()