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