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<std::vector<RRTNode *>> &ParallelSlot::cusp()
33 float ParallelSlot::DH() const
38 PolygonObstacle &ParallelSlot::slot()
43 float ParallelSlot::slotHeading()
45 float y0 = this->slot().bnodes()[0]->y();
46 float x0 = this->slot().bnodes()[0]->x();
47 float y3 = this->slot().bnodes()[3]->y();
48 float x3 = this->slot().bnodes()[3]->x();
54 SlotSide ParallelSlot::slotSide()
56 return this->slotSide_;
59 SlotType ParallelSlot::slotType()
61 return this->slotType_;
65 void ParallelSlot::DH(float dh)
70 void ParallelSlot::setAll()
73 float y0 = this->slot().bnodes()[0]->y();
74 float x0 = this->slot().bnodes()[0]->x();
75 float y1 = this->slot().bnodes()[1]->y();
76 float x1 = this->slot().bnodes()[1]->x();
77 float y3 = this->slot().bnodes()[3]->y();
78 float x3 = this->slot().bnodes()[3]->x();
79 if (sgn((x1 - x3) * (y0 - y3) - (y1 - y3) * (x0 - x3)) < 0)
80 this->slotSide_ = LEFT;
82 this->slotSide_ = RIGHT;
85 this->slot().bnodes()[0],
86 this->slot().bnodes()[1]
89 this->slot().bnodes()[1],
90 this->slot().bnodes()[2]
93 this->slotType_ = PERPENDICULAR;
95 this->slotType_ = PARALLEL;
99 void ParallelSlot::fip(
100 std::vector<CircleObstacle>& co,
101 std::vector<SegmentObstacle>& so
104 if (this->slotType() == PERPENDICULAR) {
105 // TODO different slot headings
106 // this is jus for slot heading = pi / 2
107 this->DH(0.01 / BCAR_TURNING_RADIUS);
108 BicycleCar *perc = nullptr;
109 RRTNode *cc = nullptr;
110 BicycleCar *p = nullptr;
116 x = this->slot().bnodes()[3]->x();
117 y = this->slot().bnodes()[3]->y();
120 x -= BCAR_DIST_FRONT;
121 y = this->slot().bnodes()[3]->y();
122 y += BCAR_OUT_RRADI - BCAR_TURNING_RADIUS;
125 perc = new BicycleCar(x, y, h);
131 p = perc->move(cc, i * this->DH());
134 p = perc->move(cc, i * this->DH());
138 // (reset for parking backward)
139 x = this->slot().bnodes()[0]->x();
140 y = this->slot().bnodes()[0]->y();
143 x -= BCAR_DIST_FRONT;
144 // get y from quadratic equation
145 float tmpD = pow(-2 * this->slot().bnodes()[0]->y(), 2);
147 pow(x - this->slot().bnodes()[0]->x(), 2) +
148 pow(this->slot().bnodes()[0]->y(), 2) -
151 y = 2 * this->slot().bnodes()[0]->y();
154 y -= BCAR_TURNING_RADIUS;
155 // -- end of quadratic equation
158 perc = new BicycleCar(x, y, h);
164 p = perc->move(cc, i * this->DH());
167 p = perc->move(cc, i * this->DH());
171 std::vector<RRTNode *> cusp;
172 cusp.push_back(new RRTNode(p->x(), p->y(), p->h()));
173 cusp.push_back(new RRTNode(x, y, h));
174 this->cusp().push_back(cusp);
177 // see https://courses.cs.washington.edu/courses/cse326/03su/homework/hw3/bfs.html
178 // RRTNode.s() works as iteration level
179 std::queue<BicycleCar *, std::list<BicycleCar *>> q;
180 std::queue<BicycleCar *, std::list<BicycleCar *>> empty;
182 if (this->slotSide() == LEFT)
184 BicycleCar *CC = this->getEPC();
185 BicycleCar *B = this->getEP();
186 this->DH(di * 0.01 / CC->out_radi());
189 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
190 while (!this->slot().collide(c->frame())) {
192 std::vector<RRTEdge *> eds = c->frame();
203 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
206 delete c; // not in q and collide
210 if (this->isInside(c)) {
211 goto createcuspandfinish;
212 } else if (c->s() < 9) {
213 BicycleCar *cc = this->flnc(c, co, so);
218 delete c; // not in q and collide
224 std::vector<RRTNode *> cusp;
226 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
229 std::reverse(cusp.begin(), cusp.end());
230 this->cusp().push_back(cusp);
234 void ParallelSlot::fipr(RRTNode *n)
236 return this->fipr(new BicycleCar(n->x(), n->y(), n->h()));
239 void ParallelSlot::fipr(BicycleCar *B)
241 std::vector<RRTNode *> cusp;
242 cusp.push_back(new RRTNode(B->x(), B->y(), B->h()));
244 if (this->slotSide() == LEFT)
246 if (this->slotType() == PERPENDICULAR) {
247 this->DH(di * 0.01 / B->out_radi()); // TODO car in slot h()
249 if (this->slotSide() == LEFT)
250 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
252 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
255 p = B->move(cc, i * this->DH());
257 !this->slot().collide(p->frame())
258 && this->slot().collide(p)
262 p = B->move(cc, i * this->DH());
265 p = B->move(cc, i * this->DH());
267 !this->slot().collide(p->frame())
268 && this->slot().collide(p)
272 p = B->move(cc, i * this->DH());
275 p = B->move(cc, i * this->DH());
276 cusp.push_back(new RRTNode(p->x(), p->y(), p->h()));
277 std::reverse(cusp.begin(), cusp.end());
278 this->cusp().push_back(cusp);
281 this->DH(di * 0.01 / B->out_radi());
286 this->slotSide() == LEFT
287 && this->slot().collide(new RRTNode(c->lfx(), c->lfy(), 0))
289 this->slotSide() == RIGHT
290 && this->slot().collide(new RRTNode(c->rfx(), c->rfy(), 0))
292 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
293 BicycleCar *cc = this->flncr(c);
298 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
299 std::reverse(cusp.begin(), cusp.end());
300 this->cusp().push_back(cusp);
303 BicycleCar *ParallelSlot::flnc(
305 std::vector<CircleObstacle>& co,
306 std::vector<SegmentObstacle>& so
310 if (this->slotSide() == LEFT) {
311 if (int(B->s()) % 2 == 0)
312 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
314 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
316 if (int(B->s()) % 2 == 0)
317 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
319 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
323 p = B->move(cc, i * this->DH());
325 !this->slot().collide(p->frame())
326 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
330 p = B->move(cc, i * this->DH());
332 std::vector<RRTEdge *> eds = p->frame();
345 p = B->move(cc, i * this->DH());
347 !this->slot().collide(p->frame())
348 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
350 if (this->isInside(p)) {
356 p = B->move(cc, i * this->DH());
358 std::vector<RRTEdge *> eds = p->frame();
371 return B->move(cc, (i - 1) * this->DH());
374 BicycleCar *ParallelSlot::flncr(BicycleCar *B)
377 if (this->slotSide() == LEFT) {
378 if (int(B->s()) % 2 == 0)
379 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
381 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
383 if (int(B->s()) % 2 == 0)
384 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
386 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
390 p = B->move(cc, i * this->DH());
392 !this->slot().collide(p->frame())
394 this->slotSide() == LEFT
395 && this->slot().collide(new RRTNode(
401 this->slotSide() == RIGHT
402 && this->slot().collide(new RRTNode(
411 p = B->move(cc, i * this->DH());
414 p = B->move(cc, i * this->DH());
415 while (!this->slot().collide(p->frame())) {
417 this->slotSide() == LEFT
418 && !this->slot().collide(new RRTNode(
428 this->slotSide() == RIGHT
429 && !this->slot().collide(new RRTNode(
439 p = B->move(cc, i * this->DH());
442 return B->move(cc, (i - 1) * this->DH());
445 RRTNode *ParallelSlot::fposecenter()
447 return this->slot().bnodes().front();
450 bool ParallelSlot::flast(
454 std::vector<RRTNode *> &cusp
457 BicycleCar *B = new BicycleCar(P->x(), P->y(), P->h());
460 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
462 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
465 p = B->move(cc, i * this->DH());
466 while (!this->slot().collide(p->frame())
468 (this->DH() > 0 && p->x() <= 0)
469 || (this->DH() < 0 && p->x() >= 0)
473 p = B->move(cc, i * this->DH());
476 p = B->move(cc, i * this->DH());
477 while (!this->slot().collide(p->frame())
479 (this->DH() > 0 && p->x() <= 0)
480 || (this->DH() < 0 && p->x() >= 0)
482 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
486 if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
492 p = B->move(cc, i * this->DH());
495 p = B->move(cc, (i - 1) * this->DH());
496 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
499 } else if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
504 return this->flast(p, !right, il + 1, cusp);
509 void ParallelSlot::fpose()
511 bool left = false; // right parking slot
513 BicycleCar *CC = new BicycleCar(
514 this->fposecenter()->x(),
515 this->fposecenter()->y() - 0.01,
518 BicycleCar *B = new BicycleCar(
519 CC->x() - CC->width() / 2,
520 CC->y() - (CC->length() + CC->wheelbase()) / 2,
523 if (this->slot().bnodes()[0]->x() > this->slot().bnodes()[1]->x()) {
528 CC->x() + CC->width() / 2,
529 CC->y() - (CC->length() + CC->wheelbase()) / 2,
533 this->DH(di * 0.01 / CC->out_radi());
536 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
537 while (!this->slot().collide(p->frame())) {
538 std::vector<RRTNode *> tmpcusp;
539 tmpcusp.push_back(new BicycleCar(p->x(), p->y(), p->h()));
540 if (this->flast(p, left, 0, tmpcusp)) {
541 this->cusp().push_back(tmpcusp);
546 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
550 BicycleCar *ParallelSlot::getEP()
552 // new pose for parallel parking to right slot
557 BicycleCar *CC = this->getEPC();
558 // move left by car width / 2
559 tnx = CC->x() + CC->width() / 2 * cos(CC->h() + M_PI / 2);
560 tny = CC->y() + CC->width() / 2 * sin(CC->h() + M_PI / 2);
561 if (this->slotSide() == LEFT) {
562 // move right by car width / 2
563 tnx = CC->x() + CC->width() / 2 * cos(CC->h() - M_PI / 2);
564 tny = CC->y() + CC->width() / 2 * sin(CC->h() - M_PI / 2);
566 if (this->slotType() == PARALLEL) {
568 nx = tnx - (CC->length() + CC->wheelbase()) / 2 * cos(CC->h());
569 ny = tny - (CC->length() + CC->wheelbase()) / 2 * sin(CC->h());
572 nx = tnx + (CC->length() - CC->wheelbase()) / 2 * cos(CC->h());
573 ny = tny + (CC->length() - CC->wheelbase()) / 2 * sin(CC->h());
575 return new BicycleCar(nx, ny, CC->h());
578 BicycleCar *ParallelSlot::getEPC()
580 // new pose for parallel parking to right slot
584 ta = this->slotHeading() + M_PI;
585 if (this->slotSide() == RIGHT)
589 nx = this->fposecenter()->x() + 0.01 * cos(ta);
590 ny = this->fposecenter()->y() + 0.01 * sin(ta);
591 return new BicycleCar(nx, ny, this->slotHeading());
594 BicycleCar *ParallelSlot::getFP()
596 float x = this->slot().bnodes()[3]->x();
597 float y = this->slot().bnodes()[3]->y();
598 float h = this->slotHeading();
601 if (this->slotType() == PARALLEL) {
602 if (this->slotSide() == LEFT) {
603 nx = x + BCAR_WIDTH / 2 * cos(h + M_PI / 2);
604 ny = y + BCAR_WIDTH / 2 * sin(h + M_PI / 2);
606 nx = x + BCAR_WIDTH / 2 * cos(h - M_PI / 2);
607 ny = y + BCAR_WIDTH / 2 * sin(h - M_PI / 2);
609 x = nx + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * cos(h);
610 y = ny + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * sin(h);
612 if (this->slotSide() == LEFT) {
614 nx = x + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
616 ny = y + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
618 x = nx + (BCAR_DIAG_RRADI) * cos(h + M_PI / 2);
619 y = ny + (BCAR_DIAG_RRADI) * sin(h + M_PI / 2);
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 return new BicycleCar(x, y, h);
633 bool ParallelSlot::isInside(BicycleCar *c)
637 tmpn = new RRTNode(c->lfx(), c->lfy(), 0);
638 if (!this->slot().collide(tmpn))
641 tmpn = new RRTNode(c->lrx(), c->lry(), 0);
642 if (!this->slot().collide(tmpn))
645 tmpn = new RRTNode(c->rrx(), c->rry(), 0);
646 if (!this->slot().collide(tmpn))
649 tmpn = new RRTNode(c->rfx(), c->rfy(), 0);
650 if (!this->slot().collide(tmpn))
656 struct SamplingInfo ParallelSlot::getSamplingInfo()
658 struct SamplingInfo si;
659 if (this->slotType() == PARALLEL) {
660 si.x = this->slot().bnodes().front()->x();
661 si.y = this->slot().bnodes().front()->y();
662 si.mr = BCAR_WIDTH / 2;
665 si.x = this->slot().bnodes().back()->x();
666 si.x -= this->slot().bnodes().front()->x();
668 si.x += this->slot().bnodes().front()->x();
669 si.y = this->slot().bnodes().back()->y();
670 si.y -= this->slot().bnodes().front()->y();
672 si.y += this->slot().bnodes().front()->y();
674 si.mmh = (M_PI - M_PI / 6) / 2;
679 si.sh = this->slotHeading();
680 if (this->slotSide() == RIGHT)