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 BicycleCar *ParallelSlot::flnc(
222 std::vector<CircleObstacle>& co,
223 std::vector<SegmentObstacle>& so
227 if (this->slotSide() == LEFT) {
228 if (int(B->s()) % 2 == 0)
229 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
231 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
233 if (int(B->s()) % 2 == 0)
234 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
236 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
240 p = B->move(cc, i * this->DH());
242 !this->slot().collide(p->frame())
243 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
247 p = B->move(cc, i * this->DH());
249 std::vector<RRTEdge *> eds = p->frame();
262 p = B->move(cc, i * this->DH());
264 !this->slot().collide(p->frame())
265 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
267 if (this->isInside(p)) {
273 p = B->move(cc, i * this->DH());
275 std::vector<RRTEdge *> eds = p->frame();
288 return B->move(cc, (i - 1) * this->DH());
291 void ParallelSlot::fipr(RRTNode *n)
293 return this->fipr(new BicycleCar(n->x(), n->y(), n->h()));
296 void ParallelSlot::fipr(BicycleCar *B)
298 std::vector<RRTNode *> cusp;
299 cusp.push_back(new RRTNode(B->x(), B->y(), B->h()));
301 if (this->slotSide() == LEFT)
303 if (this->slotType() == PERPENDICULAR) {
304 this->DH(di * 0.01 / B->out_radi()); // TODO car in slot h()
306 if (this->slotSide() == LEFT)
307 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
309 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
312 p = B->move(cc, i * this->DH());
314 !this->slot().collide(p->frame())
315 && this->slot().collide(p)
319 p = B->move(cc, i * this->DH());
322 p = B->move(cc, i * this->DH());
324 !this->slot().collide(p->frame())
325 && this->slot().collide(p)
329 p = B->move(cc, i * this->DH());
332 p = B->move(cc, i * this->DH());
333 cusp.push_back(new RRTNode(p->x(), p->y(), p->h()));
334 std::reverse(cusp.begin(), cusp.end());
335 this->cusp().push_back(cusp);
338 this->DH(di * 0.01 / B->out_radi());
343 this->slotSide() == LEFT
344 && this->slot().collide(new RRTNode(c->lfx(), c->lfy(), 0))
346 this->slotSide() == RIGHT
347 && this->slot().collide(new RRTNode(c->rfx(), c->rfy(), 0))
349 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
350 BicycleCar *cc = this->flncr(c);
355 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
356 std::reverse(cusp.begin(), cusp.end());
357 this->cusp().push_back(cusp);
360 BicycleCar *ParallelSlot::flncr(BicycleCar *B)
363 if (this->slotSide() == LEFT) {
364 if (int(B->s()) % 2 == 0)
365 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
367 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
369 if (int(B->s()) % 2 == 0)
370 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
372 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
376 p = B->move(cc, i * this->DH());
378 !this->slot().collide(p->frame())
380 this->slotSide() == LEFT
381 && this->slot().collide(new RRTNode(
387 this->slotSide() == RIGHT
388 && this->slot().collide(new RRTNode(
397 p = B->move(cc, i * this->DH());
400 p = B->move(cc, i * this->DH());
401 while (!this->slot().collide(p->frame())) {
403 this->slotSide() == LEFT
404 && !this->slot().collide(new RRTNode(
414 this->slotSide() == RIGHT
415 && !this->slot().collide(new RRTNode(
425 p = B->move(cc, i * this->DH());
428 return B->move(cc, (i - 1) * this->DH());
431 RRTNode *ParallelSlot::fposecenter()
433 return this->slot().bnodes().back();
436 bool ParallelSlot::flast(
440 std::vector<RRTNode *> &cusp
443 BicycleCar *B = new BicycleCar(P->x(), P->y(), P->h());
446 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
448 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
451 p = B->move(cc, i * this->DH());
452 while (!this->slot().collide(p->frame())
454 (this->DH() > 0 && p->x() <= 0)
455 || (this->DH() < 0 && p->x() >= 0)
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->DH() > 0 && p->x() <= 0)
466 || (this->DH() < 0 && p->x() >= 0)
468 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
472 if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
478 p = B->move(cc, i * this->DH());
481 p = B->move(cc, (i - 1) * this->DH());
482 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
485 } else if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
490 return this->flast(p, !right, il + 1, cusp);
495 void ParallelSlot::fpose()
497 bool left = false; // right parking slot
499 BicycleCar *CC = new BicycleCar(
500 this->fposecenter()->x(),
501 this->fposecenter()->y() - 0.01,
504 BicycleCar *B = new BicycleCar(
505 CC->x() - CC->width() / 2,
506 CC->y() - (CC->length() + CC->wheelbase()) / 2,
509 if (this->slot().bnodes()[0]->x() > this->slot().bnodes()[1]->x()) {
514 CC->x() + CC->width() / 2,
515 CC->y() - (CC->length() + CC->wheelbase()) / 2,
519 this->DH(di * 0.01 / CC->out_radi());
522 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
523 while (!this->slot().collide(p->frame())) {
524 std::vector<RRTNode *> tmpcusp;
525 tmpcusp.push_back(new BicycleCar(p->x(), p->y(), p->h()));
526 if (this->flast(p, left, 0, tmpcusp)) {
527 this->cusp().push_back(tmpcusp);
532 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
536 BicycleCar *ParallelSlot::getEP()
538 // new pose for parallel parking to right slot
543 BicycleCar *CC = this->getEPC();
544 // move left by car width / 2
545 tnx = CC->x() + CC->width() / 2 * cos(CC->h() + M_PI / 2);
546 tny = CC->y() + CC->width() / 2 * sin(CC->h() + M_PI / 2);
547 if (this->slotSide() == LEFT) {
548 // move right by car width / 2
549 tnx = CC->x() + CC->width() / 2 * cos(CC->h() - M_PI / 2);
550 tny = CC->y() + CC->width() / 2 * sin(CC->h() - M_PI / 2);
552 if (this->slotType() == PARALLEL) {
554 nx = tnx - (CC->length() + CC->wheelbase()) / 2 * cos(CC->h());
555 ny = tny - (CC->length() + CC->wheelbase()) / 2 * sin(CC->h());
558 nx = tnx + (CC->length() - CC->wheelbase()) / 2 * cos(CC->h());
559 ny = tny + (CC->length() - CC->wheelbase()) / 2 * sin(CC->h());
561 return new BicycleCar(nx, ny, CC->h());
564 BicycleCar *ParallelSlot::getEPC()
566 // new pose for parallel parking to right slot
570 ta = this->slotHeading() + M_PI;
571 if (this->slotSide() == RIGHT)
575 nx = this->fposecenter()->x() + 0.01 * cos(ta);
576 ny = this->fposecenter()->y() + 0.01 * sin(ta);
577 return new BicycleCar(nx, ny, this->slotHeading());
580 BicycleCar *ParallelSlot::getFP()
583 float x = this->slot().bnodes()[0]->x();
584 float y = this->slot().bnodes()[0]->y();
585 float h = this->slotHeading();
588 if (this->slotType() == PARALLEL) {
589 if (this->slotSide() == LEFT) {
590 nx = x + BCAR_WIDTH / 2 * cos(h + M_PI / 2);
591 ny = y + BCAR_WIDTH / 2 * sin(h + M_PI / 2);
593 nx = x + BCAR_WIDTH / 2 * cos(h - M_PI / 2);
594 ny = y + BCAR_WIDTH / 2 * sin(h - M_PI / 2);
596 x = nx + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * cos(h);
597 y = ny + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * sin(h);
599 if (this->slotSide() == LEFT) {
601 nx = x + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
603 ny = y + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
605 x = nx + (BCAR_DIAG_RRADI) * cos(h + M_PI / 2);
606 y = ny + (BCAR_DIAG_RRADI) * sin(h + M_PI / 2);
609 nx = x + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
611 ny = y + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
613 x = nx + (BCAR_DIAG_RRADI) * cos(h - M_PI / 2);
614 y = ny + (BCAR_DIAG_RRADI) * sin(h - M_PI / 2);
617 return new BicycleCar(x, y, h);
620 BicycleCar *ParallelSlot::getISPP(BicycleCar *B)
622 float x = this->slot().bnodes().back()->x();
623 float y = this->slot().bnodes().back()->y();
625 if (this->slotSide() == LEFT) // TODO only for backward parking now
629 float IR = BCAR_IN_RADI;
632 float c = pow(x, 2) + pow(y - y0, 2) - pow(IR, 2);
633 float D = pow(b, 2) - 4 * a * c;
635 if (this->slotSide() == LEFT)
640 return new BicycleCar(x0, B->y(), B->h());
643 bool ParallelSlot::isInside(BicycleCar *c)
647 tmpn = new RRTNode(c->lfx(), c->lfy(), 0);
648 if (!this->slot().collide(tmpn))
651 tmpn = new RRTNode(c->lrx(), c->lry(), 0);
652 if (!this->slot().collide(tmpn))
655 tmpn = new RRTNode(c->rrx(), c->rry(), 0);
656 if (!this->slot().collide(tmpn))
659 tmpn = new RRTNode(c->rfx(), c->rfy(), 0);
660 if (!this->slot().collide(tmpn))
666 struct SamplingInfo ParallelSlot::getSamplingInfo()
668 struct SamplingInfo si;
669 RRTNode *n = this->getMidd();
670 if (this->slotType() == PARALLEL) {
672 si.x0 = n->x() + 1.5 * BCAR_LENGTH * cos(n->h());
673 si.y0 = n->y() + 1.5 * BCAR_LENGTH * sin(n->h());
676 si.x0 = this->slot().bnodes().front()->x();
677 si.y0 = this->slot().bnodes().front()->y();
678 si.h0 = this->slotHeading();