]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blobdiff - base/main.cc
Load goals if present in JSON input
[hubacji1/iamcar.git] / base / main.cc
index 852a47ce73c7b37f9dfe44f59c9f1cd6bc1def21..2e8f7cbb46446cc81d659a8867aaf40ee876c634 100644 (file)
@@ -16,6 +16,9 @@ along with I am car. If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include <algorithm>
+#include <chrono>
+#include <cmath>
+#include <cstdlib>
 #include <iostream>
 #include <jsoncpp/json/json.h>
 #include <pthread.h>
@@ -24,35 +27,67 @@ along with I am car. If not, see <http://www.gnu.org/licenses/>.
 #include "compile.h"
 #include "obstacle.h"
 #include "rrtplanner.h"
+#include "slotplanner.h"
+
+#if USE_GL > 0
 // OpenGL
 #include <GL/gl.h>
 #include <GL/glu.h>
 #include <SDL2/SDL.h>
+#endif
 
 // debug
 //#define JSONLOGEDGES
 //#define JSONLOGSAMPLES
 
-// choose
-//#define USE_INTERRUPT
-// or
-//#define USE_TMAX
-// or
-//#define USE_LOADF
-// or
-#define USE_PTHREAD
-
-#ifdef USE_INTERRUPT
-        #define USE_GL
+#if USE_GL > 0
+        #define USE_INTERRUPT
+#else
+        // choose
+        //#define USE_INTERRUPT
+        // or
+        #define USE_TMAX
+        // or
+        //#define USE_LOADF
+        // or
+        //#define USE_PTHREAD
 #endif
 
+// OpenGL window size
+#define SCREEN_WIDTH 1000
+#define SCREEN_HEIGHT 1000
+
+std::chrono::high_resolution_clock::time_point TSTART_;
+std::chrono::high_resolution_clock::time_point TEND_;
+float TELAPSED = 0;
+float ELAPSED = 0;
+void TSTART() {TSTART_ = std::chrono::high_resolution_clock::now();}
+void TEND() {
+        std::chrono::duration<float> DT_;
+        TEND_ = std::chrono::high_resolution_clock::now();
+        DT_ = std::chrono::duration_cast<std::chrono::duration<float>>(
+                TEND_ - TSTART_
+        );
+        TELAPSED += DT_.count();
+        ELAPSED = DT_.count();
+}
+void TPRINT(const char *what) {
+        std::chrono::duration<float> DT_;
+        DT_ = std::chrono::duration_cast<std::chrono::duration<float>>(
+                TEND_ - TSTART_
+        );
+        std::cerr << what << ": " << DT_.count() << std::endl;
+}
+
 bool run_planner = true;
 
+#if USE_GL > 0
 SDL_Window* gw = NULL;
 SDL_GLContext gc;
 
 bool init();
 bool initGL();
+#endif
 
 void hint(int)
 {
@@ -80,9 +115,19 @@ void *next_run(void *arg)
 }
 #endif
 
+RRTNode *sa_tmp()
+{
+        float new_x = 1 + static_cast<float>(rand()) /
+                static_cast<float>(RAND_MAX / (6.6 - 1 - 1));
+        float new_y = 1;
+        float new_h = M_PI / 2;
+        return new RRTNode(new_x, new_y, new_h);
+}
+
 int main()
 {
-#ifdef USE_GL
+        srand(static_cast<unsigned>(time(0)));
+#if USE_GL > 0
         init();
 #endif
 
@@ -93,36 +138,84 @@ int main()
         std::cin >> jvi;
         std::string encoding = jvi.get("encoding", "UTF-8" ).asString();
 
+        if (jvi["init"] == Json::nullValue) {
+                std::cerr << "I need `init` in JSON input scenario";
+                std::cerr << std::endl;
+                return 1;
+        }
+
+        if (jvi["goal"] == Json::nullValue) {
+                std::cerr << "I need `goal` in JSON input scenario";
+                std::cerr << std::endl;
+                return 1;
+        }
+
         PLANNER p(
-                        new RRTNode(
-                                jvi["init"][0].asFloat(),
-                                jvi["init"][1].asFloat(),
-                                jvi["init"][2].asFloat()),
-                        new RRTNode(
-                                jvi["goal"][0].asFloat(),
-                                jvi["goal"][1].asFloat(),
-                                jvi["goal"][2].asFloat()));
+                new RRTNode(
+                        jvi["init"][0].asFloat(),
+                        jvi["init"][1].asFloat(),
+                        jvi["init"][2].asFloat()
+                ),
+                new RRTNode(
+                        jvi["goal"][0].asFloat(),
+                        jvi["goal"][1].asFloat(),
+                        jvi["goal"][2].asFloat()
+                )
+        );
+        jvo["init"][0] = p.root()->x();
+        jvo["init"][1] = p.root()->y();
+        jvo["init"][2] = p.root()->h();
+
+        if (jvi["goals"] != Json::nullValue) {
+                for (auto g: jvi["goals"]) {
+                        p.goals().push_back(new RRTNode(
+                                g[0].asFloat(),
+                                g[1].asFloat(),
+                                g[2].asFloat()
+                        ));
+                }
+        }
+
         std::vector<CircleObstacle> co;
         std::vector<SegmentObstacle> so;
+        p.HMIN = p.root()->x();
+        p.HMAX = p.root()->x();
+        p.VMIN = p.root()->y();
+        p.VMAX = p.root()->y();
+        j = 0;
         for (auto o: jvi["obst"]) {
-                if (o["circle"] != Json::nullValue) {
-                        co.push_back(CircleObstacle(
-                                                o["circle"][0].asFloat(),
-                                                o["circle"][1].asFloat(),
-                                                o["circle"][2].asFloat()));
-                }
-                if (o["segment"] != Json::nullValue) {
+                float tmpx;
+                float tmpy;
+                float tmpr;
+                float tmps;
+                for (i = 0; i < o.size() - 1; i++) {
+                        tmpx = o[i][0].asFloat();
+                        tmpy = o[i][1].asFloat();
+                        tmpr = o[i + 1][0].asFloat();
+                        tmps = o[i + 1][1].asFloat();
                         so.push_back(SegmentObstacle(
-                                new RRTNode(
-                                        o["segment"][0][0].asFloat(),
-                                        o["segment"][0][1].asFloat(),
-                                        0),
-                                new RRTNode(
-                                        o["segment"][1][0].asFloat(),
-                                        o["segment"][1][1].asFloat(),
-                                        0)));
+                                new RRTNode(tmpx, tmpy, 0),
+                                new RRTNode(tmpr, tmps, 0)
+                        ));
+                        p.frame().add_bnode(so.back().init());
+                        if (tmpx < p.HMIN) p.HMIN = tmpx;
+                        if (tmpx > p.HMAX) p.HMAX = tmpx;
+                        if (tmpy < p.VMIN) p.VMIN = tmpy;
+                        if (tmpy > p.VMAX) p.VMAX = tmpy;
+                        if (tmpr < p.HMIN) p.HMIN = tmpr;
+                        if (tmpr > p.HMAX) p.HMAX = tmpr;
+                        if (tmps < p.VMIN) p.VMIN = tmps;
+                        if (tmps > p.VMAX) p.VMAX = tmps;
+
+                        // output
+                        jvo["obst"][j][i][0] = tmpx;
+                        jvo["obst"][j][i][1] = tmpy;
                 }
+                jvo["obst"][j][i][0] = tmpr;
+                jvo["obst"][j][i][1] = tmps;
+                j++;
         }
+        p.defaultSamplingInfo();
         p.link_obstacles(&co, &so);
         p.ocost(p.root());
         p.ocost(p.goal());
@@ -172,8 +265,9 @@ int main()
         while (!p.goal_found() && p.elapsed() < TMAX) {
                 p.next();
                 p.tend();
-                if (p.opt_path())
+                if (p.opt_path()) {
                         p.tlog(p.findt());
+                }
         }
 #elif defined USE_PTHREAD
         bool gf = false;
@@ -197,38 +291,65 @@ int main()
         p.p_goal_.tstart();
         pthread_create(&rt, NULL, &next_run, (void *) &ra);
         pthread_create(&gt, NULL, &next_run, (void *) &ga);
-        volatile int nofrn = 0;
-        volatile int nofgn = 0;
-        RRTNode *rn = nullptr;
-        RRTNode *gn = nullptr;
-        while (!gf && p.elapsed() < TMAX) {
+        int tol = 0;
+        int ndl = 0;
+        bool ndone = true;
+        while (!gf && p.elapsed() < TMAX &&
+                        p.p_root_.nodes().size() < NOFNODES &&
+                        p.p_goal_.nodes().size() < NOFNODES) {
                 // overlap trees
-                nofrn = p.p_root_.nodes().size() - 1;  // TODO workaround
-                nofgn = p.p_goal_.nodes().size() - 1;
-                for (int i = 0; i < nofrn; i++) {
-                        rn = p.p_root_.nodes()[i];
-                        if (rn->parent() == nullptr)
-                                continue;
-                        for (int j = 0; j < nofgn; j++) {
-                                gn = p.p_goal_.nodes()[j];
-                                if (gn->parent() == nullptr)
-                                        continue;
-                                if (rn->ccost() + gn->ccost() < mc &&
-                                                IS_NEAR(rn, gn)) {
-                                        gf = true;
-                                        ron = rn;
-                                        gon = gn;
-                                        mc = rn->ccost() + gn->ccost();
-                                }
+                ndone = true;
+                for (int i = 0; i < IXSIZE; i++) {
+                for (int j = 0; j < IYSIZE; j++) {
+                        if (p.p_root_.ixy_[i][j].changed() &&
+                                        p.p_goal_.ixy_[i][j].changed()) {
+ndone = false;
+for (auto rn: p.p_root_.ixy_[i][j].nodes()) {
+for (auto gn: p.p_goal_.ixy_[i][j].nodes()) {
+        if (rn->ccost() + gn->ccost() < mc &&
+                        IS_NEAR(rn, gn)) {
+                gf = true;
+                p.goal_found(true);
+                ron = rn;
+                gon = gn;
+                mc = rn->ccost() + gn->ccost();
+        }
+}}
                         }
-                }
+                        tol++;
+                        if (ndone)
+                                ndl++;
+                        p.tend();
+                        if (p.elapsed() >= TMAX)
+                                goto escapeloop;
+                }}
                 // end of overlap trees
                 p.tend();
         }
+escapeloop:
         pthread_join(rt, NULL);
         pthread_join(gt, NULL);
-        p.goal_found(gf);
+        float nodo = ((float) ndl / (float) tol);
+        std::cerr << "nothing done is " << 100.0 * nodo;
+        std::cerr << "%" << std::endl;
+        //std::cerr << "rgf is " << p.p_root_.goal_found() << std::endl;
+        //std::cerr << "ggf is " << p.p_goal_.goal_found() << std::endl;
+        //std::cerr << "cgf is " << p.goal_found() << std::endl;
+        if (p.p_root_.goal_found() && p.p_root_.goal()->ccost() < mc) {
+                ron = p.p_root_.goal()->parent();
+                gon = p.p_root_.goal();
+                mc = p.p_root_.goal()->ccost();
+        }
+        if (p.p_goal_.goal_found() && p.p_goal_.goal()->ccost() < mc) {
+                ron = p.p_goal_.goal();
+                gon = p.p_goal_.goal()->parent();
+                mc = p.p_goal_.goal()->ccost();
+        }
+        p.root()->remove_parent();  // needed if p.p_goal_.goal_found()
+        p.root()->ccost(0);
+        p.goal()->children().clear();
         // connect trees
+        if (gf) {
         while (gon != p.goal()) {
                 p.p_root_.nodes().push_back(new RRTNode(
                                 gon->x(),
@@ -243,17 +364,22 @@ int main()
                 gon = gon->parent();
         }
         ron->add_child(p.goal(), p.p_root_.cost(ron, p.goal()));
+        }
         // end of connect trees
         if (gf)
                 p.tlog(p.findt());
         if (p.opt_path())
                 p.tlog(p.findt());
 #endif
+        TEND();
+        TPRINT("RRT");
+        jvo["rrte"] = ELAPSED;
 #ifdef JSONLOGEDGES
         p.logr(p.root());
 #endif
 
         // statistics to error output
+        std::cerr << "TELAPSED is " << TELAPSED << std::endl;
         std::cerr << "Elapsed is " << p.elapsed() << std::endl;
         std::cerr << "Goal found is " << p.goal_found() << std::endl;
         std::cerr << "#nodes is " << p.nodes().size() << std::endl;
@@ -274,7 +400,10 @@ int main()
                 std::cerr << "- " << edges.size() << std::endl;
 
         // JSON output
-        jvo["elap"] = p.elapsed();
+        jvo["elap"] = TELAPSED;
+#ifdef USE_PTHREAD
+        jvo["nodo"][0] = nodo;
+#endif
         // log cost
         for (j = 0; j < p.clog().size(); j++)
                 jvo["cost"][j] = p.clog()[j];
@@ -297,6 +426,15 @@ int main()
                         jvo["traj"][j][i][4] = n->s();
                         i++;
                 }
+                if (j == p.tlog().size() - 1) {
+                        i = 0;
+                        for (auto n: traj) {
+                                jvo["path"][i][0] = n->x();
+                                jvo["path"][i][1] = n->y();
+                                jvo["path"][i][2] = n->h();
+                                i++;
+                        }
+                }
                 j++;
         }
 #ifdef JSONLOGEDGES
@@ -329,7 +467,7 @@ int main()
         // print output
         std::cout << jvo << std::endl;
 
-#ifdef USE_GL
+#if USE_GL > 0
         SDL_DestroyWindow(gw);
         SDL_Quit();
 #endif
@@ -342,6 +480,7 @@ int main()
         return 0;
 }
 
+#if USE_GL > 0
 bool init()
 {
         if (SDL_Init(SDL_INIT_VIDEO) < 0) {
@@ -417,3 +556,4 @@ bool initGL()
         }
         return true;
 }
+#endif