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 (this->ps().parallel())
28 double heading = atan2(
29 this->ps().y2() - this->ps().y1(),
30 this->ps().x2() - this->ps().x1()
32 while (heading < 0) heading += 2 * M_PI;
33 double h = this->gc().h();
34 while (h < 0) h += 2 * M_PI;
35 if (std::abs(heading - h) < M_PI / 4)
40 void PSPlanner::gc_to_4()
42 double angl_slot = atan2(
43 this->ps().y3() - this->ps().y4(),
44 this->ps().x3() - this->ps().x4()
46 double angl_delta = M_PI / 2;
47 if (this->ps().right())
48 angl_delta = -M_PI / 2;
49 double x = this->ps().x4();
50 double y = this->ps().y4();
51 x += (this->gc().dr() + 0.01) * cos(angl_slot);
52 y += (this->gc().dr() + 0.01) * sin(angl_slot);
53 x += (this->gc().w() / 2 + 0.01) * cos(angl_slot + angl_delta);
54 y += (this->gc().w() / 2 + 0.01) * sin(angl_slot + angl_delta);
57 this->gc().h(angl_slot);
60 std::tuple<double, double, double, double> circle_line_intersection(
61 double cx, double cy, double r,
66 double t = (y2 - y1) / (x2 - x1);
67 //double a = 1 + pow(t, 2);
68 //double b = - 2 * cx - 2 * pow(t, 2) * x1 + 2 * t * y1 - 2 * t * cy;
69 //double c = pow(cx, 2) + pow(t, 2) * pow(x1, 2) - 2 * t * y1 * x1
70 // + pow(y1, 2) + 2 * t * cy * x1 - 2 * y1 * cy + pow(cy, 2)
72 double a = 1 + pow(t, 2);
73 double b = - 2 * cx + 2 * t * (-t * x1 + y1) - 2 * cy * t;
74 double c = pow(cx, 2) + pow(cy, 2) - pow(r, 2);
75 c += pow(-t * x1 + y1, 2);
76 c += 2 * cy * t * x1 - 2 * cy * y1;
77 double D = pow(b, 2) - 4 * a * c;
79 return std::make_tuple(cx, cy, cx, cy);
80 double res_x1 = (-b + sqrt(D)) / (2 * a);
81 double res_y1 = t * (res_x1 - x1) + y1;
82 double res_x2 = (-b - sqrt(D)) / (2 * a);
83 double res_y2 = t * (res_x2 - x1) + y1;
84 return std::make_tuple(res_x1, res_y1, res_x2, res_y2);
87 double edist(double x1, double y1, double x2, double y2)
89 return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
92 void PSPlanner::guess_gc()
94 double x = this->ps().x1();
95 double y = this->ps().y1();
96 double h = this->ps().heading();
97 double dts = + M_PI / 2; // direction to slot
98 if (this->ps().right())
100 if (this->ps().parallel()) {
101 x += (this->gc().w() / 2 + 0.01) * cos(h + dts);
102 x += (this->gc().dr() + 0.01) * cos(h);
103 y += (this->gc().w() / 2 + 0.01) * sin(h + dts);
104 y += (this->gc().dr() + 0.01) * sin(h);
107 double entry_width = edist(
108 this->ps().x1(), this->ps().y1(),
109 this->ps().x4(), this->ps().y4()
111 x += entry_width / 2 * cos(h);
112 y += entry_width / 2 * sin(h);
114 this->ps().y2() - this->ps().y1(),
115 this->ps().x2() - this->ps().x1()
117 while (h < 0) h += 2 * M_PI;
119 //// This is for backward parking only.
120 //double entry_width = edist(
121 // this->ps().x1(), this->ps().y1(),
122 // this->ps().x4(), this->ps().y4()
125 // this->gc().orradi()
126 // - (this->gc().mtr() + this->gc().w() / 2)
128 //double move1 = dist_l + this->gc().w() / 2;
129 //double dist_r = entry_width - this->gc().w() - dist_l;
130 //double move2 = sqrt(
131 // pow(this->gc().iradi(), 2)
132 // - pow(this->gc().iradi() - dist_r, 2)
134 //move2 -= this->gc().dr() / 2; // workaround
135 //x += move1 * cos(h);
136 //y += move1 * sin(h);
138 // this->ps().y2() - this->ps().y1(),
139 // this->ps().x2() - this->ps().x1()
141 //while (dts < 0) dts += 2 * M_PI;
142 //x += move2 * cos(h + dts);
143 //y += move2 * sin(h + dts);
144 //h += dts - M_PI / 2;
155 std::vector<BicycleCar> PSPlanner::last_maneuver()
157 std::vector<BicycleCar> lm;
158 if (this->ps().parallel()) {
159 // zig-zag out from the slot
160 this->cc() = BicycleCar(this->gc());
162 while (!this->left()) {
163 while (!this->collide() && !this->left()) {
165 lm.push_back(BicycleCar(this->cc()));
167 if (this->left() && !this->collide()) {
171 this->cc().sp(this->cc().sp() * -1);
173 this->cc().st(this->cc().st() * -1);
175 lm.push_back(BicycleCar(this->cc()));
178 if (this->cc().st() < 0) {
180 lm.push_back(BicycleCar(this->cc()));
185 BicycleCar orig_cc(this->cc());
186 for (unsigned int i = 0; i < 10; i++) {
188 lm.push_back(BicycleCar(this->cc()));
190 this->cc() = BicycleCar(orig_cc);
195 bool PSPlanner::left()
197 double lfx = this->cc().lfx();
198 double lfy = this->cc().lfy();
199 double lrx = this->cc().lrx();
200 double lry = this->cc().lry();
201 double rrx = this->cc().rrx();
202 double rry = this->cc().rry();
203 double rfx = this->cc().rfx();
204 double rfy = this->cc().rfy();
206 (lfx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
207 - (lfy - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
210 (lrx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
211 - (lry - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
214 (rrx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
215 - (rry - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
218 (rfx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
219 - (rfy - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
221 if (this->ps().parallel())
222 return lfs == rfs && (lfs != lrs || lfs != rrs);
223 else if (!this->forward())
224 return lfs == rfs && (lfs != lrs || lfs != rrs);
226 return lrs == rrs && (lrs != lfs || lrs != rfs);
229 bool PSPlanner::parked()
231 std::vector<std::tuple<double, double>> slot;
232 slot.push_back(std::make_tuple(this->ps().x1(), this->ps().y1()));
233 slot.push_back(std::make_tuple(this->ps().x2(), this->ps().y2()));
234 slot.push_back(std::make_tuple(this->ps().x3(), this->ps().y3()));
235 slot.push_back(std::make_tuple(this->ps().x4(), this->ps().y4()));
236 return inside(this->gc().lfx(), this->gc().lfy(), slot)
237 && inside(this->gc().lrx(), this->gc().lry(), slot)
238 && inside(this->gc().rrx(), this->gc().rry(), slot)
239 && inside(this->gc().rfx(), this->gc().rfy(), slot);
242 std::vector<BicycleCar> PSPlanner::possible_goals(
247 std::vector<BicycleCar> pi;
248 if (this->ps().parallel())
252 this->cc().sp(this->cc().sp() * dist);
253 BicycleCar orig_cc(this->cc());
254 for (unsigned int i = 0; i < cnt; i++) {
256 pi.push_back(BicycleCar(this->cc()));
258 this->cc() = BicycleCar(orig_cc);
259 if (this->ps().parallel()) {
261 for (unsigned int i = 0; i < cnt; i++) {
263 pi.push_back(BicycleCar(this->cc()));
265 this->cc() = BicycleCar(orig_cc);
267 if (!this->ps().right()) {
268 this->cc().set_max_steer();
269 for (unsigned int i = 0; i < cnt; i++) {
271 pi.push_back(BicycleCar(this->cc()));
274 this->cc().set_max_steer();
275 this->cc().st(this->cc().st() * -1);
276 for (unsigned int i = 0; i < cnt; i++) {
278 pi.push_back(BicycleCar(this->cc()));
281 this->cc() = BicycleCar(orig_cc);
290 if (this->ps().parallel()) {
291 return this->fe_parallel();
294 this->cc() = BicycleCar(this->gc());
295 //this->cc().set_max_steer();
296 //if (this->ps().right())
297 // this->cc().st(this->cc().st() * -1);
302 void PSPlanner::fe_parallel()
304 BicycleCar bco = BicycleCar(this->gc());
305 this->cc() = BicycleCar();
306 this->cc().sp(-0.01);
307 this->cc().set_max_steer();
308 if (!this->ps().right())
309 this->cc().st(this->cc().st() * -1);
310 this->cc().h(this->ps().heading());
311 double angl_in_slot = this->ps().heading() - M_PI / 4;
312 if (!this->ps().right())
313 angl_in_slot += M_PI / 2;
316 + this->cc().w()/2 * cos(
318 + (this->ps().right() ? + M_PI / 2 : - M_PI / 2)
320 + (this->cc().df() + 0.01) * cos(
321 this->ps().heading() + M_PI
326 + this->cc().w()/2 * sin(
328 + (this->ps().right() ? + M_PI / 2 : - M_PI / 2)
330 + (this->cc().df() + 0.01) * sin(
331 this->ps().heading() + M_PI
335 std::queue<BicycleCar, std::list<BicycleCar>> q;
336 while (!this->collide()) {
340 this->ps().y4() - 0.01,
341 ((this->ps().right()) ? 0.001 : -0.001)
344 // BFS - find entry current car `cc` and corresponding goal car `gc`
345 unsigned int iter_cntr = 0;
346 while (!q.empty() && iter_cntr < 30) {
347 this->cc() = BicycleCar(q.front());
349 if (this->ps().right() && this->cc().sp() < 0) {
350 double cclx = this->cc().ccl().x();
351 double ccly = this->cc().ccl().y();
354 pow(this->cc().lry() - ccly, 2)
355 + pow(this->cc().lrx() - cclx, 2)
357 auto cli1 = ::intersect(
359 this->ps().x1(), this->ps().y1(),
360 this->ps().x2(), this->ps().y2()
362 double a11 = ::angle_between_three_points(
363 this->cc().lrx(), this->cc().lry(),
365 std::get<1>(cli1), std::get<2>(cli1)
367 double a12 = ::angle_between_three_points(
368 this->cc().lrx(), this->cc().lry(),
370 std::get<3>(cli1), std::get<4>(cli1)
372 double a1 = std::min(a11, a12);
374 double rf = edist(cclx, ccly, this->ps().x1(), this->ps().y1());
376 edist(cclx, ccly, this->cc().lrx(), this->cc().lry()) < rf
377 && rf < edist(cclx, ccly, this->cc().rrx(), this->cc().rry())
379 auto clif = ::intersect(
381 this->cc().lrx(), this->cc().lry(),
382 this->cc().rrx(), this->cc().rry()
384 double xf = std::get<1>(clif);
385 double yf = std::get<2>(clif);
388 std::get<3>(clif), std::get<4>(clif),
389 this->cc().x(), this->cc().y()
392 < edist(xf, yf, this->cc().x(), this->cc().y())
394 xf = std::get<3>(clif);
395 yf = std::get<4>(clif);
397 double af = ::intersection(
400 this->ps().x1(), this->ps().y1()
407 pow(this->cc().rry() - ccly, 2)
408 + pow(this->cc().rrx() - cclx, 2)
410 auto cli2 = ::intersect(
412 this->ps().x1(), this->ps().y1(),
413 this->ps().x2(), this->ps().y2()
415 double a21 = ::angle_between_three_points(
416 this->cc().rrx(), this->cc().rry(),
418 std::get<1>(cli2), std::get<2>(cli2)
420 double a22 = ::angle_between_three_points(
421 this->cc().rrx(), this->cc().rry(),
423 std::get<3>(cli2), std::get<4>(cli2)
425 double a2 = std::min(a21, a22);
428 pow(this->cc().rry() - ccly, 2)
429 + pow(this->cc().rrx() - cclx, 2)
431 auto cli3 = ::intersect(
433 this->ps().x2(), this->ps().y2(),
434 this->ps().x3(), this->ps().y3()
436 double a31 = ::angle_between_three_points(
437 this->cc().rrx(), this->cc().rry(),
439 std::get<1>(cli3), std::get<2>(cli3)
441 double a32 = ::angle_between_three_points(
442 this->cc().rrx(), this->cc().rry(),
444 std::get<3>(cli3), std::get<4>(cli3)
446 double a3 = std::min(a31, a32);
448 if (std::get<0>(cli1) && (
449 (!std::get<0>(cli2) && !std::get<0>(cli3))
450 || (a1 < a2 && !std::get<0>(cli3))
451 || (a1 < a3 && !std::get<0>(cli2))
452 || (a1 < a2 && a1 < a3)
454 this->cc().rotate(cclx, ccly, -a1);
455 } else if (std::get<0>(cli2) && (
456 (!std::get<0>(cli1) && !std::get<0>(cli3))
457 || (a2 < a1 && !std::get<0>(cli3))
458 || (a2 < a3 && !std::get<0>(cli1))
459 || (a2 < a1 && a2 < a3)
461 this->cc().rotate(cclx, ccly, -a2);
462 } else if (std::get<0>(cli3) && (
463 (!std::get<0>(cli1) && !std::get<0>(cli2))
464 || (a3 < a1 && !std::get<0>(cli2))
465 || (a3 < a2 && !std::get<0>(cli1))
466 || (a3 < a1 && a3 < a2)
468 this->cc().rotate(cclx, ccly, -a3);
472 if (::right_side_of_line(
473 this->cc().x(), this->cc().y(),
474 this->cc().x() + cos(this->ps().heading()),
475 this->cc().y() + sin(this->ps().heading()),
476 this->cc().x() + cos(this->cc().h()),
477 this->cc().y() + sin(this->cc().h())
480 } else if (this->ps().right() && this->cc().sp() > 0) {
481 double ccrx = this->cc().ccr().x();
482 double ccry = this->cc().ccr().y();
485 pow(this->cc().lfy() - ccry, 2)
486 + pow(this->cc().lfx() - ccrx, 2)
488 auto clif = ::intersect(
490 this->ps().x1(), this->ps().y1(),
491 this->ps().x4(), this->ps().y4()
493 if (std::get<0>(clif)) {
494 double xf = std::get<1>(clif);
495 double yf = std::get<2>(clif);
508 xf = std::get<3>(clif);
509 yf = std::get<4>(clif);
511 auto af = ::angle_between_three_points(
517 auto tmp_cc = BicycleCar(this->cc());
518 this->cc().rotate(ccrx, ccry, -af);
522 this->ps().x1(), this->ps().y1(),
525 this->ps().x1(), this->ps().y1(),
526 this->ps().x4(), this->ps().y4()
529 this->cc().sp(-0.01);
530 this->cc().set_max_steer();
531 this->cc().st(this->cc().st() * -1);
532 this->gc() = BicycleCar(this->cc());
535 this->cc() = BicycleCar(tmp_cc);
538 // should be parked and found in
539 // previous iteration
543 pow(this->cc().rfy() - ccry, 2)
544 + pow(this->cc().rfx() - ccrx, 2)
546 auto cli1 = ::intersect(
548 this->ps().x3(), this->ps().y3(),
549 this->ps().x4(), this->ps().y4()
551 double a11 = ::angle_between_three_points(
552 this->cc().lrx(), this->cc().lry(),
554 std::get<1>(cli1), std::get<2>(cli1)
556 double a12 = ::angle_between_three_points(
557 this->cc().lrx(), this->cc().lry(),
559 std::get<3>(cli1), std::get<4>(cli1)
561 double a1 = std::min(a11, a12);
564 pow(this->cc().lfy() - ccry, 2)
565 + pow(this->cc().lfx() - ccrx, 2)
567 auto cli2 = ::intersect(
569 this->ps().x3(), this->ps().y3(),
570 this->ps().x4(), this->ps().y4()
572 double a21 = ::angle_between_three_points(
573 this->cc().rrx(), this->cc().rry(),
575 std::get<1>(cli2), std::get<2>(cli2)
577 double a22 = ::angle_between_three_points(
578 this->cc().rrx(), this->cc().rry(),
580 std::get<3>(cli2), std::get<4>(cli2)
582 double a2 = std::min(a21, a22);
585 pow(this->cc().rfy() - ccry, 2)
586 + pow(this->cc().rfx() - ccrx, 2)
588 auto cli3 = ::intersect(
590 this->ps().x3(), this->ps().y3(),
591 this->ps().x2(), this->ps().y2()
593 double a31 = ::angle_between_three_points(
594 this->cc().rrx(), this->cc().rry(),
596 std::get<1>(cli3), std::get<2>(cli3)
598 double a32 = ::angle_between_three_points(
599 this->cc().rrx(), this->cc().rry(),
601 std::get<3>(cli3), std::get<4>(cli3)
603 double a3 = std::min(a31, a32);
605 if (std::get<0>(cli1) && (
606 (!std::get<0>(cli2) && !std::get<0>(cli3))
607 || (a1 < a2 && !std::get<0>(cli3))
608 || (a1 < a3 && !std::get<0>(cli2))
609 || (a1 < a2 && a1 < a3)
611 this->cc().rotate(ccrx, ccry, -a1);
612 } else if (std::get<0>(cli2) && (
613 (!std::get<0>(cli1) && !std::get<0>(cli3))
614 || (a2 < a1 && !std::get<0>(cli3))
615 || (a2 < a3 && !std::get<0>(cli1))
616 || (a2 < a1 && a2 < a3)
618 this->cc().rotate(ccrx, ccry, -a2);
619 } else if (std::get<0>(cli3) && (
620 (!std::get<0>(cli1) && !std::get<0>(cli2))
621 || (a3 < a1 && !std::get<0>(cli2))
622 || (a3 < a2 && !std::get<0>(cli1))
623 || (a3 < a1 && a3 < a2)
625 this->cc().rotate(ccrx, ccry, -a3);
630 // TODO left parking slot (both forward, backward)
632 this->cc().sp(this->cc().sp() * -1);
634 this->gc() = BicycleCar(this->cc());
637 this->cc().st(this->cc().st() * -1);
638 q.push(BicycleCar(this->cc()));
639 if (sgn(this->cc().st()) == sgn(q.front().st()))
643 this->gc() = BicycleCar(bco);
645 return this->fer_parallel();
648 void PSPlanner::fe_perpendicular()
650 // TODO Try multiple angles when going from parking slot.
652 // Do not use just the maximum steer angle. Test angles
653 // until the whole current car `cc` is out of the parking
656 // Another approach could be testing angles from the
657 // beginning of the escape parkig slot maneuver.
659 this->cc().sp(-0.01);
662 while (!this->left())
667 void PSPlanner::fer()
670 if (this->ps().parallel()) {
672 this->cc() = BicycleCar(this->gc());
673 this->cc().set_max_steer();
674 if (!this->ps().right())
675 this->cc().st(this->cc().st() * -1);
677 return this->fer_parallel();
679 return this->fer_perpendicular();
683 void PSPlanner::fer_parallel()
685 this->cusps_.clear();
686 while (!this->left()) {
687 while (!this->collide() && !this->left())
689 if (this->left() && !this->collide()) {
692 this->cc().sp(this->cc().sp() * -1);
694 this->cc().st(this->cc().st() * -1);
696 this->cusps_.push_back(this->cc());
699 if (this->cc().st() < 0) {
701 this->cusps_.push_back(this->cc());
705 void PSPlanner::fer_perpendicular()
707 bool delta_use[] = {true, true, true};
708 double cc_h = this->cc().h();
711 // check inner radius
712 if (this->forward()) {
721 if (this->ps().right()) {
722 x1 = this->cc().ccr().x();
723 y1 = this->cc().ccr().y();
725 x1 = this->cc().ccl().x();
726 y1 = this->cc().ccl().y();
728 double IR = this->cc().iradi();
732 b = (x - x1) * 2 * cos(cc_h) + (y - y1) * 2 * sin(cc_h);
734 b = (x1 - x) * 2 * cos(cc_h) + (y1 - y) * 2 * sin(cc_h);
735 double c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
736 double D = pow(b, 2) - 4 * a * c;
738 delta = -b - sqrt(D);
740 double delta_1 = delta;
742 delta_use[0] = false;
743 // check outer radius
744 if (this->forward()) {
751 IR = this->cc().ofradi();
754 b = (x - x1) * 2 * cos(cc_h) + (y - y1) * 2 * sin(cc_h);
756 b = (x1 - x) * 2 * cos(cc_h) + (y1 - y) * 2 * sin(cc_h);
757 c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
758 D = pow(b, 2) - 4 * a * c;
759 if (this->forward()) {
760 delta = -b + sqrt(D);
763 double delta_2 = delta;
765 delta_use[1] = false;
766 delta = -b - sqrt(D);
768 double delta_3 = delta;
770 delta_use[2] = false;
771 if (delta_use[0] && delta_use[1] && delta_use[2])
772 delta = std::max(delta_1, std::max(delta_2, delta_3));
773 else if (delta_use[0] && delta_use[1])
774 delta = std::max(delta_1, delta_2);
775 else if (delta_use[0] && delta_use[2])
776 delta = std::max(delta_1, delta_3);
777 else if (delta_use[1] && delta_use[2])
778 delta = std::max(delta_2, delta_3);
779 else if (delta_use[0])
781 else if (delta_use[1])
783 else if (delta_use[2])
787 // current car `cc` can get out of slot with max steer
788 this->cc().x(this->cc().x() + delta * cos(cc_h));
789 this->cc().y(this->cc().y() + delta * sin(cc_h));
791 // get current car `cc` out of slot
793 this->cc().sp(-0.01);
796 this->cc().set_max_steer();
797 if (this->ps().right())
798 this->cc().st(this->cc().st() * -1);
799 while (!this->left()) {
800 while (!this->collide() && !this->left())
802 if (this->left() && !this->collide()) {
805 this->cc().sp(this->cc().sp() * -1);
807 this->cc().st(this->cc().st() * -1);
812 PSPlanner::PSPlanner()