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 if (!this->slotSide_) {
57 float y0 = this->slot().bnodes()[0]->y();
58 float x0 = this->slot().bnodes()[0]->x();
59 float y1 = this->slot().bnodes()[1]->y();
60 float x1 = this->slot().bnodes()[1]->x();
61 float y3 = this->slot().bnodes()[3]->y();
62 float x3 = this->slot().bnodes()[3]->x();
63 if (sgn((x1 - x3) * (y0 - y3) - (y1 - y3) * (x0 - x3)) < 0)
64 this->slotSide_ = LEFT;
66 this->slotSide_ = RIGHT;
68 return this->slotSide_;
71 SlotType ParallelSlot::slotType()
73 if (!this->slotType_) {
75 this->slot().bnodes()[0],
76 this->slot().bnodes()[1]
79 this->slot().bnodes()[1],
80 this->slot().bnodes()[2]
83 this->slotType_ = PERPENDICULAR;
85 this->slotType_ = PARALLEL;
87 return this->slotType_;
91 void ParallelSlot::DH(float dh)
97 void ParallelSlot::fip()
99 // see https://courses.cs.washington.edu/courses/cse326/03su/homework/hw3/bfs.html
100 // RRTNode.s() works as iteration level
101 std::queue<BicycleCar *, std::list<BicycleCar *>> q;
102 std::queue<BicycleCar *, std::list<BicycleCar *>> empty;
104 // new pose for parallel parking to right slot
109 // temporary tnx is angle
110 tnx = this->slotHeading() + M_PI;
111 if (this->slotSide() == RIGHT)
115 nx = this->fposecenter()->x() + 0.01 * cos(tnx);
116 ny = this->fposecenter()->y() + 0.01 * sin(tnx);
117 BicycleCar *CC = new BicycleCar(nx, ny, this->slotHeading());
118 // move left by car width / 2
119 tnx = CC->x() + CC->width() / 2 * cos(CC->h() + M_PI / 2);
120 tny = CC->y() + CC->width() / 2 * sin(CC->h() + M_PI / 2);
121 if (this->slotSide() == LEFT) {
123 // move right by car width / 2
124 tnx = CC->x() + CC->width() / 2 * cos(CC->h() - M_PI / 2);
125 tny = CC->y() + CC->width() / 2 * sin(CC->h() - M_PI / 2);
128 nx = tnx - (CC->length() + CC->wheelbase()) / 2 * cos(CC->h());
129 ny = tny - (CC->length() + CC->wheelbase()) / 2 * sin(CC->h());
130 BicycleCar *B = new BicycleCar(nx, ny, CC->h());
131 this->DH(di * 0.01 / CC->out_radi());
134 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
135 while (!this->slot().collide(c->frame())) {
137 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
140 delete c; // not in q and collide
144 if (this->isInside(c)) {
145 goto createcuspandfinish;
146 } else if (c->s() < 9) {
147 BicycleCar *cc = this->flnc(c);
152 delete c; // not in q and collide
158 std::vector<RRTNode *> cusp;
160 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
163 std::reverse(cusp.begin(), cusp.end());
164 this->cusp().push_back(cusp);
168 void ParallelSlot::fipr(BicycleCar *B)
170 std::vector<RRTNode *> cusp;
171 cusp.push_back(new RRTNode(B->x(), B->y(), B->h()));
173 if (this->slotSide() == LEFT)
175 this->DH(di * 0.01 / B->out_radi());
180 this->slotSide() == LEFT
181 && this->slot().collide(new RRTNode(c->lfx(), c->lfy(), 0))
183 this->slotSide() == RIGHT
184 && this->slot().collide(new RRTNode(c->rfx(), c->rfy(), 0))
186 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
187 BicycleCar *cc = this->flncr(c);
192 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
193 std::reverse(cusp.begin(), cusp.end());
194 this->cusp().push_back(cusp);
197 BicycleCar *ParallelSlot::flnc(BicycleCar *B)
200 if (this->slotSide() == LEFT) {
201 if (int(B->s()) % 2 == 0)
202 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
204 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
206 if (int(B->s()) % 2 == 0)
207 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
209 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
213 p = B->move(cc, i * this->DH());
215 !this->slot().collide(p->frame())
216 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
220 p = B->move(cc, i * this->DH());
223 p = B->move(cc, i * this->DH());
225 !this->slot().collide(p->frame())
226 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
228 if (this->isInside(p)) {
234 p = B->move(cc, i * this->DH());
237 return B->move(cc, (i - 1) * this->DH());
240 BicycleCar *ParallelSlot::flncr(BicycleCar *B)
243 if (this->slotSide() == LEFT) {
244 if (int(B->s()) % 2 == 0)
245 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
247 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
249 if (int(B->s()) % 2 == 0)
250 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
252 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
256 p = B->move(cc, i * this->DH());
258 !this->slot().collide(p->frame())
260 this->slotSide() == LEFT
261 && this->slot().collide(new RRTNode(
267 this->slotSide() == RIGHT
268 && this->slot().collide(new RRTNode(
277 p = B->move(cc, i * this->DH());
280 p = B->move(cc, i * this->DH());
281 while (!this->slot().collide(p->frame())) {
283 this->slotSide() == LEFT
284 && !this->slot().collide(new RRTNode(
294 this->slotSide() == RIGHT
295 && !this->slot().collide(new RRTNode(
305 p = B->move(cc, i * this->DH());
308 return B->move(cc, (i - 1) * this->DH());
311 RRTNode *ParallelSlot::fposecenter()
313 return this->slot().bnodes().front();
316 bool ParallelSlot::flast(
320 std::vector<RRTNode *> &cusp
323 BicycleCar *B = new BicycleCar(P->x(), P->y(), P->h());
326 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
328 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
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)
339 p = B->move(cc, i * this->DH());
342 p = B->move(cc, i * this->DH());
343 while (!this->slot().collide(p->frame())
345 (this->DH() > 0 && p->x() <= 0)
346 || (this->DH() < 0 && p->x() >= 0)
348 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
352 if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
358 p = B->move(cc, i * this->DH());
361 p = B->move(cc, (i - 1) * this->DH());
362 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
365 } else if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
370 return this->flast(p, !right, il + 1, cusp);
375 void ParallelSlot::fpose()
377 bool left = false; // right parking slot
379 BicycleCar *CC = new BicycleCar(
380 this->fposecenter()->x(),
381 this->fposecenter()->y() - 0.01,
384 BicycleCar *B = new BicycleCar(
385 CC->x() - CC->width() / 2,
386 CC->y() - (CC->length() + CC->wheelbase()) / 2,
389 if (this->slot().bnodes()[0]->x() > this->slot().bnodes()[1]->x()) {
394 CC->x() + CC->width() / 2,
395 CC->y() - (CC->length() + CC->wheelbase()) / 2,
399 this->DH(di * 0.01 / CC->out_radi());
402 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
403 while (!this->slot().collide(p->frame())) {
404 std::vector<RRTNode *> tmpcusp;
405 tmpcusp.push_back(new BicycleCar(p->x(), p->y(), p->h()));
406 if (this->flast(p, left, 0, tmpcusp)) {
407 this->cusp().push_back(tmpcusp);
412 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
416 BicycleCar *ParallelSlot::getFP()
418 float x = this->slot().bnodes()[3]->x();
419 float y = this->slot().bnodes()[3]->y();
420 float h = this->slotHeading();
423 if (this->slotSide() == LEFT) {
424 nx = x + BCAR_WIDTH / 2 * cos(h + M_PI / 2);
425 ny = y + BCAR_WIDTH / 2 * sin(h + M_PI / 2);
427 nx = x + BCAR_WIDTH / 2 * cos(h - M_PI / 2);
428 ny = y + BCAR_WIDTH / 2 * sin(h - M_PI / 2);
430 x = nx + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * cos(h);
431 y = ny + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * sin(h);
432 return new BicycleCar(x, y, h);
435 bool ParallelSlot::isInside(BicycleCar *c)
439 tmpn = new RRTNode(c->lfx(), c->lfy(), 0);
440 if (!this->slot().collide(tmpn))
443 tmpn = new RRTNode(c->lrx(), c->lry(), 0);
444 if (!this->slot().collide(tmpn))
447 tmpn = new RRTNode(c->rrx(), c->rry(), 0);
448 if (!this->slot().collide(tmpn))
451 tmpn = new RRTNode(c->rfx(), c->rfy(), 0);
452 if (!this->slot().collide(tmpn))
458 struct SamplingInfo ParallelSlot::getSamplingInfo()
460 struct SamplingInfo si;
461 BicycleCar *CC = new BicycleCar(
462 this->fposecenter()->x(),
463 this->fposecenter()->y() - 0.01,
466 si.x = this->slot().bnodes()[0]->x();
467 si.y = this->slot().bnodes()[0]->y();
468 si.r = CC->diag_radi();
469 si.h = this->slotHeading() - acos(EDIST( // TODO generalize
470 this->slot().bnodes()[0],
471 this->slot().bnodes()[1]