]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blob - decision_control/slotplanner.cc
Fix fipr iteration level
[hubacji1/iamcar.git] / decision_control / slotplanner.cc
1 /*
2 This file is part of I am car.
3
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.
8
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.
13
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/>.
16 */
17
18 #include <algorithm>
19 #include <cmath>
20 #include <list>
21 #include <queue>
22 #include "slotplanner.h"
23
24 ParallelSlot::ParallelSlot()
25 {}
26
27 // getter
28 std::vector<std::vector<RRTNode *>> &ParallelSlot::cusp()
29 {
30         return this->cusp_;
31 }
32
33 float ParallelSlot::DH() const
34 {
35         return this->DH_;
36 }
37
38 PolygonObstacle &ParallelSlot::slot()
39 {
40         return this->slot_;
41 }
42
43 float ParallelSlot::slotHeading()
44 {
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();
49         float dy = y0 - y3;
50         float dx = x0 - x3;
51         return atan2(dy, dx);
52 }
53
54 SlotSide ParallelSlot::slotSide()
55 {
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;
65                 else
66                         this->slotSide_ = RIGHT;
67         }
68         return this->slotSide_;
69 }
70
71 SlotType ParallelSlot::slotType()
72 {
73         if (!this->slotType_) {
74                 float d1 = EDIST(
75                         this->slot().bnodes()[0],
76                         this->slot().bnodes()[1]
77                 );
78                 float d2 = EDIST(
79                         this->slot().bnodes()[1],
80                         this->slot().bnodes()[2]
81                 );
82                 if (d1 > d2)
83                         this->slotType_ = PERPENDICULAR;
84                 else
85                         this->slotType_ = PARALLEL;
86         }
87         return this->slotType_;
88 }
89
90 // setter
91 void ParallelSlot::DH(float dh)
92 {
93         this->DH_ = dh;
94 }
95
96 // other
97 void ParallelSlot::fip()
98 {
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;
103
104         // TODO add init nodes
105         // for now just copy fpose()
106         bool left = false; // right parking slot
107         float di = -1;
108         // new pose for parallel parking to right slot
109         float tnx;
110         float tny;
111         float nx;
112         float ny;
113         // temporary tnx is angle
114         tnx = this->slotHeading() + M_PI;
115         if (this->slotSide() == RIGHT)
116                 tnx -= M_PI / 4;
117         else
118                 tnx += M_PI / 4;
119         nx = this->fposecenter()->x() + 0.01 * cos(tnx);
120         ny = this->fposecenter()->y() + 0.01 * sin(tnx);
121         BicycleCar *CC = new BicycleCar(nx, ny, this->slotHeading());
122         // move left by car width / 2
123         tnx = CC->x() + CC->width() / 2 * cos(CC->h() + M_PI / 2);
124         tny = CC->y() + CC->width() / 2 * sin(CC->h() + M_PI / 2);
125         // move down
126         nx = tnx - (CC->length() + CC->wheelbase()) / 2 * cos(CC->h());
127         ny = tny - (CC->length() + CC->wheelbase()) / 2 * sin(CC->h());
128         if (this->slotSide() == LEFT) {
129                 std::cerr << "left PS" << std::endl;
130                 left = true;
131                 di = 1;
132                 // move right by car width / 2
133                 tnx = CC->x() + CC->width() / 2 * cos(CC->h() - M_PI / 2);
134                 tny = CC->y() + CC->width() / 2 * sin(CC->h() - M_PI / 2);
135                 // move down
136                 nx = tnx - (CC->length() + CC->wheelbase()) / 2 * cos(CC->h());
137                 ny = tny - (CC->length() + CC->wheelbase()) / 2 * sin(CC->h());
138         }
139         BicycleCar *B = new BicycleCar(nx, ny, CC->h());
140         this->DH(di * 0.01 / CC->out_radi());
141         BicycleCar *c;
142         int i = 0;
143         c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
144         while (!this->slot().collide(c->frame())) {
145                 q.push(c);
146                 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
147                 i += 1;
148         }
149         delete c; // not in q and collide
150         // BFS
151         while (!q.empty()) {
152                 c = q.front();
153                 q.pop();
154                 if (this->isInside(c)) {
155                         goto createcuspandfinish;
156                 } else if (c->s() < 9) {
157                         BicycleCar *cc = this->flnc(c);
158                         cc->s(c->s() + 1);
159                         cc->bcparent(c);
160                         q.push(cc);
161                 } else {
162                         delete c; // not in q and collide
163                 }
164         }
165         std::swap(q, empty);
166         return;
167 createcuspandfinish:
168         std::vector<RRTNode *> cusp;
169         while (c) {
170                 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
171                 c = c->bcparent();
172         }
173         std::reverse(cusp.begin(), cusp.end());
174         this->cusp().push_back(cusp);
175         std::swap(q, empty);
176 }
177
178 void ParallelSlot::fipr(BicycleCar *B)
179 {
180         // TODO for right parallel parking also
181         // it's only for lpar scenario now
182         std::vector<RRTNode *> cusp;
183         cusp.push_back(new RRTNode(B->x(), B->y(), B->h()));
184         // just copied from fip()
185         int di = 1;
186         if (this->slotSide() == LEFT)
187                 di = -1;
188         this->DH(di * 0.01 / B->out_radi());
189         BicycleCar *c;
190         c = this->flncr(B);
191         c->s(B->s() + 1);
192         while (this->slot().collide(new RRTNode(c->lfx(), c->lfy(), 0))) {
193                 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
194                 BicycleCar *cc = this->flncr(c);
195                 cc->s(c->s() + 1);
196                 delete c;
197                 c = cc;
198         }
199         cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
200         std::reverse(cusp.begin(), cusp.end());
201         this->cusp().push_back(cusp);
202 }
203
204 BicycleCar *ParallelSlot::flnc(BicycleCar *B)
205 {
206         // TODO find last not colliding
207         // for now just copy flast()
208         RRTNode *cc;
209         if (int(B->s()) % 2 == 0) {
210                 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
211         } else {
212                 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
213         }
214         float di = -1;
215         if (this->slotSide() == LEFT)
216                 di = 1;
217         BicycleCar *p;
218         int i = 1;
219         p = B->move(cc, i * di * this->DH());
220         while (!this->slot().collide(p->frame())) {
221                 delete p;
222                 i += 10;
223                 p = B->move(cc, i * this->DH());
224         }
225         i -= 10;
226         p = B->move(cc, i * di * this->DH());
227         while (!this->slot().collide(p->frame())) {
228                 if (this->isInside(p)) {
229                         i += 1;
230                         break;
231                 }
232                 delete p;
233                 i += 1;
234                 p = B->move(cc, i * this->DH());
235         }
236         delete p;
237         return B->move(cc, (i - 1) * this->DH());
238 }
239
240 BicycleCar *ParallelSlot::flncr(BicycleCar *B)
241 {
242         // TODO find last not colliding
243         // for now just copy flast()
244         RRTNode *cc;
245         if (int(B->s()) % 2 == 0)
246                 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
247         else
248                 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
249         BicycleCar *p;
250         int i = 1;
251         p = B->move(cc, i * this->DH());
252         while (
253                 !this->slot().collide(p->frame())
254                 && this->slot().collide(new RRTNode(p->lfx(), p->lfy(), 0))
255         ) {
256                 delete p;
257                 i += 10;
258                 p = B->move(cc, i * this->DH());
259         }
260         i -= 10;
261         p = B->move(cc, i * this->DH());
262         while (!this->slot().collide(p->frame())) {
263                 if(!this->slot().collide(new RRTNode(p->lfx(), p->lfy(), 0))) {
264                         i += 1;
265                         break;
266                 }
267                 //if (this->DH() < 0 && p->lfx() >= 0) {
268                 //        i += 1;
269                 //        break;
270                 //}
271                 delete p;
272                 i += 1;
273                 p = B->move(cc, i * this->DH());
274         }
275         delete p;
276         return B->move(cc, (i - 1) * this->DH());
277 }
278
279 RRTNode *ParallelSlot::fposecenter()
280 {
281         return this->slot().bnodes().front();
282 }
283
284 bool ParallelSlot::flast(
285         RRTNode *P,
286         bool right,
287         int il,
288         std::vector<RRTNode *> &cusp
289 )
290 {
291         BicycleCar *B = new BicycleCar(P->x(), P->y(), P->h());
292         RRTNode *cc;
293         if (right)
294                 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
295         else
296                 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
297         BicycleCar *p;
298         int i = 1;
299         p = B->move(cc, i * this->DH());
300         while (!this->slot().collide(p->frame())
301                         && (
302                                 (this->DH() > 0 && p->x() <= 0)
303                                 || (this->DH() < 0 && p->x() >= 0)
304                         )) {
305                 delete p;
306                 i += 10;
307                 p = B->move(cc, i * this->DH());
308         }
309         i -= 10;
310         p = B->move(cc, i * this->DH());
311         while (!this->slot().collide(p->frame())
312                         && (
313                                 (this->DH() > 0 && p->x() <= 0)
314                                 || (this->DH() < 0 && p->x() >= 0)
315                         )) {
316                 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
317                         i += 1;
318                         break;
319                 }
320                 if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
321                         i += 1;
322                         break;
323                 }
324                 delete p;
325                 i += 1;
326                 p = B->move(cc, i * this->DH());
327         }
328         delete p;
329         p = B->move(cc, (i - 1) * this->DH());
330         if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
331                 cusp.push_back(p);
332                 return true;
333         } else if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
334                 cusp.push_back(p);
335                 return true;
336         } else if (il < 8) {
337                 cusp.push_back(p);
338                 return this->flast(p, !right, il + 1, cusp);
339         }
340         return false;
341 }
342
343 void ParallelSlot::fpose()
344 {
345         bool left = false; // right parking slot
346         float di = -1;
347         BicycleCar *CC = new BicycleCar(
348                 this->fposecenter()->x(),
349                 this->fposecenter()->y() - 0.01,
350                 this->slotHeading()
351         );
352         BicycleCar *B = new BicycleCar(
353                 CC->x() - CC->width() / 2,
354                 CC->y() - (CC->length() + CC->wheelbase()) / 2,
355                 this->slotHeading()
356         );
357         if (this->slot().bnodes()[0]->x() > this->slot().bnodes()[1]->x()) {
358                 left = true;
359                 di = 1;
360                 delete B;
361                 B = new BicycleCar(
362                         CC->x() + CC->width() / 2,
363                         CC->y() - (CC->length() + CC->wheelbase()) / 2,
364                         this->slotHeading()
365                 );
366         }
367         this->DH(di * 0.01 / CC->out_radi());
368         BicycleCar *p;
369         int i = 0;
370         p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
371         while (!this->slot().collide(p->frame())) {
372                 std::vector<RRTNode *> tmpcusp;
373                 tmpcusp.push_back(new BicycleCar(p->x(), p->y(), p->h()));
374                 if (this->flast(p, left, 0, tmpcusp)) {
375                         this->cusp().push_back(tmpcusp);
376                         return;
377                 }
378                 i += 1;
379                 delete p;
380                 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
381         }
382 }
383
384 BicycleCar *ParallelSlot::getFP()
385 {
386         float x = this->slot().bnodes()[3]->x();
387         float y = this->slot().bnodes()[3]->y();
388         float h = this->slotHeading();
389         float nx;
390         float ny;
391         if (this->slotSide() == LEFT) {
392                 nx = x + BCAR_WIDTH / 2 * cos(h + M_PI / 2);
393                 ny = y + BCAR_WIDTH / 2 * sin(h + M_PI / 2);
394         } else {
395                 nx = x + BCAR_WIDTH / 2 * cos(h - M_PI / 2);
396                 ny = y + BCAR_WIDTH / 2 * sin(h - M_PI / 2);
397         }
398         x = nx + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * cos(h);
399         y = ny + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * sin(h);
400         return new BicycleCar(x, y, h);
401 }
402
403 bool ParallelSlot::isInside(BicycleCar *c)
404 {
405         bool inside = true;
406         RRTNode *tmpn;
407         tmpn = new RRTNode(c->lfx(), c->lfy(), 0);
408         if (!this->slot().collide(tmpn))
409                 inside = false;
410         delete tmpn;
411         tmpn = new RRTNode(c->lrx(), c->lry(), 0);
412         if (!this->slot().collide(tmpn))
413                 inside = false;
414         delete tmpn;
415         tmpn = new RRTNode(c->rrx(), c->rry(), 0);
416         if (!this->slot().collide(tmpn))
417                 inside = false;
418         delete tmpn;
419         tmpn = new RRTNode(c->rfx(), c->rfy(), 0);
420         if (!this->slot().collide(tmpn))
421                 inside = false;
422         delete tmpn;
423         return inside;
424 }
425
426 struct SamplingInfo ParallelSlot::getSamplingInfo()
427 {
428         struct SamplingInfo si;
429         BicycleCar *CC = new BicycleCar(
430                 this->fposecenter()->x(),
431                 this->fposecenter()->y() - 0.01,
432                 this->slotHeading()
433         );
434         si.x = this->slot().bnodes()[0]->x();
435         si.y = this->slot().bnodes()[0]->y();
436         si.r = CC->diag_radi();
437         si.h = this->slotHeading() - acos(EDIST( // TODO generalize
438                 this->slot().bnodes()[0],
439                 this->slot().bnodes()[1]
440         ) / BCAR_LENGTH);
441         return si;
442 }