]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blobdiff - decision_control/slotplanner.cc
Add perpendicular reverse parking proporsal
[hubacji1/iamcar.git] / decision_control / slotplanner.cc
index 976aea55ccf3297ed7244f32f25bc6ad2323b4b6..ed1446de490c20a83eab26a9392b45d6ebe59f76 100644 (file)
@@ -15,8 +15,10 @@ You should have received a copy of the GNU General Public License
 along with I am car. If not, see <http://www.gnu.org/licenses/>.
 */
 
+#include <algorithm>
 #include <cmath>
-#include "bcar.h"
+#include <list>
+#include <queue>
 #include "slotplanner.h"
 
 ParallelSlot::ParallelSlot()
@@ -38,20 +40,266 @@ PolygonObstacle &ParallelSlot::slot()
         return this->slot_;
 }
 
+float ParallelSlot::slotHeading()
+{
+        float y0 = this->slot().bnodes()[0]->y();
+        float x0 = this->slot().bnodes()[0]->x();
+        float y3 = this->slot().bnodes()[3]->y();
+        float x3 = this->slot().bnodes()[3]->x();
+        float dy = y0 - y3;
+        float dx = x0 - x3;
+        return atan2(dy, dx);
+}
+
+SlotSide ParallelSlot::slotSide()
+{
+        return this->slotSide_;
+}
+
+SlotType ParallelSlot::slotType()
+{
+        return this->slotType_;
+}
+
 // setter
 void ParallelSlot::DH(float dh)
 {
         this->DH_ = dh;
 }
 
+void ParallelSlot::setAll()
+{
+        // slot side
+        float y0 = this->slot().bnodes()[0]->y();
+        float x0 = this->slot().bnodes()[0]->x();
+        float y1 = this->slot().bnodes()[1]->y();
+        float x1 = this->slot().bnodes()[1]->x();
+        float y3 = this->slot().bnodes()[3]->y();
+        float x3 = this->slot().bnodes()[3]->x();
+        if (sgn((x1 - x3) * (y0 - y3) - (y1 - y3) * (x0 - x3)) < 0)
+                this->slotSide_ = LEFT;
+        else
+                this->slotSide_ = RIGHT;
+        // slot type
+        float d1 = EDIST(
+                this->slot().bnodes()[0],
+                this->slot().bnodes()[1]
+        );
+        float d2 = EDIST(
+                this->slot().bnodes()[1],
+                this->slot().bnodes()[2]
+        );
+        if (d1 > d2)
+                this->slotType_ = PERPENDICULAR;
+        else
+                this->slotType_ = PARALLEL;
+}
+
 // other
+void ParallelSlot::fip()
+{
+        // see https://courses.cs.washington.edu/courses/cse326/03su/homework/hw3/bfs.html
+        // RRTNode.s() works as iteration level
+        std::queue<BicycleCar *, std::list<BicycleCar *>> q;
+        std::queue<BicycleCar *, std::list<BicycleCar *>> empty;
+        int di = -1;
+        if (this->slotSide() == LEFT)
+                di = 1;
+        BicycleCar *CC = this->getEPC();
+        BicycleCar *B = this->getEP();
+        this->DH(di * 0.01 / CC->out_radi());
+        BicycleCar *c;
+        int i = 0;
+        c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
+        while (!this->slot().collide(c->frame())) {
+                q.push(c);
+                c = B->move(CC, -i * di * 0.01 / CC->diag_radi());
+                i += 1;
+        }
+        delete c; // not in q and collide
+        while (!q.empty()) {
+                c = q.front();
+                q.pop();
+                if (this->isInside(c)) {
+                        goto createcuspandfinish;
+                } else if (c->s() < 9) {
+                        BicycleCar *cc = this->flnc(c);
+                        cc->s(c->s() + 1);
+                        cc->bcparent(c);
+                        q.push(cc);
+                } else {
+                        delete c; // not in q and collide
+                }
+        }
+        std::swap(q, empty);
+        return;
+createcuspandfinish:
+        std::vector<RRTNode *> cusp;
+        while (c) {
+                cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
+                c = c->bcparent();
+        }
+        std::reverse(cusp.begin(), cusp.end());
+        this->cusp().push_back(cusp);
+        std::swap(q, empty);
+}
+
+void ParallelSlot::fipr(BicycleCar *B)
+{
+        std::vector<RRTNode *> cusp;
+        cusp.push_back(new RRTNode(B->x(), B->y(), B->h()));
+        int di = 1;
+        if (this->slotSide() == LEFT)
+                di = -1;
+        if (this->slotType() == PERPENDICULAR) {
+                cusp.push_back(new RRTNode(
+                        B->x() - di * B->length(),
+                        B->y(),
+                        B->h()
+                ));
+                std::reverse(cusp.begin(), cusp.end());
+                this->cusp().push_back(cusp);
+                return;
+        }
+        this->DH(di * 0.01 / B->out_radi());
+        BicycleCar *c;
+        c = this->flncr(B);
+        c->s(B->s() + 1);
+        while ((
+                this->slotSide() == LEFT
+                && this->slot().collide(new RRTNode(c->lfx(), c->lfy(), 0))
+        ) || (
+                this->slotSide() == RIGHT
+                && this->slot().collide(new RRTNode(c->rfx(), c->rfy(), 0))
+        )) {
+                cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
+                BicycleCar *cc = this->flncr(c);
+                cc->s(c->s() + 1);
+                delete c;
+                c = cc;
+        }
+        cusp.push_back(new RRTNode(c->x(), c->y(), c->h()));
+        std::reverse(cusp.begin(), cusp.end());
+        this->cusp().push_back(cusp);
+}
+
+BicycleCar *ParallelSlot::flnc(BicycleCar *B)
+{
+        RRTNode *cc;
+        if (this->slotSide() == LEFT) {
+                if (int(B->s()) % 2 == 0)
+                        cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
+                else
+                        cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
+        } else {
+                if (int(B->s()) % 2 == 0)
+                        cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
+                else
+                        cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
+        }
+        BicycleCar *p;
+        int i = 1;
+        p = B->move(cc, i * this->DH());
+        while (
+                !this->slot().collide(p->frame())
+                && std::abs(this->slotHeading() - p->h()) < M_PI / 2
+        ) {
+                delete p;
+                i += 10;
+                p = B->move(cc, i * this->DH());
+        }
+        i -= 10;
+        p = B->move(cc, i * this->DH());
+        while (
+                !this->slot().collide(p->frame())
+                && std::abs(this->slotHeading() - p->h()) < M_PI / 2
+        ) {
+                if (this->isInside(p)) {
+                        i += 1;
+                        break;
+                }
+                delete p;
+                i += 1;
+                p = B->move(cc, i * this->DH());
+        }
+        delete p;
+        return B->move(cc, (i - 1) * this->DH());
+}
+
+BicycleCar *ParallelSlot::flncr(BicycleCar *B)
+{
+        RRTNode *cc;
+        if (this->slotSide() == LEFT) {
+                if (int(B->s()) % 2 == 0)
+                        cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
+                else
+                        cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
+        } else {
+                if (int(B->s()) % 2 == 0)
+                        cc = BicycleCar(B->x(), B->y(), B->h()).ccl();
+                else
+                        cc = BicycleCar(B->x(), B->y(), B->h()).ccr();
+        }
+        BicycleCar *p;
+        int i = 1;
+        p = B->move(cc, i * this->DH());
+        while (
+                !this->slot().collide(p->frame())
+                && ((
+                        this->slotSide() == LEFT
+                        && this->slot().collide(new RRTNode(
+                                p->lfx(),
+                                p->lfy(),
+                                0
+                        ))
+                ) || (
+                        this->slotSide() == RIGHT
+                        && this->slot().collide(new RRTNode(
+                                p->rfx(),
+                                p->rfy(),
+                                0
+                        ))
+                ))
+        ) {
+                delete p;
+                i += 10;
+                p = B->move(cc, i * this->DH());
+        }
+        i -= 10;
+        p = B->move(cc, i * this->DH());
+        while (!this->slot().collide(p->frame())) {
+                if(
+                        this->slotSide() == LEFT
+                        && !this->slot().collide(new RRTNode(
+                                p->lfx(),
+                                p->lfy(),
+                                0
+                        ))
+                ) {
+                        i += 1;
+                        break;
+                }
+                if(
+                        this->slotSide() == RIGHT
+                        && !this->slot().collide(new RRTNode(
+                                p->rfx(),
+                                p->rfy(),
+                                0
+                        ))
+                ) {
+                        i += 1;
+                        break;
+                }
+                i += 1;
+                p = B->move(cc, i * this->DH());
+        }
+        delete p;
+        return B->move(cc, (i - 1) * this->DH());
+}
+
 RRTNode *ParallelSlot::fposecenter()
 {
-        if (this->slot().bnodes().front()->y() >
-                        this->slot().bnodes().back()->y())
-                return this->slot().bnodes().front();
-        else
-                return this->slot().bnodes().back();
+        return this->slot().bnodes().front();
 }
 
 bool ParallelSlot::flast(
@@ -70,6 +318,17 @@ bool ParallelSlot::flast(
         BicycleCar *p;
         int i = 1;
         p = B->move(cc, i * this->DH());
+        while (!this->slot().collide(p->frame())
+                        && (
+                                (this->DH() > 0 && p->x() <= 0)
+                                || (this->DH() < 0 && p->x() >= 0)
+                        )) {
+                delete p;
+                i += 10;
+                p = B->move(cc, i * this->DH());
+        }
+        i -= 10;
+        p = B->move(cc, i * this->DH());
         while (!this->slot().collide(p->frame())
                         && (
                                 (this->DH() > 0 && p->x() <= 0)
@@ -83,8 +342,8 @@ bool ParallelSlot::flast(
                         i += 1;
                         break;
                 }
-                i += 1;
                 delete p;
+                i += 1;
                 p = B->move(cc, i * this->DH());
         }
         delete p;
@@ -109,12 +368,12 @@ void ParallelSlot::fpose()
         BicycleCar *CC = new BicycleCar(
                 this->fposecenter()->x(),
                 this->fposecenter()->y() - 0.01,
-                M_PI / 2
+                this->slotHeading()
         );
         BicycleCar *B = new BicycleCar(
                 CC->x() - CC->width() / 2,
                 CC->y() - (CC->length() + CC->wheelbase()) / 2,
-                M_PI / 2
+                this->slotHeading()
         );
         if (this->slot().bnodes()[0]->x() > this->slot().bnodes()[1]->x()) {
                 left = true;
@@ -123,7 +382,7 @@ void ParallelSlot::fpose()
                 B = new BicycleCar(
                         CC->x() + CC->width() / 2,
                         CC->y() - (CC->length() + CC->wheelbase()) / 2,
-                        M_PI / 2
+                        this->slotHeading()
                 );
         }
         this->DH(di * 0.01 / CC->out_radi());
@@ -142,3 +401,137 @@ void ParallelSlot::fpose()
                 p = B->move(CC, -i * di * 0.01 / CC->diag_radi());
         }
 }
+
+BicycleCar *ParallelSlot::getEP()
+{
+        // new pose for parallel parking to right slot
+        float tnx;
+        float tny;
+        float nx;
+        float ny;
+        BicycleCar *CC = this->getEPC();
+        // move left by car width / 2
+        tnx = CC->x() + CC->width() / 2 * cos(CC->h() + M_PI / 2);
+        tny = CC->y() + CC->width() / 2 * sin(CC->h() + M_PI / 2);
+        if (this->slotSide() == LEFT) {
+                // move right by car width / 2
+                tnx = CC->x() + CC->width() / 2 * cos(CC->h() - M_PI / 2);
+                tny = CC->y() + CC->width() / 2 * sin(CC->h() - M_PI / 2);
+        }
+        if (this->slotType() == PARALLEL) {
+                // move down
+                nx = tnx - (CC->length() + CC->wheelbase()) / 2 * cos(CC->h());
+                ny = tny - (CC->length() + CC->wheelbase()) / 2 * sin(CC->h());
+        } else {
+                // move down
+                nx = tnx + (CC->length() - CC->wheelbase()) / 2 * cos(CC->h());
+                ny = tny + (CC->length() - CC->wheelbase()) / 2 * sin(CC->h());
+        }
+        return new BicycleCar(nx, ny, CC->h());
+}
+
+BicycleCar *ParallelSlot::getEPC()
+{
+        // new pose for parallel parking to right slot
+        float ta;
+        float nx;
+        float ny;
+        ta = this->slotHeading() + M_PI;
+        if (this->slotSide() == RIGHT)
+                ta -= M_PI / 4;
+        else
+                ta += M_PI / 4;
+        nx = this->fposecenter()->x() + 0.01 * cos(ta);
+        ny = this->fposecenter()->y() + 0.01 * sin(ta);
+        return new BicycleCar(nx, ny, this->slotHeading());
+}
+
+BicycleCar *ParallelSlot::getFP()
+{
+        float x = this->slot().bnodes()[3]->x();
+        float y = this->slot().bnodes()[3]->y();
+        float h = this->slotHeading();
+        float nx;
+        float ny;
+        if (this->slotType() == PARALLEL) {
+                if (this->slotSide() == LEFT) {
+                        nx = x + BCAR_WIDTH / 2 * cos(h + M_PI / 2);
+                        ny = y + BCAR_WIDTH / 2 * sin(h + M_PI / 2);
+                } else {
+                        nx = x + BCAR_WIDTH / 2 * cos(h - M_PI / 2);
+                        ny = y + BCAR_WIDTH / 2 * sin(h - M_PI / 2);
+                }
+                x = nx + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * cos(h);
+                y = ny + ((BCAR_LENGTH - BCAR_WHEEL_BASE) / 2 + 0.01) * sin(h);
+        } else {
+                if (this->slotSide() == LEFT) {
+                        h -= M_PI / 2;
+                        nx = x + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
+                                * cos(h + M_PI);
+                        ny = y + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
+                                * sin(h + M_PI);
+                        x = nx + (BCAR_WIDTH + 0.01) * cos(h + M_PI / 2);
+                        y = ny + (BCAR_WIDTH + 0.01) * sin(h + M_PI / 2);
+                } else {
+                        h += M_PI / 2;
+                        nx = x + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
+                                * cos(h - M_PI);
+                        ny = y + (BCAR_LENGTH + BCAR_WHEEL_BASE) / 2
+                                * sin(h - M_PI);
+                        x = nx + (BCAR_WIDTH / 2 + 0.01) * cos(h - M_PI / 2);
+                        y = ny + (BCAR_WIDTH / 2 + 0.01) * sin(h - M_PI / 2);
+                }
+        }
+        return new BicycleCar(x, y, h);
+}
+
+bool ParallelSlot::isInside(BicycleCar *c)
+{
+        bool inside = true;
+        RRTNode *tmpn;
+        tmpn = new RRTNode(c->lfx(), c->lfy(), 0);
+        if (!this->slot().collide(tmpn))
+                inside = false;
+        delete tmpn;
+        tmpn = new RRTNode(c->lrx(), c->lry(), 0);
+        if (!this->slot().collide(tmpn))
+                inside = false;
+        delete tmpn;
+        tmpn = new RRTNode(c->rrx(), c->rry(), 0);
+        if (!this->slot().collide(tmpn))
+                inside = false;
+        delete tmpn;
+        tmpn = new RRTNode(c->rfx(), c->rfy(), 0);
+        if (!this->slot().collide(tmpn))
+                inside = false;
+        delete tmpn;
+        return inside;
+}
+
+struct SamplingInfo ParallelSlot::getSamplingInfo()
+{
+        struct SamplingInfo si;
+        BicycleCar *CC = this->getEPC();
+        si.x = this->slot().bnodes()[0]->x();
+        si.y = this->slot().bnodes()[0]->y();
+        if (this->slotSide() == RIGHT) {
+                si.dx = 1;
+                si.dy = 1;
+                si.dh = 1;
+        } else {
+                si.dx = -1;
+                si.dy = -1;
+                si.dh = -1;
+        }
+        si.r = CC->diag_radi();
+        si.sh = this->slotHeading();
+        if (this->slotType() == PARALLEL) {
+                si.h = this->slotHeading() - acos(EDIST(
+                        this->slot().bnodes()[0],
+                        this->slot().bnodes()[1]
+                ) / BCAR_LENGTH);
+        } else {
+                si.h = M_PI /2;
+        }
+        return si;
+}