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 float h1 = atan2(y0 - y3, x0 - x3);
64 float h2 = atan2(y1 - y3, x1 - x3);
66 this->slotSide_ = LEFT;
68 this->slotSide_ = RIGHT;
70 return this->slotSide_;
73 SlotType ParallelSlot::slotType()
75 if (!this->slotType_) {
77 this->slot().bnodes()[0],
78 this->slot().bnodes()[1]
81 this->slot().bnodes()[1],
82 this->slot().bnodes()[2]
85 this->slotType_ = PERPENDICULAR;
87 this->slotType_ = PARALLEL;
89 return this->slotType_;
93 void ParallelSlot::DH(float dh)
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;
105 // TODO add init nodes
106 // for now just copy fpose()
107 bool left = false; // right parking slot
109 BicycleCar *CC = new BicycleCar(
110 this->fposecenter()->x(),
111 this->fposecenter()->y() - 0.01,
114 BicycleCar *B = new BicycleCar(
115 CC->x() - CC->width() / 2,
116 CC->y() - (CC->length() + CC->wheelbase()) / 2,
119 if (this->slot().bnodes()[0]->x() > this->slot().bnodes()[1]->x()) {
124 CC->x() + CC->width() / 2,
125 CC->y() - (CC->length() + CC->wheelbase()) / 2,
129 this->DH(di * 0.01 / CC->out_radi());
132 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
133 while (!this->slot().collide(c->frame())) {
135 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
138 delete c; // not in q and collide
143 if (this->DH() > 0 && c->rfx() <= 0 && c->rrx() <= 0) {
144 goto createcuspandfinish;
145 } else if (this->DH() < 0 && c->lfx() >= 0 && c->lrx() >= 0) {
146 goto createcuspandfinish;
147 } else if (c->s() < 9) {
148 BicycleCar *cc = this->flnc(c);
153 delete c; // not in q and collide
157 std::vector<RRTNode *> cusp;
159 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
162 std::reverse(cusp.begin(), cusp.end());
163 this->cusp().push_back(cusp);
164 std::queue<BicycleCar *, std::list<BicycleCar *>> empty;
168 void ParallelSlot::fipr(BicycleCar *B)
170 // TODO for right parallel parking also
171 // it's only for lpar scenario now
173 std::vector<RRTNode *> cusp;
174 cusp.push_back(new RRTNode(B->x(), B->y(), B->h()));
175 // just copied from fip()
176 this->DH(-0.01 / B->out_radi());
179 while (c->lfx() < 0) {
180 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
181 BicycleCar *cc = this->flncr(c);
186 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
187 std::reverse(cusp.begin(), cusp.end());
188 this->cusp().push_back(cusp);
191 BicycleCar *ParallelSlot::flnc(BicycleCar *B)
193 // TODO find last not colliding
194 // for now just copy flast()
196 if (int(B->s()) % 2 == 0)
197 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
199 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
202 p = B->move(cc, i * this->DH());
203 while (!this->slot().collide(p->frame())
205 (this->DH() > 0 && p->x() <= 0)
206 || (this->DH() < 0 && p->x() >= 0)
210 p = B->move(cc, i * this->DH());
213 p = B->move(cc, i * this->DH());
214 while (!this->slot().collide(p->frame())
216 (this->DH() > 0 && p->x() <= 0)
217 || (this->DH() < 0 && p->x() >= 0)
219 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
223 if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
229 p = B->move(cc, i * this->DH());
232 return B->move(cc, (i - 1) * this->DH());
235 BicycleCar *ParallelSlot::flncr(BicycleCar *B)
237 // TODO find last not colliding
238 // for now just copy flast()
240 if (int(B->s()) % 2 == 0)
241 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
243 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
246 p = B->move(cc, i * this->DH());
247 while (!this->slot().collide(p->frame())
249 (this->DH() > 0 && p->x() >= 0)
250 || (this->DH() < 0 && p->lfx() <= 0)
254 p = B->move(cc, i * this->DH());
257 p = B->move(cc, i * this->DH());
258 while (!this->slot().collide(p->frame())) {
259 if (this->DH() > 0 && p->x() <= 0) {
263 if (this->DH() < 0 && p->lfx() >= 0) {
269 p = B->move(cc, i * this->DH());
272 return B->move(cc, (i - 1) * this->DH());
275 RRTNode *ParallelSlot::fposecenter()
277 return this->slot().bnodes().front();
280 bool ParallelSlot::flast(
284 std::vector<RRTNode *> &cusp
287 BicycleCar *B = new BicycleCar(P->x(), P->y(), P->h());
290 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
292 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
295 p = B->move(cc, i * this->DH());
296 while (!this->slot().collide(p->frame())
298 (this->DH() > 0 && p->x() <= 0)
299 || (this->DH() < 0 && p->x() >= 0)
303 p = B->move(cc, i * this->DH());
306 p = B->move(cc, i * this->DH());
307 while (!this->slot().collide(p->frame())
309 (this->DH() > 0 && p->x() <= 0)
310 || (this->DH() < 0 && p->x() >= 0)
312 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
316 if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
322 p = B->move(cc, i * this->DH());
325 p = B->move(cc, (i - 1) * this->DH());
326 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
329 } else if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
334 return this->flast(p, !right, il + 1, cusp);
339 void ParallelSlot::fpose()
341 bool left = false; // right parking slot
343 BicycleCar *CC = new BicycleCar(
344 this->fposecenter()->x(),
345 this->fposecenter()->y() - 0.01,
348 BicycleCar *B = new BicycleCar(
349 CC->x() - CC->width() / 2,
350 CC->y() - (CC->length() + CC->wheelbase()) / 2,
353 if (this->slot().bnodes()[0]->x() > this->slot().bnodes()[1]->x()) {
358 CC->x() + CC->width() / 2,
359 CC->y() - (CC->length() + CC->wheelbase()) / 2,
363 this->DH(di * 0.01 / CC->out_radi());
366 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
367 while (!this->slot().collide(p->frame())) {
368 std::vector<RRTNode *> tmpcusp;
369 tmpcusp.push_back(new BicycleCar(p->x(), p->y(), p->h()));
370 if (this->flast(p, left, 0, tmpcusp)) {
371 this->cusp().push_back(tmpcusp);
376 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
380 struct SamplingInfo ParallelSlot::getSamplingInfo()
382 struct SamplingInfo si;
383 BicycleCar *CC = new BicycleCar(
384 this->fposecenter()->x(),
385 this->fposecenter()->y() - 0.01,
388 si.x = this->slot().bnodes()[0]->x();
389 si.y = this->slot().bnodes()[0]->y();
390 si.r = CC->diag_radi();
391 si.h = this->slotHeading() - acos(EDIST( // TODO generalize
392 this->slot().bnodes()[0],
393 this->slot().bnodes()[1]