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 - x3) * (y0 - y3) - (y1 - y3) * (x0 - x3)) < 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
110 if (this->slotType() == PERPENDICULAR) {
111 // TODO different slot headings
112 // this is jus for slot heading = pi / 2
113 this->DH(0.01 / BCAR_TURNING_RADIUS);
114 BicycleCar *perc = nullptr;
115 RRTNode *cc = nullptr;
116 BicycleCar *p = nullptr;
122 x = this->slot().bnodes()[3]->x();
123 y = this->slot().bnodes()[3]->y();
126 x -= BCAR_DIST_FRONT;
127 y = this->slot().bnodes()[3]->y();
128 y += BCAR_OUT_RRADI - BCAR_TURNING_RADIUS;
131 perc = new BicycleCar(x, y, h);
137 p = perc->move(cc, i * this->DH());
140 p = perc->move(cc, i * this->DH());
144 // (reset for parking backward)
145 x = this->slot().bnodes()[0]->x();
146 y = this->slot().bnodes()[0]->y();
149 x -= BCAR_DIST_FRONT;
150 // get y from quadratic equation
151 float tmpD = pow(-2 * this->slot().bnodes()[0]->y(), 2);
153 pow(x - this->slot().bnodes()[0]->x(), 2) +
154 pow(this->slot().bnodes()[0]->y(), 2) -
157 y = 2 * this->slot().bnodes()[0]->y();
160 y -= BCAR_TURNING_RADIUS;
161 // -- end of quadratic equation
164 perc = new BicycleCar(x, y, h);
170 p = perc->move(cc, i * this->DH());
173 p = perc->move(cc, i * this->DH());
177 std::vector<RRTNode *> cusp;
178 cusp.push_back(new RRTNode(p->x(), p->y(), p->h()));
179 cusp.push_back(new RRTNode(x, y, h));
180 this->cusp().push_back(cusp);
183 // see https://courses.cs.washington.edu/courses/cse326/03su/homework/hw3/bfs.html
184 // RRTNode.s() works as iteration level
185 std::queue<BicycleCar *, std::list<BicycleCar *>> q;
186 std::queue<BicycleCar *, std::list<BicycleCar *>> empty;
188 if (this->slotSide() == LEFT)
190 BicycleCar *CC = this->getEPC();
191 BicycleCar *B = this->getEP();
192 this->DH(di * 0.01 / CC->out_radi());
195 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
196 while (!this->slot().collide(c->frame())) {
198 std::vector<RRTEdge *> eds = c->frame();
209 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
212 delete c; // not in q and collide
216 if (this->isInside(c)) {
217 goto createcuspandfinish;
218 } else if (c->s() < 9) {
219 BicycleCar *cc = this->flnc(c, co, so);
224 delete c; // not in q and collide
230 std::vector<RRTNode *> cusp;
232 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
235 std::reverse(cusp.begin(), cusp.end());
236 this->cusp().push_back(cusp);
240 BicycleCar *ParallelSlot::flnc(
242 std::vector<CircleObstacle>& co,
243 std::vector<SegmentObstacle>& so
247 if (this->slotSide() == LEFT) {
248 if (int(B->s()) % 2 == 0)
249 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
251 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
253 if (int(B->s()) % 2 == 0)
254 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
256 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
260 p = B->move(cc, i * this->DH());
262 !this->slot().collide(p->frame())
263 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
267 p = B->move(cc, i * this->DH());
269 std::vector<RRTEdge *> eds = p->frame();
282 p = B->move(cc, i * this->DH());
284 !this->slot().collide(p->frame())
285 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
287 if (this->isInside(p)) {
293 p = B->move(cc, i * this->DH());
295 std::vector<RRTEdge *> eds = p->frame();
308 return B->move(cc, (i - 1) * this->DH());
311 void ParallelSlot::fipr(RRTNode *n)
313 return this->fipr(new BicycleCar(n->x(), n->y(), n->h()));
316 void ParallelSlot::fipr(BicycleCar *B)
318 std::vector<RRTNode *> cusp;
319 cusp.push_back(new RRTNode(B->x(), B->y(), B->h()));
321 if (this->slotSide() == LEFT)
323 if (this->slotType() == PERPENDICULAR) {
324 this->DH(di * 0.01 / B->out_radi()); // TODO car in slot h()
326 if (this->slotSide() == LEFT)
327 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
329 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
332 p = B->move(cc, i * this->DH());
334 !this->slot().collide(p->frame())
335 && this->slot().collide(p)
339 p = B->move(cc, i * this->DH());
342 p = B->move(cc, i * this->DH());
344 !this->slot().collide(p->frame())
345 && this->slot().collide(p)
349 p = B->move(cc, i * this->DH());
352 p = B->move(cc, i * this->DH());
353 cusp.push_back(new RRTNode(p->x(), p->y(), p->h()));
354 std::reverse(cusp.begin(), cusp.end());
355 this->cusp().push_back(cusp);
358 this->DH(di * 0.01 / B->out_radi());
363 this->slotSide() == LEFT
364 && this->slot().collide(new RRTNode(c->lfx(), c->lfy(), 0))
366 this->slotSide() == RIGHT
367 && this->slot().collide(new RRTNode(c->rfx(), c->rfy(), 0))
369 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
370 BicycleCar *cc = this->flncr(c);
375 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
376 std::reverse(cusp.begin(), cusp.end());
377 this->cusp().push_back(cusp);
380 BicycleCar *ParallelSlot::flncr(BicycleCar *B)
383 if (this->slotSide() == LEFT) {
384 if (int(B->s()) % 2 == 0)
385 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
387 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
389 if (int(B->s()) % 2 == 0)
390 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
392 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
396 p = B->move(cc, i * this->DH());
398 !this->slot().collide(p->frame())
400 this->slotSide() == LEFT
401 && this->slot().collide(new RRTNode(
407 this->slotSide() == RIGHT
408 && this->slot().collide(new RRTNode(
417 p = B->move(cc, i * this->DH());
420 p = B->move(cc, i * this->DH());
421 while (!this->slot().collide(p->frame())) {
423 this->slotSide() == LEFT
424 && !this->slot().collide(new RRTNode(
434 this->slotSide() == RIGHT
435 && !this->slot().collide(new RRTNode(
445 p = B->move(cc, i * this->DH());
448 return B->move(cc, (i - 1) * this->DH());
451 RRTNode *ParallelSlot::fposecenter()
453 return this->slot().bnodes().front();
456 bool ParallelSlot::flast(
460 std::vector<RRTNode *> &cusp
463 BicycleCar *B = new BicycleCar(P->x(), P->y(), P->h());
466 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
468 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
471 p = B->move(cc, i * this->DH());
472 while (!this->slot().collide(p->frame())
474 (this->DH() > 0 && p->x() <= 0)
475 || (this->DH() < 0 && p->x() >= 0)
479 p = B->move(cc, i * this->DH());
482 p = B->move(cc, i * this->DH());
483 while (!this->slot().collide(p->frame())
485 (this->DH() > 0 && p->x() <= 0)
486 || (this->DH() < 0 && p->x() >= 0)
488 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
492 if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
498 p = B->move(cc, i * this->DH());
501 p = B->move(cc, (i - 1) * this->DH());
502 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
505 } else if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
510 return this->flast(p, !right, il + 1, cusp);
515 void ParallelSlot::fpose()
517 bool left = false; // right parking slot
519 BicycleCar *CC = new BicycleCar(
520 this->fposecenter()->x(),
521 this->fposecenter()->y() - 0.01,
524 BicycleCar *B = new BicycleCar(
525 CC->x() - CC->width() / 2,
526 CC->y() - (CC->length() + CC->wheelbase()) / 2,
529 if (this->slot().bnodes()[0]->x() > this->slot().bnodes()[1]->x()) {
534 CC->x() + CC->width() / 2,
535 CC->y() - (CC->length() + CC->wheelbase()) / 2,
539 this->DH(di * 0.01 / CC->out_radi());
542 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
543 while (!this->slot().collide(p->frame())) {
544 std::vector<RRTNode *> tmpcusp;
545 tmpcusp.push_back(new BicycleCar(p->x(), p->y(), p->h()));
546 if (this->flast(p, left, 0, tmpcusp)) {
547 this->cusp().push_back(tmpcusp);
552 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
556 BicycleCar *ParallelSlot::getEP()
558 // new pose for parallel parking to right slot
563 BicycleCar *CC = this->getEPC();
564 // move left by car width / 2
565 tnx = CC->x() + CC->width() / 2 * cos(CC->h() + M_PI / 2);
566 tny = CC->y() + CC->width() / 2 * sin(CC->h() + M_PI / 2);
567 if (this->slotSide() == LEFT) {
568 // move right by car width / 2
569 tnx = CC->x() + CC->width() / 2 * cos(CC->h() - M_PI / 2);
570 tny = CC->y() + CC->width() / 2 * sin(CC->h() - M_PI / 2);
572 if (this->slotType() == PARALLEL) {
574 nx = tnx - (CC->length() + CC->wheelbase()) / 2 * cos(CC->h());
575 ny = tny - (CC->length() + CC->wheelbase()) / 2 * sin(CC->h());
578 nx = tnx + (CC->length() - CC->wheelbase()) / 2 * cos(CC->h());
579 ny = tny + (CC->length() - CC->wheelbase()) / 2 * sin(CC->h());
581 return new BicycleCar(nx, ny, CC->h());
584 BicycleCar *ParallelSlot::getEPC()
586 // new pose for parallel parking to right slot
590 ta = this->slotHeading() + M_PI;
591 if (this->slotSide() == RIGHT)
595 nx = this->fposecenter()->x() + 0.01 * cos(ta);
596 ny = this->fposecenter()->y() + 0.01 * sin(ta);
597 return new BicycleCar(nx, ny, this->slotHeading());
600 BicycleCar *ParallelSlot::getFP()
602 float x = this->slot().bnodes()[3]->x();
603 float y = this->slot().bnodes()[3]->y();
604 float h = this->slotHeading();
607 if (this->slotType() == PARALLEL) {
608 if (this->slotSide() == LEFT) {
609 nx = x + BCAR_WIDTH / 2 * cos(h + M_PI / 2);
610 ny = y + BCAR_WIDTH / 2 * sin(h + M_PI / 2);
612 nx = x + BCAR_WIDTH / 2 * cos(h - M_PI / 2);
613 ny = y + BCAR_WIDTH / 2 * sin(h - M_PI / 2);
615 x = nx + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * cos(h);
616 y = ny + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * sin(h);
618 if (this->slotSide() == LEFT) {
620 nx = x + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
622 ny = y + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
624 x = nx + (BCAR_DIAG_RRADI) * cos(h + M_PI / 2);
625 y = ny + (BCAR_DIAG_RRADI) * sin(h + M_PI / 2);
628 nx = x + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
630 ny = y + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
632 x = nx + (BCAR_DIAG_RRADI) * cos(h - M_PI / 2);
633 y = ny + (BCAR_DIAG_RRADI) * sin(h - M_PI / 2);
636 return new BicycleCar(x, y, h);
639 bool ParallelSlot::isInside(BicycleCar *c)
643 tmpn = new RRTNode(c->lfx(), c->lfy(), 0);
644 if (!this->slot().collide(tmpn))
647 tmpn = new RRTNode(c->lrx(), c->lry(), 0);
648 if (!this->slot().collide(tmpn))
651 tmpn = new RRTNode(c->rrx(), c->rry(), 0);
652 if (!this->slot().collide(tmpn))
655 tmpn = new RRTNode(c->rfx(), c->rfy(), 0);
656 if (!this->slot().collide(tmpn))
662 struct SamplingInfo ParallelSlot::getSamplingInfo()
664 struct SamplingInfo si;
665 if (this->slotType() == PARALLEL) {
666 si.x = this->slot().bnodes().front()->x();
667 si.y = this->slot().bnodes().front()->y();
668 si.mr = BCAR_WIDTH / 2;
671 si.x = this->slot().bnodes().back()->x();
672 si.x -= this->slot().bnodes().front()->x();
674 si.x += this->slot().bnodes().front()->x();
675 si.y = this->slot().bnodes().back()->y();
676 si.y -= this->slot().bnodes().front()->y();
678 si.y += this->slot().bnodes().front()->y();
680 si.mmh = (M_PI - M_PI / 6) / 2;
685 si.sh = this->slotHeading();
686 if (this->slotSide() == RIGHT)