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