]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blob - decision_control/slotplanner.cc
Add sampling info struct
[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                 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
156                         i += 1;
157                         break;
158                 }
159                 if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
160                         i += 1;
161                         break;
162                 }
163                 i += 1;
164                 delete p;
165                 p = B->move(cc, i * this->DH());
166         }
167         delete p;
168         return B->move(cc, (i - 1) * this->DH());
169 }
170
171 RRTNode *ParallelSlot::fposecenter()
172 {
173         return this->slot().bnodes().front();
174 }
175
176 bool ParallelSlot::flast(
177         RRTNode *P,
178         bool right,
179         int il,
180         std::vector<RRTNode *> &cusp
181 )
182 {
183         BicycleCar *B = new BicycleCar(P->x(), P->y(), P->h());
184         RRTNode *cc;
185         if (right)
186                 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
187         else
188                 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
189         BicycleCar *p;
190         int i = 1;
191         p = B->move(cc, i * this->DH());
192         while (!this->slot().collide(p->frame())
193                         && (
194                                 (this->DH() > 0 && p->x() <= 0)
195                                 || (this->DH() < 0 && p->x() >= 0)
196                         )) {
197                 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
198                         i += 1;
199                         break;
200                 }
201                 if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
202                         i += 1;
203                         break;
204                 }
205                 i += 1;
206                 delete p;
207                 p = B->move(cc, i * this->DH());
208         }
209         delete p;
210         p = B->move(cc, (i - 1) * this->DH());
211         if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
212                 cusp.push_back(p);
213                 return true;
214         } else if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
215                 cusp.push_back(p);
216                 return true;
217         } else if (il < 8) {
218                 cusp.push_back(p);
219                 return this->flast(p, !right, il + 1, cusp);
220         }
221         return false;
222 }
223
224 void ParallelSlot::fpose()
225 {
226         bool left = false; // right parking slot
227         float di = -1;
228         BicycleCar *CC = new BicycleCar(
229                 this->fposecenter()->x(),
230                 this->fposecenter()->y() - 0.01,
231                 M_PI / 2
232         );
233         BicycleCar *B = new BicycleCar(
234                 CC->x() - CC->width() / 2,
235                 CC->y() - (CC->length() + CC->wheelbase()) / 2,
236                 M_PI / 2
237         );
238         if (this->slot().bnodes()[0]->x() > this->slot().bnodes()[1]->x()) {
239                 left = true;
240                 di = 1;
241                 delete B;
242                 B = new BicycleCar(
243                         CC->x() + CC->width() / 2,
244                         CC->y() - (CC->length() + CC->wheelbase()) / 2,
245                         M_PI / 2
246                 );
247         }
248         this->DH(di * 0.01 / CC->out_radi());
249         BicycleCar *p;
250         int i = 0;
251         p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
252         while (!this->slot().collide(p->frame())) {
253                 std::vector<RRTNode *> tmpcusp;
254                 tmpcusp.push_back(new BicycleCar(p->x(), p->y(), p->h()));
255                 if (this->flast(p, left, 0, tmpcusp)) {
256                         this->cusp().push_back(tmpcusp);
257                         return;
258                 }
259                 i += 1;
260                 delete p;
261                 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
262         }
263 }
264
265 struct SamplingInfo ParallelSlot::getSamplingInfo()
266 {
267         struct SamplingInfo si;
268         BicycleCar *CC = new BicycleCar(
269                 this->fposecenter()->x(),
270                 this->fposecenter()->y() - 0.01,
271                 M_PI / 2
272         );
273         si.x = this->slot().bnodes()[0]->x();
274         si.y = this->slot().bnodes()[0]->y();
275         si.r = CC->diag_radi();
276         si.h = M_PI / 2 - acos(EDIST( // TODO generalize
277                 this->slot().bnodes()[0],
278                 this->slot().bnodes()[1]
279         ) / BCAR_LENGTH);
280         return si;
281 }