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
148 && p->h() > this->slotHeading()
152 std::vector<RRTEdge *> eds = p->frame();
163 this->goals_.push_back(p);
166 p = tmpb->move(cc, i * this->DH());
169 this->cusp().push_back(tmpc);
172 // see https://courses.cs.washington.edu/courses/cse326/03su/homework/hw3/bfs.html
173 // RRTNode.s() works as iteration level
174 std::queue<BicycleCar *, std::list<BicycleCar *>> q;
175 std::queue<BicycleCar *, std::list<BicycleCar *>> empty;
177 if (this->slotSide() == LEFT)
179 BicycleCar *CC = this->getEPC();
180 BicycleCar *B = this->getEP();
181 this->DH(di * 0.01 / CC->out_radi());
184 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
185 while (!this->slot().collide(c->frame())) {
187 std::vector<RRTEdge *> eds = c->frame();
198 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
201 delete c; // not in q and collide
205 if (this->isInside(c)) {
206 goto createcuspandfinish;
207 } else if (c->s() < 9) {
208 BicycleCar *cc = this->flnc(c, co, so);
213 delete c; // not in q and collide
219 std::vector<RRTNode *> cusp;
221 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
224 std::reverse(cusp.begin(), cusp.end());
225 this->cusp().push_back(cusp);
229 void ParallelSlot::fipf(
230 std::vector<CircleObstacle>& co,
231 std::vector<SegmentObstacle>& so
235 std::vector<RRTNode *> tmpc;
236 BicycleCar *tmpf = this->getFPf();
237 BicycleCar *tmpb = this->getISPPf(tmpf);
239 if (this->slotSide() == LEFT)
243 if (this->slotSide() == LEFT)
244 this->DH(-1 * 0.5 / tmpb->out_radi());
246 this->DH(1 * 0.5 / tmpb->out_radi());
249 p = tmpb->move(cc, i * this->DH());
251 !this->slot().collide(p->frame())
253 this->slotSide() == LEFT
254 && p->h() > this->slotHeading()
256 this->slotSide() == RIGHT
257 && p->h() < this->slotHeading()
261 std::vector<RRTEdge *> eds = p->frame();
272 this->goals_.push_back(p);
275 p = tmpb->move(cc, i * this->DH());
278 this->cusp().push_back(tmpc);
282 BicycleCar *ParallelSlot::flnc(
284 std::vector<CircleObstacle>& co,
285 std::vector<SegmentObstacle>& so
289 if (this->slotSide() == LEFT) {
290 if (int(B->s()) % 2 == 0)
291 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
293 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
295 if (int(B->s()) % 2 == 0)
296 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
298 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
302 p = B->move(cc, i * this->DH());
304 !this->slot().collide(p->frame())
305 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
309 p = B->move(cc, i * this->DH());
311 std::vector<RRTEdge *> eds = p->frame();
324 p = B->move(cc, i * this->DH());
326 !this->slot().collide(p->frame())
327 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
329 if (this->isInside(p)) {
335 p = B->move(cc, i * this->DH());
337 std::vector<RRTEdge *> eds = p->frame();
350 return B->move(cc, (i - 1) * this->DH());
353 void ParallelSlot::fipr(RRTNode *n)
355 return this->fipr(new BicycleCar(n->x(), n->y(), n->h()));
358 void ParallelSlot::fipr(BicycleCar *B)
360 std::vector<RRTNode *> cusp;
361 cusp.push_back(new RRTNode(B->x(), B->y(), B->h()));
363 if (this->slotSide() == LEFT)
365 if (this->slotType() == PERPENDICULAR) {
366 this->DH(di * 0.01 / B->out_radi()); // TODO car in slot h()
368 if (this->slotSide() == LEFT)
369 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
371 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
374 p = B->move(cc, i * this->DH());
376 !this->slot().collide(p->frame())
377 && this->slot().collide(p)
381 p = B->move(cc, i * this->DH());
384 p = B->move(cc, i * this->DH());
386 !this->slot().collide(p->frame())
387 && this->slot().collide(p)
391 p = B->move(cc, i * this->DH());
394 p = B->move(cc, i * this->DH());
395 cusp.push_back(new RRTNode(p->x(), p->y(), p->h()));
396 std::reverse(cusp.begin(), cusp.end());
397 this->cusp().push_back(cusp);
400 this->DH(di * 0.01 / B->out_radi());
405 this->slotSide() == LEFT
406 && this->slot().collide(new RRTNode(c->lfx(), c->lfy(), 0))
408 this->slotSide() == RIGHT
409 && this->slot().collide(new RRTNode(c->rfx(), c->rfy(), 0))
411 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
412 BicycleCar *cc = this->flncr(c);
417 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
418 std::reverse(cusp.begin(), cusp.end());
419 this->cusp().push_back(cusp);
422 BicycleCar *ParallelSlot::flncr(BicycleCar *B)
425 if (this->slotSide() == LEFT) {
426 if (int(B->s()) % 2 == 0)
427 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
429 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
431 if (int(B->s()) % 2 == 0)
432 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
434 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
438 p = B->move(cc, i * this->DH());
440 !this->slot().collide(p->frame())
442 this->slotSide() == LEFT
443 && this->slot().collide(new RRTNode(
449 this->slotSide() == RIGHT
450 && this->slot().collide(new RRTNode(
459 p = B->move(cc, i * this->DH());
462 p = B->move(cc, i * this->DH());
463 while (!this->slot().collide(p->frame())) {
465 this->slotSide() == LEFT
466 && !this->slot().collide(new RRTNode(
476 this->slotSide() == RIGHT
477 && !this->slot().collide(new RRTNode(
487 p = B->move(cc, i * this->DH());
490 return B->move(cc, (i - 1) * this->DH());
493 RRTNode *ParallelSlot::fposecenter()
495 return this->slot().bnodes().back();
498 bool ParallelSlot::flast(
502 std::vector<RRTNode *> &cusp
505 BicycleCar *B = new BicycleCar(P->x(), P->y(), P->h());
508 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
510 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
513 p = B->move(cc, i * this->DH());
514 while (!this->slot().collide(p->frame())
516 (this->DH() > 0 && p->x() <= 0)
517 || (this->DH() < 0 && p->x() >= 0)
521 p = B->move(cc, i * this->DH());
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)
530 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
534 if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
540 p = B->move(cc, i * this->DH());
543 p = B->move(cc, (i - 1) * this->DH());
544 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
547 } else if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
552 return this->flast(p, !right, il + 1, cusp);
557 void ParallelSlot::fpose()
559 bool left = false; // right parking slot
561 BicycleCar *CC = new BicycleCar(
562 this->fposecenter()->x(),
563 this->fposecenter()->y() - 0.01,
566 BicycleCar *B = new BicycleCar(
567 CC->x() - CC->width() / 2,
568 CC->y() - (CC->length() + CC->wheelbase()) / 2,
571 if (this->slot().bnodes()[0]->x() > this->slot().bnodes()[1]->x()) {
576 CC->x() + CC->width() / 2,
577 CC->y() - (CC->length() + CC->wheelbase()) / 2,
581 this->DH(di * 0.01 / CC->out_radi());
584 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
585 while (!this->slot().collide(p->frame())) {
586 std::vector<RRTNode *> tmpcusp;
587 tmpcusp.push_back(new BicycleCar(p->x(), p->y(), p->h()));
588 if (this->flast(p, left, 0, tmpcusp)) {
589 this->cusp().push_back(tmpcusp);
594 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
598 BicycleCar *ParallelSlot::getEP()
600 // new pose for parallel parking to right slot
605 BicycleCar *CC = this->getEPC();
606 // move left by car width / 2
607 tnx = CC->x() + CC->width() / 2 * cos(CC->h() + M_PI / 2);
608 tny = CC->y() + CC->width() / 2 * sin(CC->h() + M_PI / 2);
609 if (this->slotSide() == LEFT) {
610 // move right by car width / 2
611 tnx = CC->x() + CC->width() / 2 * cos(CC->h() - M_PI / 2);
612 tny = CC->y() + CC->width() / 2 * sin(CC->h() - M_PI / 2);
614 if (this->slotType() == PARALLEL) {
616 nx = tnx - (CC->length() + CC->wheelbase()) / 2 * cos(CC->h());
617 ny = tny - (CC->length() + CC->wheelbase()) / 2 * sin(CC->h());
620 nx = tnx + (CC->length() - CC->wheelbase()) / 2 * cos(CC->h());
621 ny = tny + (CC->length() - CC->wheelbase()) / 2 * sin(CC->h());
623 return new BicycleCar(nx, ny, CC->h());
626 BicycleCar *ParallelSlot::getEPC()
628 // new pose for parallel parking to right slot
632 ta = this->slotHeading() + M_PI;
633 if (this->slotSide() == RIGHT)
637 nx = this->fposecenter()->x() + 0.01 * cos(ta);
638 ny = this->fposecenter()->y() + 0.01 * sin(ta);
639 return new BicycleCar(nx, ny, this->slotHeading());
642 BicycleCar *ParallelSlot::getFP()
645 float x = this->slot().bnodes()[0]->x();
646 float y = this->slot().bnodes()[0]->y();
647 float h = this->slotHeading();
650 if (this->slotType() == PARALLEL) {
651 if (this->slotSide() == LEFT) {
652 nx = x + BCAR_WIDTH / 2 * cos(h + M_PI / 2);
653 ny = y + BCAR_WIDTH / 2 * sin(h + M_PI / 2);
655 nx = x + BCAR_WIDTH / 2 * cos(h - M_PI / 2);
656 ny = y + BCAR_WIDTH / 2 * sin(h - M_PI / 2);
658 x = nx + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * cos(h);
659 y = ny + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * sin(h);
661 if (this->slotSide() == LEFT) {
663 nx = x + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
665 ny = y + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
667 x = nx + (BCAR_DIAG_RRADI) * cos(h + M_PI / 2);
668 y = ny + (BCAR_DIAG_RRADI) * sin(h + M_PI / 2);
671 nx = x + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
673 ny = y + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
675 x = nx + (BCAR_DIAG_RRADI) * cos(h - M_PI / 2);
676 y = ny + (BCAR_DIAG_RRADI) * sin(h - M_PI / 2);
679 return new BicycleCar(x, y, h);
682 BicycleCar *ParallelSlot::getISPP(BicycleCar *B)
684 float x = this->slot().bnodes().back()->x();
685 float y = this->slot().bnodes().back()->y();
687 if (this->slotSide() == LEFT) // TODO only for backward parking now
691 float IR = BCAR_IN_RADI;
694 float c = pow(x, 2) + pow(y - y0, 2) - pow(IR, 2);
695 float D = pow(b, 2) - 4 * a * c;
697 if (this->slotSide() == LEFT)
702 return new BicycleCar(x0, B->y(), B->h());
705 BicycleCar *ParallelSlot::getFPf()
708 float x = this->slot().bnodes().front()->x();
709 float y = this->slot().bnodes().front()->y();
710 float h = this->slotHeading();
711 float ph = this->poseHeading();
719 if (this->slotSide() == LEFT) {
720 nx = x + (BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 * cos(ph);
721 ny = y + (BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 * sin(ph);
722 x = nx + (BCAR_DIAG_RRADI) * cos(h);
723 y = ny + (BCAR_DIAG_RRADI) * sin(h);
725 nx = x + (BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 * cos(ph);
726 ny = y + (BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 * sin(ph);
727 x = nx + (BCAR_DIAG_RRADI) * cos(h);
728 y = ny + (BCAR_DIAG_RRADI) * sin(h);
730 return new BicycleCar(x, y, ph);
733 BicycleCar *ParallelSlot::getISPPf(BicycleCar *B)
735 float x = this->slot().bnodes().front()->x();
736 float y = this->slot().bnodes().front()->y();
738 if (this->slotSide() == LEFT)
742 float IR = BCAR_IN_RADI;
745 float c = pow(x, 2) + pow(y - y0, 2) - pow(IR, 2);
746 float D = pow(b, 2) - 4 * a * c;
748 if (this->slotSide() == LEFT)
755 x = this->slot().bnodes().back()->x();
756 y = this->slot().bnodes().back()->y();
760 c = pow(x, 2) + pow(y - y0, 2) - pow(IR, 2);
761 D = pow(b, 2) - 4 * a * c;
762 if (this->slotSide() == LEFT)
768 if (this->slotSide() == LEFT)
769 x0 = std::max(x0_1, x0_2);
771 x0 = std::min(x0_1, x0_2);
772 return new BicycleCar(x0, B->y(), B->h());
775 bool ParallelSlot::isInside(BicycleCar *c)
779 tmpn = new RRTNode(c->lfx(), c->lfy(), 0);
780 if (!this->slot().collide(tmpn))
783 tmpn = new RRTNode(c->lrx(), c->lry(), 0);
784 if (!this->slot().collide(tmpn))
787 tmpn = new RRTNode(c->rrx(), c->rry(), 0);
788 if (!this->slot().collide(tmpn))
791 tmpn = new RRTNode(c->rfx(), c->rfy(), 0);
792 if (!this->slot().collide(tmpn))
798 struct SamplingInfo ParallelSlot::getSamplingInfo()
800 struct SamplingInfo si;
801 RRTNode *n = this->getMidd();
802 if (this->slotType() == PARALLEL) {
804 si.x0 = n->x() + 1.5 * BCAR_LENGTH * cos(n->h());
805 si.y0 = n->y() + 1.5 * BCAR_LENGTH * sin(n->h());
808 si.x0 = this->slot().bnodes().front()->x();
809 si.y0 = this->slot().bnodes().front()->y();
810 si.h0 = this->slotHeading();