]> rtime.felk.cvut.cz Git - hubacji1/psp.git/blob - src/psp.cc
Add possible inits ut, skeleton
[hubacji1/psp.git] / src / psp.cc
1 #include <cmath>
2 #include <list>
3 #include <queue>
4 #include "psp.h"
5
6 bool PSPlanner::collide()
7 {
8         std::vector<std::tuple<double, double>> bc;
9         bc.push_back(std::make_tuple(this->cc().lfx(), this->cc().lfy()));
10         bc.push_back(std::make_tuple(this->cc().lrx(), this->cc().lry()));
11         bc.push_back(std::make_tuple(this->cc().rrx(), this->cc().rry()));
12         bc.push_back(std::make_tuple(this->cc().rfx(), this->cc().rfy()));
13         bc.push_back(std::make_tuple(this->cc().lfx(), this->cc().lfy()));
14         std::vector<std::tuple<double, double>> ps;
15         ps.push_back(std::make_tuple(this->ps().x1(), this->ps().y1()));
16         ps.push_back(std::make_tuple(this->ps().x2(), this->ps().y2()));
17         ps.push_back(std::make_tuple(this->ps().x3(), this->ps().y3()));
18         ps.push_back(std::make_tuple(this->ps().x4(), this->ps().y4()));
19         return std::get<0>(::collide(bc, ps));
20 }
21
22 bool PSPlanner::forward()
23 {
24         double heading = this->ps().heading();
25         while (heading < 0) heading += 2 * M_PI;
26         if (!this->ps().parallel())
27                 heading -= M_PI / 2;
28         double h = this->gc().h();
29         while (h < 0) h += 2 * M_PI;
30         if (-0.00001 < heading - h && heading - h < 0.00001)
31                 return true;
32         else
33                 return false;
34 }
35
36 void PSPlanner::guess_gc()
37 {
38         double x = this->ps().x1();
39         double y = this->ps().y1();
40         double h = this->ps().heading();
41         double dts = + M_PI / 2; // direction to slot
42         if (this->ps().right())
43                 dts = - M_PI / 2;
44         if (this->ps().parallel()) {
45                 x += (this->gc().w() + 0.01) * cos(h + dts);
46                 x += (this->gc().dr() + 0.01) * cos(h);
47                 y += (this->gc().w() + 0.01) * sin(h + dts);
48                 y += (this->gc().dr() + 0.01) * sin(h);
49         } else {
50                 x += (this->ps().x4() - this->ps().x1()) / 2;
51                 x += (this->gc().df() + 0.01) * cos(h + dts);
52                 y += (this->ps().y4() - this->ps().y1()) / 2;
53                 y += (this->gc().df() + 0.01) * sin(h + dts);
54                 if (this->ps().right())
55                         h += M_PI / 2;
56                 else
57                         h -= M_PI / 2;
58         }
59         while (h > M_PI)
60                 h -= 2 * M_PI;
61         while (h <= -M_PI)
62                 h += 2 * M_PI;
63         this->gc().x(x);
64         this->gc().y(y);
65         this->gc().h(h);
66 }
67
68 bool PSPlanner::left()
69 {
70         double lfx = this->cc().lfx();
71         double lfy = this->cc().lfy();
72         double lrx = this->cc().lrx();
73         double lry = this->cc().lry();
74         double rrx = this->cc().rrx();
75         double rry = this->cc().rry();
76         double rfx = this->cc().rfx();
77         double rfy = this->cc().rfy();
78         double lfs = sgn(
79                 (lfx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
80                 - (lfy - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
81         );
82         double lrs = sgn(
83                 (lrx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
84                 - (lry - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
85         );
86         double rrs = sgn(
87                 (rrx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
88                 - (rry - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
89         );
90         double rfs = sgn(
91                 (rfx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
92                 - (rfy - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
93         );
94         if (this->ps().parallel())
95                 return lfs == rfs && (lfs != lrs || lfs != rrs);
96         else if (!this->forward())
97                 return lfs == rfs && (lfs != lrs || lfs != rrs);
98         else
99                 return lrs == rrs && (lrs != lfs || lrs != rfs);
100 }
101
102 bool PSPlanner::parked()
103 {
104         std::vector<std::tuple<double, double>> slot;
105         slot.push_back(std::make_tuple(this->ps().x1(), this->ps().y1()));
106         slot.push_back(std::make_tuple(this->ps().x2(), this->ps().y2()));
107         slot.push_back(std::make_tuple(this->ps().x3(), this->ps().y3()));
108         slot.push_back(std::make_tuple(this->ps().x4(), this->ps().y4()));
109         return inside(this->gc().lfx(), this->gc().lfy(), slot)
110                 && inside(this->gc().lrx(), this->gc().lry(), slot)
111                 && inside(this->gc().rrx(), this->gc().rry(), slot)
112                 && inside(this->gc().rfx(), this->gc().rfy(), slot);
113 }
114
115 std::vector<BicycleCar> PSPlanner::possible_inits()
116 {
117         std::vector<BicycleCar> pi;
118         return pi;
119 }
120
121 // find entry
122 void PSPlanner::fe()
123 {
124         if (this->ps().parallel())
125                 return this->fe_parallel();
126         else
127                 return this->fe_perpendicular();
128 }
129
130 void PSPlanner::fe_parallel()
131 {
132         // angle for distance from "entry" corner
133         double dist_angl = this->ps().heading() + M_PI;
134         dist_angl += (this->ps().right()) ? - M_PI / 4 : + M_PI / 4;
135         // set bicycle car `bci` basic dimensions and heading
136         BicycleCar bci = BicycleCar(this->gc());
137         BicycleCar bco = BicycleCar(this->gc());
138         bci.h(this->ps().heading());
139         // move 0.01 from the "entry" corner
140         bci.x(this->ps().x4() + 0.01 * cos(dist_angl));
141         bci.y(this->ps().y4() + 0.01 * sin(dist_angl));
142         // align with parking "top" of slot (move backward)
143         dist_angl = bci.h() + M_PI;
144         bci.x(bci.x() + bci.df() * cos(dist_angl));
145         bci.y(bci.y() + bci.df() * sin(dist_angl));
146         // align with "entry" to pakring slot (move outside)
147         dist_angl = this->ps().heading();
148         dist_angl += (this->ps().right()) ? + M_PI / 2 : - M_PI / 2;
149         bci.x(bci.x() + bci.w() / 2 * cos(dist_angl));
150         bci.y(bci.y() + bci.w() / 2 * sin(dist_angl));
151         // BFS - init all starts
152         // see https://courses.cs.washington.edu/courses/cse326/03su/homework/hw3/bfs.html
153         double dist_diag = sqrt(pow(bci.w() / 2, 2) + pow(bci.df(), 2));
154         if (this->ps().right())
155                 dist_angl = atan2(bci.y() - bci.rfy(), bci.x() - bci.rfx());
156         else
157                 dist_angl = atan2(bci.y() - bci.lfy(), bci.x() - bci.lfx());
158         double DIST_ANGL = dist_angl;
159         std::queue<BicycleCar, std::list<BicycleCar>> q;
160         while (
161                 (
162                         this->ps().right()
163                         && dist_angl < DIST_ANGL + 3 * M_PI / 4
164                 )
165                 || (
166                         !this->ps().right()
167                         && dist_angl > DIST_ANGL - 3 * M_PI / 4
168                 )
169         ) {
170                 this->cc() = BicycleCar(bci);
171                 if (this->ps().right()) {
172                         this->cc().x(bci.rfx() + dist_diag * cos(dist_angl));
173                         this->cc().y(bci.rfy() + dist_diag * sin(dist_angl));
174                 } else {
175                         this->cc().x(bci.lfx() + dist_diag * cos(dist_angl));
176                         this->cc().y(bci.lfy() + dist_diag * sin(dist_angl));
177                 }
178                 this->cc().h(this->ps().heading() + dist_angl - DIST_ANGL);
179                 if (!this->collide()) {
180                         this->cc().st(this->cc().wb() / this->cc().mtr());
181                         if (!this->ps().right())
182                                 this->cc().st(this->cc().st() * -1);
183                         this->cc().sp(-0.01);
184                         q.push(BicycleCar(this->cc()));
185                 }
186                 dist_angl += (this->ps().right()) ? + 0.01 : - 0.01;
187         }
188         // BFS - find entry current car `cc` and corresponding goal car `gc`
189         unsigned int iter_cntr;
190         while (!q.empty() && iter_cntr < 9) {
191                 this->cc() = BicycleCar(q.front());
192                 q.pop();
193                 while (
194                         !this->collide()
195                         && (std::abs(
196                                 this->cc().h() - this->ps().heading()
197                         ) < M_PI / 2)
198                 )
199                         this->cc().next();
200                 this->cc().sp(this->cc().sp() * -1);
201                 this->cc().next();
202                 this->gc() = BicycleCar(this->cc());
203                 if (this->parked())
204                         goto successfinish;
205                 this->cc().st(this->cc().st() * -1);
206                 q.push(BicycleCar(this->cc()));
207                 if (sgn(this->cc().st()) == sgn(q.front().st()))
208                         iter_cntr++;
209         }
210         // fallback to fer
211         this->gc() = BicycleCar(bco);
212 successfinish:
213         return this->fer_parallel();
214 }
215
216 void PSPlanner::fe_perpendicular()
217 {
218         // TODO Try multiple angles when going from parking slot.
219         //
220         //      Do not use just the maximum steer angle. Test angles
221         //      until the whole current car `cc` is out of the parking
222         //      slot `ps`.
223         //
224         //      Another approach could be testing angles from the
225         //      beginning of the escape parkig slot maneuver.
226         return fer_perpendicular();
227 }
228
229 void PSPlanner::fer()
230 {
231         if (this->ps().parallel())
232                 return this->fer_parallel();
233         else
234                 return this->fer_perpendicular();
235 }
236
237 void PSPlanner::fer_parallel()
238 {
239         this->cc().st(this->cc().wb() / this->cc().mtr());
240         if (!this->ps().right())
241                 this->cc().st(this->cc().st() * -1);
242         this->cc().sp(0.01);
243         while (!this->left()) {
244                 while (!this->collide() && !this->left())
245                         this->cc().next();
246                 if (this->left() && !this->collide()) {
247                         break;
248                 } else {
249                         this->cc().sp(this->cc().sp() * -1);
250                         this->cc().next();
251                         this->cc().st(this->cc().st() * -1);
252                 }
253         }
254 }
255
256 void PSPlanner::fer_perpendicular()
257 {
258         bool delta_use[] = {true, true, true};
259         double cc_h = this->cc().h();
260         double x;
261         double y;
262         // check inner radius
263         if (this->forward()) {
264                 x = this->ps().x1();
265                 y = this->ps().y1();
266         } else {
267                 x = this->ps().x4();
268                 y = this->ps().y4();
269         }
270         double x1;
271         double y1;
272         if (this->ps().right()) {
273                 x1 = this->cc().ccr().x();
274                 y1 = this->cc().ccr().y();
275         } else {
276                 x1 = this->cc().ccl().x();
277                 y1 = this->cc().ccl().y();
278         }
279         double IR = this->cc().iradi();
280         double a = 1;
281         double b;
282         if (this->forward())
283                 b = (x - x1) * 2 * cos(cc_h) + (y - y1) * 2 * sin(cc_h);
284         else
285                 b = (x1 - x) * 2 * cos(cc_h) + (y1 - y) * 2 * sin(cc_h);
286         double c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
287         double D = pow(b, 2) - 4 * a * c;
288         double delta;
289         delta = -b - sqrt(D);
290         delta /= 2 * a;
291         double delta_1 = delta;
292         if (D < 0)
293                 delta_use[0] = false;
294         // check outer radius
295         if (this->forward()) {
296                 x = this->ps().x4();
297                 y = this->ps().y4();
298         } else {
299                 x = this->ps().x1();
300                 y = this->ps().y1();
301         }
302         IR = this->cc().ofradi();
303         a = 1;
304         if (this->forward())
305                 b = (x - x1) * 2 * cos(cc_h) + (y - y1) * 2 * sin(cc_h);
306         else
307                 b = (x1 - x) * 2 * cos(cc_h) + (y1 - y) * 2 * sin(cc_h);
308         c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
309         D = pow(b, 2) - 4 * a * c;
310         if (this->forward()) {
311                 delta = -b + sqrt(D);
312                 delta /= 2 * a;
313         }
314         double delta_2 = delta;
315         if (D < 0)
316                 delta_use[1] = false;
317         delta = -b - sqrt(D);
318         delta /= 2 * a;
319         double delta_3 = delta;
320         if (D < 0)
321                 delta_use[2] = false;
322         if (delta_use[0] && delta_use[1] && delta_use[22])
323                 delta = std::max(delta_1, std::max(delta_2, delta_3));
324         else if (delta_use[0] && delta_use[1])
325                 delta = std::max(delta_1, delta_2);
326         else if (delta_use[0] && delta_use[2])
327                 delta = std::max(delta_1, delta_3);
328         else if (delta_use[1] && delta_use[2])
329                 delta = std::max(delta_2, delta_3);
330         else if (delta_use[0])
331                 delta = delta_1;
332         else if (delta_use[1])
333                 delta = delta_2;
334         else if (delta_use[2])
335                 delta = delta_3;
336         else
337                 return;
338         // current car `cc` can get out of slot with max steer
339         this->cc().x(this->cc().x() + delta * cos(cc_h));
340         this->cc().y(this->cc().y() + delta * sin(cc_h));
341         this->cc().h(cc_h);
342         // get current car `cc` out of slot
343         if (this->forward())
344                 this->cc().sp(-0.1);
345         else
346                 this->cc().sp(0.1);
347         this->cc().st(this->cc().wb() / this->cc().mtr());
348         if (this->ps().right())
349                 this->cc().st(this->cc().st() * -1);
350         while (!this->left()) {
351                 while (!this->collide() && !this->left())
352                         this->cc().next();
353                 if (this->left() && !this->collide()) {
354                         break;
355                 } else {
356                         this->cc().sp(this->cc().sp() * -1);
357                         this->cc().next();
358                         this->cc().st(this->cc().st() * -1);
359                 }
360         }
361 }
362
363 PSPlanner::PSPlanner()
364 {
365 }