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 RRTNode *ParallelSlot::getMidd()
30 if (this->cusp().size() > 0)
31 return this->cusp().front().front();
36 std::vector<std::vector<RRTNode *>> &ParallelSlot::cusp()
41 float ParallelSlot::DH() const
46 PolygonObstacle &ParallelSlot::slot()
51 float ParallelSlot::slotHeading()
53 return this->slotHeading_;
56 SlotSide ParallelSlot::slotSide()
58 return this->slotSide_;
61 SlotType ParallelSlot::slotType()
63 return this->slotType_;
67 void ParallelSlot::DH(float dh)
72 void ParallelSlot::setAll()
75 float y0 = this->slot().bnodes()[0]->y();
76 float x0 = this->slot().bnodes()[0]->x();
77 float y3 = this->slot().bnodes()[3]->y();
78 float x3 = this->slot().bnodes()[3]->x();
81 this->slotHeading_ = atan2(dy, dx);
83 float y1 = this->slot().bnodes()[1]->y();
84 float x1 = this->slot().bnodes()[1]->x();
85 if (sgn((x1 - x0) * (y3 - y0) - (y1 - y0) * (x3 - x0)) < 0)
86 this->slotSide_ = LEFT;
88 this->slotSide_ = RIGHT;
91 this->slot().bnodes()[0],
92 this->slot().bnodes()[1]
95 this->slot().bnodes()[1],
96 this->slot().bnodes()[2]
99 this->slotType_ = PERPENDICULAR;
101 this->slotType_ = PARALLEL;
105 void ParallelSlot::fip(
106 std::vector<CircleObstacle>& co,
107 std::vector<SegmentObstacle>& so
111 if (this->slotType() == PERPENDICULAR) {
112 // TODO different slot headings
113 // this is jus for slot heading = pi / 2
114 this->DH(0.01 / BCAR_TURNING_RADIUS);
115 BicycleCar *perc = nullptr;
116 RRTNode *cc = nullptr;
117 BicycleCar *p = nullptr;
123 x = this->slot().bnodes()[3]->x();
124 y = this->slot().bnodes()[3]->y();
127 x -= BCAR_DIST_FRONT;
128 y = this->slot().bnodes()[3]->y();
129 y += BCAR_OUT_RRADI - BCAR_TURNING_RADIUS;
132 perc = new BicycleCar(x, y, h);
138 p = perc->move(cc, i * this->DH());
141 p = perc->move(cc, i * this->DH());
145 // (reset for parking backward)
146 x = this->slot().bnodes()[0]->x();
147 y = this->slot().bnodes()[0]->y();
150 x -= BCAR_DIST_FRONT;
151 // get y from quadratic equation
152 float tmpD = pow(-2 * this->slot().bnodes()[0]->y(), 2);
154 pow(x - this->slot().bnodes()[0]->x(), 2) +
155 pow(this->slot().bnodes()[0]->y(), 2) -
158 y = 2 * this->slot().bnodes()[0]->y();
161 y -= BCAR_TURNING_RADIUS;
162 // -- end of quadratic equation
165 perc = new BicycleCar(x, y, h);
171 p = perc->move(cc, i * this->DH());
174 p = perc->move(cc, i * this->DH());
178 std::vector<RRTNode *> cusp;
179 cusp.push_back(new RRTNode(p->x(), p->y(), p->h()));
180 cusp.push_back(new RRTNode(x, y, h));
181 this->cusp().push_back(cusp);
184 // see https://courses.cs.washington.edu/courses/cse326/03su/homework/hw3/bfs.html
185 // RRTNode.s() works as iteration level
186 std::queue<BicycleCar *, std::list<BicycleCar *>> q;
187 std::queue<BicycleCar *, std::list<BicycleCar *>> empty;
189 if (this->slotSide() == LEFT)
191 BicycleCar *CC = this->getEPC();
192 BicycleCar *B = this->getEP();
193 this->DH(di * 0.01 / CC->out_radi());
196 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
197 while (!this->slot().collide(c->frame())) {
199 std::vector<RRTEdge *> eds = c->frame();
210 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
213 delete c; // not in q and collide
217 if (this->isInside(c)) {
218 goto createcuspandfinish;
219 } else if (c->s() < 9) {
220 BicycleCar *cc = this->flnc(c, co, so);
225 delete c; // not in q and collide
231 std::vector<RRTNode *> cusp;
233 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
236 std::reverse(cusp.begin(), cusp.end());
237 this->cusp().push_back(cusp);
241 BicycleCar *ParallelSlot::flnc(
243 std::vector<CircleObstacle>& co,
244 std::vector<SegmentObstacle>& so
248 if (this->slotSide() == LEFT) {
249 if (int(B->s()) % 2 == 0)
250 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
252 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
254 if (int(B->s()) % 2 == 0)
255 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
257 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
261 p = B->move(cc, i * this->DH());
263 !this->slot().collide(p->frame())
264 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
268 p = B->move(cc, i * this->DH());
270 std::vector<RRTEdge *> eds = p->frame();
283 p = B->move(cc, i * this->DH());
285 !this->slot().collide(p->frame())
286 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
288 if (this->isInside(p)) {
294 p = B->move(cc, i * this->DH());
296 std::vector<RRTEdge *> eds = p->frame();
309 return B->move(cc, (i - 1) * this->DH());
312 void ParallelSlot::fipr(RRTNode *n)
314 return this->fipr(new BicycleCar(n->x(), n->y(), n->h()));
317 void ParallelSlot::fipr(BicycleCar *B)
319 std::vector<RRTNode *> cusp;
320 cusp.push_back(new RRTNode(B->x(), B->y(), B->h()));
322 if (this->slotSide() == LEFT)
324 if (this->slotType() == PERPENDICULAR) {
325 this->DH(di * 0.01 / B->out_radi()); // TODO car in slot h()
327 if (this->slotSide() == LEFT)
328 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
330 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
333 p = B->move(cc, i * this->DH());
335 !this->slot().collide(p->frame())
336 && this->slot().collide(p)
340 p = B->move(cc, i * this->DH());
343 p = B->move(cc, i * this->DH());
345 !this->slot().collide(p->frame())
346 && this->slot().collide(p)
350 p = B->move(cc, i * this->DH());
353 p = B->move(cc, i * this->DH());
354 cusp.push_back(new RRTNode(p->x(), p->y(), p->h()));
355 std::reverse(cusp.begin(), cusp.end());
356 this->cusp().push_back(cusp);
359 this->DH(di * 0.01 / B->out_radi());
364 this->slotSide() == LEFT
365 && this->slot().collide(new RRTNode(c->lfx(), c->lfy(), 0))
367 this->slotSide() == RIGHT
368 && this->slot().collide(new RRTNode(c->rfx(), c->rfy(), 0))
370 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
371 BicycleCar *cc = this->flncr(c);
376 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
377 std::reverse(cusp.begin(), cusp.end());
378 this->cusp().push_back(cusp);
381 BicycleCar *ParallelSlot::flncr(BicycleCar *B)
384 if (this->slotSide() == LEFT) {
385 if (int(B->s()) % 2 == 0)
386 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
388 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
390 if (int(B->s()) % 2 == 0)
391 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
393 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
397 p = B->move(cc, i * this->DH());
399 !this->slot().collide(p->frame())
401 this->slotSide() == LEFT
402 && this->slot().collide(new RRTNode(
408 this->slotSide() == RIGHT
409 && this->slot().collide(new RRTNode(
418 p = B->move(cc, i * this->DH());
421 p = B->move(cc, i * this->DH());
422 while (!this->slot().collide(p->frame())) {
424 this->slotSide() == LEFT
425 && !this->slot().collide(new RRTNode(
435 this->slotSide() == RIGHT
436 && !this->slot().collide(new RRTNode(
446 p = B->move(cc, i * this->DH());
449 return B->move(cc, (i - 1) * this->DH());
452 RRTNode *ParallelSlot::fposecenter()
454 return this->slot().bnodes().back();
457 bool ParallelSlot::flast(
461 std::vector<RRTNode *> &cusp
464 BicycleCar *B = new BicycleCar(P->x(), P->y(), P->h());
467 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
469 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
472 p = B->move(cc, i * this->DH());
473 while (!this->slot().collide(p->frame())
475 (this->DH() > 0 && p->x() <= 0)
476 || (this->DH() < 0 && p->x() >= 0)
480 p = B->move(cc, i * this->DH());
483 p = B->move(cc, i * this->DH());
484 while (!this->slot().collide(p->frame())
486 (this->DH() > 0 && p->x() <= 0)
487 || (this->DH() < 0 && p->x() >= 0)
489 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
493 if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
499 p = B->move(cc, i * this->DH());
502 p = B->move(cc, (i - 1) * this->DH());
503 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
506 } else if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
511 return this->flast(p, !right, il + 1, cusp);
516 void ParallelSlot::fpose()
518 bool left = false; // right parking slot
520 BicycleCar *CC = new BicycleCar(
521 this->fposecenter()->x(),
522 this->fposecenter()->y() - 0.01,
525 BicycleCar *B = new BicycleCar(
526 CC->x() - CC->width() / 2,
527 CC->y() - (CC->length() + CC->wheelbase()) / 2,
530 if (this->slot().bnodes()[0]->x() > this->slot().bnodes()[1]->x()) {
535 CC->x() + CC->width() / 2,
536 CC->y() - (CC->length() + CC->wheelbase()) / 2,
540 this->DH(di * 0.01 / CC->out_radi());
543 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
544 while (!this->slot().collide(p->frame())) {
545 std::vector<RRTNode *> tmpcusp;
546 tmpcusp.push_back(new BicycleCar(p->x(), p->y(), p->h()));
547 if (this->flast(p, left, 0, tmpcusp)) {
548 this->cusp().push_back(tmpcusp);
553 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
557 BicycleCar *ParallelSlot::getEP()
559 // new pose for parallel parking to right slot
564 BicycleCar *CC = this->getEPC();
565 // move left by car width / 2
566 tnx = CC->x() + CC->width() / 2 * cos(CC->h() + M_PI / 2);
567 tny = CC->y() + CC->width() / 2 * sin(CC->h() + M_PI / 2);
568 if (this->slotSide() == LEFT) {
569 // move right by car width / 2
570 tnx = CC->x() + CC->width() / 2 * cos(CC->h() - M_PI / 2);
571 tny = CC->y() + CC->width() / 2 * sin(CC->h() - M_PI / 2);
573 if (this->slotType() == PARALLEL) {
575 nx = tnx - (CC->length() + CC->wheelbase()) / 2 * cos(CC->h());
576 ny = tny - (CC->length() + CC->wheelbase()) / 2 * sin(CC->h());
579 nx = tnx + (CC->length() - CC->wheelbase()) / 2 * cos(CC->h());
580 ny = tny + (CC->length() - CC->wheelbase()) / 2 * sin(CC->h());
582 return new BicycleCar(nx, ny, CC->h());
585 BicycleCar *ParallelSlot::getEPC()
587 // new pose for parallel parking to right slot
591 ta = this->slotHeading() + M_PI;
592 if (this->slotSide() == RIGHT)
596 nx = this->fposecenter()->x() + 0.01 * cos(ta);
597 ny = this->fposecenter()->y() + 0.01 * sin(ta);
598 return new BicycleCar(nx, ny, this->slotHeading());
601 BicycleCar *ParallelSlot::getFP()
604 float x = this->slot().bnodes()[0]->x();
605 float y = this->slot().bnodes()[0]->y();
606 float h = this->slotHeading();
609 if (this->slotType() == PARALLEL) {
610 if (this->slotSide() == LEFT) {
611 nx = x + BCAR_WIDTH / 2 * cos(h + M_PI / 2);
612 ny = y + BCAR_WIDTH / 2 * sin(h + M_PI / 2);
614 nx = x + BCAR_WIDTH / 2 * cos(h - M_PI / 2);
615 ny = y + BCAR_WIDTH / 2 * sin(h - M_PI / 2);
617 x = nx + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * cos(h);
618 y = ny + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * sin(h);
620 if (this->slotSide() == LEFT) {
622 nx = x + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
624 ny = y + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
626 x = nx + (BCAR_DIAG_RRADI) * cos(h + M_PI / 2);
627 y = ny + (BCAR_DIAG_RRADI) * sin(h + M_PI / 2);
630 nx = x + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
632 ny = y + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
634 x = nx + (BCAR_DIAG_RRADI) * cos(h - M_PI / 2);
635 y = ny + (BCAR_DIAG_RRADI) * sin(h - M_PI / 2);
638 return new BicycleCar(x, y, h);
641 BicycleCar *ParallelSlot::getISPP(BicycleCar *B)
643 float x = this->slot().bnodes().back()->x();
644 float y = this->slot().bnodes().back()->y();
646 if (this->slotSide() == LEFT) // TODO only for backward parking now
650 float IR = BCAR_IN_RADI;
653 float c = pow(x, 2) + pow(y - y0, 2) - pow(IR, 2);
654 float D = pow(b, 2) - 4 * a * c;
655 float x0 = -b + sqrt(D);
657 return new BicycleCar(x0, B->y(), B->h());
660 bool ParallelSlot::isInside(BicycleCar *c)
664 tmpn = new RRTNode(c->lfx(), c->lfy(), 0);
665 if (!this->slot().collide(tmpn))
668 tmpn = new RRTNode(c->lrx(), c->lry(), 0);
669 if (!this->slot().collide(tmpn))
672 tmpn = new RRTNode(c->rrx(), c->rry(), 0);
673 if (!this->slot().collide(tmpn))
676 tmpn = new RRTNode(c->rfx(), c->rfy(), 0);
677 if (!this->slot().collide(tmpn))
683 struct SamplingInfo ParallelSlot::getSamplingInfo()
685 struct SamplingInfo si;
686 RRTNode *n = this->getMidd();
687 if (this->slotType() == PARALLEL) {
689 si.x0 = n->x() + 1.5 * BCAR_LENGTH * cos(n->h());
690 si.y0 = n->y() + 1.5 * BCAR_LENGTH * sin(n->h());
693 si.x0 = this->slot().bnodes().front()->x();
694 si.y0 = this->slot().bnodes().front()->y();
695 si.h0 = this->slotHeading();