]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blob - decision_control/slotplanner.cc
Generalize sampling info
[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 {
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;
104         std::queue<BicycleCar *, std::list<BicycleCar *>> empty;
105         int di = -1;
106         if (this->slotSide() == LEFT)
107                 di = 1;
108         BicycleCar *CC = this->getEPC();
109         BicycleCar *B = this->getEP();
110         this->DH(di * 0.01 / CC->out_radi());
111         BicycleCar *c;
112         int i = 0;
113         c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
114         while (!this->slot().collide(c->frame())) {
115                 q.push(c);
116                 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
117                 i += 1;
118         }
119         delete c; // not in q and collide
120         while (!q.empty()) {
121                 c = q.front();
122                 q.pop();
123                 if (this->isInside(c)) {
124                         goto createcuspandfinish;
125                 } else if (c->s() < 9) {
126                         BicycleCar *cc = this->flnc(c);
127                         cc->s(c->s() + 1);
128                         cc->bcparent(c);
129                         q.push(cc);
130                 } else {
131                         delete c; // not in q and collide
132                 }
133         }
134         std::swap(q, empty);
135         return;
136 createcuspandfinish:
137         std::vector<RRTNode *> cusp;
138         while (c) {
139                 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
140                 c = c->bcparent();
141         }
142         std::reverse(cusp.begin(), cusp.end());
143         this->cusp().push_back(cusp);
144         std::swap(q, empty);
145 }
146
147 void ParallelSlot::fipr(BicycleCar *B)
148 {
149         std::vector<RRTNode *> cusp;
150         cusp.push_back(new RRTNode(B->x(), B->y(), B->h()));
151         int di = 1;
152         if (this->slotSide() == LEFT)
153                 di = -1;
154         this->DH(di * 0.01 / B->out_radi());
155         BicycleCar *c;
156         c = this->flncr(B);
157         c->s(B->s() + 1);
158         while ((
159                 this->slotSide() == LEFT
160                 && this->slot().collide(new RRTNode(c->lfx(), c->lfy(), 0))
161         ) || (
162                 this->slotSide() == RIGHT
163                 && this->slot().collide(new RRTNode(c->rfx(), c->rfy(), 0))
164         )) {
165                 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
166                 BicycleCar *cc = this->flncr(c);
167                 cc->s(c->s() + 1);
168                 delete c;
169                 c = cc;
170         }
171         cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
172         std::reverse(cusp.begin(), cusp.end());
173         this->cusp().push_back(cusp);
174 }
175
176 BicycleCar *ParallelSlot::flnc(BicycleCar *B)
177 {
178         RRTNode *cc;
179         if (this->slotSide() == LEFT) {
180                 if (int(B->s()) % 2 == 0)
181                         cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
182                 else
183                         cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
184         } else {
185                 if (int(B->s()) % 2 == 0)
186                         cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
187                 else
188                         cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
189         }
190         BicycleCar *p;
191         int i = 1;
192         p = B->move(cc, i * this->DH());
193         while (
194                 !this->slot().collide(p->frame())
195                 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
196         ) {
197                 delete p;
198                 i += 10;
199                 p = B->move(cc, i * this->DH());
200         }
201         i -= 10;
202         p = B->move(cc, i * this->DH());
203         while (
204                 !this->slot().collide(p->frame())
205                 && std::abs(this->slotHeading() - p->h()) < M_PI / 2
206         ) {
207                 if (this->isInside(p)) {
208                         i += 1;
209                         break;
210                 }
211                 delete p;
212                 i += 1;
213                 p = B->move(cc, i * this->DH());
214         }
215         delete p;
216         return B->move(cc, (i - 1) * this->DH());
217 }
218
219 BicycleCar *ParallelSlot::flncr(BicycleCar *B)
220 {
221         RRTNode *cc;
222         if (this->slotSide() == LEFT) {
223                 if (int(B->s()) % 2 == 0)
224                         cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
225                 else
226                         cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
227         } else {
228                 if (int(B->s()) % 2 == 0)
229                         cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
230                 else
231                         cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
232         }
233         BicycleCar *p;
234         int i = 1;
235         p = B->move(cc, i * this->DH());
236         while (
237                 !this->slot().collide(p->frame())
238                 && ((
239                         this->slotSide() == LEFT
240                         && this->slot().collide(new RRTNode(
241                                 p->lfx(),
242                                 p->lfy(),
243                                 0
244                         ))
245                 ) || (
246                         this->slotSide() == RIGHT
247                         && this->slot().collide(new RRTNode(
248                                 p->rfx(),
249                                 p->rfy(),
250                                 0
251                         ))
252                 ))
253         ) {
254                 delete p;
255                 i += 10;
256                 p = B->move(cc, i * this->DH());
257         }
258         i -= 10;
259         p = B->move(cc, i * this->DH());
260         while (!this->slot().collide(p->frame())) {
261                 if(
262                         this->slotSide() == LEFT
263                         && !this->slot().collide(new RRTNode(
264                                 p->lfx(),
265                                 p->lfy(),
266                                 0
267                         ))
268                 ) {
269                         i += 1;
270                         break;
271                 }
272                 if(
273                         this->slotSide() == RIGHT
274                         && !this->slot().collide(new RRTNode(
275                                 p->rfx(),
276                                 p->rfy(),
277                                 0
278                         ))
279                 ) {
280                         i += 1;
281                         break;
282                 }
283                 i += 1;
284                 p = B->move(cc, i * this->DH());
285         }
286         delete p;
287         return B->move(cc, (i - 1) * this->DH());
288 }
289
290 RRTNode *ParallelSlot::fposecenter()
291 {
292         return this->slot().bnodes().front();
293 }
294
295 bool ParallelSlot::flast(
296         RRTNode *P,
297         bool right,
298         int il,
299         std::vector<RRTNode *> &cusp
300 )
301 {
302         BicycleCar *B = new BicycleCar(P->x(), P->y(), P->h());
303         RRTNode *cc;
304         if (right)
305                 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
306         else
307                 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
308         BicycleCar *p;
309         int i = 1;
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                 delete p;
317                 i += 10;
318                 p = B->move(cc, i * this->DH());
319         }
320         i -= 10;
321         p = B->move(cc, i * this->DH());
322         while (!this->slot().collide(p->frame())
323                         && (
324                                 (this->DH() > 0 && p->x() <= 0)
325                                 || (this->DH() < 0 && p->x() >= 0)
326                         )) {
327                 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
328                         i += 1;
329                         break;
330                 }
331                 if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
332                         i += 1;
333                         break;
334                 }
335                 delete p;
336                 i += 1;
337                 p = B->move(cc, i * this->DH());
338         }
339         delete p;
340         p = B->move(cc, (i - 1) * this->DH());
341         if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
342                 cusp.push_back(p);
343                 return true;
344         } else if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
345                 cusp.push_back(p);
346                 return true;
347         } else if (il < 8) {
348                 cusp.push_back(p);
349                 return this->flast(p, !right, il + 1, cusp);
350         }
351         return false;
352 }
353
354 void ParallelSlot::fpose()
355 {
356         bool left = false; // right parking slot
357         float di = -1;
358         BicycleCar *CC = new BicycleCar(
359                 this->fposecenter()->x(),
360                 this->fposecenter()->y() - 0.01,
361                 this->slotHeading()
362         );
363         BicycleCar *B = new BicycleCar(
364                 CC->x() - CC->width() / 2,
365                 CC->y() - (CC->length() + CC->wheelbase()) / 2,
366                 this->slotHeading()
367         );
368         if (this->slot().bnodes()[0]->x() > this->slot().bnodes()[1]->x()) {
369                 left = true;
370                 di = 1;
371                 delete B;
372                 B = new BicycleCar(
373                         CC->x() + CC->width() / 2,
374                         CC->y() - (CC->length() + CC->wheelbase()) / 2,
375                         this->slotHeading()
376                 );
377         }
378         this->DH(di * 0.01 / CC->out_radi());
379         BicycleCar *p;
380         int i = 0;
381         p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
382         while (!this->slot().collide(p->frame())) {
383                 std::vector<RRTNode *> tmpcusp;
384                 tmpcusp.push_back(new BicycleCar(p->x(), p->y(), p->h()));
385                 if (this->flast(p, left, 0, tmpcusp)) {
386                         this->cusp().push_back(tmpcusp);
387                         return;
388                 }
389                 i += 1;
390                 delete p;
391                 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
392         }
393 }
394
395 BicycleCar *ParallelSlot::getEP()
396 {
397         // new pose for parallel parking to right slot
398         float tnx;
399         float tny;
400         float nx;
401         float ny;
402         BicycleCar *CC = this->getEPC();
403         // move left by car width / 2
404         tnx = CC->x() + CC->width() / 2 * cos(CC->h() + M_PI / 2);
405         tny = CC->y() + CC->width() / 2 * sin(CC->h() + M_PI / 2);
406         if (this->slotSide() == LEFT) {
407                 // move right by car width / 2
408                 tnx = CC->x() + CC->width() / 2 * cos(CC->h() - M_PI / 2);
409                 tny = CC->y() + CC->width() / 2 * sin(CC->h() - M_PI / 2);
410         }
411         // move down
412         nx = tnx - (CC->length() + CC->wheelbase()) / 2 * cos(CC->h());
413         ny = tny - (CC->length() + CC->wheelbase()) / 2 * sin(CC->h());
414         return new BicycleCar(nx, ny, CC->h());
415 }
416
417 BicycleCar *ParallelSlot::getEPC()
418 {
419         // new pose for parallel parking to right slot
420         float ta;
421         float nx;
422         float ny;
423         ta = this->slotHeading() + M_PI;
424         if (this->slotSide() == RIGHT)
425                 ta -= M_PI / 4;
426         else
427                 ta += M_PI / 4;
428         nx = this->fposecenter()->x() + 0.01 * cos(ta);
429         ny = this->fposecenter()->y() + 0.01 * sin(ta);
430         return new BicycleCar(nx, ny, this->slotHeading());
431 }
432
433 BicycleCar *ParallelSlot::getFP()
434 {
435         float x = this->slot().bnodes()[3]->x();
436         float y = this->slot().bnodes()[3]->y();
437         float h = this->slotHeading();
438         float nx;
439         float ny;
440         if (this->slotSide() == LEFT) {
441                 nx = x + BCAR_WIDTH / 2 * cos(h + M_PI / 2);
442                 ny = y + BCAR_WIDTH / 2 * sin(h + M_PI / 2);
443         } else {
444                 nx = x + BCAR_WIDTH / 2 * cos(h - M_PI / 2);
445                 ny = y + BCAR_WIDTH / 2 * sin(h - M_PI / 2);
446         }
447         x = nx + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * cos(h);
448         y = ny + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * sin(h);
449         return new BicycleCar(x, y, h);
450 }
451
452 bool ParallelSlot::isInside(BicycleCar *c)
453 {
454         bool inside = true;
455         RRTNode *tmpn;
456         tmpn = new RRTNode(c->lfx(), c->lfy(), 0);
457         if (!this->slot().collide(tmpn))
458                 inside = false;
459         delete tmpn;
460         tmpn = new RRTNode(c->lrx(), c->lry(), 0);
461         if (!this->slot().collide(tmpn))
462                 inside = false;
463         delete tmpn;
464         tmpn = new RRTNode(c->rrx(), c->rry(), 0);
465         if (!this->slot().collide(tmpn))
466                 inside = false;
467         delete tmpn;
468         tmpn = new RRTNode(c->rfx(), c->rfy(), 0);
469         if (!this->slot().collide(tmpn))
470                 inside = false;
471         delete tmpn;
472         return inside;
473 }
474
475 struct SamplingInfo ParallelSlot::getSamplingInfo()
476 {
477         struct SamplingInfo si;
478         BicycleCar *CC = this->getEPC();
479         si.x = this->slot().bnodes()[0]->x();
480         si.y = this->slot().bnodes()[0]->y();
481         if (this->slotSide() == RIGHT) {
482                 si.dx = 1;
483                 si.dy = 1;
484                 si.dh = 1;
485         } else {
486                 si.dx = -1;
487                 si.dy = -1;
488                 si.dh = -1;
489         }
490         si.r = CC->diag_radi();
491         si.sh = this->slotHeading();
492         if (this->slotType() == PARALLEL) {
493                 si.h = this->slotHeading() - acos(EDIST(
494                         this->slot().bnodes()[0],
495                         this->slot().bnodes()[1]
496                 ) / BCAR_LENGTH);
497         } else {
498                 si.h = M_PI /2;
499         }
500         return si;
501 }