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;
103 // TODO add init nodes
104 // for now just copy fpose()
105 bool left = false; // right parking slot
107 // new pose for parallel parking to right slot
112 // temporary tnx is angle
113 tnx = this->slotHeading() + M_PI;
114 if (this->slotSide() == RIGHT)
118 nx = this->fposecenter()->x() + 0.01 * cos(tnx);
119 ny = this->fposecenter()->y() + 0.01 * sin(tnx);
120 BicycleCar *CC = new BicycleCar(nx, ny, this->slotHeading());
121 // move left by car width / 2
122 tnx = CC->x() + CC->width() / 2 * cos(CC->h() + M_PI / 2);
123 tny = CC->y() + CC->width() / 2 * sin(CC->h() + M_PI / 2);
125 nx = tnx - (CC->length() + CC->wheelbase()) / 2 * cos(CC->h());
126 ny = tny - (CC->length() + CC->wheelbase()) / 2 * sin(CC->h());
127 if (this->slotSide() == LEFT) {
128 std::cerr << "left PS" << std::endl;
131 // move right by car width / 2
132 tnx = CC->x() + CC->width() / 2 * cos(CC->h() - M_PI / 2);
133 tny = CC->y() + CC->width() / 2 * sin(CC->h() - M_PI / 2);
135 nx = tnx - (CC->length() + CC->wheelbase()) / 2 * cos(CC->h());
136 ny = tny - (CC->length() + CC->wheelbase()) / 2 * sin(CC->h());
138 BicycleCar *B = new BicycleCar(nx, ny, CC->h());
139 this->DH(di * 0.01 / CC->out_radi());
142 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
143 while (!this->slot().collide(c->frame())) {
145 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
148 delete c; // not in q and collide
153 if (this->isInside(c)) {
154 goto createcuspandfinish;
155 } else if (c->s() < 9) {
156 BicycleCar *cc = this->flnc(c);
161 delete c; // not in q and collide
166 std::vector<RRTNode *> cusp;
168 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
171 std::reverse(cusp.begin(), cusp.end());
172 this->cusp().push_back(cusp);
173 std::queue<BicycleCar *, std::list<BicycleCar *>> empty;
177 void ParallelSlot::fipr(BicycleCar *B)
179 // TODO for right parallel parking also
180 // it's only for lpar scenario now
181 std::vector<RRTNode *> cusp;
182 cusp.push_back(new RRTNode(B->x(), B->y(), B->h()));
183 // just copied from fip()
185 if (this->slotSide() == LEFT)
187 this->DH(di * 0.01 / B->out_radi());
190 while (this->slot().collide(new RRTNode(c->lfx(), c->lfy(), 0))) {
191 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
192 BicycleCar *cc = this->flncr(c);
197 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
198 std::reverse(cusp.begin(), cusp.end());
199 this->cusp().push_back(cusp);
202 BicycleCar *ParallelSlot::flnc(BicycleCar *B)
204 // TODO find last not colliding
205 // for now just copy flast()
207 if (int(B->s()) % 2 == 0) {
208 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
210 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
213 if (this->slotSide() == LEFT)
217 p = B->move(cc, i * di * this->DH());
218 while (!this->slot().collide(p->frame())) {
221 p = B->move(cc, i * this->DH());
224 p = B->move(cc, i * di * this->DH());
225 while (!this->slot().collide(p->frame())) {
226 if (this->isInside(p)) {
232 p = B->move(cc, i * this->DH());
235 return B->move(cc, (i - 1) * this->DH());
238 BicycleCar *ParallelSlot::flncr(BicycleCar *B)
240 // TODO find last not colliding
241 // for now just copy flast()
243 if (int(B->s()) % 2 == 0)
244 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
246 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
249 p = B->move(cc, i * this->DH());
251 !this->slot().collide(p->frame())
252 && this->slot().collide(new RRTNode(p->lfx(), p->lfy(), 0))
256 p = B->move(cc, i * this->DH());
259 p = B->move(cc, i * this->DH());
260 while (!this->slot().collide(p->frame())) {
261 if(!this->slot().collide(new RRTNode(p->lfx(), p->lfy(), 0))) {
265 //if (this->DH() < 0 && p->lfx() >= 0) {
271 p = B->move(cc, i * this->DH());
274 return B->move(cc, (i - 1) * this->DH());
277 RRTNode *ParallelSlot::fposecenter()
279 return this->slot().bnodes().front();
282 bool ParallelSlot::flast(
286 std::vector<RRTNode *> &cusp
289 BicycleCar *B = new BicycleCar(P->x(), P->y(), P->h());
292 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
294 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
297 p = B->move(cc, i * this->DH());
298 while (!this->slot().collide(p->frame())
300 (this->DH() > 0 && p->x() <= 0)
301 || (this->DH() < 0 && p->x() >= 0)
305 p = B->move(cc, i * this->DH());
308 p = B->move(cc, i * this->DH());
309 while (!this->slot().collide(p->frame())
311 (this->DH() > 0 && p->x() <= 0)
312 || (this->DH() < 0 && p->x() >= 0)
314 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
318 if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
324 p = B->move(cc, i * this->DH());
327 p = B->move(cc, (i - 1) * this->DH());
328 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
331 } else if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
336 return this->flast(p, !right, il + 1, cusp);
341 void ParallelSlot::fpose()
343 bool left = false; // right parking slot
345 BicycleCar *CC = new BicycleCar(
346 this->fposecenter()->x(),
347 this->fposecenter()->y() - 0.01,
350 BicycleCar *B = new BicycleCar(
351 CC->x() - CC->width() / 2,
352 CC->y() - (CC->length() + CC->wheelbase()) / 2,
355 if (this->slot().bnodes()[0]->x() > this->slot().bnodes()[1]->x()) {
360 CC->x() + CC->width() / 2,
361 CC->y() - (CC->length() + CC->wheelbase()) / 2,
365 this->DH(di * 0.01 / CC->out_radi());
368 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
369 while (!this->slot().collide(p->frame())) {
370 std::vector<RRTNode *> tmpcusp;
371 tmpcusp.push_back(new BicycleCar(p->x(), p->y(), p->h()));
372 if (this->flast(p, left, 0, tmpcusp)) {
373 this->cusp().push_back(tmpcusp);
378 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
382 BicycleCar *ParallelSlot::getFP()
384 float x = this->slot().bnodes()[3]->x();
385 float y = this->slot().bnodes()[3]->y();
386 float h = this->slotHeading();
389 if (this->slotSide() == LEFT) {
390 nx = x + BCAR_WIDTH / 2 * cos(h + M_PI / 2);
391 ny = y + BCAR_WIDTH / 2 * sin(h + M_PI / 2);
393 nx = x + BCAR_WIDTH / 2 * cos(h - M_PI / 2);
394 ny = y + BCAR_WIDTH / 2 * sin(h - M_PI / 2);
396 x = nx + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * cos(h);
397 y = ny + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * sin(h);
398 return new BicycleCar(x, y, h);
401 bool ParallelSlot::isInside(BicycleCar *c)
405 tmpn = new RRTNode(c->lfx(), c->lfy(), 0);
406 if (!this->slot().collide(tmpn))
409 tmpn = new RRTNode(c->lrx(), c->lry(), 0);
410 if (!this->slot().collide(tmpn))
413 tmpn = new RRTNode(c->rrx(), c->rry(), 0);
414 if (!this->slot().collide(tmpn))
417 tmpn = new RRTNode(c->rfx(), c->rfy(), 0);
418 if (!this->slot().collide(tmpn))
424 struct SamplingInfo ParallelSlot::getSamplingInfo()
426 struct SamplingInfo si;
427 BicycleCar *CC = new BicycleCar(
428 this->fposecenter()->x(),
429 this->fposecenter()->y() - 0.01,
432 si.x = this->slot().bnodes()[0]->x();
433 si.y = this->slot().bnodes()[0]->y();
434 si.r = CC->diag_radi();
435 si.h = this->slotHeading() - acos(EDIST( // TODO generalize
436 this->slot().bnodes()[0],
437 this->slot().bnodes()[1]