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_;
72 void ParallelSlot::DH(float dh)
77 void ParallelSlot::setAll()
80 float y0 = this->slot().bnodes()[0]->y();
81 float x0 = this->slot().bnodes()[0]->x();
82 float y3 = this->slot().bnodes()[3]->y();
83 float x3 = this->slot().bnodes()[3]->x();
86 this->slotHeading_ = atan2(dy, dx);
88 float y1 = this->slot().bnodes()[1]->y();
89 float x1 = this->slot().bnodes()[1]->x();
90 if (sgn((x1 - x0) * (y3 - y0) - (y1 - y0) * (x3 - x0)) < 0)
91 this->slotSide_ = LEFT;
93 this->slotSide_ = RIGHT;
96 this->slot().bnodes()[0],
97 this->slot().bnodes()[1]
100 this->slot().bnodes()[1],
101 this->slot().bnodes()[2]
104 this->slotType_ = PERPENDICULAR;
106 this->slotType_ = PARALLEL;
110 void ParallelSlot::fip(
111 std::vector<CircleObstacle>& co,
112 std::vector<SegmentObstacle>& so
116 if (this->slotType() == PERPENDICULAR) {
117 std::vector<RRTNode *> tmpc;
118 BicycleCar *tmpf = this->getFP();
119 BicycleCar *tmpb = this->getISPP(tmpf);
121 if (this->slotSide() == LEFT)
125 if (this->slotSide() == LEFT)
126 this->DH(1 * 0.5 / tmpb->out_radi());
128 this->DH(-1 * 0.5 / tmpb->out_radi());
131 p = tmpb->move(cc, i * this->DH());
133 !this->slot().collide(p->frame())
135 this->slotSide() == LEFT
136 && p->h() < this->slotHeading()
138 this->slotSide() == RIGHT
139 && p->h() > this->slotHeading()
143 std::vector<RRTEdge *> eds = p->frame();
154 this->goals_.push_back(p);
157 p = tmpb->move(cc, i * this->DH());
160 this->cusp().push_back(tmpc);
163 // see https://courses.cs.washington.edu/courses/cse326/03su/homework/hw3/bfs.html
164 // RRTNode.s() works as iteration level
165 std::queue<BicycleCar *, std::list<BicycleCar *>> q;
166 std::queue<BicycleCar *, std::list<BicycleCar *>> empty;
168 if (this->slotSide() == LEFT)
170 BicycleCar *CC = this->getEPC();
171 BicycleCar *B = this->getEP();
172 this->DH(di * 0.01 / CC->out_radi());
175 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
176 while (!this->slot().collide(c->frame())) {
178 std::vector<RRTEdge *> eds = c->frame();
189 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
192 delete c; // not in q and collide
196 if (this->isInside(c)) {
197 goto createcuspandfinish;
198 } else if (c->s() < 9) {
199 BicycleCar *cc = this->flnc(c, co, so);
204 delete c; // not in q and collide
210 std::vector<RRTNode *> cusp;
212 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
215 std::reverse(cusp.begin(), cusp.end());
216 this->cusp().push_back(cusp);
220 void ParallelSlot::fipf(
221 std::vector<CircleObstacle>& co,
222 std::vector<SegmentObstacle>& so
226 std::vector<RRTNode *> tmpc;
227 BicycleCar *tmpf = this->getFPf();
228 BicycleCar *tmpb = this->getISPPf(tmpf);
230 if (this->slotSide() == LEFT)
234 if (this->slotSide() == LEFT)
235 this->DH(-1 * 0.5 / tmpb->out_radi());
237 this->DH(1 * 0.5 / tmpb->out_radi());
240 p = tmpb->move(cc, i * this->DH());
242 !this->slot().collide(p->frame())
244 this->slotSide() == LEFT
245 && p->h() > this->slotHeading()
247 this->slotSide() == RIGHT
248 && p->h() < this->slotHeading()
252 std::vector<RRTEdge *> eds = p->frame();
263 this->goals_.push_back(p);
266 p = tmpb->move(cc, i * this->DH());
269 this->cusp().push_back(tmpc);
273 BicycleCar *ParallelSlot::flnc(
275 std::vector<CircleObstacle>& co,
276 std::vector<SegmentObstacle>& so
280 if (this->slotSide() == LEFT) {
281 if (int(B->s()) % 2 == 0)
282 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
284 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
286 if (int(B->s()) % 2 == 0)
287 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
289 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
293 p = B->move(cc, i * this->DH());
295 !this->slot().collide(p->frame())
296 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
300 p = B->move(cc, i * this->DH());
302 std::vector<RRTEdge *> eds = p->frame();
315 p = B->move(cc, i * this->DH());
317 !this->slot().collide(p->frame())
318 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
320 if (this->isInside(p)) {
326 p = B->move(cc, i * this->DH());
328 std::vector<RRTEdge *> eds = p->frame();
341 return B->move(cc, (i - 1) * this->DH());
344 void ParallelSlot::fipr(RRTNode *n)
346 return this->fipr(new BicycleCar(n->x(), n->y(), n->h()));
349 void ParallelSlot::fipr(BicycleCar *B)
351 std::vector<RRTNode *> cusp;
352 cusp.push_back(new RRTNode(B->x(), B->y(), B->h()));
354 if (this->slotSide() == LEFT)
356 if (this->slotType() == PERPENDICULAR) {
357 this->DH(di * 0.01 / B->out_radi()); // TODO car in slot h()
359 if (this->slotSide() == LEFT)
360 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
362 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
365 p = B->move(cc, i * this->DH());
367 !this->slot().collide(p->frame())
368 && this->slot().collide(p)
372 p = B->move(cc, i * this->DH());
375 p = B->move(cc, i * this->DH());
377 !this->slot().collide(p->frame())
378 && this->slot().collide(p)
382 p = B->move(cc, i * this->DH());
385 p = B->move(cc, i * this->DH());
386 cusp.push_back(new RRTNode(p->x(), p->y(), p->h()));
387 std::reverse(cusp.begin(), cusp.end());
388 this->cusp().push_back(cusp);
391 this->DH(di * 0.01 / B->out_radi());
396 this->slotSide() == LEFT
397 && this->slot().collide(new RRTNode(c->lfx(), c->lfy(), 0))
399 this->slotSide() == RIGHT
400 && this->slot().collide(new RRTNode(c->rfx(), c->rfy(), 0))
402 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
403 BicycleCar *cc = this->flncr(c);
408 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
409 std::reverse(cusp.begin(), cusp.end());
410 this->cusp().push_back(cusp);
413 BicycleCar *ParallelSlot::flncr(BicycleCar *B)
416 if (this->slotSide() == LEFT) {
417 if (int(B->s()) % 2 == 0)
418 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
420 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
422 if (int(B->s()) % 2 == 0)
423 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
425 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
429 p = B->move(cc, i * this->DH());
431 !this->slot().collide(p->frame())
433 this->slotSide() == LEFT
434 && this->slot().collide(new RRTNode(
440 this->slotSide() == RIGHT
441 && this->slot().collide(new RRTNode(
450 p = B->move(cc, i * this->DH());
453 p = B->move(cc, i * this->DH());
454 while (!this->slot().collide(p->frame())) {
456 this->slotSide() == LEFT
457 && !this->slot().collide(new RRTNode(
467 this->slotSide() == RIGHT
468 && !this->slot().collide(new RRTNode(
478 p = B->move(cc, i * this->DH());
481 return B->move(cc, (i - 1) * this->DH());
484 RRTNode *ParallelSlot::fposecenter()
486 return this->slot().bnodes().back();
489 bool ParallelSlot::flast(
493 std::vector<RRTNode *> &cusp
496 BicycleCar *B = new BicycleCar(P->x(), P->y(), P->h());
499 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
501 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
504 p = B->move(cc, i * this->DH());
505 while (!this->slot().collide(p->frame())
507 (this->DH() > 0 && p->x() <= 0)
508 || (this->DH() < 0 && p->x() >= 0)
512 p = B->move(cc, i * this->DH());
515 p = B->move(cc, i * this->DH());
516 while (!this->slot().collide(p->frame())
518 (this->DH() > 0 && p->x() <= 0)
519 || (this->DH() < 0 && p->x() >= 0)
521 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
525 if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
531 p = B->move(cc, i * this->DH());
534 p = B->move(cc, (i - 1) * this->DH());
535 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
538 } else if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
543 return this->flast(p, !right, il + 1, cusp);
548 void ParallelSlot::fpose()
550 bool left = false; // right parking slot
552 BicycleCar *CC = new BicycleCar(
553 this->fposecenter()->x(),
554 this->fposecenter()->y() - 0.01,
557 BicycleCar *B = new BicycleCar(
558 CC->x() - CC->width() / 2,
559 CC->y() - (CC->length() + CC->wheelbase()) / 2,
562 if (this->slot().bnodes()[0]->x() > this->slot().bnodes()[1]->x()) {
567 CC->x() + CC->width() / 2,
568 CC->y() - (CC->length() + CC->wheelbase()) / 2,
572 this->DH(di * 0.01 / CC->out_radi());
575 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
576 while (!this->slot().collide(p->frame())) {
577 std::vector<RRTNode *> tmpcusp;
578 tmpcusp.push_back(new BicycleCar(p->x(), p->y(), p->h()));
579 if (this->flast(p, left, 0, tmpcusp)) {
580 this->cusp().push_back(tmpcusp);
585 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
589 BicycleCar *ParallelSlot::getEP()
591 // new pose for parallel parking to right slot
596 BicycleCar *CC = this->getEPC();
597 // move left by car width / 2
598 tnx = CC->x() + CC->width() / 2 * cos(CC->h() + M_PI / 2);
599 tny = CC->y() + CC->width() / 2 * sin(CC->h() + M_PI / 2);
600 if (this->slotSide() == LEFT) {
601 // move right by car width / 2
602 tnx = CC->x() + CC->width() / 2 * cos(CC->h() - M_PI / 2);
603 tny = CC->y() + CC->width() / 2 * sin(CC->h() - M_PI / 2);
605 if (this->slotType() == PARALLEL) {
607 nx = tnx - (CC->length() + CC->wheelbase()) / 2 * cos(CC->h());
608 ny = tny - (CC->length() + CC->wheelbase()) / 2 * sin(CC->h());
611 nx = tnx + (CC->length() - CC->wheelbase()) / 2 * cos(CC->h());
612 ny = tny + (CC->length() - CC->wheelbase()) / 2 * sin(CC->h());
614 return new BicycleCar(nx, ny, CC->h());
617 BicycleCar *ParallelSlot::getEPC()
619 // new pose for parallel parking to right slot
623 ta = this->slotHeading() + M_PI;
624 if (this->slotSide() == RIGHT)
628 nx = this->fposecenter()->x() + 0.01 * cos(ta);
629 ny = this->fposecenter()->y() + 0.01 * sin(ta);
630 return new BicycleCar(nx, ny, this->slotHeading());
633 BicycleCar *ParallelSlot::getFP()
636 float x = this->slot().bnodes()[0]->x();
637 float y = this->slot().bnodes()[0]->y();
638 float h = this->slotHeading();
641 if (this->slotType() == PARALLEL) {
642 if (this->slotSide() == LEFT) {
643 nx = x + BCAR_WIDTH / 2 * cos(h + M_PI / 2);
644 ny = y + BCAR_WIDTH / 2 * sin(h + M_PI / 2);
646 nx = x + BCAR_WIDTH / 2 * cos(h - M_PI / 2);
647 ny = y + BCAR_WIDTH / 2 * sin(h - M_PI / 2);
649 x = nx + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * cos(h);
650 y = ny + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * sin(h);
652 if (this->slotSide() == LEFT) {
654 nx = x + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
656 ny = y + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
658 x = nx + (BCAR_DIAG_RRADI) * cos(h + M_PI / 2);
659 y = ny + (BCAR_DIAG_RRADI) * sin(h + M_PI / 2);
662 nx = x + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
664 ny = y + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
666 x = nx + (BCAR_DIAG_RRADI) * cos(h - M_PI / 2);
667 y = ny + (BCAR_DIAG_RRADI) * sin(h - M_PI / 2);
670 return new BicycleCar(x, y, h);
673 BicycleCar *ParallelSlot::getISPP(BicycleCar *B)
675 float x = this->slot().bnodes().back()->x();
676 float y = this->slot().bnodes().back()->y();
678 if (this->slotSide() == LEFT) // TODO only for backward parking now
682 float IR = BCAR_IN_RADI;
685 float c = pow(x, 2) + pow(y - y0, 2) - pow(IR, 2);
686 float D = pow(b, 2) - 4 * a * c;
688 if (this->slotSide() == LEFT)
693 return new BicycleCar(x0, B->y(), B->h());
696 BicycleCar *ParallelSlot::getFPf()
699 float x = this->slot().bnodes().front()->x();
700 float y = this->slot().bnodes().front()->y();
701 float h = this->slotHeading();
704 if (this->slotSide() == LEFT) {
706 nx = x + (BCAR_LENGTH - BCAR_WHEEL_BASE) / 2
708 ny = y + (BCAR_LENGTH - BCAR_WHEEL_BASE) / 2
710 x = nx + (BCAR_DIAG_RRADI) * cos(h - M_PI / 2);
711 y = ny + (BCAR_DIAG_RRADI) * sin(h - M_PI / 2);
714 nx = x + (BCAR_LENGTH - BCAR_WHEEL_BASE) / 2
716 ny = y + (BCAR_LENGTH - BCAR_WHEEL_BASE) / 2
718 x = nx + (BCAR_DIAG_RRADI) * cos(h + M_PI / 2);
719 y = ny + (BCAR_DIAG_RRADI) * sin(h + M_PI / 2);
721 return new BicycleCar(x, y, h);
724 BicycleCar *ParallelSlot::getISPPf(BicycleCar *B)
726 float x = this->slot().bnodes().front()->x();
727 float y = this->slot().bnodes().front()->y();
729 if (this->slotSide() == LEFT)
733 float IR = BCAR_IN_RADI;
736 float c = pow(x, 2) + pow(y - y0, 2) - pow(IR, 2);
737 float D = pow(b, 2) - 4 * a * c;
739 if (this->slotSide() == LEFT)
746 x = this->slot().bnodes().back()->x();
747 y = this->slot().bnodes().back()->y();
751 c = pow(x, 2) + pow(y - y0, 2) - pow(IR, 2);
752 D = pow(b, 2) - 4 * a * c;
753 if (this->slotSide() == LEFT)
759 if (this->slotSide() == LEFT)
760 x0 = std::max(x0_1, x0_2);
762 x0 = std::min(x0_1, x0_2);
763 return new BicycleCar(x0, B->y(), B->h());
766 bool ParallelSlot::isInside(BicycleCar *c)
770 tmpn = new RRTNode(c->lfx(), c->lfy(), 0);
771 if (!this->slot().collide(tmpn))
774 tmpn = new RRTNode(c->lrx(), c->lry(), 0);
775 if (!this->slot().collide(tmpn))
778 tmpn = new RRTNode(c->rrx(), c->rry(), 0);
779 if (!this->slot().collide(tmpn))
782 tmpn = new RRTNode(c->rfx(), c->rfy(), 0);
783 if (!this->slot().collide(tmpn))
789 struct SamplingInfo ParallelSlot::getSamplingInfo()
791 struct SamplingInfo si;
792 RRTNode *n = this->getMidd();
793 if (this->slotType() == PARALLEL) {
795 si.x0 = n->x() + 1.5 * BCAR_LENGTH * cos(n->h());
796 si.y0 = n->y() + 1.5 * BCAR_LENGTH * sin(n->h());
799 si.x0 = this->slot().bnodes().front()->x();
800 si.y0 = this->slot().bnodes().front()->y();
801 si.h0 = this->slotHeading();