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 double smaller_angle_of_two(
303 double sx, double sy, // common start point
304 double cx, double cy, // common middle point
305 double x1, double y1, // first ending point
306 double x2, double y2 // second ending point
308 double a1 = ::angle_between_three_points(sx, sy, cx, cy, x1, y1);
309 double a2 = ::angle_between_three_points(sx, sy, cx, cy, x2, y2);
310 return std::min(a1, a2);
313 void PSPlanner::fe_parallel()
315 BicycleCar bco = BicycleCar(this->gc());
316 this->cc() = BicycleCar();
317 this->cc().sp(-0.01);
318 this->cc().set_max_steer();
319 if (!this->ps().right())
320 this->cc().st(this->cc().st() * -1);
321 this->cc().h(this->ps().heading());
322 double angl_in_slot = this->ps().heading() - M_PI / 4;
323 if (!this->ps().right())
324 angl_in_slot += M_PI / 2;
327 + this->cc().w()/2 * cos(
329 + (this->ps().right() ? + M_PI / 2 : - M_PI / 2)
331 + (this->cc().df() + 0.01) * cos(
332 this->ps().heading() + M_PI
337 + this->cc().w()/2 * sin(
339 + (this->ps().right() ? + M_PI / 2 : - M_PI / 2)
341 + (this->cc().df() + 0.01) * sin(
342 this->ps().heading() + M_PI
346 std::queue<BicycleCar, std::list<BicycleCar>> q;
347 while (!this->collide()) {
351 this->ps().y4() - 0.01,
352 ((this->ps().right()) ? 0.001 : -0.001)
355 // BFS - find entry current car `cc` and corresponding goal car `gc`
356 unsigned int iter_cntr = 0;
357 while (!q.empty() && iter_cntr < 30) {
358 this->cc() = BicycleCar(q.front());
360 if (this->ps().right() && this->cc().sp() < 0) {
361 double cclx = this->cc().ccl().x();
362 double ccly = this->cc().ccl().y();
363 double ccl_lr = edist(
365 this->cc().lrx(), this->cc().lry()
367 double ccl_rr = edist(
369 this->cc().rrx(), this->cc().rry()
371 double ccl_p1 = edist(
373 this->ps().x1(), this->ps().y1()
375 if (ccl_rr < ccl_p1) {
378 } else if (ccl_rr >= ccl_p1 && ccl_lr < ccl_p1) {
379 // partially out of parking slot
380 auto cli1 = ::intersect(
382 this->cc().lrx(), this->cc().lry(),
383 this->cc().rrx(), this->cc().rry()
385 double a1 = ::smaller_angle_of_two(
386 this->ps().x1(), this->ps().y1(),
388 std::get<1>(cli1), std::get<2>(cli1),
389 std::get<3>(cli1), std::get<4>(cli1)
391 auto cli2 = ::intersect(
393 this->ps().x2(), this->ps().y2(),
394 this->ps().x3(), this->ps().y3()
396 double a2 = smaller_angle_of_two(
397 this->cc().rrx(), this->cc().rry(),
399 std::get<1>(cli2), std::get<2>(cli2),
400 std::get<3>(cli2), std::get<4>(cli2)
402 if (std::get<0>(cli1) && (
406 this->cc().rotate(cclx, ccly, -a1);
407 } else if (std::get<0>(cli2) && (
411 this->cc().rotate(cclx, ccly, -a2);
415 } else if (ccl_lr >= ccl_p1) {
417 auto cli1 = ::intersect(
419 this->ps().x1(), this->ps().y1(),
420 this->ps().x2(), this->ps().y2()
422 double a1 = smaller_angle_of_two(
423 this->cc().lrx(), this->cc().lry(),
425 std::get<1>(cli1), std::get<2>(cli1),
426 std::get<3>(cli1), std::get<4>(cli1)
428 auto cli2 = ::intersect(
430 this->ps().x2(), this->ps().y2(),
431 this->ps().x3(), this->ps().y3()
433 double a2 = smaller_angle_of_two(
434 this->cc().rrx(), this->cc().rry(),
436 std::get<1>(cli2), std::get<2>(cli2),
437 std::get<3>(cli2), std::get<4>(cli2)
439 if (std::get<0>(cli1) && (
443 this->cc().rotate(cclx, ccly, -a1);
444 } else if (std::get<0>(cli2) && (
448 this->cc().rotate(cclx, ccly, -a2);
453 } else if (this->ps().right() && this->cc().sp() > 0) {
454 double ccrx = this->cc().ccr().x();
455 double ccry = this->cc().ccr().y();
456 double ccr_lf = edist(
458 this->cc().lfx(), this->cc().lfy()
460 double ccr_rf = edist(
462 this->cc().rfx(), this->cc().rfy()
465 auto clif = ::intersect(
467 this->ps().x1(), this->ps().y1(),
468 this->ps().x4(), this->ps().y4()
470 double af = std::abs(
474 double xf = this->ps().x4();
475 double yf = this->ps().y4();
476 if (std::get<0>(clif)) {
477 xf = std::get<1>(clif);
478 yf = std::get<2>(clif);
491 xf = std::get<3>(clif);
492 yf = std::get<4>(clif);
494 af = ::angle_between_three_points(
501 auto tmp_cc = BicycleCar(this->cc());
502 this->cc().rotate(ccrx, ccry, -af);
516 || !std::get<0>(clif)
519 this->cc().sp(this->cc().sp() * -1);
520 this->gc() = BicycleCar(this->cc());
523 this->cc() = BicycleCar(tmp_cc);
526 auto cli1 = ::intersect(
528 this->ps().x3(), this->ps().y3(),
529 this->ps().x4(), this->ps().y4()
531 double a1 = smaller_angle_of_two(
532 this->cc().rfx(), this->cc().rfy(),
534 std::get<1>(cli1), std::get<2>(cli1),
535 std::get<3>(cli1), std::get<4>(cli1)
537 auto cli2 = ::intersect(
539 this->ps().x2(), this->ps().y2(),
540 this->ps().x3(), this->ps().y3()
542 double a2 = smaller_angle_of_two(
543 this->cc().rfx(), this->cc().rfy(),
545 std::get<1>(cli2), std::get<2>(cli2),
546 std::get<3>(cli2), std::get<4>(cli2)
548 auto cli3 = ::intersect(
550 this->ps().x3(), this->ps().y3(),
551 this->ps().x4(), this->ps().y4()
553 double a3 = smaller_angle_of_two(
554 this->cc().lfx(), this->cc().lfy(),
556 std::get<1>(cli3), std::get<2>(cli3),
557 std::get<3>(cli3), std::get<4>(cli3)
559 if (std::get<0>(cli1) && (
560 (!std::get<0>(cli2) && !std::get<0>(cli3))
561 || (a1 < a2 && !std::get<0>(cli3))
562 || (a1 < a3 && !std::get<0>(cli2))
563 || (a1 < a2 && a1 < a3)
565 this->cc().rotate(ccrx, ccry, -a1);
566 } else if (std::get<0>(cli2) && (
567 (!std::get<0>(cli1) && !std::get<0>(cli3))
568 || (a2 < a1 && !std::get<0>(cli3))
569 || (a2 < a3 && !std::get<0>(cli1))
570 || (a2 < a1 && a2 < a3)
572 this->cc().rotate(ccrx, ccry, -a2);
573 } else if (std::get<0>(cli3) && (
574 (!std::get<0>(cli1) && !std::get<0>(cli2))
575 || (a3 < a1 && !std::get<0>(cli2))
576 || (a3 < a2 && !std::get<0>(cli1))
577 || (a3 < a1 && a3 < a2)
579 this->cc().rotate(ccrx, ccry, -a3);
584 // TODO left parking slot (both forward, backward)
586 this->cc().sp(this->cc().sp() * -1);
588 this->gc() = BicycleCar(this->cc());
591 this->cc().st(this->cc().st() * -1);
592 q.push(BicycleCar(this->cc()));
593 if (sgn(this->cc().st()) == sgn(q.front().st()))
597 this->gc() = BicycleCar(bco);
599 return this->fer_parallel();
602 void PSPlanner::fe_perpendicular()
604 // TODO Try multiple angles when going from parking slot.
606 // Do not use just the maximum steer angle. Test angles
607 // until the whole current car `cc` is out of the parking
610 // Another approach could be testing angles from the
611 // beginning of the escape parkig slot maneuver.
613 this->cc().sp(-0.01);
616 while (!this->left())
621 void PSPlanner::fer()
624 if (this->ps().parallel()) {
626 this->cc() = BicycleCar(this->gc());
627 this->cc().set_max_steer();
628 if (!this->ps().right())
629 this->cc().st(this->cc().st() * -1);
631 return this->fer_parallel();
633 return this->fer_perpendicular();
637 void PSPlanner::fer_parallel()
639 this->cusps_.clear();
640 while (!this->left()) {
641 while (!this->collide() && !this->left())
643 if (this->left() && !this->collide()) {
646 this->cc().sp(this->cc().sp() * -1);
648 this->cc().st(this->cc().st() * -1);
650 this->cusps_.push_back(this->cc());
653 if (this->cc().st() < 0) {
655 this->cusps_.push_back(this->cc());
659 void PSPlanner::fer_perpendicular()
661 bool delta_use[] = {true, true, true};
662 double cc_h = this->cc().h();
665 // check inner radius
666 if (this->forward()) {
675 if (this->ps().right()) {
676 x1 = this->cc().ccr().x();
677 y1 = this->cc().ccr().y();
679 x1 = this->cc().ccl().x();
680 y1 = this->cc().ccl().y();
682 double IR = this->cc().iradi();
686 b = (x - x1) * 2 * cos(cc_h) + (y - y1) * 2 * sin(cc_h);
688 b = (x1 - x) * 2 * cos(cc_h) + (y1 - y) * 2 * sin(cc_h);
689 double c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
690 double D = pow(b, 2) - 4 * a * c;
692 delta = -b - sqrt(D);
694 double delta_1 = delta;
696 delta_use[0] = false;
697 // check outer radius
698 if (this->forward()) {
705 IR = this->cc().ofradi();
708 b = (x - x1) * 2 * cos(cc_h) + (y - y1) * 2 * sin(cc_h);
710 b = (x1 - x) * 2 * cos(cc_h) + (y1 - y) * 2 * sin(cc_h);
711 c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
712 D = pow(b, 2) - 4 * a * c;
713 if (this->forward()) {
714 delta = -b + sqrt(D);
717 double delta_2 = delta;
719 delta_use[1] = false;
720 delta = -b - sqrt(D);
722 double delta_3 = delta;
724 delta_use[2] = false;
725 if (delta_use[0] && delta_use[1] && delta_use[2])
726 delta = std::max(delta_1, std::max(delta_2, delta_3));
727 else if (delta_use[0] && delta_use[1])
728 delta = std::max(delta_1, delta_2);
729 else if (delta_use[0] && delta_use[2])
730 delta = std::max(delta_1, delta_3);
731 else if (delta_use[1] && delta_use[2])
732 delta = std::max(delta_2, delta_3);
733 else if (delta_use[0])
735 else if (delta_use[1])
737 else if (delta_use[2])
741 // current car `cc` can get out of slot with max steer
742 this->cc().x(this->cc().x() + delta * cos(cc_h));
743 this->cc().y(this->cc().y() + delta * sin(cc_h));
745 // get current car `cc` out of slot
747 this->cc().sp(-0.01);
750 this->cc().set_max_steer();
751 if (this->ps().right())
752 this->cc().st(this->cc().st() * -1);
753 while (!this->left()) {
754 while (!this->collide() && !this->left())
756 if (this->left() && !this->collide()) {
759 this->cc().sp(this->cc().sp() * -1);
761 this->cc().st(this->cc().st() * -1);
766 PSPlanner::PSPlanner()