]> rtime.felk.cvut.cz Git - hubacji1/psp.git/blob - src/psp.cc
Check when discriminant < 0
[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 // find entry
116 void PSPlanner::fe()
117 {
118         if (this->ps().parallel())
119                 return this->fe_parallel();
120         else
121                 return this->fe_perpendicular();
122 }
123
124 void PSPlanner::fe_parallel()
125 {
126         // angle for distance from "entry" corner
127         double dist_angl = this->ps().heading() + M_PI;
128         dist_angl += (this->ps().right()) ? - M_PI / 4 : + M_PI / 4;
129         // set bicycle car `bci` basic dimensions and heading
130         BicycleCar bci = BicycleCar(this->gc());
131         BicycleCar bco = BicycleCar(this->gc());
132         bci.h(this->ps().heading());
133         // move 0.01 from the "entry" corner
134         bci.x(this->ps().x4() + 0.01 * cos(dist_angl));
135         bci.y(this->ps().y4() + 0.01 * sin(dist_angl));
136         // align with parking "top" of slot (move backward)
137         dist_angl = bci.h() + M_PI;
138         bci.x(bci.x() + bci.df() * cos(dist_angl));
139         bci.y(bci.y() + bci.df() * sin(dist_angl));
140         // align with "entry" to pakring slot (move outside)
141         dist_angl = this->ps().heading();
142         dist_angl += (this->ps().right()) ? + M_PI / 2 : - M_PI / 2;
143         bci.x(bci.x() + bci.w() / 2 * cos(dist_angl));
144         bci.y(bci.y() + bci.w() / 2 * sin(dist_angl));
145         // BFS - init all starts
146         // see https://courses.cs.washington.edu/courses/cse326/03su/homework/hw3/bfs.html
147         double dist_diag = sqrt(pow(bci.w() / 2, 2) + pow(bci.df(), 2));
148         if (this->ps().right())
149                 dist_angl = atan2(bci.y() - bci.rfy(), bci.x() - bci.rfx());
150         else
151                 dist_angl = atan2(bci.y() - bci.lfy(), bci.x() - bci.lfx());
152         double DIST_ANGL = dist_angl;
153         std::queue<BicycleCar, std::list<BicycleCar>> q;
154         while (
155                 (
156                         this->ps().right()
157                         && dist_angl < DIST_ANGL + 3 * M_PI / 4
158                 )
159                 || (
160                         !this->ps().right()
161                         && dist_angl > DIST_ANGL - 3 * M_PI / 4
162                 )
163         ) {
164                 this->cc() = BicycleCar(bci);
165                 if (this->ps().right()) {
166                         this->cc().x(bci.rfx() + dist_diag * cos(dist_angl));
167                         this->cc().y(bci.rfy() + dist_diag * sin(dist_angl));
168                 } else {
169                         this->cc().x(bci.lfx() + dist_diag * cos(dist_angl));
170                         this->cc().y(bci.lfy() + dist_diag * sin(dist_angl));
171                 }
172                 this->cc().h(this->ps().heading() + dist_angl - DIST_ANGL);
173                 if (!this->collide()) {
174                         this->cc().st(this->cc().wb() / this->cc().mtr());
175                         if (!this->ps().right())
176                                 this->cc().st(this->cc().st() * -1);
177                         this->cc().sp(-0.01);
178                         q.push(BicycleCar(this->cc()));
179                 }
180                 dist_angl += (this->ps().right()) ? + 0.01 : - 0.01;
181         }
182         // BFS - find entry current car `cc` and corresponding goal car `gc`
183         unsigned int iter_cntr;
184         while (!q.empty() && iter_cntr < 9) {
185                 this->cc() = BicycleCar(q.front());
186                 q.pop();
187                 while (
188                         !this->collide()
189                         && (std::abs(
190                                 this->cc().h() - this->ps().heading()
191                         ) < M_PI / 2)
192                 )
193                         this->cc().next();
194                 this->cc().sp(this->cc().sp() * -1);
195                 this->cc().next();
196                 this->gc() = BicycleCar(this->cc());
197                 if (this->parked())
198                         goto successfinish;
199                 this->cc().st(this->cc().st() * -1);
200                 q.push(BicycleCar(this->cc()));
201                 if (sgn(this->cc().st()) == sgn(q.front().st()))
202                         iter_cntr++;
203         }
204         // fallback to fer
205         this->gc() = BicycleCar(bco);
206 successfinish:
207         return this->fer_parallel();
208 }
209
210 void PSPlanner::fe_perpendicular()
211 {
212         // TODO Try multiple angles when going from parking slot.
213         //
214         //      Do not use just the maximum steer angle. Test angles
215         //      until the whole current car `cc` is out of the parking
216         //      slot `ps`.
217         //
218         //      Another approach could be testing angles from the
219         //      beginning of the escape parkig slot maneuver.
220         return fer_perpendicular();
221 }
222
223 void PSPlanner::fer()
224 {
225         if (this->ps().parallel())
226                 return this->fer_parallel();
227         else
228                 return this->fer_perpendicular();
229 }
230
231 void PSPlanner::fer_parallel()
232 {
233         this->cc().st(this->cc().wb() / this->cc().mtr());
234         if (!this->ps().right())
235                 this->cc().st(this->cc().st() * -1);
236         this->cc().sp(0.01);
237         while (!this->left()) {
238                 while (!this->collide() && !this->left())
239                         this->cc().next();
240                 if (this->left() && !this->collide()) {
241                         break;
242                 } else {
243                         this->cc().sp(this->cc().sp() * -1);
244                         this->cc().next();
245                         this->cc().st(this->cc().st() * -1);
246                 }
247         }
248 }
249
250 void PSPlanner::fer_perpendicular()
251 {
252         bool delta_use[] = {true, true, true};
253         double cc_h = this->cc().h();
254         double x;
255         double y;
256         // check inner radius
257         if (this->forward()) {
258                 x = this->ps().x1();
259                 y = this->ps().y1();
260         } else {
261                 x = this->ps().x4();
262                 y = this->ps().y4();
263         }
264         double x1;
265         double y1;
266         if (this->ps().right()) {
267                 x1 = this->cc().ccr().x();
268                 y1 = this->cc().ccr().y();
269         } else {
270                 x1 = this->cc().ccl().x();
271                 y1 = this->cc().ccl().y();
272         }
273         double IR = this->cc().iradi();
274         double a = 1;
275         double b;
276         if (this->forward())
277                 b = (x - x1) * 2 * cos(cc_h) + (y - y1) * 2 * sin(cc_h);
278         else
279                 b = (x1 - x) * 2 * cos(cc_h) + (y1 - y) * 2 * sin(cc_h);
280         double c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
281         double D = pow(b, 2) - 4 * a * c;
282         double delta;
283         delta = -b - sqrt(D);
284         delta /= 2 * a;
285         double delta_1 = delta;
286         if (D < 0)
287                 delta_use[0] = false;
288         // check outer radius
289         if (this->forward()) {
290                 x = this->ps().x4();
291                 y = this->ps().y4();
292         } else {
293                 x = this->ps().x1();
294                 y = this->ps().y1();
295         }
296         IR = this->cc().ofradi();
297         a = 1;
298         if (this->forward())
299                 b = (x - x1) * 2 * cos(cc_h) + (y - y1) * 2 * sin(cc_h);
300         else
301                 b = (x1 - x) * 2 * cos(cc_h) + (y1 - y) * 2 * sin(cc_h);
302         c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
303         D = pow(b, 2) - 4 * a * c;
304         if (this->forward()) {
305                 delta = -b + sqrt(D);
306                 delta /= 2 * a;
307         }
308         double delta_2 = delta;
309         if (D < 0)
310                 delta_use[1] = false;
311         delta = -b - sqrt(D);
312         delta /= 2 * a;
313         double delta_3 = delta;
314         if (D < 0)
315                 delta_use[2] = false;
316         if (delta_use[0] && delta_use[1] && delta_use[22])
317                 delta = std::max(delta_1, std::max(delta_2, delta_3));
318         else if (delta_use[0] && delta_use[1])
319                 delta = std::max(delta_1, delta_2);
320         else if (delta_use[0] && delta_use[2])
321                 delta = std::max(delta_1, delta_3);
322         else if (delta_use[1] && delta_use[2])
323                 delta = std::max(delta_2, delta_3);
324         else if (delta_use[0])
325                 delta = delta_1;
326         else if (delta_use[1])
327                 delta = delta_2;
328         else if (delta_use[2])
329                 delta = delta_3;
330         else
331                 return;
332         // current car `cc` can get out of slot with max steer
333         this->cc().x(this->cc().x() + delta * cos(cc_h));
334         this->cc().y(this->cc().y() + delta * sin(cc_h));
335         this->cc().h(cc_h);
336         // get current car `cc` out of slot
337         if (this->forward())
338                 this->cc().sp(-0.1);
339         else
340                 this->cc().sp(0.1);
341         this->cc().st(this->cc().wb() / this->cc().mtr());
342         if (this->ps().right())
343                 this->cc().st(this->cc().st() * -1);
344         while (!this->left()) {
345                 while (!this->collide() && !this->left())
346                         this->cc().next();
347                 if (this->left() && !this->collide()) {
348                         break;
349                 } else {
350                         this->cc().sp(this->cc().sp() * -1);
351                         this->cc().next();
352                         this->cc().st(this->cc().st() * -1);
353                 }
354         }
355 }
356
357 PSPlanner::PSPlanner()
358 {
359 }