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()
101 // see https://courses.cs.washington.edu/courses/cse326/03su/homework/hw3/bfs.html
102 // RRTNode.s() works as iteration level
103 std::queue<BicycleCar *, std::list<BicycleCar *>> q;
104 std::queue<BicycleCar *, std::list<BicycleCar *>> empty;
106 if (this->slotSide() == LEFT)
108 BicycleCar *CC = this->getEPC();
109 BicycleCar *B = this->getEP();
110 this->DH(di * 0.01 / CC->out_radi());
113 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
114 while (!this->slot().collide(c->frame())) {
116 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
119 delete c; // not in q and collide
123 if (this->isInside(c)) {
124 goto createcuspandfinish;
125 } else if (c->s() < 9) {
126 BicycleCar *cc = this->flnc(c);
131 delete c; // not in q and collide
137 std::vector<RRTNode *> cusp;
139 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
142 std::reverse(cusp.begin(), cusp.end());
143 this->cusp().push_back(cusp);
147 void ParallelSlot::fipr(BicycleCar *B)
149 std::vector<RRTNode *> cusp;
150 cusp.push_back(new RRTNode(B->x(), B->y(), B->h()));
152 if (this->slotSide() == LEFT)
154 if (this->slotType() == PERPENDICULAR) {
155 this->DH(di * 0.01 / B->out_radi()); // TODO car in slot h()
157 if (this->slotSide() == LEFT)
158 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
160 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
163 p = B->move(cc, i * this->DH());
165 !this->slot().collide(p->frame())
166 && this->slot().collide(p)
170 p = B->move(cc, i * this->DH());
173 p = B->move(cc, i * this->DH());
175 !this->slot().collide(p->frame())
176 && this->slot().collide(p)
180 p = B->move(cc, i * this->DH());
183 p = B->move(cc, i * this->DH());
184 cusp.push_back(new RRTNode(p->x(), p->y(), p->h()));
185 std::reverse(cusp.begin(), cusp.end());
186 this->cusp().push_back(cusp);
189 this->DH(di * 0.01 / B->out_radi());
194 this->slotSide() == LEFT
195 && this->slot().collide(new RRTNode(c->lfx(), c->lfy(), 0))
197 this->slotSide() == RIGHT
198 && this->slot().collide(new RRTNode(c->rfx(), c->rfy(), 0))
200 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
201 BicycleCar *cc = this->flncr(c);
206 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
207 std::reverse(cusp.begin(), cusp.end());
208 this->cusp().push_back(cusp);
211 BicycleCar *ParallelSlot::flnc(BicycleCar *B)
214 if (this->slotSide() == LEFT) {
215 if (int(B->s()) % 2 == 0)
216 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
218 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
220 if (int(B->s()) % 2 == 0)
221 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
223 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
227 p = B->move(cc, i * this->DH());
229 !this->slot().collide(p->frame())
230 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
234 p = B->move(cc, i * this->DH());
237 p = B->move(cc, i * this->DH());
239 !this->slot().collide(p->frame())
240 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
242 if (this->isInside(p)) {
248 p = B->move(cc, i * this->DH());
251 return B->move(cc, (i - 1) * this->DH());
254 BicycleCar *ParallelSlot::flncr(BicycleCar *B)
257 if (this->slotSide() == LEFT) {
258 if (int(B->s()) % 2 == 0)
259 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
261 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
263 if (int(B->s()) % 2 == 0)
264 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
266 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
270 p = B->move(cc, i * this->DH());
272 !this->slot().collide(p->frame())
274 this->slotSide() == LEFT
275 && this->slot().collide(new RRTNode(
281 this->slotSide() == RIGHT
282 && this->slot().collide(new RRTNode(
291 p = B->move(cc, i * this->DH());
294 p = B->move(cc, i * this->DH());
295 while (!this->slot().collide(p->frame())) {
297 this->slotSide() == LEFT
298 && !this->slot().collide(new RRTNode(
308 this->slotSide() == RIGHT
309 && !this->slot().collide(new RRTNode(
319 p = B->move(cc, i * this->DH());
322 return B->move(cc, (i - 1) * this->DH());
325 RRTNode *ParallelSlot::fposecenter()
327 return this->slot().bnodes().front();
330 bool ParallelSlot::flast(
334 std::vector<RRTNode *> &cusp
337 BicycleCar *B = new BicycleCar(P->x(), P->y(), P->h());
340 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
342 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
345 p = B->move(cc, i * this->DH());
346 while (!this->slot().collide(p->frame())
348 (this->DH() > 0 && p->x() <= 0)
349 || (this->DH() < 0 && p->x() >= 0)
353 p = B->move(cc, i * this->DH());
356 p = B->move(cc, i * this->DH());
357 while (!this->slot().collide(p->frame())
359 (this->DH() > 0 && p->x() <= 0)
360 || (this->DH() < 0 && p->x() >= 0)
362 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
366 if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
372 p = B->move(cc, i * this->DH());
375 p = B->move(cc, (i - 1) * this->DH());
376 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
379 } else if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
384 return this->flast(p, !right, il + 1, cusp);
389 void ParallelSlot::fpose()
391 bool left = false; // right parking slot
393 BicycleCar *CC = new BicycleCar(
394 this->fposecenter()->x(),
395 this->fposecenter()->y() - 0.01,
398 BicycleCar *B = new BicycleCar(
399 CC->x() - CC->width() / 2,
400 CC->y() - (CC->length() + CC->wheelbase()) / 2,
403 if (this->slot().bnodes()[0]->x() > this->slot().bnodes()[1]->x()) {
408 CC->x() + CC->width() / 2,
409 CC->y() - (CC->length() + CC->wheelbase()) / 2,
413 this->DH(di * 0.01 / CC->out_radi());
416 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
417 while (!this->slot().collide(p->frame())) {
418 std::vector<RRTNode *> tmpcusp;
419 tmpcusp.push_back(new BicycleCar(p->x(), p->y(), p->h()));
420 if (this->flast(p, left, 0, tmpcusp)) {
421 this->cusp().push_back(tmpcusp);
426 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
430 BicycleCar *ParallelSlot::getEP()
432 // new pose for parallel parking to right slot
437 BicycleCar *CC = this->getEPC();
438 // move left by car width / 2
439 tnx = CC->x() + CC->width() / 2 * cos(CC->h() + M_PI / 2);
440 tny = CC->y() + CC->width() / 2 * sin(CC->h() + M_PI / 2);
441 if (this->slotSide() == LEFT) {
442 // move right by car width / 2
443 tnx = CC->x() + CC->width() / 2 * cos(CC->h() - M_PI / 2);
444 tny = CC->y() + CC->width() / 2 * sin(CC->h() - M_PI / 2);
446 if (this->slotType() == PARALLEL) {
448 nx = tnx - (CC->length() + CC->wheelbase()) / 2 * cos(CC->h());
449 ny = tny - (CC->length() + CC->wheelbase()) / 2 * sin(CC->h());
452 nx = tnx + (CC->length() - CC->wheelbase()) / 2 * cos(CC->h());
453 ny = tny + (CC->length() - CC->wheelbase()) / 2 * sin(CC->h());
455 return new BicycleCar(nx, ny, CC->h());
458 BicycleCar *ParallelSlot::getEPC()
460 // new pose for parallel parking to right slot
464 ta = this->slotHeading() + M_PI;
465 if (this->slotSide() == RIGHT)
469 nx = this->fposecenter()->x() + 0.01 * cos(ta);
470 ny = this->fposecenter()->y() + 0.01 * sin(ta);
471 return new BicycleCar(nx, ny, this->slotHeading());
474 BicycleCar *ParallelSlot::getFP()
476 float x = this->slot().bnodes()[3]->x();
477 float y = this->slot().bnodes()[3]->y();
478 float h = this->slotHeading();
481 if (this->slotType() == PARALLEL) {
482 if (this->slotSide() == LEFT) {
483 nx = x + BCAR_WIDTH / 2 * cos(h + M_PI / 2);
484 ny = y + BCAR_WIDTH / 2 * sin(h + M_PI / 2);
486 nx = x + BCAR_WIDTH / 2 * cos(h - M_PI / 2);
487 ny = y + BCAR_WIDTH / 2 * sin(h - M_PI / 2);
489 x = nx + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * cos(h);
490 y = ny + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * sin(h);
492 if (this->slotSide() == LEFT) {
494 nx = x + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
496 ny = y + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
498 x = nx + (BCAR_DIAG_RRADI) * cos(h + M_PI / 2);
499 y = ny + (BCAR_DIAG_RRADI) * sin(h + M_PI / 2);
502 nx = x + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
504 ny = y + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
506 x = nx + (BCAR_DIAG_RRADI) * cos(h - M_PI / 2);
507 y = ny + (BCAR_DIAG_RRADI) * sin(h - M_PI / 2);
510 return new BicycleCar(x, y, h);
513 bool ParallelSlot::isInside(BicycleCar *c)
517 tmpn = new RRTNode(c->lfx(), c->lfy(), 0);
518 if (!this->slot().collide(tmpn))
521 tmpn = new RRTNode(c->lrx(), c->lry(), 0);
522 if (!this->slot().collide(tmpn))
525 tmpn = new RRTNode(c->rrx(), c->rry(), 0);
526 if (!this->slot().collide(tmpn))
529 tmpn = new RRTNode(c->rfx(), c->rfy(), 0);
530 if (!this->slot().collide(tmpn))
536 struct SamplingInfo ParallelSlot::getSamplingInfo()
538 struct SamplingInfo si;
539 BicycleCar *CC = this->getEPC();
540 si.x = this->slot().bnodes()[0]->x();
541 si.y = this->slot().bnodes()[0]->y();
542 if (this->slotSide() == RIGHT) {
551 si.r = CC->diag_radi();
552 si.sh = this->slotHeading();
553 if (this->slotType() == PARALLEL) {
554 si.h = this->slotHeading() - acos(EDIST(
555 this->slot().bnodes()[0],
556 this->slot().bnodes()[1]