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