]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blob - decision_control/slotplanner.cc
Merge branch 'feature/generalize-parallel-parking'
[hubacji1/iamcar.git] / decision_control / slotplanner.cc
1 /*
2 This file is part of I am car.
3
4 I am car is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 I am car is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with I am car. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <cmath>
19 #include "bcar.h"
20 #include "slotplanner.h"
21
22 ParallelSlot::ParallelSlot()
23 {}
24
25 // getter
26 std::vector<std::vector<RRTNode *>> &ParallelSlot::cusp()
27 {
28         return this->cusp_;
29 }
30
31 float ParallelSlot::DH() const
32 {
33         return this->DH_;
34 }
35
36 PolygonObstacle &ParallelSlot::slot()
37 {
38         return this->slot_;
39 }
40
41 // setter
42 void ParallelSlot::DH(float dh)
43 {
44         this->DH_ = dh;
45 }
46
47 // other
48 RRTNode *ParallelSlot::fposecenter()
49 {
50         if (this->slot().bnodes().front()->y() >
51                         this->slot().bnodes().back()->y())
52                 return this->slot().bnodes().front();
53         else
54                 return this->slot().bnodes().back();
55 }
56
57 bool ParallelSlot::flast(
58         RRTNode *P,
59         bool right,
60         int il,
61         std::vector<RRTNode *> &cusp
62 )
63 {
64         BicycleCar *B = new BicycleCar(P->x(), P->y(), P->h());
65         RRTNode *cc;
66         if (right)
67                 cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
68         else
69                 cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
70         BicycleCar *p;
71         int i = 1;
72         p = B->move(cc, i * this->DH());
73         while (!this->slot().collide(p->frame())
74                         && (
75                                 (this->DH() > 0 && p->x() <= 0)
76                                 || (this->DH() < 0 && p->x() >= 0)
77                         )) {
78                 if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
79                         i += 1;
80                         break;
81                 }
82                 if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
83                         i += 1;
84                         break;
85                 }
86                 i += 1;
87                 delete p;
88                 p = B->move(cc, i * this->DH());
89         }
90         delete p;
91         p = B->move(cc, (i - 1) * this->DH());
92         if (this->DH() > 0 && p->rfx() <= 0 && p->rrx() <= 0) {
93                 cusp.push_back(p);
94                 return true;
95         } else if (this->DH() < 0 && p->lfx() >= 0 && p->lrx() >= 0) {
96                 cusp.push_back(p);
97                 return true;
98         } else if (il < 8) {
99                 cusp.push_back(p);
100                 return this->flast(p, !right, il + 1, cusp);
101         }
102         return false;
103 }
104
105 void ParallelSlot::fpose()
106 {
107         bool left = false; // right parking slot
108         float di = -1;
109         BicycleCar *CC = new BicycleCar(
110                 this->fposecenter()->x(),
111                 this->fposecenter()->y() - 0.01,
112                 M_PI / 2
113         );
114         BicycleCar *B = new BicycleCar(
115                 CC->x() - CC->width() / 2,
116                 CC->y() - (CC->length() + CC->wheelbase()) / 2,
117                 M_PI / 2
118         );
119         if (this->slot().bnodes()[0]->x() > this->slot().bnodes()[1]->x()) {
120                 left = true;
121                 di = 1;
122                 delete B;
123                 B = new BicycleCar(
124                         CC->x() + CC->width() / 2,
125                         CC->y() - (CC->length() + CC->wheelbase()) / 2,
126                         M_PI / 2
127                 );
128         }
129         this->DH(di * 0.01 / CC->out_radi());
130         BicycleCar *p;
131         int i = 0;
132         p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
133         while (!this->slot().collide(p->frame())) {
134                 std::vector<RRTNode *> tmpcusp;
135                 tmpcusp.push_back(new BicycleCar(p->x(), p->y(), p->h()));
136                 if (this->flast(p, left, 0, tmpcusp)) {
137                         this->cusp().push_back(tmpcusp);
138                         return;
139                 }
140                 i += 1;
141                 delete p;
142                 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
143         }
144 }