]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blobdiff - decision_control/slotplanner.cc
Add find init pose in forward direction method
[hubacji1/iamcar.git] / decision_control / slotplanner.cc
index 4d15a2df7212d27026364f31670c3fc04d9f10b0..e4a61ebdbd88215c56b950be72d809c01b0ed54b 100644 (file)
@@ -25,6 +25,11 @@ ParallelSlot::ParallelSlot()
 {}
 
 // getter
+std::vector<RRTNode *> &ParallelSlot::goals()
+{
+        return this->goals_;
+}
+
 RRTNode *ParallelSlot::getMidd()
 {
         if (this->cusp().size() > 0)
@@ -109,76 +114,50 @@ void ParallelSlot::fip(
 {
         this->setAll();
         if (this->slotType() == PERPENDICULAR) {
-                // TODO different slot headings
-                // this is jus for slot heading = pi / 2
-                this->DH(0.01 / BCAR_TURNING_RADIUS);
-                BicycleCar *perc = nullptr;
-                RRTNode *cc = nullptr;
-                BicycleCar *p = nullptr;
-                int i = 0;
-                float x;
-                float h;
-                float y;
-                // parking backward
-                x = this->slot().bnodes()[3]->x();
-                y = this->slot().bnodes()[3]->y();
-                h = 0;
-                // -> top
-                x -= BCAR_DIST_FRONT;
-                y = this->slot().bnodes()[3]->y();
-                y += BCAR_OUT_RRADI - BCAR_TURNING_RADIUS;
-                if (perc)
-                        delete perc;
-                perc = new BicycleCar(x, y, h);
-                if (cc)
-                        delete cc;
-                cc = perc->ccl();
-                if (p)
-                        delete p;
-                p = perc->move(cc, i * this->DH());
-                while (p->x() < 0) {
-                        delete p;
-                        p = perc->move(cc, i * this->DH());
-                        i++;
-                }
-                // -> bottom
-                // (reset for parking backward)
-                x = this->slot().bnodes()[0]->x();
-                y = this->slot().bnodes()[0]->y();
-                h = 0;
-                // -> bottom
-                x -= BCAR_DIST_FRONT;
-                // get y from quadratic equation
-                float tmpD = pow(-2 * this->slot().bnodes()[0]->y(), 2);
-                tmpD -= 4 * (
-                        pow(x - this->slot().bnodes()[0]->x(), 2) +
-                        pow(this->slot().bnodes()[0]->y(), 2) -
-                        pow(BCAR_IN_RADI, 2)
-                );
-                y = 2 * this->slot().bnodes()[0]->y();
-                y += sqrt(tmpD);
-                y /= 2;
-                y -= BCAR_TURNING_RADIUS;
-                // -- end of quadratic equation
-                if (perc)
-                        delete perc;
-                perc = new BicycleCar(x, y, h);
-                if (cc)
-                        delete cc;
-                cc = perc->ccl();
-                if (p)
-                        delete p;
-                p = perc->move(cc, i * this->DH());
-                while (p->x() < 0) {
-                        delete p;
-                        p = perc->move(cc, i * this->DH());
-                        i++;
+                std::vector<RRTNode *> tmpc;
+                BicycleCar *tmpf = this->getFP();
+                BicycleCar *tmpb = this->getISPP(tmpf);
+                RRTNode *cc;
+                if (this->slotSide() == LEFT)
+                        cc = tmpb->ccl();
+                else
+                        cc = tmpb->ccr();
+                if (this->slotSide() == LEFT)
+                        this->DH(1 * 0.5 / tmpb->out_radi());
+                else
+                        this->DH(-1 * 0.5 / tmpb->out_radi());
+                BicycleCar *p;
+                int i = 1;
+                p = tmpb->move(cc, i * this->DH());
+                while (
+                        !this->slot().collide(p->frame())
+                        && ((
+                                this->slotSide() == LEFT
+                                && p->h() < this->slotHeading()
+                        ) || (
+                                this->slotSide() == RIGHT
+                                && p->h() > this->slotHeading()
+                        ))
+                ) {
+                        bool end = false;
+                        std::vector<RRTEdge *> eds = p->frame();
+                        for (auto o: co)
+                                if (o.collide(eds))
+                                        end = true;
+                        for (auto o: so)
+                                if (o.collide(eds))
+                                        end = true;
+                        for (auto e: eds)
+                                delete e;
+                        if (end)
+                                break;
+                        this->goals_.push_back(p);
+                        tmpc.push_back(p);
+                        i += 1;
+                        p = tmpb->move(cc, i * this->DH());
                 }
-                // store nodes
-                std::vector<RRTNode *> cusp;
-                cusp.push_back(new RRTNode(p->x(), p->y(), p->h()));
-                cusp.push_back(new RRTNode(x, y, h));
-                this->cusp().push_back(cusp);
+                if (tmpc.size() > 0)
+                        this->cusp().push_back(tmpc);
                 return;
         }
         // see https://courses.cs.washington.edu/courses/cse326/03su/homework/hw3/bfs.html
@@ -238,6 +217,59 @@ createcuspandfinish:
         std::swap(q, empty);
 }
 
+void ParallelSlot::fipf(
+        std::vector<CircleObstacle>& co,
+        std::vector<SegmentObstacle>& so
+)
+{
+        this->setAll();
+        std::vector<RRTNode *> tmpc;
+        BicycleCar *tmpf = this->getFPf();
+        BicycleCar *tmpb = this->getISPPf(tmpf);
+        RRTNode *cc;
+        if (this->slotSide() == LEFT)
+                cc = tmpb->ccl();
+        else
+                cc = tmpb->ccr();
+        if (this->slotSide() == LEFT)
+                this->DH(-1 * 0.5 / tmpb->out_radi());
+        else
+                this->DH(1 * 0.5 / tmpb->out_radi());
+        BicycleCar *p;
+        int i = 1;
+        p = tmpb->move(cc, i * this->DH());
+        while (
+                !this->slot().collide(p->frame())
+                && ((
+                        this->slotSide() == LEFT
+                        && p->h() > this->slotHeading()
+                ) || (
+                        this->slotSide() == RIGHT
+                        && p->h() < this->slotHeading()
+                ))
+        ) {
+                bool end = false;
+                std::vector<RRTEdge *> eds = p->frame();
+                for (auto o: co)
+                        if (o.collide(eds))
+                                end = true;
+                for (auto o: so)
+                        if (o.collide(eds))
+                                end = true;
+                for (auto e: eds)
+                        delete e;
+                if (end)
+                        break;
+                this->goals_.push_back(p);
+                tmpc.push_back(p);
+                i += 1;
+                p = tmpb->move(cc, i * this->DH());
+        }
+        if (tmpc.size() > 0)
+                this->cusp().push_back(tmpc);
+        return;
+}
+
 BicycleCar *ParallelSlot::flnc(
         BicycleCar *B,
         std::vector<CircleObstacle>& co,
@@ -600,8 +632,9 @@ BicycleCar *ParallelSlot::getEPC()
 
 BicycleCar *ParallelSlot::getFP()
 {
-        float x = this->slot().bnodes()[3]->x();
-        float y = this->slot().bnodes()[3]->y();
+        this->setAll();
+        float x = this->slot().bnodes()[0]->x();
+        float y = this->slot().bnodes()[0]->y();
         float h = this->slotHeading();
         float nx;
         float ny;
@@ -637,6 +670,99 @@ BicycleCar *ParallelSlot::getFP()
         return new BicycleCar(x, y, h);
 }
 
+BicycleCar *ParallelSlot::getISPP(BicycleCar *B)
+{
+        float x = this->slot().bnodes().back()->x();
+        float y = this->slot().bnodes().back()->y();
+        float y0;
+        if (this->slotSide() == LEFT) // TODO only for backward parking now
+                y0 = B->ccl()->y();
+        else
+                y0 = B->ccr()->y();
+        float IR = BCAR_IN_RADI;
+        float a = 1;
+        float b = -2 * x;
+        float c = pow(x, 2) + pow(y - y0, 2) - pow(IR, 2);
+        float D = pow(b, 2) - 4 * a * c;
+        float x0;
+        if (this->slotSide() == LEFT)
+                x0 = -b - sqrt(D);
+        else
+                x0 = -b + sqrt(D);
+        x0 /= 2 * a;
+        return new BicycleCar(x0, B->y(), B->h());
+}
+
+BicycleCar *ParallelSlot::getFPf()
+{
+        this->setAll();
+        float x = this->slot().bnodes().front()->x();
+        float y = this->slot().bnodes().front()->y();
+        float h = this->slotHeading();
+        float nx;
+        float ny;
+        if (this->slotSide() == LEFT) {
+                h += M_PI / 2;
+                nx = x + (BCAR_LENGTH - BCAR_WHEEL_BASE) / 2
+                        * cos(h);
+                ny = y + (BCAR_LENGTH - BCAR_WHEEL_BASE) / 2
+                        * sin(h);
+                x = nx + (BCAR_DIAG_RRADI) * cos(h - M_PI / 2);
+                y = ny + (BCAR_DIAG_RRADI) * sin(h - M_PI / 2);
+        } else {
+                h -= M_PI / 2;
+                nx = x + (BCAR_LENGTH - BCAR_WHEEL_BASE) / 2
+                        * cos(h);
+                ny = y + (BCAR_LENGTH - BCAR_WHEEL_BASE) / 2
+                        * sin(h);
+                x = nx + (BCAR_DIAG_RRADI) * cos(h + M_PI / 2);
+                y = ny + (BCAR_DIAG_RRADI) * sin(h + M_PI / 2);
+        }
+        return new BicycleCar(x, y, h);
+}
+
+BicycleCar *ParallelSlot::getISPPf(BicycleCar *B)
+{
+        float x = this->slot().bnodes().front()->x();
+        float y = this->slot().bnodes().front()->y();
+        float y0;
+        if (this->slotSide() == LEFT)
+                y0 = B->ccl()->y();
+        else
+                y0 = B->ccr()->y();
+        float IR = BCAR_IN_RADI;
+        float a = 1;
+        float b = -2 * x;
+        float c = pow(x, 2) + pow(y - y0, 2) - pow(IR, 2);
+        float D = pow(b, 2) - 4 * a * c;
+        float x0;
+        if (this->slotSide() == LEFT)
+                x0 = -b - sqrt(D);
+        else
+                x0 = -b + sqrt(D);
+        x0 /= 2 * a;
+        float x0_1 = x0;
+        // left front
+        x = this->slot().bnodes().back()->x();
+        y = this->slot().bnodes().back()->y();
+        IR = BCAR_OUT_RADI;
+        a = 1;
+        b = -2 * x;
+        c = pow(x, 2) + pow(y - y0, 2) - pow(IR, 2);
+        D = pow(b, 2) - 4 * a * c;
+        if (this->slotSide() == LEFT)
+                x0 = -b + sqrt(D);
+        else
+                x0 = -b - sqrt(D);
+        x0 /= 2 * a;
+        float x0_2 = x0;
+        if (this->slotSide() == LEFT)
+                x0 = std::max(x0_1, x0_2);
+        else
+                x0 = std::min(x0_1, x0_2);
+        return new BicycleCar(x0, B->y(), B->h());
+}
+
 bool ParallelSlot::isInside(BicycleCar *c)
 {
         bool inside = true;