]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blob - decision_control/slotplanner.cc
Add perpendicular slot planner proposal
[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         return this->slotSide_;
57 }
58
59 SlotType ParallelSlot::slotType()
60 {
61         return this->slotType_;
62 }
63
64 // setter
65 void ParallelSlot::DH(float dh)
66 {
67         this->DH_ = dh;
68 }
69
70 void ParallelSlot::setAll()
71 {
72         // slot side
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;
81         else
82                 this->slotSide_ = RIGHT;
83         // slot type
84         float d1 = EDIST(
85                 this->slot().bnodes()[0],
86                 this->slot().bnodes()[1]
87         );
88         float d2 = EDIST(
89                 this->slot().bnodes()[1],
90                 this->slot().bnodes()[2]
91         );
92         if (d1 > d2)
93                 this->slotType_ = PERPENDICULAR;
94         else
95                 this->slotType_ = PARALLEL;
96 }
97
98 // other
99 void ParallelSlot::fip(
100         std::vector<CircleObstacle>& co,
101         std::vector<SegmentObstacle>& so
102 )
103 {
104         if (this->slotType() == PERPENDICULAR) {
105                 // TODO different slot headings
106                 // this is jus for slot heading = pi / 2
107                 this->DH(0.01 / BCAR_TURNING_RADIUS);
108                 BicycleCar *perc = nullptr;
109                 RRTNode *cc = nullptr;
110                 BicycleCar *p = nullptr;
111                 int i = 0;
112                 float x;
113                 float h;
114                 float y;
115                 // parking backward
116                 x = this->slot().bnodes()[3]->x();
117                 y = this->slot().bnodes()[3]->y();
118                 h = 0;
119                 // -> top
120                 x -= BCAR_DIST_FRONT;
121                 y = this->slot().bnodes()[3]->y();
122                 y += BCAR_OUT_RRADI - BCAR_TURNING_RADIUS;
123                 if (perc)
124                         delete perc;
125                 perc = new BicycleCar(x, y, h);
126                 if (cc)
127                         delete cc;
128                 cc = perc->ccl();
129                 if (p)
130                         delete p;
131                 p = perc->move(cc, i * this->DH());
132                 while (p->x() < 0) {
133                         delete p;
134                         p = perc->move(cc, i * this->DH());
135                         i++;
136                 }
137                 // -> bottom
138                 // (reset for parking backward)
139                 x = this->slot().bnodes()[0]->x();
140                 y = this->slot().bnodes()[0]->y();
141                 h = 0;
142                 // -> bottom
143                 x -= BCAR_DIST_FRONT;
144                 // get y from quadratic equation
145                 float tmpD = pow(-2 * this->slot().bnodes()[0]->y(), 2);
146                 tmpD -= 4 * (
147                         pow(x - this->slot().bnodes()[0]->x(), 2) +
148                         pow(this->slot().bnodes()[0]->y(), 2) -
149                         pow(BCAR_IN_RADI, 2)
150                 );
151                 y = 2 * this->slot().bnodes()[0]->y();
152                 y += sqrt(tmpD);
153                 y /= 2;
154                 y -= BCAR_TURNING_RADIUS;
155                 // -- end of quadratic equation
156                 if (perc)
157                         delete perc;
158                 perc = new BicycleCar(x, y, h);
159                 if (cc)
160                         delete cc;
161                 cc = perc->ccl();
162                 if (p)
163                         delete p;
164                 p = perc->move(cc, i * this->DH());
165                 while (p->x() < 0) {
166                         delete p;
167                         p = perc->move(cc, i * this->DH());
168                         i++;
169                 }
170                 // store nodes
171                 std::vector<RRTNode *> cusp;
172                 cusp.push_back(new RRTNode(p->x(), p->y(), p->h()));
173                 cusp.push_back(new RRTNode(x, y, h));
174                 this->cusp().push_back(cusp);
175                 return;
176         }
177         // see https://courses.cs.washington.edu/courses/cse326/03su/homework/hw3/bfs.html
178         // RRTNode.s() works as iteration level
179         std::queue<BicycleCar *, std::list<BicycleCar *>> q;
180         std::queue<BicycleCar *, std::list<BicycleCar *>> empty;
181         int di = -1;
182         if (this->slotSide() == LEFT)
183                 di = 1;
184         BicycleCar *CC = this->getEPC();
185         BicycleCar *B = this->getEP();
186         this->DH(di * 0.01 / CC->out_radi());
187         BicycleCar *c;
188         int i = 0;
189         c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
190         while (!this->slot().collide(c->frame())) {
191                 bool end = false;
192                 std::vector<RRTEdge *> eds = c->frame();
193                 for (auto o: co)
194                         if (o.collide(eds))
195                                 end = true;
196                 for (auto o: so)
197                         if (o.collide(eds))
198                                 end = true;
199                 for (auto e: eds)
200                         delete e;
201                 if (!end)
202                         q.push(c);
203                 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
204                 i += 1;
205         }
206         delete c; // not in q and collide
207         while (!q.empty()) {
208                 c = q.front();
209                 q.pop();
210                 if (this->isInside(c)) {
211                         goto createcuspandfinish;
212                 } else if (c->s() < 9) {
213                         BicycleCar *cc = this->flnc(c, co, so);
214                         cc->s(c->s() + 1);
215                         cc->bcparent(c);
216                         q.push(cc);
217                 } else {
218                         delete c; // not in q and collide
219                 }
220         }
221         std::swap(q, empty);
222         return;
223 createcuspandfinish:
224         std::vector<RRTNode *> cusp;
225         while (c) {
226                 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
227                 c = c->bcparent();
228         }
229         std::reverse(cusp.begin(), cusp.end());
230         this->cusp().push_back(cusp);
231         std::swap(q, empty);
232 }
233
234 void ParallelSlot::fipr(RRTNode *n)
235 {
236         return this->fipr(new BicycleCar(n->x(), n->y(), n->h()));
237 }
238
239 void ParallelSlot::fipr(BicycleCar *B)
240 {
241         std::vector<RRTNode *> cusp;
242         cusp.push_back(new RRTNode(B->x(), B->y(), B->h()));
243         int di = 1;
244         if (this->slotSide() == LEFT)
245                 di = -1;
246         if (this->slotType() == PERPENDICULAR) {
247                 this->DH(di * 0.01 / B->out_radi()); // TODO car in slot h()
248                 RRTNode *cc;
249                 if (this->slotSide() == LEFT)
250                         cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
251                 else
252                         cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
253                 BicycleCar *p;
254                 int i = 1;
255                 p = B->move(cc, i * this->DH());
256                 while (
257                         !this->slot().collide(p->frame())
258                         && this->slot().collide(p)
259                 ) {
260                         delete p;
261                         i += 10;
262                         p = B->move(cc, i * this->DH());
263                 }
264                 i -= 10;
265                 p = B->move(cc, i * this->DH());
266                 while (
267                         !this->slot().collide(p->frame())
268                         && this->slot().collide(p)
269                 ) {
270                         delete p;
271                         i += 1;
272                         p = B->move(cc, i * this->DH());
273                 }
274                 i -= 1;
275                 p = B->move(cc, i * this->DH());
276                 cusp.push_back(new RRTNode(p->x(), p->y(), p->h()));
277                 std::reverse(cusp.begin(), cusp.end());
278                 this->cusp().push_back(cusp);
279                 return;
280         }
281         this->DH(di * 0.01 / B->out_radi());
282         BicycleCar *c;
283         c = this->flncr(B);
284         c->s(B->s() + 1);
285         while ((
286                 this->slotSide() == LEFT
287                 && this->slot().collide(new RRTNode(c->lfx(), c->lfy(), 0))
288         ) || (
289                 this->slotSide() == RIGHT
290                 && this->slot().collide(new RRTNode(c->rfx(), c->rfy(), 0))
291         )) {
292                 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
293                 BicycleCar *cc = this->flncr(c);
294                 cc->s(c->s() + 1);
295                 delete c;
296                 c = cc;
297         }
298         cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
299         std::reverse(cusp.begin(), cusp.end());
300         this->cusp().push_back(cusp);
301 }
302
303 BicycleCar *ParallelSlot::flnc(
304         BicycleCar *B,
305         std::vector<CircleObstacle>& co,
306         std::vector<SegmentObstacle>& so
307 )
308 {
309         RRTNode *cc;
310         if (this->slotSide() == LEFT) {
311                 if (int(B->s()) % 2 == 0)
312                         cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
313                 else
314                         cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
315         } else {
316                 if (int(B->s()) % 2 == 0)
317                         cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
318                 else
319                         cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
320         }
321         BicycleCar *p;
322         int i = 1;
323         p = B->move(cc, i * this->DH());
324         while (
325                 !this->slot().collide(p->frame())
326                 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
327         ) {
328                 delete p;
329                 i += 10;
330                 p = B->move(cc, i * this->DH());
331                 bool end = false;
332                 std::vector<RRTEdge *> eds = p->frame();
333                 for (auto o: co)
334                         if (o.collide(eds))
335                                 end = true;
336                 for (auto o: so)
337                         if (o.collide(eds))
338                                 end = true;
339                 for (auto e: eds)
340                         delete e;
341                 if (end)
342                         break;
343         }
344         i -= 10;
345         p = B->move(cc, i * this->DH());
346         while (
347                 !this->slot().collide(p->frame())
348                 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
349         ) {
350                 if (this->isInside(p)) {
351                         i += 1;
352                         break;
353                 }
354                 delete p;
355                 i += 1;
356                 p = B->move(cc, i * this->DH());
357                 bool end = false;
358                 std::vector<RRTEdge *> eds = p->frame();
359                 for (auto o: co)
360                         if (o.collide(eds))
361                                 end = true;
362                 for (auto o: so)
363                         if (o.collide(eds))
364                                 end = true;
365                 for (auto e: eds)
366                         delete e;
367                 if (end)
368                         break;
369         }
370         delete p;
371         return B->move(cc, (i - 1) * this->DH());
372 }
373
374 BicycleCar *ParallelSlot::flncr(BicycleCar *B)
375 {
376         RRTNode *cc;
377         if (this->slotSide() == LEFT) {
378                 if (int(B->s()) % 2 == 0)
379                         cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
380                 else
381                         cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
382         } else {
383                 if (int(B->s()) % 2 == 0)
384                         cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
385                 else
386                         cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
387         }
388         BicycleCar *p;
389         int i = 1;
390         p = B->move(cc, i * this->DH());
391         while (
392                 !this->slot().collide(p->frame())
393                 && ((
394                         this->slotSide() == LEFT
395                         && this->slot().collide(new RRTNode(
396                                 p->lfx(),
397                                 p->lfy(),
398                                 0
399                         ))
400                 ) || (
401                         this->slotSide() == RIGHT
402                         && this->slot().collide(new RRTNode(
403                                 p->rfx(),
404                                 p->rfy(),
405                                 0
406                         ))
407                 ))
408         ) {
409                 delete p;
410                 i += 10;
411                 p = B->move(cc, i * this->DH());
412         }
413         i -= 10;
414         p = B->move(cc, i * this->DH());
415         while (!this->slot().collide(p->frame())) {
416                 if(
417                         this->slotSide() == LEFT
418                         && !this->slot().collide(new RRTNode(
419                                 p->lfx(),
420                                 p->lfy(),
421                                 0
422                         ))
423                 ) {
424                         i += 1;
425                         break;
426                 }
427                 if(
428                         this->slotSide() == RIGHT
429                         && !this->slot().collide(new RRTNode(
430                                 p->rfx(),
431                                 p->rfy(),
432                                 0
433                         ))
434                 ) {
435                         i += 1;
436                         break;
437                 }
438                 i += 1;
439                 p = B->move(cc, i * this->DH());
440         }
441         delete p;
442         return B->move(cc, (i - 1) * this->DH());
443 }
444
445 RRTNode *ParallelSlot::fposecenter()
446 {
447         return this->slot().bnodes().front();
448 }
449
450 bool ParallelSlot::flast(
451         RRTNode *P,
452         bool right,
453         int il,
454         std::vector<RRTNode *> &cusp
455 )
456 {
457         BicycleCar *B = new BicycleCar(P->x(), P->y(), P->h());
458         RRTNode *cc;
459         if (right)
460                 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
461         else
462                 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
463         BicycleCar *p;
464         int i = 1;
465         p = B->move(cc, i * this->DH());
466         while (!this->slot().collide(p->frame())
467                         && (
468                                 (this->DH() > 0 && p->x() <= 0)
469                                 || (this->DH() < 0 && p->x() >= 0)
470                         )) {
471                 delete p;
472                 i += 10;
473                 p = B->move(cc, i * this->DH());
474         }
475         i -= 10;
476         p = B->move(cc, i * this->DH());
477         while (!this->slot().collide(p->frame())
478                         && (
479                                 (this->DH() > 0 && p->x() <= 0)
480                                 || (this->DH() < 0 && p->x() >= 0)
481                         )) {
482                 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
483                         i += 1;
484                         break;
485                 }
486                 if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
487                         i += 1;
488                         break;
489                 }
490                 delete p;
491                 i += 1;
492                 p = B->move(cc, i * this->DH());
493         }
494         delete p;
495         p = B->move(cc, (i - 1) * this->DH());
496         if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
497                 cusp.push_back(p);
498                 return true;
499         } else if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
500                 cusp.push_back(p);
501                 return true;
502         } else if (il < 8) {
503                 cusp.push_back(p);
504                 return this->flast(p, !right, il + 1, cusp);
505         }
506         return false;
507 }
508
509 void ParallelSlot::fpose()
510 {
511         bool left = false; // right parking slot
512         float di = -1;
513         BicycleCar *CC = new BicycleCar(
514                 this->fposecenter()->x(),
515                 this->fposecenter()->y() - 0.01,
516                 this->slotHeading()
517         );
518         BicycleCar *B = new BicycleCar(
519                 CC->x() - CC->width() / 2,
520                 CC->y() - (CC->length() + CC->wheelbase()) / 2,
521                 this->slotHeading()
522         );
523         if (this->slot().bnodes()[0]->x() > this->slot().bnodes()[1]->x()) {
524                 left = true;
525                 di = 1;
526                 delete B;
527                 B = new BicycleCar(
528                         CC->x() + CC->width() / 2,
529                         CC->y() - (CC->length() + CC->wheelbase()) / 2,
530                         this->slotHeading()
531                 );
532         }
533         this->DH(di * 0.01 / CC->out_radi());
534         BicycleCar *p;
535         int i = 0;
536         p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
537         while (!this->slot().collide(p->frame())) {
538                 std::vector<RRTNode *> tmpcusp;
539                 tmpcusp.push_back(new BicycleCar(p->x(), p->y(), p->h()));
540                 if (this->flast(p, left, 0, tmpcusp)) {
541                         this->cusp().push_back(tmpcusp);
542                         return;
543                 }
544                 i += 1;
545                 delete p;
546                 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
547         }
548 }
549
550 BicycleCar *ParallelSlot::getEP()
551 {
552         // new pose for parallel parking to right slot
553         float tnx;
554         float tny;
555         float nx;
556         float ny;
557         BicycleCar *CC = this->getEPC();
558         // move left by car width / 2
559         tnx = CC->x() + CC->width() / 2 * cos(CC->h() + M_PI / 2);
560         tny = CC->y() + CC->width() / 2 * sin(CC->h() + M_PI / 2);
561         if (this->slotSide() == LEFT) {
562                 // move right by car width / 2
563                 tnx = CC->x() + CC->width() / 2 * cos(CC->h() - M_PI / 2);
564                 tny = CC->y() + CC->width() / 2 * sin(CC->h() - M_PI / 2);
565         }
566         if (this->slotType() == PARALLEL) {
567                 // move down
568                 nx = tnx - (CC->length() + CC->wheelbase()) / 2 * cos(CC->h());
569                 ny = tny - (CC->length() + CC->wheelbase()) / 2 * sin(CC->h());
570         } else {
571                 // move down
572                 nx = tnx + (CC->length() - CC->wheelbase()) / 2 * cos(CC->h());
573                 ny = tny + (CC->length() - CC->wheelbase()) / 2 * sin(CC->h());
574         }
575         return new BicycleCar(nx, ny, CC->h());
576 }
577
578 BicycleCar *ParallelSlot::getEPC()
579 {
580         // new pose for parallel parking to right slot
581         float ta;
582         float nx;
583         float ny;
584         ta = this->slotHeading() + M_PI;
585         if (this->slotSide() == RIGHT)
586                 ta -= M_PI / 4;
587         else
588                 ta += M_PI / 4;
589         nx = this->fposecenter()->x() + 0.01 * cos(ta);
590         ny = this->fposecenter()->y() + 0.01 * sin(ta);
591         return new BicycleCar(nx, ny, this->slotHeading());
592 }
593
594 BicycleCar *ParallelSlot::getFP()
595 {
596         float x = this->slot().bnodes()[3]->x();
597         float y = this->slot().bnodes()[3]->y();
598         float h = this->slotHeading();
599         float nx;
600         float ny;
601         if (this->slotType() == PARALLEL) {
602                 if (this->slotSide() == LEFT) {
603                         nx = x + BCAR_WIDTH / 2 * cos(h + M_PI / 2);
604                         ny = y + BCAR_WIDTH / 2 * sin(h + M_PI / 2);
605                 } else {
606                         nx = x + BCAR_WIDTH / 2 * cos(h - M_PI / 2);
607                         ny = y + BCAR_WIDTH / 2 * sin(h - M_PI / 2);
608                 }
609                 x = nx + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * cos(h);
610                 y = ny + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * sin(h);
611         } else {
612                 if (this->slotSide() == LEFT) {
613                         h -= M_PI / 2;
614                         nx = x + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
615                                 * cos(h + M_PI);
616                         ny = y + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
617                                 * sin(h + M_PI);
618                         x = nx + (BCAR_DIAG_RRADI) * cos(h + M_PI / 2);
619                         y = ny + (BCAR_DIAG_RRADI) * sin(h + M_PI / 2);
620                 } else {
621                         h += M_PI / 2;
622                         nx = x + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
623                                 * cos(h - M_PI);
624                         ny = y + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
625                                 * sin(h - M_PI);
626                         x = nx + (BCAR_DIAG_RRADI) * cos(h - M_PI / 2);
627                         y = ny + (BCAR_DIAG_RRADI) * sin(h - M_PI / 2);
628                 }
629         }
630         return new BicycleCar(x, y, h);
631 }
632
633 bool ParallelSlot::isInside(BicycleCar *c)
634 {
635         bool inside = true;
636         RRTNode *tmpn;
637         tmpn = new RRTNode(c->lfx(), c->lfy(), 0);
638         if (!this->slot().collide(tmpn))
639                 inside = false;
640         delete tmpn;
641         tmpn = new RRTNode(c->lrx(), c->lry(), 0);
642         if (!this->slot().collide(tmpn))
643                 inside = false;
644         delete tmpn;
645         tmpn = new RRTNode(c->rrx(), c->rry(), 0);
646         if (!this->slot().collide(tmpn))
647                 inside = false;
648         delete tmpn;
649         tmpn = new RRTNode(c->rfx(), c->rfy(), 0);
650         if (!this->slot().collide(tmpn))
651                 inside = false;
652         delete tmpn;
653         return inside;
654 }
655
656 struct SamplingInfo ParallelSlot::getSamplingInfo()
657 {
658         struct SamplingInfo si;
659         if (this->slotType() == PARALLEL) {
660                 si.x = this->slot().bnodes().front()->x();
661                 si.y = this->slot().bnodes().front()->y();
662                 si.mr = BCAR_WIDTH / 2;
663                 si.mmh = 0;
664         } else {
665                 si.x = this->slot().bnodes().back()->x();
666                 si.x -= this->slot().bnodes().front()->x();
667                 si.x /= 2;
668                 si.x += this->slot().bnodes().front()->x();
669                 si.y = this->slot().bnodes().back()->y();
670                 si.y -= this->slot().bnodes().front()->y();
671                 si.y /= 2;
672                 si.y += this->slot().bnodes().front()->y();
673                 si.mr = 0;
674                 si.mmh = (M_PI - M_PI / 6) / 2;
675         }
676         si.r = BCAR_LENGTH;
677         si.h = M_PI;
678         si.mh = M_PI / 2;
679         si.sh = this->slotHeading();
680         if (this->slotSide() == RIGHT)
681                 si.dh = 1;
682         else
683                 si.dh = -1;
684         return si;
685 }