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 this->DH(di * 0.01 / B->out_radi());
159 this->slotSide() == LEFT
160 && this->slot().collide(new RRTNode(c->lfx(), c->lfy(), 0))
162 this->slotSide() == RIGHT
163 && this->slot().collide(new RRTNode(c->rfx(), c->rfy(), 0))
165 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
166 BicycleCar *cc = this->flncr(c);
171 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
172 std::reverse(cusp.begin(), cusp.end());
173 this->cusp().push_back(cusp);
176 BicycleCar *ParallelSlot::flnc(BicycleCar *B)
179 if (this->slotSide() == LEFT) {
180 if (int(B->s()) % 2 == 0)
181 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
183 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
185 if (int(B->s()) % 2 == 0)
186 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
188 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
192 p = B->move(cc, i * this->DH());
194 !this->slot().collide(p->frame())
195 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
199 p = B->move(cc, i * this->DH());
202 p = B->move(cc, i * this->DH());
204 !this->slot().collide(p->frame())
205 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
207 if (this->isInside(p)) {
213 p = B->move(cc, i * this->DH());
216 return B->move(cc, (i - 1) * this->DH());
219 BicycleCar *ParallelSlot::flncr(BicycleCar *B)
222 if (this->slotSide() == LEFT) {
223 if (int(B->s()) % 2 == 0)
224 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
226 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
228 if (int(B->s()) % 2 == 0)
229 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
231 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
235 p = B->move(cc, i * this->DH());
237 !this->slot().collide(p->frame())
239 this->slotSide() == LEFT
240 && this->slot().collide(new RRTNode(
246 this->slotSide() == RIGHT
247 && this->slot().collide(new RRTNode(
256 p = B->move(cc, i * this->DH());
259 p = B->move(cc, i * this->DH());
260 while (!this->slot().collide(p->frame())) {
262 this->slotSide() == LEFT
263 && !this->slot().collide(new RRTNode(
273 this->slotSide() == RIGHT
274 && !this->slot().collide(new RRTNode(
284 p = B->move(cc, i * this->DH());
287 return B->move(cc, (i - 1) * this->DH());
290 RRTNode *ParallelSlot::fposecenter()
292 return this->slot().bnodes().front();
295 bool ParallelSlot::flast(
299 std::vector<RRTNode *> &cusp
302 BicycleCar *B = new BicycleCar(P->x(), P->y(), P->h());
305 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
307 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
310 p = B->move(cc, i * this->DH());
311 while (!this->slot().collide(p->frame())
313 (this->DH() > 0 && p->x() <= 0)
314 || (this->DH() < 0 && p->x() >= 0)
318 p = B->move(cc, i * this->DH());
321 p = B->move(cc, i * this->DH());
322 while (!this->slot().collide(p->frame())
324 (this->DH() > 0 && p->x() <= 0)
325 || (this->DH() < 0 && p->x() >= 0)
327 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
331 if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
337 p = B->move(cc, i * this->DH());
340 p = B->move(cc, (i - 1) * this->DH());
341 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
344 } else if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
349 return this->flast(p, !right, il + 1, cusp);
354 void ParallelSlot::fpose()
356 bool left = false; // right parking slot
358 BicycleCar *CC = new BicycleCar(
359 this->fposecenter()->x(),
360 this->fposecenter()->y() - 0.01,
363 BicycleCar *B = new BicycleCar(
364 CC->x() - CC->width() / 2,
365 CC->y() - (CC->length() + CC->wheelbase()) / 2,
368 if (this->slot().bnodes()[0]->x() > this->slot().bnodes()[1]->x()) {
373 CC->x() + CC->width() / 2,
374 CC->y() - (CC->length() + CC->wheelbase()) / 2,
378 this->DH(di * 0.01 / CC->out_radi());
381 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
382 while (!this->slot().collide(p->frame())) {
383 std::vector<RRTNode *> tmpcusp;
384 tmpcusp.push_back(new BicycleCar(p->x(), p->y(), p->h()));
385 if (this->flast(p, left, 0, tmpcusp)) {
386 this->cusp().push_back(tmpcusp);
391 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
395 BicycleCar *ParallelSlot::getEP()
397 // new pose for parallel parking to right slot
402 BicycleCar *CC = this->getEPC();
403 // move left by car width / 2
404 tnx = CC->x() + CC->width() / 2 * cos(CC->h() + M_PI / 2);
405 tny = CC->y() + CC->width() / 2 * sin(CC->h() + M_PI / 2);
406 if (this->slotSide() == LEFT) {
407 // move right by car width / 2
408 tnx = CC->x() + CC->width() / 2 * cos(CC->h() - M_PI / 2);
409 tny = CC->y() + CC->width() / 2 * sin(CC->h() - M_PI / 2);
411 if (this->slotType() == PARALLEL) {
413 nx = tnx - (CC->length() + CC->wheelbase()) / 2 * cos(CC->h());
414 ny = tny - (CC->length() + CC->wheelbase()) / 2 * sin(CC->h());
417 nx = tnx + (CC->length() - CC->wheelbase()) / 2 * cos(CC->h());
418 ny = tny + (CC->length() - CC->wheelbase()) / 2 * sin(CC->h());
420 return new BicycleCar(nx, ny, CC->h());
423 BicycleCar *ParallelSlot::getEPC()
425 // new pose for parallel parking to right slot
429 ta = this->slotHeading() + M_PI;
430 if (this->slotSide() == RIGHT)
434 nx = this->fposecenter()->x() + 0.01 * cos(ta);
435 ny = this->fposecenter()->y() + 0.01 * sin(ta);
436 return new BicycleCar(nx, ny, this->slotHeading());
439 BicycleCar *ParallelSlot::getFP()
441 float x = this->slot().bnodes()[3]->x();
442 float y = this->slot().bnodes()[3]->y();
443 float h = this->slotHeading();
446 if (this->slotType() == PARALLEL) {
447 if (this->slotSide() == LEFT) {
448 nx = x + BCAR_WIDTH / 2 * cos(h + M_PI / 2);
449 ny = y + BCAR_WIDTH / 2 * sin(h + M_PI / 2);
451 nx = x + BCAR_WIDTH / 2 * cos(h - M_PI / 2);
452 ny = y + BCAR_WIDTH / 2 * sin(h - M_PI / 2);
454 x = nx + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * cos(h);
455 y = ny + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * sin(h);
457 if (this->slotSide() == LEFT) {
459 nx = x + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
461 ny = y + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
463 x = nx + (BCAR_WIDTH + 0.01) * cos(h + M_PI / 2);
464 y = ny + (BCAR_WIDTH + 0.01) * sin(h + M_PI / 2);
467 nx = x + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
469 ny = y + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
471 x = nx + (BCAR_WIDTH / 2 + 0.01) * cos(h - M_PI / 2);
472 y = ny + (BCAR_WIDTH / 2 + 0.01) * sin(h - M_PI / 2);
475 return new BicycleCar(x, y, h);
478 bool ParallelSlot::isInside(BicycleCar *c)
482 tmpn = new RRTNode(c->lfx(), c->lfy(), 0);
483 if (!this->slot().collide(tmpn))
486 tmpn = new RRTNode(c->lrx(), c->lry(), 0);
487 if (!this->slot().collide(tmpn))
490 tmpn = new RRTNode(c->rrx(), c->rry(), 0);
491 if (!this->slot().collide(tmpn))
494 tmpn = new RRTNode(c->rfx(), c->rfy(), 0);
495 if (!this->slot().collide(tmpn))
501 struct SamplingInfo ParallelSlot::getSamplingInfo()
503 struct SamplingInfo si;
504 BicycleCar *CC = this->getEPC();
505 si.x = this->slot().bnodes()[0]->x();
506 si.y = this->slot().bnodes()[0]->y();
507 if (this->slotSide() == RIGHT) {
516 si.r = CC->diag_radi();
517 si.sh = this->slotHeading();
518 if (this->slotType() == PARALLEL) {
519 si.h = this->slotHeading() - acos(EDIST(
520 this->slot().bnodes()[0],
521 this->slot().bnodes()[1]