]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blob - decision_control/slotplanner.cc
Find entry point by reverse approach
[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 SlotType ParallelSlot::slotType()
44 {
45         if (!this->slotType_) {
46                 float d1 = EDIST(
47                         this->slot().bnodes()[0],
48                         this->slot().bnodes()[1]
49                 );
50                 float d2 = EDIST(
51                         this->slot().bnodes()[1],
52                         this->slot().bnodes()[2]
53                 );
54                 if (d1 > d2)
55                         this->slotType_ = PERPENDICULAR;
56                 else
57                         this->slotType_ = PARALLEL;
58         }
59         return this->slotType_;
60 }
61
62 // setter
63 void ParallelSlot::DH(float dh)
64 {
65         this->DH_ = dh;
66 }
67
68 // other
69 void ParallelSlot::fip()
70 {
71         // see https://courses.cs.washington.edu/courses/cse326/03su/homework/hw3/bfs.html
72         // RRTNode.s() works as iteration level
73         std::queue<BicycleCar *, std::list<BicycleCar *>> q;
74
75         // TODO add init nodes
76         // for now just copy fpose()
77         bool left = false; // right parking slot
78         float di = -1;
79         BicycleCar *CC = new BicycleCar(
80                 this->fposecenter()->x(),
81                 this->fposecenter()->y() - 0.01,
82                 M_PI / 2
83         );
84         BicycleCar *B = new BicycleCar(
85                 CC->x() - CC->width() / 2,
86                 CC->y() - (CC->length() + CC->wheelbase()) / 2,
87                 M_PI / 2
88         );
89         if (this->slot().bnodes()[0]->x() > this->slot().bnodes()[1]->x()) {
90                 left = true;
91                 di = 1;
92                 delete B;
93                 B = new BicycleCar(
94                         CC->x() + CC->width() / 2,
95                         CC->y() - (CC->length() + CC->wheelbase()) / 2,
96                         M_PI / 2
97                 );
98         }
99         this->DH(di * 0.01 / CC->out_radi());
100         BicycleCar *c;
101         int i = 0;
102         c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
103         while (!this->slot().collide(c->frame())) {
104                 q.push(c);
105                 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
106                 i += 1;
107         }
108         delete c; // not in q and collide
109         // BFS
110         while (!q.empty()) {
111                 c = q.front();
112                 q.pop();
113                 if (this->DH() > 0 && c->rfx() <= 0 && c->rrx() <= 0) {
114                         goto createcuspandfinish;
115                 } else if (this->DH() < 0 && c->lfx() >= 0 && c->lrx() >= 0) {
116                         goto createcuspandfinish;
117                 } else if (c->s() < 9) {
118                         BicycleCar *cc = this->flnc(c);
119                         cc->s(c->s() + 1);
120                         cc->bcparent(c);
121                         q.push(cc);
122                 } else {
123                         delete c; // not in q and collide
124                 }
125         }
126 createcuspandfinish:
127         std::vector<RRTNode *> cusp;
128         while (c) {
129                 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
130                 c = c->bcparent();
131         }
132         std::reverse(cusp.begin(), cusp.end());
133         this->cusp().push_back(cusp);
134         std::queue<BicycleCar *, std::list<BicycleCar *>> empty;
135         std::swap(q, empty);
136 }
137
138 void ParallelSlot::fipr(BicycleCar *B)
139 {
140         // TODO for right parallel parking also
141         // it's only for lpar scenario now
142
143         std::vector<RRTNode *> cusp;
144         cusp.push_back(new RRTNode(B->x(), B->y(), B->h()));
145         // just copied from fip()
146         this->DH(-0.01 / B->out_radi());
147         BicycleCar *c;
148         c = this->flncr(B);
149         while (c->lfx() < 0) {
150                 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
151                 BicycleCar *cc = this->flncr(c);
152                 cc->s(c->s() + 1);
153                 delete c;
154                 c = cc;
155         }
156         cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
157         std::reverse(cusp.begin(), cusp.end());
158         this->cusp().push_back(cusp);
159 }
160
161 BicycleCar *ParallelSlot::flnc(BicycleCar *B)
162 {
163         // TODO find last not colliding
164         // for now just copy flast()
165         RRTNode *cc;
166         if (int(B->s()) % 2 == 0)
167                 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
168         else
169                 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
170         BicycleCar *p;
171         int i = 1;
172         p = B->move(cc, i * this->DH());
173         while (!this->slot().collide(p->frame())
174                         && (
175                                 (this->DH() > 0 && p->x() <= 0)
176                                 || (this->DH() < 0 && p->x() >= 0)
177                         )) {
178                 delete p;
179                 i += 10;
180                 p = B->move(cc, i * this->DH());
181         }
182         i -= 10;
183         p = B->move(cc, i * this->DH());
184         while (!this->slot().collide(p->frame())
185                         && (
186                                 (this->DH() > 0 && p->x() <= 0)
187                                 || (this->DH() < 0 && p->x() >= 0)
188                         )) {
189                 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
190                         i += 1;
191                         break;
192                 }
193                 if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
194                         i += 1;
195                         break;
196                 }
197                 delete p;
198                 i += 1;
199                 p = B->move(cc, i * this->DH());
200         }
201         delete p;
202         return B->move(cc, (i - 1) * this->DH());
203 }
204
205 BicycleCar *ParallelSlot::flncr(BicycleCar *B)
206 {
207         // TODO find last not colliding
208         // for now just copy flast()
209         RRTNode *cc;
210         if (int(B->s()) % 2 == 0)
211                 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
212         else
213                 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
214         BicycleCar *p;
215         int i = 1;
216         p = B->move(cc, i * this->DH());
217         while (!this->slot().collide(p->frame())
218                         && (
219                                 (this->DH() > 0 && p->x() >= 0)
220                                 || (this->DH() < 0 && p->lfx() <= 0)
221                         )) {
222                 delete p;
223                 i += 10;
224                 p = B->move(cc, i * this->DH());
225         }
226         i -= 10;
227         p = B->move(cc, i * this->DH());
228         while (!this->slot().collide(p->frame())) {
229                 if (this->DH() > 0 && p->x() <= 0) {
230                         i += 1;
231                         break;
232                 }
233                 if (this->DH() < 0 && p->lfx() >= 0) {
234                         i += 1;
235                         break;
236                 }
237                 delete p;
238                 i += 1;
239                 p = B->move(cc, i * this->DH());
240         }
241         delete p;
242         return B->move(cc, (i - 1) * this->DH());
243 }
244
245 RRTNode *ParallelSlot::fposecenter()
246 {
247         return this->slot().bnodes().front();
248 }
249
250 bool ParallelSlot::flast(
251         RRTNode *P,
252         bool right,
253         int il,
254         std::vector<RRTNode *> &cusp
255 )
256 {
257         BicycleCar *B = new BicycleCar(P->x(), P->y(), P->h());
258         RRTNode *cc;
259         if (right)
260                 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
261         else
262                 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
263         BicycleCar *p;
264         int i = 1;
265         p = B->move(cc, i * this->DH());
266         while (!this->slot().collide(p->frame())
267                         && (
268                                 (this->DH() > 0 && p->x() <= 0)
269                                 || (this->DH() < 0 && p->x() >= 0)
270                         )) {
271                 delete p;
272                 i += 10;
273                 p = B->move(cc, i * this->DH());
274         }
275         i -= 10;
276         p = B->move(cc, i * this->DH());
277         while (!this->slot().collide(p->frame())
278                         && (
279                                 (this->DH() > 0 && p->x() <= 0)
280                                 || (this->DH() < 0 && p->x() >= 0)
281                         )) {
282                 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
283                         i += 1;
284                         break;
285                 }
286                 if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
287                         i += 1;
288                         break;
289                 }
290                 delete p;
291                 i += 1;
292                 p = B->move(cc, i * this->DH());
293         }
294         delete p;
295         p = B->move(cc, (i - 1) * this->DH());
296         if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
297                 cusp.push_back(p);
298                 return true;
299         } else if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
300                 cusp.push_back(p);
301                 return true;
302         } else if (il < 8) {
303                 cusp.push_back(p);
304                 return this->flast(p, !right, il + 1, cusp);
305         }
306         return false;
307 }
308
309 void ParallelSlot::fpose()
310 {
311         bool left = false; // right parking slot
312         float di = -1;
313         BicycleCar *CC = new BicycleCar(
314                 this->fposecenter()->x(),
315                 this->fposecenter()->y() - 0.01,
316                 M_PI / 2
317         );
318         BicycleCar *B = new BicycleCar(
319                 CC->x() - CC->width() / 2,
320                 CC->y() - (CC->length() + CC->wheelbase()) / 2,
321                 M_PI / 2
322         );
323         if (this->slot().bnodes()[0]->x() > this->slot().bnodes()[1]->x()) {
324                 left = true;
325                 di = 1;
326                 delete B;
327                 B = new BicycleCar(
328                         CC->x() + CC->width() / 2,
329                         CC->y() - (CC->length() + CC->wheelbase()) / 2,
330                         M_PI / 2
331                 );
332         }
333         this->DH(di * 0.01 / CC->out_radi());
334         BicycleCar *p;
335         int i = 0;
336         p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
337         while (!this->slot().collide(p->frame())) {
338                 std::vector<RRTNode *> tmpcusp;
339                 tmpcusp.push_back(new BicycleCar(p->x(), p->y(), p->h()));
340                 if (this->flast(p, left, 0, tmpcusp)) {
341                         this->cusp().push_back(tmpcusp);
342                         return;
343                 }
344                 i += 1;
345                 delete p;
346                 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
347         }
348 }
349
350 struct SamplingInfo ParallelSlot::getSamplingInfo()
351 {
352         struct SamplingInfo si;
353         BicycleCar *CC = new BicycleCar(
354                 this->fposecenter()->x(),
355                 this->fposecenter()->y() - 0.01,
356                 M_PI / 2
357         );
358         si.x = this->slot().bnodes()[0]->x();
359         si.y = this->slot().bnodes()[0]->y();
360         si.r = CC->diag_radi();
361         si.h = M_PI / 2 - acos(EDIST( // TODO generalize
362                 this->slot().bnodes()[0],
363                 this->slot().bnodes()[1]
364         ) / BCAR_LENGTH);
365         return si;
366 }