]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blob - decision_control/slotplanner.cc
Use first slot node as rotate center
[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 // setter
44 void ParallelSlot::DH(float dh)
45 {
46         this->DH_ = dh;
47 }
48
49 // other
50 void ParallelSlot::fip()
51 {
52         // see https://courses.cs.washington.edu/courses/cse326/03su/homework/hw3/bfs.html
53         // RRTNode.s() works as iteration level
54         std::queue<BicycleCar *, std::list<BicycleCar *>> q;
55
56         // TODO add init nodes
57         // for now just copy fpose()
58         bool left = false; // right parking slot
59         float di = -1;
60         BicycleCar *CC = new BicycleCar(
61                 this->fposecenter()->x(),
62                 this->fposecenter()->y() - 0.01,
63                 M_PI / 2
64         );
65         BicycleCar *B = new BicycleCar(
66                 CC->x() - CC->width() / 2,
67                 CC->y() - (CC->length() + CC->wheelbase()) / 2,
68                 M_PI / 2
69         );
70         if (this->slot().bnodes()[0]->x() > this->slot().bnodes()[1]->x()) {
71                 left = true;
72                 di = 1;
73                 delete B;
74                 B = new BicycleCar(
75                         CC->x() + CC->width() / 2,
76                         CC->y() - (CC->length() + CC->wheelbase()) / 2,
77                         M_PI / 2
78                 );
79         }
80         this->DH(di * 0.01 / CC->out_radi());
81         BicycleCar *c;
82         int i = 0;
83         c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
84         while (!this->slot().collide(c->frame())) {
85                 q.push(c);
86                 c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
87                 i += 1;
88         }
89         delete c; // not in q and collide
90         // BFS
91         while (!q.empty()) {
92                 c = q.front();
93                 q.pop();
94                 if (this->DH() > 0 && c->rfx() <= 0 && c->rrx() <= 0) {
95                         goto createcuspandfinish;
96                 } else if (this->DH() < 0 && c->lfx() >= 0 && c->lrx() >= 0) {
97                         goto createcuspandfinish;
98                 } else if (c->s() < 9) {
99                         BicycleCar *cc = this->flnc(c);
100                         cc->s(c->s() + 1);
101                         cc->bcparent(c);
102                         q.push(cc);
103                 } else {
104                         delete c; // not in q and collide
105                 }
106         }
107 createcuspandfinish:
108         std::vector<RRTNode *> cusp;
109         while (c) {
110                 cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
111                 c = c->bcparent();
112         }
113         std::reverse(cusp.begin(), cusp.end());
114         this->cusp().push_back(cusp);
115         std::queue<BicycleCar *, std::list<BicycleCar *>> empty;
116         std::swap(q, empty);
117 }
118
119 BicycleCar *ParallelSlot::flnc(BicycleCar *B)
120 {
121         // TODO find last not colliding
122         // for now just copy flast()
123         RRTNode *cc;
124         if (int(B->s()) % 2 == 0)
125                 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
126         else
127                 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
128         BicycleCar *p;
129         int i = 1;
130         p = B->move(cc, i * this->DH());
131         while (!this->slot().collide(p->frame())
132                         && (
133                                 (this->DH() > 0 && p->x() <= 0)
134                                 || (this->DH() < 0 && p->x() >= 0)
135                         )) {
136                 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
137                         i += 1;
138                         break;
139                 }
140                 if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
141                         i += 1;
142                         break;
143                 }
144                 i += 1;
145                 delete p;
146                 p = B->move(cc, i * this->DH());
147         }
148         delete p;
149         return B->move(cc, (i - 1) * this->DH());
150 }
151
152 RRTNode *ParallelSlot::fposecenter()
153 {
154         return this->slot().bnodes().front();
155 }
156
157 bool ParallelSlot::flast(
158         RRTNode *P,
159         bool right,
160         int il,
161         std::vector<RRTNode *> &cusp
162 )
163 {
164         BicycleCar *B = new BicycleCar(P->x(), P->y(), P->h());
165         RRTNode *cc;
166         if (right)
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                 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
179                         i += 1;
180                         break;
181                 }
182                 if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
183                         i += 1;
184                         break;
185                 }
186                 i += 1;
187                 delete p;
188                 p = B->move(cc, i * this->DH());
189         }
190         delete p;
191         p = B->move(cc, (i - 1) * this->DH());
192         if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
193                 cusp.push_back(p);
194                 return true;
195         } else if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
196                 cusp.push_back(p);
197                 return true;
198         } else if (il < 8) {
199                 cusp.push_back(p);
200                 return this->flast(p, !right, il + 1, cusp);
201         }
202         return false;
203 }
204
205 void ParallelSlot::fpose()
206 {
207         bool left = false; // right parking slot
208         float di = -1;
209         BicycleCar *CC = new BicycleCar(
210                 this->fposecenter()->x(),
211                 this->fposecenter()->y() - 0.01,
212                 M_PI / 2
213         );
214         BicycleCar *B = new BicycleCar(
215                 CC->x() - CC->width() / 2,
216                 CC->y() - (CC->length() + CC->wheelbase()) / 2,
217                 M_PI / 2
218         );
219         if (this->slot().bnodes()[0]->x() > this->slot().bnodes()[1]->x()) {
220                 left = true;
221                 di = 1;
222                 delete B;
223                 B = new BicycleCar(
224                         CC->x() + CC->width() / 2,
225                         CC->y() - (CC->length() + CC->wheelbase()) / 2,
226                         M_PI / 2
227                 );
228         }
229         this->DH(di * 0.01 / CC->out_radi());
230         BicycleCar *p;
231         int i = 0;
232         p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
233         while (!this->slot().collide(p->frame())) {
234                 std::vector<RRTNode *> tmpcusp;
235                 tmpcusp.push_back(new BicycleCar(p->x(), p->y(), p->h()));
236                 if (this->flast(p, left, 0, tmpcusp)) {
237                         this->cusp().push_back(tmpcusp);
238                         return;
239                 }
240                 i += 1;
241                 delete p;
242                 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
243         }
244 }