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 // TODO different slot headings
118 // this is jus for slot heading = pi / 2
119 this->DH(0.01 / BCAR_TURNING_RADIUS);
120 BicycleCar *perc = nullptr;
121 RRTNode *cc = nullptr;
122 BicycleCar *p = nullptr;
128 x = this->slot().bnodes()[3]->x();
129 y = this->slot().bnodes()[3]->y();
132 x -= BCAR_DIST_FRONT;
133 y = this->slot().bnodes()[3]->y();
134 y += BCAR_OUT_RRADI - BCAR_TURNING_RADIUS;
137 perc = new BicycleCar(x, y, h);
143 p = perc->move(cc, i * this->DH());
146 p = perc->move(cc, i * this->DH());
150 // (reset for parking backward)
151 x = this->slot().bnodes()[0]->x();
152 y = this->slot().bnodes()[0]->y();
155 x -= BCAR_DIST_FRONT;
156 // get y from quadratic equation
157 float tmpD = pow(-2 * this->slot().bnodes()[0]->y(), 2);
159 pow(x - this->slot().bnodes()[0]->x(), 2) +
160 pow(this->slot().bnodes()[0]->y(), 2) -
163 y = 2 * this->slot().bnodes()[0]->y();
166 y -= BCAR_TURNING_RADIUS;
167 // -- end of quadratic equation
170 perc = new BicycleCar(x, y, h);
176 p = perc->move(cc, i * this->DH());
179 p = perc->move(cc, i * this->DH());
183 std::vector<RRTNode *> cusp;
184 cusp.push_back(new RRTNode(p->x(), p->y(), p->h()));
185 cusp.push_back(new RRTNode(x, y, h));
186 this->cusp().push_back(cusp);
189 // see https://courses.cs.washington.edu/courses/cse326/03su/homework/hw3/bfs.html
190 // RRTNode.s() works as iteration level
191 std::queue<BicycleCar *, std::list<BicycleCar *>> q;
192 std::queue<BicycleCar *, std::list<BicycleCar *>> empty;
194 if (this->slotSide() == LEFT)
196 BicycleCar *CC = this->getEPC();
197 BicycleCar *B = this->getEP();
198 this->DH(di * 0.01 / CC->out_radi());
201 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
202 while (!this->slot().collide(c->frame())) {
204 std::vector<RRTEdge *> eds = c->frame();
215 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
218 delete c; // not in q and collide
222 if (this->isInside(c)) {
223 goto createcuspandfinish;
224 } else if (c->s() < 9) {
225 BicycleCar *cc = this->flnc(c, co, so);
230 delete c; // not in q and collide
236 std::vector<RRTNode *> cusp;
238 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
241 std::reverse(cusp.begin(), cusp.end());
242 this->cusp().push_back(cusp);
246 BicycleCar *ParallelSlot::flnc(
248 std::vector<CircleObstacle>& co,
249 std::vector<SegmentObstacle>& so
253 if (this->slotSide() == LEFT) {
254 if (int(B->s()) % 2 == 0)
255 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
257 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
259 if (int(B->s()) % 2 == 0)
260 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
262 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
266 p = B->move(cc, i * this->DH());
268 !this->slot().collide(p->frame())
269 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
273 p = B->move(cc, i * this->DH());
275 std::vector<RRTEdge *> eds = p->frame();
288 p = B->move(cc, i * this->DH());
290 !this->slot().collide(p->frame())
291 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
293 if (this->isInside(p)) {
299 p = B->move(cc, i * this->DH());
301 std::vector<RRTEdge *> eds = p->frame();
314 return B->move(cc, (i - 1) * this->DH());
317 void ParallelSlot::fipr(RRTNode *n)
319 return this->fipr(new BicycleCar(n->x(), n->y(), n->h()));
322 void ParallelSlot::fipr(BicycleCar *B)
324 std::vector<RRTNode *> cusp;
325 cusp.push_back(new RRTNode(B->x(), B->y(), B->h()));
327 if (this->slotSide() == LEFT)
329 if (this->slotType() == PERPENDICULAR) {
330 this->DH(di * 0.01 / B->out_radi()); // TODO car in slot h()
332 if (this->slotSide() == LEFT)
333 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
335 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
338 p = B->move(cc, i * this->DH());
340 !this->slot().collide(p->frame())
341 && this->slot().collide(p)
345 p = B->move(cc, i * this->DH());
348 p = B->move(cc, i * this->DH());
350 !this->slot().collide(p->frame())
351 && this->slot().collide(p)
355 p = B->move(cc, i * this->DH());
358 p = B->move(cc, i * this->DH());
359 cusp.push_back(new RRTNode(p->x(), p->y(), p->h()));
360 std::reverse(cusp.begin(), cusp.end());
361 this->cusp().push_back(cusp);
364 this->DH(di * 0.01 / B->out_radi());
369 this->slotSide() == LEFT
370 && this->slot().collide(new RRTNode(c->lfx(), c->lfy(), 0))
372 this->slotSide() == RIGHT
373 && this->slot().collide(new RRTNode(c->rfx(), c->rfy(), 0))
375 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
376 BicycleCar *cc = this->flncr(c);
381 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
382 std::reverse(cusp.begin(), cusp.end());
383 this->cusp().push_back(cusp);
386 BicycleCar *ParallelSlot::flncr(BicycleCar *B)
389 if (this->slotSide() == LEFT) {
390 if (int(B->s()) % 2 == 0)
391 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
393 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
395 if (int(B->s()) % 2 == 0)
396 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
398 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
402 p = B->move(cc, i * this->DH());
404 !this->slot().collide(p->frame())
406 this->slotSide() == LEFT
407 && this->slot().collide(new RRTNode(
413 this->slotSide() == RIGHT
414 && this->slot().collide(new RRTNode(
423 p = B->move(cc, i * this->DH());
426 p = B->move(cc, i * this->DH());
427 while (!this->slot().collide(p->frame())) {
429 this->slotSide() == LEFT
430 && !this->slot().collide(new RRTNode(
440 this->slotSide() == RIGHT
441 && !this->slot().collide(new RRTNode(
451 p = B->move(cc, i * this->DH());
454 return B->move(cc, (i - 1) * this->DH());
457 RRTNode *ParallelSlot::fposecenter()
459 return this->slot().bnodes().back();
462 bool ParallelSlot::flast(
466 std::vector<RRTNode *> &cusp
469 BicycleCar *B = new BicycleCar(P->x(), P->y(), P->h());
472 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
474 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
477 p = B->move(cc, i * this->DH());
478 while (!this->slot().collide(p->frame())
480 (this->DH() > 0 && p->x() <= 0)
481 || (this->DH() < 0 && p->x() >= 0)
485 p = B->move(cc, i * this->DH());
488 p = B->move(cc, i * this->DH());
489 while (!this->slot().collide(p->frame())
491 (this->DH() > 0 && p->x() <= 0)
492 || (this->DH() < 0 && p->x() >= 0)
494 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
498 if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
504 p = B->move(cc, i * this->DH());
507 p = B->move(cc, (i - 1) * this->DH());
508 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
511 } else if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
516 return this->flast(p, !right, il + 1, cusp);
521 void ParallelSlot::fpose()
523 bool left = false; // right parking slot
525 BicycleCar *CC = new BicycleCar(
526 this->fposecenter()->x(),
527 this->fposecenter()->y() - 0.01,
530 BicycleCar *B = new BicycleCar(
531 CC->x() - CC->width() / 2,
532 CC->y() - (CC->length() + CC->wheelbase()) / 2,
535 if (this->slot().bnodes()[0]->x() > this->slot().bnodes()[1]->x()) {
540 CC->x() + CC->width() / 2,
541 CC->y() - (CC->length() + CC->wheelbase()) / 2,
545 this->DH(di * 0.01 / CC->out_radi());
548 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
549 while (!this->slot().collide(p->frame())) {
550 std::vector<RRTNode *> tmpcusp;
551 tmpcusp.push_back(new BicycleCar(p->x(), p->y(), p->h()));
552 if (this->flast(p, left, 0, tmpcusp)) {
553 this->cusp().push_back(tmpcusp);
558 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
562 BicycleCar *ParallelSlot::getEP()
564 // new pose for parallel parking to right slot
569 BicycleCar *CC = this->getEPC();
570 // move left by car width / 2
571 tnx = CC->x() + CC->width() / 2 * cos(CC->h() + M_PI / 2);
572 tny = CC->y() + CC->width() / 2 * sin(CC->h() + M_PI / 2);
573 if (this->slotSide() == LEFT) {
574 // move right by car width / 2
575 tnx = CC->x() + CC->width() / 2 * cos(CC->h() - M_PI / 2);
576 tny = CC->y() + CC->width() / 2 * sin(CC->h() - M_PI / 2);
578 if (this->slotType() == PARALLEL) {
580 nx = tnx - (CC->length() + CC->wheelbase()) / 2 * cos(CC->h());
581 ny = tny - (CC->length() + CC->wheelbase()) / 2 * sin(CC->h());
584 nx = tnx + (CC->length() - CC->wheelbase()) / 2 * cos(CC->h());
585 ny = tny + (CC->length() - CC->wheelbase()) / 2 * sin(CC->h());
587 return new BicycleCar(nx, ny, CC->h());
590 BicycleCar *ParallelSlot::getEPC()
592 // new pose for parallel parking to right slot
596 ta = this->slotHeading() + M_PI;
597 if (this->slotSide() == RIGHT)
601 nx = this->fposecenter()->x() + 0.01 * cos(ta);
602 ny = this->fposecenter()->y() + 0.01 * sin(ta);
603 return new BicycleCar(nx, ny, this->slotHeading());
606 BicycleCar *ParallelSlot::getFP()
609 float x = this->slot().bnodes()[0]->x();
610 float y = this->slot().bnodes()[0]->y();
611 float h = this->slotHeading();
614 if (this->slotType() == PARALLEL) {
615 if (this->slotSide() == LEFT) {
616 nx = x + BCAR_WIDTH / 2 * cos(h + M_PI / 2);
617 ny = y + BCAR_WIDTH / 2 * sin(h + M_PI / 2);
619 nx = x + BCAR_WIDTH / 2 * cos(h - M_PI / 2);
620 ny = y + BCAR_WIDTH / 2 * sin(h - M_PI / 2);
622 x = nx + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * cos(h);
623 y = ny + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * sin(h);
625 if (this->slotSide() == LEFT) {
627 nx = x + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
629 ny = y + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
631 x = nx + (BCAR_DIAG_RRADI) * cos(h + M_PI / 2);
632 y = ny + (BCAR_DIAG_RRADI) * sin(h + M_PI / 2);
635 nx = x + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
637 ny = y + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
639 x = nx + (BCAR_DIAG_RRADI) * cos(h - M_PI / 2);
640 y = ny + (BCAR_DIAG_RRADI) * sin(h - M_PI / 2);
643 return new BicycleCar(x, y, h);
646 BicycleCar *ParallelSlot::getISPP(BicycleCar *B)
648 float x = this->slot().bnodes().back()->x();
649 float y = this->slot().bnodes().back()->y();
651 if (this->slotSide() == LEFT) // TODO only for backward parking now
655 float IR = BCAR_IN_RADI;
658 float c = pow(x, 2) + pow(y - y0, 2) - pow(IR, 2);
659 float D = pow(b, 2) - 4 * a * c;
660 float x0 = -b + sqrt(D);
662 return new BicycleCar(x0, B->y(), B->h());
665 bool ParallelSlot::isInside(BicycleCar *c)
669 tmpn = new RRTNode(c->lfx(), c->lfy(), 0);
670 if (!this->slot().collide(tmpn))
673 tmpn = new RRTNode(c->lrx(), c->lry(), 0);
674 if (!this->slot().collide(tmpn))
677 tmpn = new RRTNode(c->rrx(), c->rry(), 0);
678 if (!this->slot().collide(tmpn))
681 tmpn = new RRTNode(c->rfx(), c->rfy(), 0);
682 if (!this->slot().collide(tmpn))
688 struct SamplingInfo ParallelSlot::getSamplingInfo()
690 struct SamplingInfo si;
691 RRTNode *n = this->getMidd();
692 if (this->slotType() == PARALLEL) {
694 si.x0 = n->x() + 1.5 * BCAR_LENGTH * cos(n->h());
695 si.y0 = n->y() + 1.5 * BCAR_LENGTH * sin(n->h());
698 si.x0 = this->slot().bnodes().front()->x();
699 si.y0 = this->slot().bnodes().front()->y();
700 si.h0 = this->slotHeading();