2 This file is part of I am car.
4 I am car is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 I am car is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with I am car. If not, see <http://www.gnu.org/licenses/>.
22 #include "slotplanner.h"
24 ParallelSlot::ParallelSlot()
28 std::vector<RRTNode *> &ParallelSlot::goals()
33 RRTNode *ParallelSlot::getMidd()
35 if (this->cusp().size() > 0)
36 return this->cusp().front().front();
41 std::vector<std::vector<RRTNode *>> &ParallelSlot::cusp()
46 float ParallelSlot::DH() const
51 PolygonObstacle &ParallelSlot::slot()
56 float ParallelSlot::slotHeading()
58 return this->slotHeading_;
61 SlotSide ParallelSlot::slotSide()
63 return this->slotSide_;
66 SlotType ParallelSlot::slotType()
68 return this->slotType_;
71 float ParallelSlot::poseHeading()
73 return this->poseHeading_;
77 void ParallelSlot::DH(float dh)
82 void ParallelSlot::setAll()
85 float y0 = this->slot().bnodes()[0]->y();
86 float x0 = this->slot().bnodes()[0]->x();
87 float y3 = this->slot().bnodes()[3]->y();
88 float x3 = this->slot().bnodes()[3]->x();
91 this->slotHeading_ = atan2(dy, dx);
93 float y1 = this->slot().bnodes()[1]->y();
94 float x1 = this->slot().bnodes()[1]->x();
97 this->poseHeading_ = atan2(dy, dx);
99 if (sgn((x1 - x0) * (y3 - y0) - (y1 - y0) * (x3 - x0)) < 0)
100 this->slotSide_ = LEFT;
102 this->slotSide_ = RIGHT;
105 this->slot().bnodes()[0],
106 this->slot().bnodes()[1]
109 this->slot().bnodes()[1],
110 this->slot().bnodes()[2]
113 this->slotType_ = PERPENDICULAR;
115 this->slotType_ = PARALLEL;
119 void ParallelSlot::fip(
120 std::vector<CircleObstacle>& co,
121 std::vector<SegmentObstacle>& so
125 if (this->slotType() == PERPENDICULAR) {
126 std::vector<RRTNode *> tmpc;
127 BicycleCar *tmpf = this->getFP();
128 BicycleCar *tmpb = this->getISPP(tmpf);
130 if (this->slotSide() == LEFT)
134 if (this->slotSide() == LEFT)
135 this->DH(1 * 0.5 / tmpb->out_radi());
137 this->DH(-1 * 0.5 / tmpb->out_radi());
140 p = tmpb->move(cc, i * this->DH());
142 !this->slot().collide(p->frame())
144 this->slotSide() == LEFT
145 && p->h() < this->slotHeading()
147 this->slotSide() == RIGHT
149 p->h() > this->slotHeading()
154 if (tmpc.size() == 0) {
159 std::vector<RRTEdge *> eds = p->frame();
170 this->goals_.push_back(p);
173 p = tmpb->move(cc, i * this->DH());
176 this->cusp().push_back(tmpc);
179 // see https://courses.cs.washington.edu/courses/cse326/03su/homework/hw3/bfs.html
180 // RRTNode.s() works as iteration level
181 std::queue<BicycleCar *, std::list<BicycleCar *>> q;
182 std::queue<BicycleCar *, std::list<BicycleCar *>> empty;
184 if (this->slotSide() == LEFT)
186 BicycleCar *CC = this->getEPC();
187 BicycleCar *B = this->getEP();
188 this->DH(di * 0.01 / CC->out_radi());
191 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
192 while (!this->slot().collide(c->frame())) {
194 std::vector<RRTEdge *> eds = c->frame();
205 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
208 delete c; // not in q and collide
212 if (this->isInside(c)) {
213 goto createcuspandfinish;
214 } else if (c->s() < 9) {
215 BicycleCar *cc = this->flnc(c, co, so);
220 delete c; // not in q and collide
226 std::vector<RRTNode *> cusp;
228 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
231 std::reverse(cusp.begin(), cusp.end());
232 this->cusp().push_back(cusp);
236 void ParallelSlot::fipf(
237 std::vector<CircleObstacle>& co,
238 std::vector<SegmentObstacle>& so
242 std::vector<RRTNode *> tmpc;
243 BicycleCar *tmpf = this->getFPf();
244 BicycleCar *tmpb = this->getISPPf(tmpf);
246 if (this->slotSide() == LEFT)
250 if (this->slotSide() == LEFT)
251 this->DH(-1 * 0.5 / tmpb->out_radi());
253 this->DH(1 * 0.5 / tmpb->out_radi());
256 p = tmpb->move(cc, i * this->DH());
258 !this->slot().collide(p->frame())
260 this->slotSide() == LEFT
261 && p->h() > this->slotHeading()
263 this->slotSide() == RIGHT
264 && p->h() < this->slotHeading()
267 if (tmpc.size() == 0) {
272 std::vector<RRTEdge *> eds = p->frame();
283 this->goals_.push_back(p);
286 p = tmpb->move(cc, i * this->DH());
289 this->cusp().push_back(tmpc);
293 BicycleCar *ParallelSlot::flnc(
295 std::vector<CircleObstacle>& co,
296 std::vector<SegmentObstacle>& so
300 if (this->slotSide() == LEFT) {
301 if (int(B->s()) % 2 == 0)
302 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
304 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
306 if (int(B->s()) % 2 == 0)
307 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
309 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
313 p = B->move(cc, i * this->DH());
315 !this->slot().collide(p->frame())
316 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
320 p = B->move(cc, i * this->DH());
322 std::vector<RRTEdge *> eds = p->frame();
335 p = B->move(cc, i * this->DH());
337 !this->slot().collide(p->frame())
338 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
340 if (this->isInside(p)) {
346 p = B->move(cc, i * this->DH());
348 std::vector<RRTEdge *> eds = p->frame();
361 return B->move(cc, (i - 1) * this->DH());
364 void ParallelSlot::fipr(RRTNode *n)
366 return this->fipr(new BicycleCar(n->x(), n->y(), n->h()));
369 void ParallelSlot::fipr(BicycleCar *B)
371 std::vector<RRTNode *> cusp;
372 cusp.push_back(new RRTNode(B->x(), B->y(), B->h()));
374 if (this->slotSide() == LEFT)
376 if (this->slotType() == PERPENDICULAR) {
377 this->DH(di * 0.01 / B->out_radi()); // TODO car in slot h()
379 if (this->slotSide() == LEFT)
380 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
382 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
385 p = B->move(cc, i * this->DH());
387 !this->slot().collide(p->frame())
388 && this->slot().collide(p)
392 p = B->move(cc, i * this->DH());
395 p = B->move(cc, i * this->DH());
397 !this->slot().collide(p->frame())
398 && this->slot().collide(p)
402 p = B->move(cc, i * this->DH());
405 p = B->move(cc, i * this->DH());
406 cusp.push_back(new RRTNode(p->x(), p->y(), p->h()));
407 std::reverse(cusp.begin(), cusp.end());
408 this->cusp().push_back(cusp);
411 this->DH(di * 0.01 / B->out_radi());
416 this->slotSide() == LEFT
417 && this->slot().collide(new RRTNode(c->lfx(), c->lfy(), 0))
419 this->slotSide() == RIGHT
420 && this->slot().collide(new RRTNode(c->rfx(), c->rfy(), 0))
422 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
423 BicycleCar *cc = this->flncr(c);
428 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
429 std::reverse(cusp.begin(), cusp.end());
430 this->cusp().push_back(cusp);
433 BicycleCar *ParallelSlot::flncr(BicycleCar *B)
436 if (this->slotSide() == LEFT) {
437 if (int(B->s()) % 2 == 0)
438 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
440 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
442 if (int(B->s()) % 2 == 0)
443 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
445 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
449 p = B->move(cc, i * this->DH());
451 !this->slot().collide(p->frame())
453 this->slotSide() == LEFT
454 && this->slot().collide(new RRTNode(
460 this->slotSide() == RIGHT
461 && this->slot().collide(new RRTNode(
470 p = B->move(cc, i * this->DH());
473 p = B->move(cc, i * this->DH());
474 while (!this->slot().collide(p->frame())) {
476 this->slotSide() == LEFT
477 && !this->slot().collide(new RRTNode(
487 this->slotSide() == RIGHT
488 && !this->slot().collide(new RRTNode(
498 p = B->move(cc, i * this->DH());
501 return B->move(cc, (i - 1) * this->DH());
504 RRTNode *ParallelSlot::fposecenter()
506 return this->slot().bnodes().back();
509 bool ParallelSlot::flast(
513 std::vector<RRTNode *> &cusp
516 BicycleCar *B = new BicycleCar(P->x(), P->y(), P->h());
519 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
521 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
524 p = B->move(cc, i * this->DH());
525 while (!this->slot().collide(p->frame())
527 (this->DH() > 0 && p->x() <= 0)
528 || (this->DH() < 0 && p->x() >= 0)
532 p = B->move(cc, i * this->DH());
535 p = B->move(cc, i * this->DH());
536 while (!this->slot().collide(p->frame())
538 (this->DH() > 0 && p->x() <= 0)
539 || (this->DH() < 0 && p->x() >= 0)
541 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
545 if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
551 p = B->move(cc, i * this->DH());
554 p = B->move(cc, (i - 1) * this->DH());
555 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
558 } else if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
563 return this->flast(p, !right, il + 1, cusp);
568 void ParallelSlot::fpose()
570 bool left = false; // right parking slot
572 BicycleCar *CC = new BicycleCar(
573 this->fposecenter()->x(),
574 this->fposecenter()->y() - 0.01,
577 BicycleCar *B = new BicycleCar(
578 CC->x() - CC->width() / 2,
579 CC->y() - (CC->length() + CC->wheelbase()) / 2,
582 if (this->slot().bnodes()[0]->x() > this->slot().bnodes()[1]->x()) {
587 CC->x() + CC->width() / 2,
588 CC->y() - (CC->length() + CC->wheelbase()) / 2,
592 this->DH(di * 0.01 / CC->out_radi());
595 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
596 while (!this->slot().collide(p->frame())) {
597 std::vector<RRTNode *> tmpcusp;
598 tmpcusp.push_back(new BicycleCar(p->x(), p->y(), p->h()));
599 if (this->flast(p, left, 0, tmpcusp)) {
600 this->cusp().push_back(tmpcusp);
605 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
609 BicycleCar *ParallelSlot::getEP()
611 // new pose for parallel parking to right slot
616 BicycleCar *CC = this->getEPC();
617 // move left by car width / 2
618 tnx = CC->x() + CC->width() / 2 * cos(CC->h() + M_PI / 2);
619 tny = CC->y() + CC->width() / 2 * sin(CC->h() + M_PI / 2);
620 if (this->slotSide() == LEFT) {
621 // move right by car width / 2
622 tnx = CC->x() + CC->width() / 2 * cos(CC->h() - M_PI / 2);
623 tny = CC->y() + CC->width() / 2 * sin(CC->h() - M_PI / 2);
625 if (this->slotType() == PARALLEL) {
627 nx = tnx - (CC->length() + CC->wheelbase()) / 2 * cos(CC->h());
628 ny = tny - (CC->length() + CC->wheelbase()) / 2 * sin(CC->h());
631 nx = tnx + (CC->length() - CC->wheelbase()) / 2 * cos(CC->h());
632 ny = tny + (CC->length() - CC->wheelbase()) / 2 * sin(CC->h());
634 return new BicycleCar(nx, ny, CC->h());
637 BicycleCar *ParallelSlot::getEPC()
639 // new pose for parallel parking to right slot
643 ta = this->slotHeading() + M_PI;
644 if (this->slotSide() == RIGHT)
648 nx = this->fposecenter()->x() + 0.01 * cos(ta);
649 ny = this->fposecenter()->y() + 0.01 * sin(ta);
650 return new BicycleCar(nx, ny, this->slotHeading());
653 BicycleCar *ParallelSlot::getFP()
656 float x = this->slot().bnodes()[0]->x();
657 float y = this->slot().bnodes()[0]->y();
658 float h = this->slotHeading();
659 float ph = this->poseHeading();
662 if (this->slotType() == PARALLEL) {
663 if (this->slotSide() == LEFT) {
664 nx = x + BCAR_WIDTH / 2 * cos(h + M_PI / 2);
665 ny = y + BCAR_WIDTH / 2 * sin(h + M_PI / 2);
667 nx = x + BCAR_WIDTH / 2 * cos(h - M_PI / 2);
668 ny = y + BCAR_WIDTH / 2 * sin(h - M_PI / 2);
670 x = nx + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * cos(h);
671 y = ny + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * sin(h);
673 if (this->slotSide() == LEFT) {
674 nx = x + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
676 ny = y + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
678 x = nx + (BCAR_DIAG_RRADI) * cos(h);
679 y = ny + (BCAR_DIAG_RRADI) * sin(h);
681 nx = x + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
683 ny = y + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
685 x = nx + (BCAR_DIAG_RRADI) * cos(h);
686 y = ny + (BCAR_DIAG_RRADI) * sin(h);
689 return new BicycleCar(x, y, ph);
692 BicycleCar *ParallelSlot::getISPP(BicycleCar *B)
694 // rigt side (for right parking slot)
695 float x = this->slot().bnodes().back()->x();
696 float y = this->slot().bnodes().back()->y();
699 if (this->slotSide() == LEFT) {
706 float IR = BCAR_IN_RADI;
708 float b = (x1 - x) * 2 * cos(B->h()) + (y1 - y) * 2 * sin(B->h());
709 float c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
710 float D = pow(b, 2) - 4 * a * c;
712 delta = -b - sqrt(D);
714 float delta_1 = delta;
715 // left front (for right parking slot)
716 x = this->slot().bnodes().front()->x();
717 y = this->slot().bnodes().front()->y();
720 b = (x1 - x) * 2 * cos(B->h()) + (y1 - y) * 2 * sin(B->h());
721 c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
722 D = pow(b, 2) - 4 * a * c;
723 //delta = -b + sqrt(D);
725 float delta_2 = delta;
726 delta = -b - sqrt(D);
728 float delta_3 = delta;
729 delta = std::max(delta_1, std::max(delta_2, delta_3));
730 return new BicycleCar(
731 B->x() + delta * cos(B->h()),
732 B->y() + delta * sin(B->h()),
737 BicycleCar *ParallelSlot::getFPf()
740 float x = this->slot().bnodes().front()->x();
741 float y = this->slot().bnodes().front()->y();
742 float h = this->slotHeading();
743 float ph = this->poseHeading();
751 nx = x + (BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 * cos(ph);
752 ny = y + (BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 * sin(ph);
753 x = nx + (BCAR_DIAG_RRADI) * cos(h);
754 y = ny + (BCAR_DIAG_RRADI) * sin(h);
755 return new BicycleCar(x, y, ph);
758 BicycleCar *ParallelSlot::getISPPf(BicycleCar *B)
760 // right rear (for right parking slot)
761 float x = this->slot().bnodes().front()->x();
762 float y = this->slot().bnodes().front()->y();
765 if (this->slotSide() == LEFT) {
772 float IR = BCAR_IN_RADI;
774 float b = (x - x1) * 2 * cos(B->h()) + (y - y1) * 2 * sin(B->h());
775 float c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
776 float D = pow(b, 2) - 4 * a * c;
778 delta = -b - sqrt(D); // TODO why this works?
780 float delta_1 = delta;
781 // left front (for right parking slot)
782 x = this->slot().bnodes().back()->x();
783 y = this->slot().bnodes().back()->y();
786 b = (x - x1) * 2 * cos(B->h()) + (y - y1) * 2 * sin(B->h());
787 c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
788 D = pow(b, 2) - 4 * a * c;
789 delta = -b + sqrt(D);
791 float delta_2 = delta;
792 delta = -b - sqrt(D);
794 float delta_3 = delta;
795 delta = std::max(delta_1, std::max(delta_2, delta_3));
796 return new BicycleCar(
797 B->x() - delta * cos(B->h()),
798 B->y() - delta * sin(B->h()),
803 bool ParallelSlot::isInside(BicycleCar *c)
807 tmpn = new RRTNode(c->lfx(), c->lfy(), 0);
808 if (!this->slot().collide(tmpn))
811 tmpn = new RRTNode(c->lrx(), c->lry(), 0);
812 if (!this->slot().collide(tmpn))
815 tmpn = new RRTNode(c->rrx(), c->rry(), 0);
816 if (!this->slot().collide(tmpn))
819 tmpn = new RRTNode(c->rfx(), c->rfy(), 0);
820 if (!this->slot().collide(tmpn))
826 struct SamplingInfo ParallelSlot::getSamplingInfo()
828 struct SamplingInfo si;
829 RRTNode *n = this->getMidd();
830 if (this->slotType() == PARALLEL) {
832 si.x0 = n->x() + 1.5 * BCAR_LENGTH * cos(n->h());
833 si.y0 = n->y() + 1.5 * BCAR_LENGTH * sin(n->h());
836 si.x0 = this->slot().bnodes().front()->x();
837 si.y0 = this->slot().bnodes().front()->y();
838 si.h0 = this->slotHeading();