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