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 return this->slotHeading_;
48 SlotSide ParallelSlot::slotSide()
50 return this->slotSide_;
53 SlotType ParallelSlot::slotType()
55 return this->slotType_;
59 void ParallelSlot::DH(float dh)
64 void ParallelSlot::setAll()
67 float y0 = this->slot().bnodes()[0]->y();
68 float x0 = this->slot().bnodes()[0]->x();
69 float y3 = this->slot().bnodes()[3]->y();
70 float x3 = this->slot().bnodes()[3]->x();
73 this->slotHeading_ = atan2(dy, dx);
75 float y1 = this->slot().bnodes()[1]->y();
76 float x1 = this->slot().bnodes()[1]->x();
77 if (sgn((x1 - x3) * (y0 - y3) - (y1 - y3) * (x0 - x3)) < 0)
78 this->slotSide_ = LEFT;
80 this->slotSide_ = RIGHT;
83 this->slot().bnodes()[0],
84 this->slot().bnodes()[1]
87 this->slot().bnodes()[1],
88 this->slot().bnodes()[2]
91 this->slotType_ = PERPENDICULAR;
93 this->slotType_ = PARALLEL;
97 void ParallelSlot::fip(
98 std::vector<CircleObstacle>& co,
99 std::vector<SegmentObstacle>& so
102 if (this->slotType() == PERPENDICULAR) {
103 // TODO different slot headings
104 // this is jus for slot heading = pi / 2
105 this->DH(0.01 / BCAR_TURNING_RADIUS);
106 BicycleCar *perc = nullptr;
107 RRTNode *cc = nullptr;
108 BicycleCar *p = nullptr;
114 x = this->slot().bnodes()[3]->x();
115 y = this->slot().bnodes()[3]->y();
118 x -= BCAR_DIST_FRONT;
119 y = this->slot().bnodes()[3]->y();
120 y += BCAR_OUT_RRADI - BCAR_TURNING_RADIUS;
123 perc = new BicycleCar(x, y, h);
129 p = perc->move(cc, i * this->DH());
132 p = perc->move(cc, i * this->DH());
136 // (reset for parking backward)
137 x = this->slot().bnodes()[0]->x();
138 y = this->slot().bnodes()[0]->y();
141 x -= BCAR_DIST_FRONT;
142 // get y from quadratic equation
143 float tmpD = pow(-2 * this->slot().bnodes()[0]->y(), 2);
145 pow(x - this->slot().bnodes()[0]->x(), 2) +
146 pow(this->slot().bnodes()[0]->y(), 2) -
149 y = 2 * this->slot().bnodes()[0]->y();
152 y -= BCAR_TURNING_RADIUS;
153 // -- end of quadratic equation
156 perc = new BicycleCar(x, y, h);
162 p = perc->move(cc, i * this->DH());
165 p = perc->move(cc, i * this->DH());
169 std::vector<RRTNode *> cusp;
170 cusp.push_back(new RRTNode(p->x(), p->y(), p->h()));
171 cusp.push_back(new RRTNode(x, y, h));
172 this->cusp().push_back(cusp);
175 // see https://courses.cs.washington.edu/courses/cse326/03su/homework/hw3/bfs.html
176 // RRTNode.s() works as iteration level
177 std::queue<BicycleCar *, std::list<BicycleCar *>> q;
178 std::queue<BicycleCar *, std::list<BicycleCar *>> empty;
180 if (this->slotSide() == LEFT)
182 BicycleCar *CC = this->getEPC();
183 BicycleCar *B = this->getEP();
184 this->DH(di * 0.01 / CC->out_radi());
187 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
188 while (!this->slot().collide(c->frame())) {
190 std::vector<RRTEdge *> eds = c->frame();
201 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
204 delete c; // not in q and collide
208 if (this->isInside(c)) {
209 goto createcuspandfinish;
210 } else if (c->s() < 9) {
211 BicycleCar *cc = this->flnc(c, co, so);
216 delete c; // not in q and collide
222 std::vector<RRTNode *> cusp;
224 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
227 std::reverse(cusp.begin(), cusp.end());
228 this->cusp().push_back(cusp);
232 void ParallelSlot::fipr(RRTNode *n)
234 return this->fipr(new BicycleCar(n->x(), n->y(), n->h()));
237 void ParallelSlot::fipr(BicycleCar *B)
239 std::vector<RRTNode *> cusp;
240 cusp.push_back(new RRTNode(B->x(), B->y(), B->h()));
242 if (this->slotSide() == LEFT)
244 if (this->slotType() == PERPENDICULAR) {
245 this->DH(di * 0.01 / B->out_radi()); // TODO car in slot h()
247 if (this->slotSide() == LEFT)
248 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
250 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
253 p = B->move(cc, i * this->DH());
255 !this->slot().collide(p->frame())
256 && this->slot().collide(p)
260 p = B->move(cc, i * this->DH());
263 p = B->move(cc, i * this->DH());
265 !this->slot().collide(p->frame())
266 && this->slot().collide(p)
270 p = B->move(cc, i * this->DH());
273 p = B->move(cc, i * this->DH());
274 cusp.push_back(new RRTNode(p->x(), p->y(), p->h()));
275 std::reverse(cusp.begin(), cusp.end());
276 this->cusp().push_back(cusp);
279 this->DH(di * 0.01 / B->out_radi());
284 this->slotSide() == LEFT
285 && this->slot().collide(new RRTNode(c->lfx(), c->lfy(), 0))
287 this->slotSide() == RIGHT
288 && this->slot().collide(new RRTNode(c->rfx(), c->rfy(), 0))
290 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
291 BicycleCar *cc = this->flncr(c);
296 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
297 std::reverse(cusp.begin(), cusp.end());
298 this->cusp().push_back(cusp);
301 BicycleCar *ParallelSlot::flnc(
303 std::vector<CircleObstacle>& co,
304 std::vector<SegmentObstacle>& so
308 if (this->slotSide() == LEFT) {
309 if (int(B->s()) % 2 == 0)
310 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
312 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
314 if (int(B->s()) % 2 == 0)
315 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
317 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
321 p = B->move(cc, i * this->DH());
323 !this->slot().collide(p->frame())
324 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
328 p = B->move(cc, i * this->DH());
330 std::vector<RRTEdge *> eds = p->frame();
343 p = B->move(cc, i * this->DH());
345 !this->slot().collide(p->frame())
346 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
348 if (this->isInside(p)) {
354 p = B->move(cc, i * this->DH());
356 std::vector<RRTEdge *> eds = p->frame();
369 return B->move(cc, (i - 1) * this->DH());
372 BicycleCar *ParallelSlot::flncr(BicycleCar *B)
375 if (this->slotSide() == LEFT) {
376 if (int(B->s()) % 2 == 0)
377 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
379 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
381 if (int(B->s()) % 2 == 0)
382 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
384 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
388 p = B->move(cc, i * this->DH());
390 !this->slot().collide(p->frame())
392 this->slotSide() == LEFT
393 && this->slot().collide(new RRTNode(
399 this->slotSide() == RIGHT
400 && this->slot().collide(new RRTNode(
409 p = B->move(cc, i * this->DH());
412 p = B->move(cc, i * this->DH());
413 while (!this->slot().collide(p->frame())) {
415 this->slotSide() == LEFT
416 && !this->slot().collide(new RRTNode(
426 this->slotSide() == RIGHT
427 && !this->slot().collide(new RRTNode(
437 p = B->move(cc, i * this->DH());
440 return B->move(cc, (i - 1) * this->DH());
443 RRTNode *ParallelSlot::fposecenter()
445 return this->slot().bnodes().front();
448 bool ParallelSlot::flast(
452 std::vector<RRTNode *> &cusp
455 BicycleCar *B = new BicycleCar(P->x(), P->y(), P->h());
458 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
460 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
463 p = B->move(cc, i * this->DH());
464 while (!this->slot().collide(p->frame())
466 (this->DH() > 0 && p->x() <= 0)
467 || (this->DH() < 0 && p->x() >= 0)
471 p = B->move(cc, i * this->DH());
474 p = B->move(cc, i * this->DH());
475 while (!this->slot().collide(p->frame())
477 (this->DH() > 0 && p->x() <= 0)
478 || (this->DH() < 0 && p->x() >= 0)
480 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
484 if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
490 p = B->move(cc, i * this->DH());
493 p = B->move(cc, (i - 1) * this->DH());
494 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
497 } else if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
502 return this->flast(p, !right, il + 1, cusp);
507 void ParallelSlot::fpose()
509 bool left = false; // right parking slot
511 BicycleCar *CC = new BicycleCar(
512 this->fposecenter()->x(),
513 this->fposecenter()->y() - 0.01,
516 BicycleCar *B = new BicycleCar(
517 CC->x() - CC->width() / 2,
518 CC->y() - (CC->length() + CC->wheelbase()) / 2,
521 if (this->slot().bnodes()[0]->x() > this->slot().bnodes()[1]->x()) {
526 CC->x() + CC->width() / 2,
527 CC->y() - (CC->length() + CC->wheelbase()) / 2,
531 this->DH(di * 0.01 / CC->out_radi());
534 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
535 while (!this->slot().collide(p->frame())) {
536 std::vector<RRTNode *> tmpcusp;
537 tmpcusp.push_back(new BicycleCar(p->x(), p->y(), p->h()));
538 if (this->flast(p, left, 0, tmpcusp)) {
539 this->cusp().push_back(tmpcusp);
544 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
548 BicycleCar *ParallelSlot::getEP()
550 // new pose for parallel parking to right slot
555 BicycleCar *CC = this->getEPC();
556 // move left by car width / 2
557 tnx = CC->x() + CC->width() / 2 * cos(CC->h() + M_PI / 2);
558 tny = CC->y() + CC->width() / 2 * sin(CC->h() + M_PI / 2);
559 if (this->slotSide() == LEFT) {
560 // move right by car width / 2
561 tnx = CC->x() + CC->width() / 2 * cos(CC->h() - M_PI / 2);
562 tny = CC->y() + CC->width() / 2 * sin(CC->h() - M_PI / 2);
564 if (this->slotType() == PARALLEL) {
566 nx = tnx - (CC->length() + CC->wheelbase()) / 2 * cos(CC->h());
567 ny = tny - (CC->length() + CC->wheelbase()) / 2 * sin(CC->h());
570 nx = tnx + (CC->length() - CC->wheelbase()) / 2 * cos(CC->h());
571 ny = tny + (CC->length() - CC->wheelbase()) / 2 * sin(CC->h());
573 return new BicycleCar(nx, ny, CC->h());
576 BicycleCar *ParallelSlot::getEPC()
578 // new pose for parallel parking to right slot
582 ta = this->slotHeading() + M_PI;
583 if (this->slotSide() == RIGHT)
587 nx = this->fposecenter()->x() + 0.01 * cos(ta);
588 ny = this->fposecenter()->y() + 0.01 * sin(ta);
589 return new BicycleCar(nx, ny, this->slotHeading());
592 BicycleCar *ParallelSlot::getFP()
594 float x = this->slot().bnodes()[3]->x();
595 float y = this->slot().bnodes()[3]->y();
596 float h = this->slotHeading();
599 if (this->slotType() == PARALLEL) {
600 if (this->slotSide() == LEFT) {
601 nx = x + BCAR_WIDTH / 2 * cos(h + M_PI / 2);
602 ny = y + BCAR_WIDTH / 2 * sin(h + M_PI / 2);
604 nx = x + BCAR_WIDTH / 2 * cos(h - M_PI / 2);
605 ny = y + BCAR_WIDTH / 2 * sin(h - M_PI / 2);
607 x = nx + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * cos(h);
608 y = ny + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * sin(h);
610 if (this->slotSide() == LEFT) {
612 nx = x + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
614 ny = y + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
616 x = nx + (BCAR_DIAG_RRADI) * cos(h + M_PI / 2);
617 y = ny + (BCAR_DIAG_RRADI) * sin(h + M_PI / 2);
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 return new BicycleCar(x, y, h);
631 bool ParallelSlot::isInside(BicycleCar *c)
635 tmpn = new RRTNode(c->lfx(), c->lfy(), 0);
636 if (!this->slot().collide(tmpn))
639 tmpn = new RRTNode(c->lrx(), c->lry(), 0);
640 if (!this->slot().collide(tmpn))
643 tmpn = new RRTNode(c->rrx(), c->rry(), 0);
644 if (!this->slot().collide(tmpn))
647 tmpn = new RRTNode(c->rfx(), c->rfy(), 0);
648 if (!this->slot().collide(tmpn))
654 struct SamplingInfo ParallelSlot::getSamplingInfo()
656 struct SamplingInfo si;
657 if (this->slotType() == PARALLEL) {
658 si.x = this->slot().bnodes().front()->x();
659 si.y = this->slot().bnodes().front()->y();
660 si.mr = BCAR_WIDTH / 2;
663 si.x = this->slot().bnodes().back()->x();
664 si.x -= this->slot().bnodes().front()->x();
666 si.x += this->slot().bnodes().front()->x();
667 si.y = this->slot().bnodes().back()->y();
668 si.y -= this->slot().bnodes().front()->y();
670 si.y += this->slot().bnodes().front()->y();
672 si.mmh = (M_PI - M_PI / 6) / 2;
677 si.sh = this->slotHeading();
678 if (this->slotSide() == RIGHT)