]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blob - decision_control/slotplanner.cc
Find last pose 10x faster, slow down when collide
[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 BicycleCar *ParallelSlot::flnc(BicycleCar *B)
139 {
140         // TODO find last not colliding
141         // for now just copy flast()
142         RRTNode *cc;
143         if (int(B->s()) % 2 == 0)
144                 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
145         else
146                 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
147         BicycleCar *p;
148         int i = 1;
149         p = B->move(cc, i * this->DH());
150         while (!this->slot().collide(p->frame())
151                         && (
152                                 (this->DH() > 0 && p->x() <= 0)
153                                 || (this->DH() < 0 && p->x() >= 0)
154                         )) {
155                 delete p;
156                 i += 10;
157                 p = B->move(cc, i * this->DH());
158         }
159         i -= 10;
160         p = B->move(cc, i * this->DH());
161         while (!this->slot().collide(p->frame())
162                         && (
163                                 (this->DH() > 0 && p->x() <= 0)
164                                 || (this->DH() < 0 && p->x() >= 0)
165                         )) {
166                 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
167                         i += 1;
168                         break;
169                 }
170                 if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
171                         i += 1;
172                         break;
173                 }
174                 delete p;
175                 i += 1;
176                 p = B->move(cc, i * this->DH());
177         }
178         delete p;
179         return B->move(cc, (i - 1) * this->DH());
180 }
181
182 RRTNode *ParallelSlot::fposecenter()
183 {
184         return this->slot().bnodes().front();
185 }
186
187 bool ParallelSlot::flast(
188         RRTNode *P,
189         bool right,
190         int il,
191         std::vector<RRTNode *> &cusp
192 )
193 {
194         BicycleCar *B = new BicycleCar(P->x(), P->y(), P->h());
195         RRTNode *cc;
196         if (right)
197                 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
198         else
199                 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
200         BicycleCar *p;
201         int i = 1;
202         p = B->move(cc, i * this->DH());
203         while (!this->slot().collide(p->frame())
204                         && (
205                                 (this->DH() > 0 && p->x() <= 0)
206                                 || (this->DH() < 0 && p->x() >= 0)
207                         )) {
208                 delete p;
209                 i += 10;
210                 p = B->move(cc, i * this->DH());
211         }
212         i -= 10;
213         p = B->move(cc, i * this->DH());
214         while (!this->slot().collide(p->frame())
215                         && (
216                                 (this->DH() > 0 && p->x() <= 0)
217                                 || (this->DH() < 0 && p->x() >= 0)
218                         )) {
219                 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
220                         i += 1;
221                         break;
222                 }
223                 if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
224                         i += 1;
225                         break;
226                 }
227                 delete p;
228                 i += 1;
229                 p = B->move(cc, i * this->DH());
230         }
231         delete p;
232         p = B->move(cc, (i - 1) * this->DH());
233         if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
234                 cusp.push_back(p);
235                 return true;
236         } else if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
237                 cusp.push_back(p);
238                 return true;
239         } else if (il < 8) {
240                 cusp.push_back(p);
241                 return this->flast(p, !right, il + 1, cusp);
242         }
243         return false;
244 }
245
246 void ParallelSlot::fpose()
247 {
248         bool left = false; // right parking slot
249         float di = -1;
250         BicycleCar *CC = new BicycleCar(
251                 this->fposecenter()->x(),
252                 this->fposecenter()->y() - 0.01,
253                 M_PI / 2
254         );
255         BicycleCar *B = new BicycleCar(
256                 CC->x() - CC->width() / 2,
257                 CC->y() - (CC->length() + CC->wheelbase()) / 2,
258                 M_PI / 2
259         );
260         if (this->slot().bnodes()[0]->x() > this->slot().bnodes()[1]->x()) {
261                 left = true;
262                 di = 1;
263                 delete B;
264                 B = new BicycleCar(
265                         CC->x() + CC->width() / 2,
266                         CC->y() - (CC->length() + CC->wheelbase()) / 2,
267                         M_PI / 2
268                 );
269         }
270         this->DH(di * 0.01 / CC->out_radi());
271         BicycleCar *p;
272         int i = 0;
273         p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
274         while (!this->slot().collide(p->frame())) {
275                 std::vector<RRTNode *> tmpcusp;
276                 tmpcusp.push_back(new BicycleCar(p->x(), p->y(), p->h()));
277                 if (this->flast(p, left, 0, tmpcusp)) {
278                         this->cusp().push_back(tmpcusp);
279                         return;
280                 }
281                 i += 1;
282                 delete p;
283                 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
284         }
285 }
286
287 struct SamplingInfo ParallelSlot::getSamplingInfo()
288 {
289         struct SamplingInfo si;
290         BicycleCar *CC = new BicycleCar(
291                 this->fposecenter()->x(),
292                 this->fposecenter()->y() - 0.01,
293                 M_PI / 2
294         );
295         si.x = this->slot().bnodes()[0]->x();
296         si.y = this->slot().bnodes()[0]->y();
297         si.r = CC->diag_radi();
298         si.h = M_PI / 2 - acos(EDIST( // TODO generalize
299                 this->slot().bnodes()[0],
300                 this->slot().bnodes()[1]
301         ) / BCAR_LENGTH);
302         return si;
303 }