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(RRTNode *n)
149 return this->fipr(new BicycleCar(n->x(), n->y(), n->h()));
152 void ParallelSlot::fipr(BicycleCar *B)
154 std::vector<RRTNode *> cusp;
155 cusp.push_back(new RRTNode(B->x(), B->y(), B->h()));
157 if (this->slotSide() == LEFT)
159 if (this->slotType() == PERPENDICULAR) {
160 this->DH(di * 0.01 / B->out_radi()); // TODO car in slot h()
162 if (this->slotSide() == LEFT)
163 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
165 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
168 p = B->move(cc, i * this->DH());
170 !this->slot().collide(p->frame())
171 && this->slot().collide(p)
175 p = B->move(cc, i * this->DH());
178 p = B->move(cc, i * this->DH());
180 !this->slot().collide(p->frame())
181 && this->slot().collide(p)
185 p = B->move(cc, i * this->DH());
188 p = B->move(cc, i * this->DH());
189 cusp.push_back(new RRTNode(p->x(), p->y(), p->h()));
190 std::reverse(cusp.begin(), cusp.end());
191 this->cusp().push_back(cusp);
194 this->DH(di * 0.01 / B->out_radi());
199 this->slotSide() == LEFT
200 && this->slot().collide(new RRTNode(c->lfx(), c->lfy(), 0))
202 this->slotSide() == RIGHT
203 && this->slot().collide(new RRTNode(c->rfx(), c->rfy(), 0))
205 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
206 BicycleCar *cc = this->flncr(c);
211 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
212 std::reverse(cusp.begin(), cusp.end());
213 this->cusp().push_back(cusp);
216 BicycleCar *ParallelSlot::flnc(BicycleCar *B)
219 if (this->slotSide() == LEFT) {
220 if (int(B->s()) % 2 == 0)
221 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
223 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
225 if (int(B->s()) % 2 == 0)
226 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
228 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
232 p = B->move(cc, i * this->DH());
234 !this->slot().collide(p->frame())
235 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
239 p = B->move(cc, i * this->DH());
242 p = B->move(cc, i * this->DH());
244 !this->slot().collide(p->frame())
245 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
247 if (this->isInside(p)) {
253 p = B->move(cc, i * this->DH());
256 return B->move(cc, (i - 1) * this->DH());
259 BicycleCar *ParallelSlot::flncr(BicycleCar *B)
262 if (this->slotSide() == LEFT) {
263 if (int(B->s()) % 2 == 0)
264 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
266 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
268 if (int(B->s()) % 2 == 0)
269 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
271 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
275 p = B->move(cc, i * this->DH());
277 !this->slot().collide(p->frame())
279 this->slotSide() == LEFT
280 && this->slot().collide(new RRTNode(
286 this->slotSide() == RIGHT
287 && this->slot().collide(new RRTNode(
296 p = B->move(cc, i * this->DH());
299 p = B->move(cc, i * this->DH());
300 while (!this->slot().collide(p->frame())) {
302 this->slotSide() == LEFT
303 && !this->slot().collide(new RRTNode(
313 this->slotSide() == RIGHT
314 && !this->slot().collide(new RRTNode(
324 p = B->move(cc, i * this->DH());
327 return B->move(cc, (i - 1) * this->DH());
330 RRTNode *ParallelSlot::fposecenter()
332 return this->slot().bnodes().front();
335 bool ParallelSlot::flast(
339 std::vector<RRTNode *> &cusp
342 BicycleCar *B = new BicycleCar(P->x(), P->y(), P->h());
345 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
347 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
350 p = B->move(cc, i * this->DH());
351 while (!this->slot().collide(p->frame())
353 (this->DH() > 0 && p->x() <= 0)
354 || (this->DH() < 0 && p->x() >= 0)
358 p = B->move(cc, i * this->DH());
361 p = B->move(cc, i * this->DH());
362 while (!this->slot().collide(p->frame())
364 (this->DH() > 0 && p->x() <= 0)
365 || (this->DH() < 0 && p->x() >= 0)
367 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
371 if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
377 p = B->move(cc, i * this->DH());
380 p = B->move(cc, (i - 1) * this->DH());
381 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
384 } else if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
389 return this->flast(p, !right, il + 1, cusp);
394 void ParallelSlot::fpose()
396 bool left = false; // right parking slot
398 BicycleCar *CC = new BicycleCar(
399 this->fposecenter()->x(),
400 this->fposecenter()->y() - 0.01,
403 BicycleCar *B = new BicycleCar(
404 CC->x() - CC->width() / 2,
405 CC->y() - (CC->length() + CC->wheelbase()) / 2,
408 if (this->slot().bnodes()[0]->x() > this->slot().bnodes()[1]->x()) {
413 CC->x() + CC->width() / 2,
414 CC->y() - (CC->length() + CC->wheelbase()) / 2,
418 this->DH(di * 0.01 / CC->out_radi());
421 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
422 while (!this->slot().collide(p->frame())) {
423 std::vector<RRTNode *> tmpcusp;
424 tmpcusp.push_back(new BicycleCar(p->x(), p->y(), p->h()));
425 if (this->flast(p, left, 0, tmpcusp)) {
426 this->cusp().push_back(tmpcusp);
431 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
435 BicycleCar *ParallelSlot::getEP()
437 // new pose for parallel parking to right slot
442 BicycleCar *CC = this->getEPC();
443 // move left by car width / 2
444 tnx = CC->x() + CC->width() / 2 * cos(CC->h() + M_PI / 2);
445 tny = CC->y() + CC->width() / 2 * sin(CC->h() + M_PI / 2);
446 if (this->slotSide() == LEFT) {
447 // move right by car width / 2
448 tnx = CC->x() + CC->width() / 2 * cos(CC->h() - M_PI / 2);
449 tny = CC->y() + CC->width() / 2 * sin(CC->h() - M_PI / 2);
451 if (this->slotType() == PARALLEL) {
453 nx = tnx - (CC->length() + CC->wheelbase()) / 2 * cos(CC->h());
454 ny = tny - (CC->length() + CC->wheelbase()) / 2 * sin(CC->h());
457 nx = tnx + (CC->length() - CC->wheelbase()) / 2 * cos(CC->h());
458 ny = tny + (CC->length() - CC->wheelbase()) / 2 * sin(CC->h());
460 return new BicycleCar(nx, ny, CC->h());
463 BicycleCar *ParallelSlot::getEPC()
465 // new pose for parallel parking to right slot
469 ta = this->slotHeading() + M_PI;
470 if (this->slotSide() == RIGHT)
474 nx = this->fposecenter()->x() + 0.01 * cos(ta);
475 ny = this->fposecenter()->y() + 0.01 * sin(ta);
476 return new BicycleCar(nx, ny, this->slotHeading());
479 BicycleCar *ParallelSlot::getFP()
481 float x = this->slot().bnodes()[3]->x();
482 float y = this->slot().bnodes()[3]->y();
483 float h = this->slotHeading();
486 if (this->slotType() == PARALLEL) {
487 if (this->slotSide() == LEFT) {
488 nx = x + BCAR_WIDTH / 2 * cos(h + M_PI / 2);
489 ny = y + BCAR_WIDTH / 2 * sin(h + M_PI / 2);
491 nx = x + BCAR_WIDTH / 2 * cos(h - M_PI / 2);
492 ny = y + BCAR_WIDTH / 2 * sin(h - M_PI / 2);
494 x = nx + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * cos(h);
495 y = ny + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * sin(h);
497 if (this->slotSide() == LEFT) {
499 nx = x + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
501 ny = y + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
503 x = nx + (BCAR_DIAG_RRADI) * cos(h + M_PI / 2);
504 y = ny + (BCAR_DIAG_RRADI) * sin(h + M_PI / 2);
507 nx = x + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
509 ny = y + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
511 x = nx + (BCAR_DIAG_RRADI) * cos(h - M_PI / 2);
512 y = ny + (BCAR_DIAG_RRADI) * sin(h - M_PI / 2);
515 return new BicycleCar(x, y, h);
518 bool ParallelSlot::isInside(BicycleCar *c)
522 tmpn = new RRTNode(c->lfx(), c->lfy(), 0);
523 if (!this->slot().collide(tmpn))
526 tmpn = new RRTNode(c->lrx(), c->lry(), 0);
527 if (!this->slot().collide(tmpn))
530 tmpn = new RRTNode(c->rrx(), c->rry(), 0);
531 if (!this->slot().collide(tmpn))
534 tmpn = new RRTNode(c->rfx(), c->rfy(), 0);
535 if (!this->slot().collide(tmpn))
541 struct SamplingInfo ParallelSlot::getSamplingInfo()
543 struct SamplingInfo si;
544 BicycleCar *CC = this->getEPC();
545 si.x = this->slot().bnodes()[0]->x();
546 si.y = this->slot().bnodes()[0]->y();
547 if (this->slotSide() == RIGHT) {
556 si.r = CC->diag_radi();
557 si.sh = this->slotHeading();
558 if (this->slotType() == PARALLEL) {
559 si.h = this->slotHeading() - acos(EDIST(
560 this->slot().bnodes()[0],
561 this->slot().bnodes()[1]