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()
155 std::vector<RRTEdge *> eds = p->frame();
166 this->goals_.push_back(p);
169 p = tmpb->move(cc, i * this->DH());
172 this->cusp().push_back(tmpc);
175 // see https://courses.cs.washington.edu/courses/cse326/03su/homework/hw3/bfs.html
176 // RRTNode.s() works as iteration level
177 std::queue<BicycleCar *, std::list<BicycleCar *>> q;
178 std::queue<BicycleCar *, std::list<BicycleCar *>> empty;
180 if (this->slotSide() == LEFT)
182 BicycleCar *CC = this->getEPC();
183 BicycleCar *B = this->getEP();
184 this->DH(di * 0.01 / CC->out_radi());
187 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
188 while (!this->slot().collide(c->frame())) {
190 std::vector<RRTEdge *> eds = c->frame();
201 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
204 delete c; // not in q and collide
208 if (this->isInside(c)) {
209 goto createcuspandfinish;
210 } else if (c->s() < 9) {
211 BicycleCar *cc = this->flnc(c, co, so);
216 delete c; // not in q and collide
222 std::vector<RRTNode *> cusp;
224 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
227 std::reverse(cusp.begin(), cusp.end());
228 this->cusp().push_back(cusp);
232 void ParallelSlot::fipf(
233 std::vector<CircleObstacle>& co,
234 std::vector<SegmentObstacle>& so
238 std::vector<RRTNode *> tmpc;
239 BicycleCar *tmpf = this->getFPf();
240 BicycleCar *tmpb = this->getISPPf(tmpf);
242 if (this->slotSide() == LEFT)
246 if (this->slotSide() == LEFT)
247 this->DH(-1 * 0.5 / tmpb->out_radi());
249 this->DH(1 * 0.5 / tmpb->out_radi());
252 p = tmpb->move(cc, i * this->DH());
254 !this->slot().collide(p->frame())
256 this->slotSide() == LEFT
257 && p->h() > this->slotHeading()
259 this->slotSide() == RIGHT
260 && p->h() < this->slotHeading()
264 std::vector<RRTEdge *> eds = p->frame();
275 this->goals_.push_back(p);
278 p = tmpb->move(cc, i * this->DH());
281 this->cusp().push_back(tmpc);
285 BicycleCar *ParallelSlot::flnc(
287 std::vector<CircleObstacle>& co,
288 std::vector<SegmentObstacle>& so
292 if (this->slotSide() == LEFT) {
293 if (int(B->s()) % 2 == 0)
294 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
296 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
298 if (int(B->s()) % 2 == 0)
299 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
301 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
305 p = B->move(cc, i * this->DH());
307 !this->slot().collide(p->frame())
308 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
312 p = B->move(cc, i * this->DH());
314 std::vector<RRTEdge *> eds = p->frame();
327 p = B->move(cc, i * this->DH());
329 !this->slot().collide(p->frame())
330 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
332 if (this->isInside(p)) {
338 p = B->move(cc, i * this->DH());
340 std::vector<RRTEdge *> eds = p->frame();
353 return B->move(cc, (i - 1) * this->DH());
356 void ParallelSlot::fipr(RRTNode *n)
358 return this->fipr(new BicycleCar(n->x(), n->y(), n->h()));
361 void ParallelSlot::fipr(BicycleCar *B)
363 std::vector<RRTNode *> cusp;
364 cusp.push_back(new RRTNode(B->x(), B->y(), B->h()));
366 if (this->slotSide() == LEFT)
368 if (this->slotType() == PERPENDICULAR) {
369 this->DH(di * 0.01 / B->out_radi()); // TODO car in slot h()
371 if (this->slotSide() == LEFT)
372 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
374 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
377 p = B->move(cc, i * this->DH());
379 !this->slot().collide(p->frame())
380 && this->slot().collide(p)
384 p = B->move(cc, i * this->DH());
387 p = B->move(cc, i * this->DH());
389 !this->slot().collide(p->frame())
390 && this->slot().collide(p)
394 p = B->move(cc, i * this->DH());
397 p = B->move(cc, i * this->DH());
398 cusp.push_back(new RRTNode(p->x(), p->y(), p->h()));
399 std::reverse(cusp.begin(), cusp.end());
400 this->cusp().push_back(cusp);
403 this->DH(di * 0.01 / B->out_radi());
408 this->slotSide() == LEFT
409 && this->slot().collide(new RRTNode(c->lfx(), c->lfy(), 0))
411 this->slotSide() == RIGHT
412 && this->slot().collide(new RRTNode(c->rfx(), c->rfy(), 0))
414 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
415 BicycleCar *cc = this->flncr(c);
420 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
421 std::reverse(cusp.begin(), cusp.end());
422 this->cusp().push_back(cusp);
425 BicycleCar *ParallelSlot::flncr(BicycleCar *B)
428 if (this->slotSide() == LEFT) {
429 if (int(B->s()) % 2 == 0)
430 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
432 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
434 if (int(B->s()) % 2 == 0)
435 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
437 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
441 p = B->move(cc, i * this->DH());
443 !this->slot().collide(p->frame())
445 this->slotSide() == LEFT
446 && this->slot().collide(new RRTNode(
452 this->slotSide() == RIGHT
453 && this->slot().collide(new RRTNode(
462 p = B->move(cc, i * this->DH());
465 p = B->move(cc, i * this->DH());
466 while (!this->slot().collide(p->frame())) {
468 this->slotSide() == LEFT
469 && !this->slot().collide(new RRTNode(
479 this->slotSide() == RIGHT
480 && !this->slot().collide(new RRTNode(
490 p = B->move(cc, i * this->DH());
493 return B->move(cc, (i - 1) * this->DH());
496 RRTNode *ParallelSlot::fposecenter()
498 return this->slot().bnodes().back();
501 bool ParallelSlot::flast(
505 std::vector<RRTNode *> &cusp
508 BicycleCar *B = new BicycleCar(P->x(), P->y(), P->h());
511 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
513 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
516 p = B->move(cc, i * this->DH());
517 while (!this->slot().collide(p->frame())
519 (this->DH() > 0 && p->x() <= 0)
520 || (this->DH() < 0 && p->x() >= 0)
524 p = B->move(cc, i * this->DH());
527 p = B->move(cc, i * this->DH());
528 while (!this->slot().collide(p->frame())
530 (this->DH() > 0 && p->x() <= 0)
531 || (this->DH() < 0 && p->x() >= 0)
533 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
537 if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
543 p = B->move(cc, i * this->DH());
546 p = B->move(cc, (i - 1) * this->DH());
547 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
550 } else if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
555 return this->flast(p, !right, il + 1, cusp);
560 void ParallelSlot::fpose()
562 bool left = false; // right parking slot
564 BicycleCar *CC = new BicycleCar(
565 this->fposecenter()->x(),
566 this->fposecenter()->y() - 0.01,
569 BicycleCar *B = new BicycleCar(
570 CC->x() - CC->width() / 2,
571 CC->y() - (CC->length() + CC->wheelbase()) / 2,
574 if (this->slot().bnodes()[0]->x() > this->slot().bnodes()[1]->x()) {
579 CC->x() + CC->width() / 2,
580 CC->y() - (CC->length() + CC->wheelbase()) / 2,
584 this->DH(di * 0.01 / CC->out_radi());
587 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
588 while (!this->slot().collide(p->frame())) {
589 std::vector<RRTNode *> tmpcusp;
590 tmpcusp.push_back(new BicycleCar(p->x(), p->y(), p->h()));
591 if (this->flast(p, left, 0, tmpcusp)) {
592 this->cusp().push_back(tmpcusp);
597 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
601 BicycleCar *ParallelSlot::getEP()
603 // new pose for parallel parking to right slot
608 BicycleCar *CC = this->getEPC();
609 // move left by car width / 2
610 tnx = CC->x() + CC->width() / 2 * cos(CC->h() + M_PI / 2);
611 tny = CC->y() + CC->width() / 2 * sin(CC->h() + M_PI / 2);
612 if (this->slotSide() == LEFT) {
613 // move right by car width / 2
614 tnx = CC->x() + CC->width() / 2 * cos(CC->h() - M_PI / 2);
615 tny = CC->y() + CC->width() / 2 * sin(CC->h() - M_PI / 2);
617 if (this->slotType() == PARALLEL) {
619 nx = tnx - (CC->length() + CC->wheelbase()) / 2 * cos(CC->h());
620 ny = tny - (CC->length() + CC->wheelbase()) / 2 * sin(CC->h());
623 nx = tnx + (CC->length() - CC->wheelbase()) / 2 * cos(CC->h());
624 ny = tny + (CC->length() - CC->wheelbase()) / 2 * sin(CC->h());
626 return new BicycleCar(nx, ny, CC->h());
629 BicycleCar *ParallelSlot::getEPC()
631 // new pose for parallel parking to right slot
635 ta = this->slotHeading() + M_PI;
636 if (this->slotSide() == RIGHT)
640 nx = this->fposecenter()->x() + 0.01 * cos(ta);
641 ny = this->fposecenter()->y() + 0.01 * sin(ta);
642 return new BicycleCar(nx, ny, this->slotHeading());
645 BicycleCar *ParallelSlot::getFP()
648 float x = this->slot().bnodes()[0]->x();
649 float y = this->slot().bnodes()[0]->y();
650 float h = this->slotHeading();
651 float ph = this->poseHeading();
654 if (this->slotType() == PARALLEL) {
655 if (this->slotSide() == LEFT) {
656 nx = x + BCAR_WIDTH / 2 * cos(h + M_PI / 2);
657 ny = y + BCAR_WIDTH / 2 * sin(h + M_PI / 2);
659 nx = x + BCAR_WIDTH / 2 * cos(h - M_PI / 2);
660 ny = y + BCAR_WIDTH / 2 * sin(h - M_PI / 2);
662 x = nx + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * cos(h);
663 y = ny + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * sin(h);
665 if (this->slotSide() == LEFT) {
666 nx = x + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
668 ny = y + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
670 x = nx + (BCAR_DIAG_RRADI) * cos(h);
671 y = ny + (BCAR_DIAG_RRADI) * sin(h);
673 nx = x + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
675 ny = y + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
677 x = nx + (BCAR_DIAG_RRADI) * cos(h);
678 y = ny + (BCAR_DIAG_RRADI) * sin(h);
681 return new BicycleCar(x, y, ph);
684 BicycleCar *ParallelSlot::getISPP(BicycleCar *B)
686 // rigt side (for right parking slot)
687 float x = this->slot().bnodes().back()->x();
688 float y = this->slot().bnodes().back()->y();
691 if (this->slotSide() == LEFT) {
698 float IR = BCAR_IN_RADI;
700 float b = (x1 - x) * 2 * cos(B->h()) + (y1 - y) * 2 * sin(B->h());
701 float c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
702 float D = pow(b, 2) - 4 * a * c;
704 delta = -b - sqrt(D);
706 float delta_1 = delta;
707 // left front (for right parking slot)
708 x = this->slot().bnodes().front()->x();
709 y = this->slot().bnodes().front()->y();
712 b = (x1 - x) * 2 * cos(B->h()) + (y1 - y) * 2 * sin(B->h());
713 c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
714 D = pow(b, 2) - 4 * a * c;
715 //delta = -b + sqrt(D);
717 float delta_2 = delta;
718 delta = -b - sqrt(D);
720 float delta_3 = delta;
721 delta = std::max(delta_1, std::max(delta_2, delta_3));
722 return new BicycleCar(
723 B->x() + delta * cos(B->h()),
724 B->y() + delta * sin(B->h()),
729 BicycleCar *ParallelSlot::getFPf()
732 float x = this->slot().bnodes().front()->x();
733 float y = this->slot().bnodes().front()->y();
734 float h = this->slotHeading();
735 float ph = this->poseHeading();
743 nx = x + (BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 * cos(ph);
744 ny = y + (BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 * sin(ph);
745 x = nx + (BCAR_DIAG_RRADI) * cos(h);
746 y = ny + (BCAR_DIAG_RRADI) * sin(h);
747 return new BicycleCar(x, y, ph);
750 BicycleCar *ParallelSlot::getISPPf(BicycleCar *B)
752 // right rear (for right parking slot)
753 float x = this->slot().bnodes().front()->x();
754 float y = this->slot().bnodes().front()->y();
757 if (this->slotSide() == LEFT) {
764 float IR = BCAR_IN_RADI;
766 float b = (x - x1) * 2 * cos(B->h()) + (y - y1) * 2 * sin(B->h());
767 float c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
768 float D = pow(b, 2) - 4 * a * c;
770 delta = -b - sqrt(D); // TODO why this works?
772 float delta_1 = delta;
773 // left front (for right parking slot)
774 x = this->slot().bnodes().back()->x();
775 y = this->slot().bnodes().back()->y();
778 b = (x - x1) * 2 * cos(B->h()) + (y - y1) * 2 * sin(B->h());
779 c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
780 D = pow(b, 2) - 4 * a * c;
781 delta = -b + sqrt(D);
783 float delta_2 = delta;
784 delta = -b - sqrt(D);
786 float delta_3 = delta;
787 delta = std::max(delta_1, std::max(delta_2, delta_3));
788 return new BicycleCar(
789 B->x() - delta * cos(B->h()),
790 B->y() - delta * sin(B->h()),
795 bool ParallelSlot::isInside(BicycleCar *c)
799 tmpn = new RRTNode(c->lfx(), c->lfy(), 0);
800 if (!this->slot().collide(tmpn))
803 tmpn = new RRTNode(c->lrx(), c->lry(), 0);
804 if (!this->slot().collide(tmpn))
807 tmpn = new RRTNode(c->rrx(), c->rry(), 0);
808 if (!this->slot().collide(tmpn))
811 tmpn = new RRTNode(c->rfx(), c->rfy(), 0);
812 if (!this->slot().collide(tmpn))
818 struct SamplingInfo ParallelSlot::getSamplingInfo()
820 struct SamplingInfo si;
821 RRTNode *n = this->getMidd();
822 if (this->slotType() == PARALLEL) {
824 si.x0 = n->x() + 1.5 * BCAR_LENGTH * cos(n->h());
825 si.y0 = n->y() + 1.5 * BCAR_LENGTH * sin(n->h());
828 si.x0 = this->slot().bnodes().front()->x();
829 si.y0 = this->slot().bnodes().front()->y();
830 si.h0 = this->slotHeading();