]> rtime.felk.cvut.cz Git - hubacji1/psp.git/blob - src/psp.cc
Add cusps array
[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         if (this->ps().parallel())
25                 return false;
26         double heading = atan2(
27                 this->ps().y2() - this->ps().y1(),
28                 this->ps().x2() - this->ps().x1()
29         );
30         while (heading < 0) heading += 2 * M_PI;
31         double h = this->gc().h();
32         while (h < 0) h += 2 * M_PI;
33         if (std::abs(heading - h) < M_PI / 4)
34                 return true;
35         return false;
36 }
37
38 void PSPlanner::gc_to_4()
39 {
40         double angl_slot = atan2(
41                 this->ps().y3() - this->ps().y4(),
42                 this->ps().x3() - this->ps().x4()
43         );
44         double angl_delta = M_PI / 2;
45         if (this->ps().right())
46                 angl_delta = -M_PI / 2;
47         double x = this->ps().x4();
48         double y = this->ps().y4();
49         x += (this->gc().dr() + 0.01) * cos(angl_slot);
50         y += (this->gc().dr() + 0.01) * sin(angl_slot);
51         x += (this->gc().w() / 2 + 0.01) * cos(angl_slot + angl_delta);
52         y += (this->gc().w() / 2 + 0.01) * sin(angl_slot + angl_delta);
53         this->gc().x(x);
54         this->gc().y(y);
55         this->gc().h(angl_slot);
56 }
57
58 std::tuple<double, double, double, double> circle_line_intersection(
59         double cx, double cy, double r,
60         double x1, double y1,
61         double x2, double y2
62 )
63 {
64         double t = (y2 - y1) / (x2 - x1);
65         //double a = 1 + pow(t, 2);
66         //double b = - 2 * cx - 2 * pow(t, 2) * x1 + 2 * t * y1 - 2 * t * cy;
67         //double c = pow(cx, 2) + pow(t, 2) * pow(x1, 2) - 2 * t * y1 * x1
68         //        + pow(y1, 2) + 2 * t * cy * x1 - 2 * y1 * cy + pow(cy, 2)
69         //        - pow(r, 2);
70         double a = 1 + pow(t, 2);
71         double b = - 2 * cx + 2 * t * (-t * x1 + y1) - 2 * cy * t;
72         double c = pow(cx, 2) + pow(cy, 2) - pow(r, 2);
73         c += pow(-t * x1 + y1, 2);
74         c += 2 * cy * t * x1 - 2 * cy * y1;
75         double D = pow(b, 2) - 4 * a * c;
76         if (D < 0)
77                 return std::make_tuple(cx, cy, cx, cy);
78         double res_x1 = (-b + sqrt(D)) / (2 * a);
79         double res_y1 = t * (res_x1 - x1) + y1;
80         double res_x2 = (-b - sqrt(D)) / (2 * a);
81         double res_y2 = t * (res_x2 - x1) + y1;
82         return std::make_tuple(res_x1, res_y1, res_x2, res_y2);
83 }
84
85 double edist(double x1, double y1, double x2, double y2)
86 {
87         return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
88 }
89
90 void PSPlanner::guess_gc()
91 {
92         double x = this->ps().x1();
93         double y = this->ps().y1();
94         double h = this->ps().heading();
95         double dts = + M_PI / 2; // direction to slot
96         if (this->ps().right())
97                 dts = - M_PI / 2;
98         if (this->ps().parallel()) {
99                 dts *= 0.99; // precision workaround
100                 x += (this->gc().w() / 2 + 0.01) * cos(h + dts);
101                 x += (this->gc().dr() + 0.01) * cos(h);
102                 y += (this->gc().w() / 2 + 0.01) * sin(h + dts);
103                 y += (this->gc().dr() + 0.01) * sin(h);
104         } else {
105                 if (std::abs(
106                         atan2(
107                                 this->ps().y2() - this->ps().y1(),
108                                 this->ps().x2() - this->ps().x1()
109                         )
110                         - this->ps().heading()
111                 ) < M_PI / 2) {
112                         // forward parking
113                         this->gc_to_4();
114                         double bx;
115                         double by;
116                         double cx;
117                         double cy;
118                         if (this->ps().right()) {
119                                 bx = this->gc().lfx();
120                                 by = this->gc().lfy();
121                                 cx = this->gc().ccr().x();
122                                 cy = this->gc().ccr().y();
123                         } else {
124                                 bx = this->gc().rfx();
125                                 by = this->gc().rfy();
126                                 cx = this->gc().ccl().x();
127                                 cy = this->gc().ccl().y();
128                         }
129                         double radi_angl = atan2(by - cy, bx - cx);
130                         radi_angl += dts;
131                         double angl_delta = this->gc().h() - radi_angl;
132                         this->gc().rotate(bx, by, angl_delta);
133                         // TODO there is a bug somewhere :/
134                         //
135                         // cli returns not exact intersection, therefore the
136                         // distance to x1, y1 of border is shorter. Then, when
137                         // moving, the distance `dist_o` is not sufficient and
138                         // car still collide with parking slot. It shouldn't be
139                         // problem until it collides with obstacle.
140                         //
141                         if (this->ps().right()) {
142                                 cx = this->gc().ccr().x();
143                                 cy = this->gc().ccr().y();
144                         } else {
145                                 cx = this->gc().ccl().x();
146                                 cy = this->gc().ccl().y();
147                         }
148                         auto cli = circle_line_intersection(
149                                 cx, cy, this->gc().iradi(),
150                                 this->ps().x1(), this->ps().y1(),
151                                 this->ps().x2(), this->ps().y2()
152                         );
153                         double d1 = edist(
154                                 this->ps().x1(), this->ps().y1(),
155                                 std::get<0>(cli), std::get<1>(cli)
156                         );
157                         double d2 = edist(
158                                 this->ps().x1(), this->ps().y1(),
159                                 std::get<2>(cli), std::get<3>(cli)
160                         );
161                         double dist_o = std::min<double>(d1, d2);
162                         double angl_o = atan2(
163                                 this->ps().y4() - this->ps().y3(),
164                                 this->ps().x4() - this->ps().x3()
165                         );
166                         // projection
167                         double angl_d = atan2(
168                                 this->ps().y1() - this->ps().y2(),
169                                 this->ps().x1() - this->ps().x2()
170                         );
171                         angl_d -= angl_o;
172                         dist_o *= cos(angl_d);
173                         this->gc().x(this->gc().x() + dist_o * cos(angl_o));
174                         this->gc().y(this->gc().y() + dist_o * sin(angl_o));
175                         // --- ENDTODO ---
176                         this->gc().sp(-0.01);
177                         this->gc().st(dts);
178                         return;
179                 } else {
180                         dts = atan2(
181                                 this->ps().y2() - this->ps().y1(),
182                                 this->ps().x2() - this->ps().x1()
183                         );
184                         dts *= 1.01; // precision workaround
185                         // backward parking
186                         h = dts + M_PI;
187                         x += -(this->gc().df() + 0.01) * cos(h);
188                         y += -(this->gc().df() + 0.01) * sin(h);
189                         if (this->ps().right())
190                                 dts += M_PI / 2;
191                         else
192                                 dts -= M_PI / 2;
193                         x += (this->gc().w() / 2 + 0.01) * cos(dts);
194                         y += (this->gc().w() / 2 + 0.01) * sin(dts);
195                 }
196         }
197         while (h > M_PI)
198                 h -= 2 * M_PI;
199         while (h <= -M_PI)
200                 h += 2 * M_PI;
201         this->gc().x(x);
202         this->gc().y(y);
203         this->gc().h(h);
204 }
205
206 bool PSPlanner::left()
207 {
208         double lfx = this->cc().lfx();
209         double lfy = this->cc().lfy();
210         double lrx = this->cc().lrx();
211         double lry = this->cc().lry();
212         double rrx = this->cc().rrx();
213         double rry = this->cc().rry();
214         double rfx = this->cc().rfx();
215         double rfy = this->cc().rfy();
216         double lfs = sgn(
217                 (lfx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
218                 - (lfy - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
219         );
220         double lrs = sgn(
221                 (lrx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
222                 - (lry - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
223         );
224         double rrs = sgn(
225                 (rrx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
226                 - (rry - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
227         );
228         double rfs = sgn(
229                 (rfx - this->ps().x1()) * (this->ps().y4() - this->ps().y1())
230                 - (rfy - this->ps().y1()) * (this->ps().x4() - this->ps().x1())
231         );
232         if (this->ps().parallel())
233                 return lfs == rfs && (lfs != lrs || lfs != rrs);
234         else if (!this->forward())
235                 return lfs == rfs && (lfs != lrs || lfs != rrs);
236         else
237                 return lrs == rrs && (lrs != lfs || lrs != rfs);
238 }
239
240 bool PSPlanner::parked()
241 {
242         std::vector<std::tuple<double, double>> slot;
243         slot.push_back(std::make_tuple(this->ps().x1(), this->ps().y1()));
244         slot.push_back(std::make_tuple(this->ps().x2(), this->ps().y2()));
245         slot.push_back(std::make_tuple(this->ps().x3(), this->ps().y3()));
246         slot.push_back(std::make_tuple(this->ps().x4(), this->ps().y4()));
247         return inside(this->gc().lfx(), this->gc().lfy(), slot)
248                 && inside(this->gc().lrx(), this->gc().lry(), slot)
249                 && inside(this->gc().rrx(), this->gc().rry(), slot)
250                 && inside(this->gc().rfx(), this->gc().rfy(), slot);
251 }
252
253 std::vector<BicycleCar> PSPlanner::possible_goals(
254         unsigned int cnt,
255         double dist
256 )
257 {
258         std::vector<BicycleCar> pi;
259         this->cc().sp(this->cc().sp() * dist);
260         this->cc().st(this->cc().st() * 1);
261         BicycleCar orig_cc(this->cc());
262         for (unsigned int i = 0; i < cnt; i++) {
263                 this->cc().next();
264                 pi.push_back(BicycleCar(this->cc()));
265         }
266         this->cc() = BicycleCar(orig_cc);
267         return pi;
268 }
269
270 // find entry
271 void PSPlanner::fe()
272 {
273         if (this->ps().parallel())
274                 return this->fe_parallel();
275         else
276                 return this->fe_perpendicular();
277 }
278
279 void PSPlanner::fe_parallel()
280 {
281         // angle for distance from "entry" corner
282         double dist_angl = this->ps().heading() + M_PI;
283         dist_angl += (this->ps().right()) ? - M_PI / 4 : + M_PI / 4;
284         // set bicycle car `bci` basic dimensions and heading
285         BicycleCar bci = BicycleCar(this->gc());
286         BicycleCar bco = BicycleCar(this->gc());
287         bci.h(this->ps().heading());
288         // move 0.01 from the "entry" corner
289         bci.x(this->ps().x4() + 0.01 * cos(dist_angl));
290         bci.y(this->ps().y4() + 0.01 * sin(dist_angl));
291         // align with parking "top" of slot (move backward)
292         dist_angl = bci.h() + M_PI;
293         bci.x(bci.x() + bci.df() * cos(dist_angl));
294         bci.y(bci.y() + bci.df() * sin(dist_angl));
295         // align with "entry" to pakring slot (move outside)
296         dist_angl = this->ps().heading();
297         dist_angl += (this->ps().right()) ? + M_PI / 2 : - M_PI / 2;
298         bci.x(bci.x() + bci.w() / 2 * cos(dist_angl));
299         bci.y(bci.y() + bci.w() / 2 * sin(dist_angl));
300         // set default speed, steer
301         bci.st(bci.wb() / bci.mtr());
302         if (!this->ps().right())
303                 bci.st(bci.st() * -1);
304         bci.sp(-0.01);
305         // BFS - init all starts
306         // see https://courses.cs.washington.edu/courses/cse326/03su/homework/hw3/bfs.html
307         double dist_diag = sqrt(pow(bci.w() / 2, 2) + pow(bci.df(), 2));
308         if (this->ps().right())
309                 dist_angl = atan2(bci.y() - bci.rfy(), bci.x() - bci.rfx());
310         else
311                 dist_angl = atan2(bci.y() - bci.lfy(), bci.x() - bci.lfx());
312         double DIST_ANGL = dist_angl;
313         std::queue<BicycleCar, std::list<BicycleCar>> q;
314         while (
315                 (
316                         this->ps().right()
317                         && dist_angl < DIST_ANGL + 3 * M_PI / 4
318                 )
319                 || (
320                         !this->ps().right()
321                         && dist_angl > DIST_ANGL - 3 * M_PI / 4
322                 )
323         ) {
324                 this->cc() = BicycleCar(bci);
325                 if (this->ps().right()) {
326                         this->cc().x(bci.rfx() + dist_diag * cos(dist_angl));
327                         this->cc().y(bci.rfy() + dist_diag * sin(dist_angl));
328                 } else {
329                         this->cc().x(bci.lfx() + dist_diag * cos(dist_angl));
330                         this->cc().y(bci.lfy() + dist_diag * sin(dist_angl));
331                 }
332                 this->cc().h(this->ps().heading() + dist_angl - DIST_ANGL);
333                 if (!this->collide()) {
334                         q.push(BicycleCar(this->cc()));
335                 }
336                 dist_angl += (this->ps().right()) ? + 0.01 : - 0.01;
337         }
338         // BFS - find entry current car `cc` and corresponding goal car `gc`
339         unsigned int iter_cntr = 0;
340         while (!q.empty() && iter_cntr < 9) {
341                 this->cc() = BicycleCar(q.front());
342                 q.pop();
343                 while (
344                         !this->collide()
345                         && (std::abs(
346                                 this->cc().h() - this->ps().heading()
347                         ) > M_PI / 32)
348                         && (std::abs(
349                                 this->cc().h() - this->ps().heading()
350                         ) < M_PI / 2)
351                 )
352                         this->cc().next();
353                 this->cc().sp(this->cc().sp() * -1);
354                 this->cc().next();
355                 this->gc() = BicycleCar(this->cc());
356                 if (this->parked())
357                         goto successfinish;
358                 this->cc().st(this->cc().st() * -1);
359                 q.push(BicycleCar(this->cc()));
360                 if (sgn(this->cc().st()) == sgn(q.front().st()))
361                         iter_cntr++;
362         }
363         // fallback to fer
364         this->gc() = BicycleCar(bco);
365 successfinish:
366         return this->fer_parallel();
367 }
368
369 void PSPlanner::fe_perpendicular()
370 {
371         // TODO Try multiple angles when going from parking slot.
372         //
373         //      Do not use just the maximum steer angle. Test angles
374         //      until the whole current car `cc` is out of the parking
375         //      slot `ps`.
376         //
377         //      Another approach could be testing angles from the
378         //      beginning of the escape parkig slot maneuver.
379         if (this->forward())
380                 this->cc().sp(-0.01);
381         else
382                 this->cc().sp(0.01);
383         while (!this->left())
384                 this->cc().next();
385         return;
386 }
387
388 void PSPlanner::fer()
389 {
390         if (this->ps().parallel())
391                 return this->fer_parallel();
392         else
393                 return this->fer_perpendicular();
394 }
395
396 void PSPlanner::fer_parallel()
397 {
398         this->cc().st(this->cc().wb() / this->cc().mtr());
399         if (!this->ps().right())
400                 this->cc().st(this->cc().st() * -1);
401         this->cc().sp(0.01);
402         this->cusps_.clear();
403         while (!this->left()) {
404                 while (!this->collide() && !this->left())
405                         this->cc().next();
406                 if (this->left() && !this->collide()) {
407                         break;
408                 } else {
409                         this->cc().sp(this->cc().sp() * -1);
410                         this->cc().next();
411                         this->cc().st(this->cc().st() * -1);
412                         this->c_++;
413                         this->cusps_.push_back(this->cc());
414                 }
415         }
416 }
417
418 void PSPlanner::fer_perpendicular()
419 {
420         bool delta_use[] = {true, true, true};
421         double cc_h = this->cc().h();
422         double x;
423         double y;
424         // check inner radius
425         if (this->forward()) {
426                 x = this->ps().x1();
427                 y = this->ps().y1();
428         } else {
429                 x = this->ps().x4();
430                 y = this->ps().y4();
431         }
432         double x1;
433         double y1;
434         if (this->ps().right()) {
435                 x1 = this->cc().ccr().x();
436                 y1 = this->cc().ccr().y();
437         } else {
438                 x1 = this->cc().ccl().x();
439                 y1 = this->cc().ccl().y();
440         }
441         double IR = this->cc().iradi();
442         double a = 1;
443         double b;
444         if (this->forward())
445                 b = (x - x1) * 2 * cos(cc_h) + (y - y1) * 2 * sin(cc_h);
446         else
447                 b = (x1 - x) * 2 * cos(cc_h) + (y1 - y) * 2 * sin(cc_h);
448         double c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
449         double D = pow(b, 2) - 4 * a * c;
450         double delta;
451         delta = -b - sqrt(D);
452         delta /= 2 * a;
453         double delta_1 = delta;
454         if (D < 0)
455                 delta_use[0] = false;
456         // check outer radius
457         if (this->forward()) {
458                 x = this->ps().x4();
459                 y = this->ps().y4();
460         } else {
461                 x = this->ps().x1();
462                 y = this->ps().y1();
463         }
464         IR = this->cc().ofradi();
465         a = 1;
466         if (this->forward())
467                 b = (x - x1) * 2 * cos(cc_h) + (y - y1) * 2 * sin(cc_h);
468         else
469                 b = (x1 - x) * 2 * cos(cc_h) + (y1 - y) * 2 * sin(cc_h);
470         c = pow(x - x1, 2) + pow(y - y1, 2) - pow(IR, 2);
471         D = pow(b, 2) - 4 * a * c;
472         if (this->forward()) {
473                 delta = -b + sqrt(D);
474                 delta /= 2 * a;
475         }
476         double delta_2 = delta;
477         if (D < 0)
478                 delta_use[1] = false;
479         delta = -b - sqrt(D);
480         delta /= 2 * a;
481         double delta_3 = delta;
482         if (D < 0)
483                 delta_use[2] = false;
484         if (delta_use[0] && delta_use[1] && delta_use[22])
485                 delta = std::max(delta_1, std::max(delta_2, delta_3));
486         else if (delta_use[0] && delta_use[1])
487                 delta = std::max(delta_1, delta_2);
488         else if (delta_use[0] && delta_use[2])
489                 delta = std::max(delta_1, delta_3);
490         else if (delta_use[1] && delta_use[2])
491                 delta = std::max(delta_2, delta_3);
492         else if (delta_use[0])
493                 delta = delta_1;
494         else if (delta_use[1])
495                 delta = delta_2;
496         else if (delta_use[2])
497                 delta = delta_3;
498         else
499                 return;
500         // current car `cc` can get out of slot with max steer
501         this->cc().x(this->cc().x() + delta * cos(cc_h));
502         this->cc().y(this->cc().y() + delta * sin(cc_h));
503         this->cc().h(cc_h);
504         // get current car `cc` out of slot
505         if (this->forward())
506                 this->cc().sp(-0.01);
507         else
508                 this->cc().sp(0.01);
509         this->cc().st(this->cc().wb() / this->cc().mtr());
510         if (this->ps().right())
511                 this->cc().st(this->cc().st() * -1);
512         while (!this->left()) {
513                 while (!this->collide() && !this->left())
514                         this->cc().next();
515                 if (this->left() && !this->collide()) {
516                         break;
517                 } else {
518                         this->cc().sp(this->cc().sp() * -1);
519                         this->cc().next();
520                         this->cc().st(this->cc().st() * -1);
521                 }
522         }
523 }
524
525 PSPlanner::PSPlanner()
526 {
527 }