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 // see https://courses.cs.washington.edu/courses/cse326/03su/homework/hw3/bfs.html
105 // RRTNode.s() works as iteration level
106 std::queue<BicycleCar *, std::list<BicycleCar *>> q;
107 std::queue<BicycleCar *, std::list<BicycleCar *>> empty;
109 if (this->slotSide() == LEFT)
111 BicycleCar *CC = this->getEPC();
112 BicycleCar *B = this->getEP();
113 this->DH(di * 0.01 / CC->out_radi());
116 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
117 while (!this->slot().collide(c->frame())) {
119 std::vector<RRTEdge *> eds = c->frame();
130 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
133 delete c; // not in q and collide
137 if (this->isInside(c)) {
138 goto createcuspandfinish;
139 } else if (c->s() < 9) {
140 BicycleCar *cc = this->flnc(c, co, so);
145 delete c; // not in q and collide
151 std::vector<RRTNode *> cusp;
153 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
156 std::reverse(cusp.begin(), cusp.end());
157 this->cusp().push_back(cusp);
161 void ParallelSlot::fipr(RRTNode *n)
163 return this->fipr(new BicycleCar(n->x(), n->y(), n->h()));
166 void ParallelSlot::fipr(BicycleCar *B)
168 std::vector<RRTNode *> cusp;
169 cusp.push_back(new RRTNode(B->x(), B->y(), B->h()));
171 if (this->slotSide() == LEFT)
173 if (this->slotType() == PERPENDICULAR) {
174 this->DH(di * 0.01 / B->out_radi()); // TODO car in slot h()
176 if (this->slotSide() == LEFT)
177 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
179 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
182 p = B->move(cc, i * this->DH());
184 !this->slot().collide(p->frame())
185 && this->slot().collide(p)
189 p = B->move(cc, i * this->DH());
192 p = B->move(cc, i * this->DH());
194 !this->slot().collide(p->frame())
195 && this->slot().collide(p)
199 p = B->move(cc, i * this->DH());
202 p = B->move(cc, i * this->DH());
203 cusp.push_back(new RRTNode(p->x(), p->y(), p->h()));
204 std::reverse(cusp.begin(), cusp.end());
205 this->cusp().push_back(cusp);
208 this->DH(di * 0.01 / B->out_radi());
213 this->slotSide() == LEFT
214 && this->slot().collide(new RRTNode(c->lfx(), c->lfy(), 0))
216 this->slotSide() == RIGHT
217 && this->slot().collide(new RRTNode(c->rfx(), c->rfy(), 0))
219 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
220 BicycleCar *cc = this->flncr(c);
225 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
226 std::reverse(cusp.begin(), cusp.end());
227 this->cusp().push_back(cusp);
230 BicycleCar *ParallelSlot::flnc(
232 std::vector<CircleObstacle>& co,
233 std::vector<SegmentObstacle>& so
237 if (this->slotSide() == LEFT) {
238 if (int(B->s()) % 2 == 0)
239 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
241 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
243 if (int(B->s()) % 2 == 0)
244 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
246 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
250 p = B->move(cc, i * this->DH());
252 !this->slot().collide(p->frame())
253 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
257 p = B->move(cc, i * this->DH());
259 std::vector<RRTEdge *> eds = p->frame();
272 p = B->move(cc, i * this->DH());
274 !this->slot().collide(p->frame())
275 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
277 if (this->isInside(p)) {
283 p = B->move(cc, i * this->DH());
285 std::vector<RRTEdge *> eds = p->frame();
298 return B->move(cc, (i - 1) * this->DH());
301 BicycleCar *ParallelSlot::flncr(BicycleCar *B)
304 if (this->slotSide() == LEFT) {
305 if (int(B->s()) % 2 == 0)
306 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
308 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
310 if (int(B->s()) % 2 == 0)
311 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
313 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
317 p = B->move(cc, i * this->DH());
319 !this->slot().collide(p->frame())
321 this->slotSide() == LEFT
322 && this->slot().collide(new RRTNode(
328 this->slotSide() == RIGHT
329 && this->slot().collide(new RRTNode(
338 p = B->move(cc, i * this->DH());
341 p = B->move(cc, i * this->DH());
342 while (!this->slot().collide(p->frame())) {
344 this->slotSide() == LEFT
345 && !this->slot().collide(new RRTNode(
355 this->slotSide() == RIGHT
356 && !this->slot().collide(new RRTNode(
366 p = B->move(cc, i * this->DH());
369 return B->move(cc, (i - 1) * this->DH());
372 RRTNode *ParallelSlot::fposecenter()
374 return this->slot().bnodes().front();
377 bool ParallelSlot::flast(
381 std::vector<RRTNode *> &cusp
384 BicycleCar *B = new BicycleCar(P->x(), P->y(), P->h());
387 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
389 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
392 p = B->move(cc, i * this->DH());
393 while (!this->slot().collide(p->frame())
395 (this->DH() > 0 && p->x() <= 0)
396 || (this->DH() < 0 && p->x() >= 0)
400 p = B->move(cc, i * this->DH());
403 p = B->move(cc, i * this->DH());
404 while (!this->slot().collide(p->frame())
406 (this->DH() > 0 && p->x() <= 0)
407 || (this->DH() < 0 && p->x() >= 0)
409 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
413 if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
419 p = B->move(cc, i * this->DH());
422 p = B->move(cc, (i - 1) * this->DH());
423 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
426 } else if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
431 return this->flast(p, !right, il + 1, cusp);
436 void ParallelSlot::fpose()
438 bool left = false; // right parking slot
440 BicycleCar *CC = new BicycleCar(
441 this->fposecenter()->x(),
442 this->fposecenter()->y() - 0.01,
445 BicycleCar *B = new BicycleCar(
446 CC->x() - CC->width() / 2,
447 CC->y() - (CC->length() + CC->wheelbase()) / 2,
450 if (this->slot().bnodes()[0]->x() > this->slot().bnodes()[1]->x()) {
455 CC->x() + CC->width() / 2,
456 CC->y() - (CC->length() + CC->wheelbase()) / 2,
460 this->DH(di * 0.01 / CC->out_radi());
463 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
464 while (!this->slot().collide(p->frame())) {
465 std::vector<RRTNode *> tmpcusp;
466 tmpcusp.push_back(new BicycleCar(p->x(), p->y(), p->h()));
467 if (this->flast(p, left, 0, tmpcusp)) {
468 this->cusp().push_back(tmpcusp);
473 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
477 BicycleCar *ParallelSlot::getEP()
479 // new pose for parallel parking to right slot
484 BicycleCar *CC = this->getEPC();
485 // move left by car width / 2
486 tnx = CC->x() + CC->width() / 2 * cos(CC->h() + M_PI / 2);
487 tny = CC->y() + CC->width() / 2 * sin(CC->h() + M_PI / 2);
488 if (this->slotSide() == LEFT) {
489 // move right by car width / 2
490 tnx = CC->x() + CC->width() / 2 * cos(CC->h() - M_PI / 2);
491 tny = CC->y() + CC->width() / 2 * sin(CC->h() - M_PI / 2);
493 if (this->slotType() == PARALLEL) {
495 nx = tnx - (CC->length() + CC->wheelbase()) / 2 * cos(CC->h());
496 ny = tny - (CC->length() + CC->wheelbase()) / 2 * sin(CC->h());
499 nx = tnx + (CC->length() - CC->wheelbase()) / 2 * cos(CC->h());
500 ny = tny + (CC->length() - CC->wheelbase()) / 2 * sin(CC->h());
502 return new BicycleCar(nx, ny, CC->h());
505 BicycleCar *ParallelSlot::getEPC()
507 // new pose for parallel parking to right slot
511 ta = this->slotHeading() + M_PI;
512 if (this->slotSide() == RIGHT)
516 nx = this->fposecenter()->x() + 0.01 * cos(ta);
517 ny = this->fposecenter()->y() + 0.01 * sin(ta);
518 return new BicycleCar(nx, ny, this->slotHeading());
521 BicycleCar *ParallelSlot::getFP()
523 float x = this->slot().bnodes()[3]->x();
524 float y = this->slot().bnodes()[3]->y();
525 float h = this->slotHeading();
528 if (this->slotType() == PARALLEL) {
529 if (this->slotSide() == LEFT) {
530 nx = x + BCAR_WIDTH / 2 * cos(h + M_PI / 2);
531 ny = y + BCAR_WIDTH / 2 * sin(h + M_PI / 2);
533 nx = x + BCAR_WIDTH / 2 * cos(h - M_PI / 2);
534 ny = y + BCAR_WIDTH / 2 * sin(h - M_PI / 2);
536 x = nx + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * cos(h);
537 y = ny + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * sin(h);
539 if (this->slotSide() == LEFT) {
541 nx = x + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
543 ny = y + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
545 x = nx + (BCAR_DIAG_RRADI) * cos(h + M_PI / 2);
546 y = ny + (BCAR_DIAG_RRADI) * sin(h + M_PI / 2);
549 nx = x + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
551 ny = y + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
553 x = nx + (BCAR_DIAG_RRADI) * cos(h - M_PI / 2);
554 y = ny + (BCAR_DIAG_RRADI) * sin(h - M_PI / 2);
557 return new BicycleCar(x, y, h);
560 bool ParallelSlot::isInside(BicycleCar *c)
564 tmpn = new RRTNode(c->lfx(), c->lfy(), 0);
565 if (!this->slot().collide(tmpn))
568 tmpn = new RRTNode(c->lrx(), c->lry(), 0);
569 if (!this->slot().collide(tmpn))
572 tmpn = new RRTNode(c->rrx(), c->rry(), 0);
573 if (!this->slot().collide(tmpn))
576 tmpn = new RRTNode(c->rfx(), c->rfy(), 0);
577 if (!this->slot().collide(tmpn))
583 struct SamplingInfo ParallelSlot::getSamplingInfo()
585 struct SamplingInfo si;
586 if (this->slotType() == PARALLEL) {
587 si.x = this->slot().bnodes().front()->x();
588 si.y = this->slot().bnodes().front()->y();
589 si.mr = BCAR_WIDTH / 2;
592 si.x = this->slot().bnodes().back()->x();
593 si.x -= this->slot().bnodes().front()->x();
595 si.x += this->slot().bnodes().front()->x();
596 si.y = this->slot().bnodes().back()->y();
597 si.y -= this->slot().bnodes().front()->y();
599 si.y += this->slot().bnodes().front()->y();
601 si.mmh = (M_PI - M_PI / 6) / 2;
606 si.sh = this->slotHeading();
607 if (this->slotSide() == RIGHT)