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 cusp.push_back(new RRTNode(
156 B->x() - di * B->length() * cos(B->h()),
157 B->y() - di * B->length() * sin(B->h()),
160 std::reverse(cusp.begin(), cusp.end());
161 this->cusp().push_back(cusp);
164 this->DH(di * 0.01 / B->out_radi());
169 this->slotSide() == LEFT
170 && this->slot().collide(new RRTNode(c->lfx(), c->lfy(), 0))
172 this->slotSide() == RIGHT
173 && this->slot().collide(new RRTNode(c->rfx(), c->rfy(), 0))
175 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
176 BicycleCar *cc = this->flncr(c);
181 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
182 std::reverse(cusp.begin(), cusp.end());
183 this->cusp().push_back(cusp);
186 BicycleCar *ParallelSlot::flnc(BicycleCar *B)
189 if (this->slotSide() == LEFT) {
190 if (int(B->s()) % 2 == 0)
191 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
193 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
195 if (int(B->s()) % 2 == 0)
196 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
198 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
202 p = B->move(cc, i * this->DH());
204 !this->slot().collide(p->frame())
205 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
209 p = B->move(cc, i * this->DH());
212 p = B->move(cc, i * this->DH());
214 !this->slot().collide(p->frame())
215 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
217 if (this->isInside(p)) {
223 p = B->move(cc, i * this->DH());
226 return B->move(cc, (i - 1) * this->DH());
229 BicycleCar *ParallelSlot::flncr(BicycleCar *B)
232 if (this->slotSide() == LEFT) {
233 if (int(B->s()) % 2 == 0)
234 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
236 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
238 if (int(B->s()) % 2 == 0)
239 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
241 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
245 p = B->move(cc, i * this->DH());
247 !this->slot().collide(p->frame())
249 this->slotSide() == LEFT
250 && this->slot().collide(new RRTNode(
256 this->slotSide() == RIGHT
257 && this->slot().collide(new RRTNode(
266 p = B->move(cc, i * this->DH());
269 p = B->move(cc, i * this->DH());
270 while (!this->slot().collide(p->frame())) {
272 this->slotSide() == LEFT
273 && !this->slot().collide(new RRTNode(
283 this->slotSide() == RIGHT
284 && !this->slot().collide(new RRTNode(
294 p = B->move(cc, i * this->DH());
297 return B->move(cc, (i - 1) * this->DH());
300 RRTNode *ParallelSlot::fposecenter()
302 return this->slot().bnodes().front();
305 bool ParallelSlot::flast(
309 std::vector<RRTNode *> &cusp
312 BicycleCar *B = new BicycleCar(P->x(), P->y(), P->h());
315 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
317 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
320 p = B->move(cc, i * this->DH());
321 while (!this->slot().collide(p->frame())
323 (this->DH() > 0 && p->x() <= 0)
324 || (this->DH() < 0 && p->x() >= 0)
328 p = B->move(cc, i * this->DH());
331 p = B->move(cc, i * this->DH());
332 while (!this->slot().collide(p->frame())
334 (this->DH() > 0 && p->x() <= 0)
335 || (this->DH() < 0 && p->x() >= 0)
337 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
341 if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
347 p = B->move(cc, i * this->DH());
350 p = B->move(cc, (i - 1) * this->DH());
351 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
354 } else if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
359 return this->flast(p, !right, il + 1, cusp);
364 void ParallelSlot::fpose()
366 bool left = false; // right parking slot
368 BicycleCar *CC = new BicycleCar(
369 this->fposecenter()->x(),
370 this->fposecenter()->y() - 0.01,
373 BicycleCar *B = new BicycleCar(
374 CC->x() - CC->width() / 2,
375 CC->y() - (CC->length() + CC->wheelbase()) / 2,
378 if (this->slot().bnodes()[0]->x() > this->slot().bnodes()[1]->x()) {
383 CC->x() + CC->width() / 2,
384 CC->y() - (CC->length() + CC->wheelbase()) / 2,
388 this->DH(di * 0.01 / CC->out_radi());
391 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
392 while (!this->slot().collide(p->frame())) {
393 std::vector<RRTNode *> tmpcusp;
394 tmpcusp.push_back(new BicycleCar(p->x(), p->y(), p->h()));
395 if (this->flast(p, left, 0, tmpcusp)) {
396 this->cusp().push_back(tmpcusp);
401 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
405 BicycleCar *ParallelSlot::getEP()
407 // new pose for parallel parking to right slot
412 BicycleCar *CC = this->getEPC();
413 // move left by car width / 2
414 tnx = CC->x() + CC->width() / 2 * cos(CC->h() + M_PI / 2);
415 tny = CC->y() + CC->width() / 2 * sin(CC->h() + M_PI / 2);
416 if (this->slotSide() == LEFT) {
417 // move right by car width / 2
418 tnx = CC->x() + CC->width() / 2 * cos(CC->h() - M_PI / 2);
419 tny = CC->y() + CC->width() / 2 * sin(CC->h() - M_PI / 2);
421 if (this->slotType() == PARALLEL) {
423 nx = tnx - (CC->length() + CC->wheelbase()) / 2 * cos(CC->h());
424 ny = tny - (CC->length() + CC->wheelbase()) / 2 * sin(CC->h());
427 nx = tnx + (CC->length() - CC->wheelbase()) / 2 * cos(CC->h());
428 ny = tny + (CC->length() - CC->wheelbase()) / 2 * sin(CC->h());
430 return new BicycleCar(nx, ny, CC->h());
433 BicycleCar *ParallelSlot::getEPC()
435 // new pose for parallel parking to right slot
439 ta = this->slotHeading() + M_PI;
440 if (this->slotSide() == RIGHT)
444 nx = this->fposecenter()->x() + 0.01 * cos(ta);
445 ny = this->fposecenter()->y() + 0.01 * sin(ta);
446 return new BicycleCar(nx, ny, this->slotHeading());
449 BicycleCar *ParallelSlot::getFP()
451 float x = this->slot().bnodes()[3]->x();
452 float y = this->slot().bnodes()[3]->y();
453 float h = this->slotHeading();
456 if (this->slotType() == PARALLEL) {
457 if (this->slotSide() == LEFT) {
458 nx = x + BCAR_WIDTH / 2 * cos(h + M_PI / 2);
459 ny = y + BCAR_WIDTH / 2 * sin(h + M_PI / 2);
461 nx = x + BCAR_WIDTH / 2 * cos(h - M_PI / 2);
462 ny = y + BCAR_WIDTH / 2 * sin(h - M_PI / 2);
464 x = nx + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * cos(h);
465 y = ny + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * sin(h);
467 if (this->slotSide() == LEFT) {
469 nx = x + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
471 ny = y + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
473 x = nx + (BCAR_DIAG_RRADI) * cos(h + M_PI / 2);
474 y = ny + (BCAR_DIAG_RRADI) * sin(h + M_PI / 2);
477 nx = x + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
479 ny = y + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
481 x = nx + (BCAR_DIAG_RRADI) * cos(h - M_PI / 2);
482 y = ny + (BCAR_DIAG_RRADI) * sin(h - M_PI / 2);
485 return new BicycleCar(x, y, h);
488 bool ParallelSlot::isInside(BicycleCar *c)
492 tmpn = new RRTNode(c->lfx(), c->lfy(), 0);
493 if (!this->slot().collide(tmpn))
496 tmpn = new RRTNode(c->lrx(), c->lry(), 0);
497 if (!this->slot().collide(tmpn))
500 tmpn = new RRTNode(c->rrx(), c->rry(), 0);
501 if (!this->slot().collide(tmpn))
504 tmpn = new RRTNode(c->rfx(), c->rfy(), 0);
505 if (!this->slot().collide(tmpn))
511 struct SamplingInfo ParallelSlot::getSamplingInfo()
513 struct SamplingInfo si;
514 BicycleCar *CC = this->getEPC();
515 si.x = this->slot().bnodes()[0]->x();
516 si.y = this->slot().bnodes()[0]->y();
517 if (this->slotSide() == RIGHT) {
526 si.r = CC->diag_radi();
527 si.sh = this->slotHeading();
528 if (this->slotType() == PARALLEL) {
529 si.h = this->slotHeading() - acos(EDIST(
530 this->slot().bnodes()[0],
531 this->slot().bnodes()[1]