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()));
167 // go 0.1 m forward/backward
168 #if FORWARD_PARKING > 0
173 BicycleCar orig_cc(this->cc());
174 for (unsigned int i = 0; i < 10; i++) {
176 lm.push_back(BicycleCar(this->cc()));
178 this->cc() = BicycleCar(orig_cc);
183 bool PSPlanner::left()
185 double lfx = this->cc().lfx();
186 double lfy = this->cc().lfy();
187 double lrx = this->cc().lrx();
188 double lry = this->cc().lry();
189 double rrx = this->cc().rrx();
190 double rry = this->cc().rry();
191 double rfx = this->cc().rfx();
192 double rfy = this->cc().rfy();
194 (lfx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
195 - (lfy - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
198 (lrx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
199 - (lry - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
202 (rrx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
203 - (rry - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
206 (rfx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
207 - (rfy - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
209 if (this->ps().parallel())
210 return lfs == rfs && (lfs != lrs || lfs != rrs);
211 else if (!this->forward())
212 return lfs == rfs && (lfs != lrs || lfs != rrs);
214 return lrs == rrs && (lrs != lfs || lrs != rfs);
217 bool PSPlanner::parked()
219 std::vector<std::tuple<double, double>> slot;
220 slot.push_back(std::make_tuple(this->ps().x1(), this->ps().y1()));
221 slot.push_back(std::make_tuple(this->ps().x2(), this->ps().y2()));
222 slot.push_back(std::make_tuple(this->ps().x3(), this->ps().y3()));
223 slot.push_back(std::make_tuple(this->ps().x4(), this->ps().y4()));
224 return inside(this->gc().lfx(), this->gc().lfy(), slot)
225 && inside(this->gc().lrx(), this->gc().lry(), slot)
226 && inside(this->gc().rrx(), this->gc().rry(), slot)
227 && inside(this->gc().rfx(), this->gc().rfy(), slot);
230 std::vector<BicycleCar> PSPlanner::possible_goals(
235 std::vector<BicycleCar> pi;
236 if (this->ps().parallel())
240 this->cc().sp(this->cc().sp() * dist);
241 BicycleCar orig_cc(this->cc());
242 for (unsigned int i = 0; i < cnt; i++) {
244 pi.push_back(BicycleCar(this->cc()));
246 this->cc() = BicycleCar(orig_cc);
247 if (this->ps().parallel()) {
249 for (unsigned int i = 0; i < cnt; i++) {
251 pi.push_back(BicycleCar(this->cc()));
253 this->cc() = BicycleCar(orig_cc);
255 if (!this->ps().right()) {
256 this->cc().set_max_steer();
257 for (unsigned int i = 0; i < cnt; i++) {
259 pi.push_back(BicycleCar(this->cc()));
262 this->cc().set_max_steer();
263 this->cc().st(this->cc().st() * -1);
264 for (unsigned int i = 0; i < cnt; i++) {
266 pi.push_back(BicycleCar(this->cc()));
269 this->cc() = BicycleCar(orig_cc);
274 void PSPlanner::shrink_to_perfect_len()
276 if (!this->ps().parallel())
278 double perfect_len = this->gc().perfect_parking_slot_len();
280 this->ps().x1(), this->ps().y1(),
281 this->ps().x4(), this->ps().y4()
284 double h = this->ps().heading();
286 while (h < 0) h += 2 * M_PI;
287 double ch = perfect_len * cos(h);
288 double sh = perfect_len * sin(h);
290 this->ps().x4() + ch, this->ps().y4() + sh,
291 this->ps().x3() + ch, this->ps().y3() + sh,
292 this->ps().x3(), this->ps().y3(),
293 this->ps().x4(), this->ps().y4()
301 if (this->ps().parallel()) {
302 return this->fe_parallel();
305 this->cc() = BicycleCar(this->gc());
306 //this->cc().set_max_steer();
307 //if (this->ps().right())
308 // this->cc().st(this->cc().st() * -1);
312 double angle_between_closer_point(
313 double sx, double sy, // common start point
314 double cx, double cy, // common middle point
315 double x1, double y1, // first ending point
316 double x2, double y2 // second ending point
318 if (edist(sx, sy, x1, y1) < edist(sx, sy, x2, y2))
319 return ::angle_between_three_points(sx, sy, cx, cy, x1, y1);
321 return ::angle_between_three_points(sx, sy, cx, cy, x2, y2);
324 void PSPlanner::fe_parallel()
326 this->shrink_to_perfect_len();
327 BicycleCar bco = BicycleCar(this->gc());
328 this->cc() = BicycleCar();
329 this->cc().sp(-0.01);
330 this->cc().set_max_steer();
331 if (!this->ps().right())
332 this->cc().st(this->cc().st() * -1);
333 this->cc().h(this->ps().heading());
334 double angl_in_slot = this->ps().heading() - M_PI / 4;
335 if (!this->ps().right())
336 angl_in_slot += M_PI / 2;
339 + this->cc().w()/2 * cos(
341 + (this->ps().right() ? + M_PI / 2 : - M_PI / 2)
343 + (this->cc().df() + 0.01) * cos(
344 this->ps().heading() + M_PI
349 + this->cc().w()/2 * sin(
351 + (this->ps().right() ? + M_PI / 2 : - M_PI / 2)
353 + (this->cc().df() + 0.01) * sin(
354 this->ps().heading() + M_PI
358 std::queue<BicycleCar, std::list<BicycleCar>> q;
359 while (!this->collide()) {
363 this->ps().y4() - 0.01,
364 ((this->ps().right()) ? 0.001 : -0.001)
367 // BFS - find entry current car `cc` and corresponding goal car `gc`
368 unsigned int iter_cntr = 0;
369 while (!q.empty() && iter_cntr < 30) {
370 this->cc() = BicycleCar(q.front());
372 if (this->ps().right() && this->cc().sp() < 0) {
373 double cclx = this->cc().ccl().x();
374 double ccly = this->cc().ccl().y();
375 double ccl_lr = edist(
377 this->cc().lrx(), this->cc().lry()
379 double ccl_rr = edist(
381 this->cc().rrx(), this->cc().rry()
383 double ccl_p1 = edist(
385 this->ps().x1(), this->ps().y1()
387 if (ccl_rr < ccl_p1) {
390 } else if (ccl_rr >= ccl_p1 && ccl_lr < ccl_p1) {
391 // partially out of parking slot
392 auto cli1 = ::intersect(
394 this->cc().lrx(), this->cc().lry(),
395 this->cc().rrx(), this->cc().rry()
397 double a1 = ::angle_between_closer_point(
398 this->ps().x1(), this->ps().y1(),
400 std::get<1>(cli1), std::get<2>(cli1),
401 std::get<3>(cli1), std::get<4>(cli1)
403 auto cli2 = ::intersect(
405 this->ps().x2(), this->ps().y2(),
406 this->ps().x3(), this->ps().y3()
408 double a2 = angle_between_closer_point(
409 this->cc().rrx(), this->cc().rry(),
411 std::get<1>(cli2), std::get<2>(cli2),
412 std::get<3>(cli2), std::get<4>(cli2)
414 if (std::get<0>(cli1) && (
418 this->cc().rotate(cclx, ccly, -a1);
419 if (::right_side_of_line(
420 this->cc().x(), this->cc().y(),
423 + cos(this->ps().heading()),
425 + sin(this->ps().heading()),
428 + cos(this->cc().h()),
430 + sin(this->cc().h())
434 } else if (std::get<0>(cli2) && (
438 this->cc().rotate(cclx, ccly, -a2);
442 } else if (ccl_lr >= ccl_p1) {
444 auto cli1 = ::intersect(
446 this->ps().x1(), this->ps().y1(),
447 this->ps().x2(), this->ps().y2()
449 double a1 = angle_between_closer_point(
450 this->cc().lrx(), this->cc().lry(),
452 std::get<1>(cli1), std::get<2>(cli1),
453 std::get<3>(cli1), std::get<4>(cli1)
455 auto cli2 = ::intersect(
457 this->ps().x2(), this->ps().y2(),
458 this->ps().x3(), this->ps().y3()
460 double a2 = angle_between_closer_point(
461 this->cc().rrx(), this->cc().rry(),
463 std::get<1>(cli2), std::get<2>(cli2),
464 std::get<3>(cli2), std::get<4>(cli2)
466 if (std::get<0>(cli1) && (
470 this->cc().rotate(cclx, ccly, -a1);
471 if (::right_side_of_line(
472 this->cc().x(), this->cc().y(),
475 + cos(this->ps().heading()),
477 + sin(this->ps().heading()),
480 + cos(this->cc().h()),
482 + sin(this->cc().h())
486 } else if (std::get<0>(cli2) && (
490 this->cc().rotate(cclx, ccly, -a2);
495 } else if (this->ps().right() && this->cc().sp() > 0) {
496 double ccrx = this->cc().ccr().x();
497 double ccry = this->cc().ccr().y();
498 double ccr_lf = edist(
500 this->cc().lfx(), this->cc().lfy()
502 double ccr_rf = edist(
504 this->cc().rfx(), this->cc().rfy()
507 double af = std::abs(
511 auto tmp_cc = BicycleCar(this->cc());
512 this->cc().rotate(ccrx, ccry, -af);
513 this->gc() = BicycleCar(this->cc());
518 this->cc().sp(this->cc().sp() * -1);
519 this->gc() = BicycleCar(this->cc());
522 this->cc() = BicycleCar(tmp_cc);
525 auto cli1 = ::intersect(
527 this->ps().x3(), this->ps().y3(),
528 this->ps().x4(), this->ps().y4()
530 double a1 = angle_between_closer_point(
531 this->cc().rfx(), this->cc().rfy(),
533 std::get<1>(cli1), std::get<2>(cli1),
534 std::get<3>(cli1), std::get<4>(cli1)
536 auto cli2 = ::intersect(
538 this->ps().x2(), this->ps().y2(),
539 this->ps().x3(), this->ps().y3()
541 double a2 = angle_between_closer_point(
542 this->cc().rfx(), this->cc().rfy(),
544 std::get<1>(cli2), std::get<2>(cli2),
545 std::get<3>(cli2), std::get<4>(cli2)
547 auto cli3 = ::intersect(
549 this->ps().x3(), this->ps().y3(),
550 this->ps().x4(), this->ps().y4()
552 double a3 = angle_between_closer_point(
553 this->cc().lfx(), this->cc().lfy(),
555 std::get<1>(cli3), std::get<2>(cli3),
556 std::get<3>(cli3), std::get<4>(cli3)
558 if (std::get<0>(cli1) && (
559 (!std::get<0>(cli2) && !std::get<0>(cli3))
560 || (a1 < a2 && !std::get<0>(cli3))
561 || (a1 < a3 && !std::get<0>(cli2))
562 || (a1 < a2 && a1 < a3)
564 this->cc().rotate(ccrx, ccry, -a1);
565 } else if (std::get<0>(cli2) && (
566 (!std::get<0>(cli1) && !std::get<0>(cli3))
567 || (a2 < a1 && !std::get<0>(cli3))
568 || (a2 < a3 && !std::get<0>(cli1))
569 || (a2 < a1 && a2 < a3)
571 this->cc().rotate(ccrx, ccry, -a2);
572 } else if (std::get<0>(cli3) && (
573 (!std::get<0>(cli1) && !std::get<0>(cli2))
574 || (a3 < a1 && !std::get<0>(cli2))
575 || (a3 < a2 && !std::get<0>(cli1))
576 || (a3 < a1 && a3 < a2)
578 this->cc().rotate(ccrx, ccry, -a3);
582 } else if (!this->ps().right() && this->cc().sp() < 0) {
583 double ccrx = this->cc().ccr().x();
584 double ccry = this->cc().ccr().y();
585 double ccr_rr = edist(
587 this->cc().rrx(), this->cc().rry()
589 double ccr_lr = edist(
591 this->cc().lrx(), this->cc().lry()
593 double ccr_p1 = edist(
595 this->ps().x1(), this->ps().y1()
597 if (ccr_lr < ccr_p1) {
600 } else if (ccr_lr >= ccr_p1 && ccr_rr < ccr_p1) {
601 // partially out of parking slot
602 auto cli1 = ::intersect(
604 this->cc().lrx(), this->cc().lry(),
605 this->cc().rrx(), this->cc().rry()
607 double a1 = ::angle_between_closer_point(
608 this->ps().x1(), this->ps().y1(),
610 std::get<1>(cli1), std::get<2>(cli1),
611 std::get<3>(cli1), std::get<4>(cli1)
613 auto cli2 = ::intersect(
615 this->ps().x2(), this->ps().y2(),
616 this->ps().x3(), this->ps().y3()
618 double a2 = angle_between_closer_point(
619 this->cc().lrx(), this->cc().lry(),
621 std::get<1>(cli2), std::get<2>(cli2),
622 std::get<3>(cli2), std::get<4>(cli2)
624 if (std::get<0>(cli1) && (
628 this->cc().rotate(ccrx, ccry, a1);
629 if (!::right_side_of_line(
630 this->cc().x(), this->cc().y(),
633 + cos(this->ps().heading()),
635 + sin(this->ps().heading()),
638 + cos(this->cc().h()),
640 + sin(this->cc().h())
644 } else if (std::get<0>(cli2) && (
648 this->cc().rotate(ccrx, ccry, a2);
652 } else if (ccr_rr >= ccr_p1) {
654 auto cli1 = ::intersect(
656 this->ps().x1(), this->ps().y1(),
657 this->ps().x2(), this->ps().y2()
659 double a1 = angle_between_closer_point(
660 this->cc().rrx(), this->cc().rry(),
662 std::get<1>(cli1), std::get<2>(cli1),
663 std::get<3>(cli1), std::get<4>(cli1)
665 auto cli2 = ::intersect(
667 this->ps().x2(), this->ps().y2(),
668 this->ps().x3(), this->ps().y3()
670 double a2 = angle_between_closer_point(
671 this->cc().lrx(), this->cc().lry(),
673 std::get<1>(cli2), std::get<2>(cli2),
674 std::get<3>(cli2), std::get<4>(cli2)
676 if (std::get<0>(cli1) && (
680 this->cc().rotate(ccrx, ccry, a1);
681 if (!::right_side_of_line(
682 this->cc().x(), this->cc().y(),
685 + cos(this->ps().heading()),
687 + sin(this->ps().heading()),
690 + cos(this->cc().h()),
692 + sin(this->cc().h())
696 } else if (std::get<0>(cli2) && (
700 this->cc().rotate(ccrx, ccry, a2);
705 } else if (!this->ps().right() && this->cc().sp() > 0) {
706 double cclx = this->cc().ccl().x();
707 double ccly = this->cc().ccl().y();
708 double ccl_rf = edist(
710 this->cc().rfx(), this->cc().rfy()
712 double ccl_lf = edist(
714 this->cc().lfx(), this->cc().lfy()
717 double af = std::abs(
721 auto tmp_cc = BicycleCar(this->cc());
722 this->cc().rotate(cclx, ccly, af);
723 this->gc() = BicycleCar(this->cc());
728 this->cc().sp(this->cc().sp() * -1);
729 this->gc() = BicycleCar(this->cc());
732 this->cc() = BicycleCar(tmp_cc);
735 auto cli1 = ::intersect(
737 this->ps().x3(), this->ps().y3(),
738 this->ps().x4(), this->ps().y4()
740 double a1 = angle_between_closer_point(
741 this->cc().lfx(), this->cc().lfy(),
743 std::get<1>(cli1), std::get<2>(cli1),
744 std::get<3>(cli1), std::get<4>(cli1)
746 auto cli2 = ::intersect(
748 this->ps().x2(), this->ps().y2(),
749 this->ps().x3(), this->ps().y3()
751 double a2 = angle_between_closer_point(
752 this->cc().lfx(), this->cc().lfy(),
754 std::get<1>(cli2), std::get<2>(cli2),
755 std::get<3>(cli2), std::get<4>(cli2)
757 auto cli3 = ::intersect(
759 this->ps().x3(), this->ps().y3(),
760 this->ps().x4(), this->ps().y4()
762 double a3 = angle_between_closer_point(
763 this->cc().rfx(), this->cc().rfy(),
765 std::get<1>(cli3), std::get<2>(cli3),
766 std::get<3>(cli3), std::get<4>(cli3)
768 if (std::get<0>(cli1) && (
769 (!std::get<0>(cli2) && !std::get<0>(cli3))
770 || (a1 < a2 && !std::get<0>(cli3))
771 || (a1 < a3 && !std::get<0>(cli2))
772 || (a1 < a2 && a1 < a3)
774 this->cc().rotate(cclx, ccly, a1);
775 } else if (std::get<0>(cli2) && (
776 (!std::get<0>(cli1) && !std::get<0>(cli3))
777 || (a2 < a1 && !std::get<0>(cli3))
778 || (a2 < a3 && !std::get<0>(cli1))
779 || (a2 < a1 && a2 < a3)
781 this->cc().rotate(cclx, ccly, a2);
782 } else if (std::get<0>(cli3) && (
783 (!std::get<0>(cli1) && !std::get<0>(cli2))
784 || (a3 < a1 && !std::get<0>(cli2))
785 || (a3 < a2 && !std::get<0>(cli1))
786 || (a3 < a1 && a3 < a2)
788 this->cc().rotate(cclx, ccly, a3);
793 // TODO left parking slot (both forward, backward)
795 this->cc().sp(this->cc().sp() * -1);
797 this->gc() = BicycleCar(this->cc());
800 this->cc().st(this->cc().st() * -1);
801 q.push(BicycleCar(this->cc()));
802 if (sgn(this->cc().st()) == sgn(q.front().st()))
806 this->gc() = BicycleCar(bco);
808 return this->fer_parallel();
811 void PSPlanner::fe_perpendicular()
813 // TODO Try multiple angles when going from parking slot.
815 // Do not use just the maximum steer angle. Test angles
816 // until the whole current car `cc` is out of the parking
819 // Another approach could be testing angles from the
820 // beginning of the escape parkig slot maneuver.
822 this->cc().sp(-0.01);
825 while (!this->left())
830 void PSPlanner::fer()
833 if (this->ps().parallel()) {
835 this->cc() = BicycleCar(this->gc());
836 this->cc().set_max_steer();
837 if (!this->ps().right())
838 this->cc().st(this->cc().st() * -1);
840 return this->fer_parallel();
842 return this->fer_perpendicular();
846 void PSPlanner::fer_parallel()
848 this->cusps_.clear();
849 while (!this->left()) {
850 while (!this->collide() && !this->left())
852 if (this->left() && !this->collide()) {
855 this->cc().sp(this->cc().sp() * -1);
857 this->cc().st(this->cc().st() * -1);
859 this->cusps_.push_back(this->cc());
862 if (this->cc().st() < 0) {
864 this->cusps_.push_back(this->cc());
868 void PSPlanner::fer_perpendicular()
870 bool delta_use[] = {true, true, true};
871 double cc_h = this->cc().h();
874 // check inner radius
875 if (this->forward()) {
884 if (this->ps().right()) {
885 x1 = this->cc().ccr().x();
886 y1 = this->cc().ccr().y();
888 x1 = this->cc().ccl().x();
889 y1 = this->cc().ccl().y();
891 double IR = this->cc().iradi();
895 b = (x - x1) * 2 * cos(cc_h) + (y - y1) * 2 * sin(cc_h);
897 b = (x1 - x) * 2 * cos(cc_h) + (y1 - y) * 2 * sin(cc_h);
898 double c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
899 double D = pow(b, 2) - 4 * a * c;
901 delta = -b - sqrt(D);
903 double delta_1 = delta;
905 delta_use[0] = false;
906 // check outer radius
907 if (this->forward()) {
914 IR = this->cc().ofradi();
917 b = (x - x1) * 2 * cos(cc_h) + (y - y1) * 2 * sin(cc_h);
919 b = (x1 - x) * 2 * cos(cc_h) + (y1 - y) * 2 * sin(cc_h);
920 c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
921 D = pow(b, 2) - 4 * a * c;
922 if (this->forward()) {
923 delta = -b + sqrt(D);
926 double delta_2 = delta;
928 delta_use[1] = false;
929 delta = -b - sqrt(D);
931 double delta_3 = delta;
933 delta_use[2] = false;
934 if (delta_use[0] && delta_use[1] && delta_use[2])
935 delta = std::max(delta_1, std::max(delta_2, delta_3));
936 else if (delta_use[0] && delta_use[1])
937 delta = std::max(delta_1, delta_2);
938 else if (delta_use[0] && delta_use[2])
939 delta = std::max(delta_1, delta_3);
940 else if (delta_use[1] && delta_use[2])
941 delta = std::max(delta_2, delta_3);
942 else if (delta_use[0])
944 else if (delta_use[1])
946 else if (delta_use[2])
950 // current car `cc` can get out of slot with max steer
951 this->cc().x(this->cc().x() + delta * cos(cc_h));
952 this->cc().y(this->cc().y() + delta * sin(cc_h));
954 // get current car `cc` out of slot
956 this->cc().sp(-0.01);
959 this->cc().set_max_steer();
960 if (this->ps().right())
961 this->cc().st(this->cc().st() * -1);
962 while (!this->left()) {
963 while (!this->collide() && !this->left())
965 if (this->left() && !this->collide()) {
968 this->cc().sp(this->cc().sp() * -1);
970 this->cc().st(this->cc().st() * -1);
975 PSPlanner::PSPlanner()