]> rtime.felk.cvut.cz Git - hubacji1/iamcar2.git/blob - rrts/incl/rrts.hh
Refactor rrt node
[hubacji1/iamcar2.git] / rrts / incl / rrts.hh
1 /*
2  * SPDX-FileCopyrightText: 2021 Jiri Vlasak <jiri.vlasak.2@cvut.cz>
3  *
4  * SPDX-License-Identifier: GPL-3.0-only
5  */
6
7 /*! \brief RRT* structure and default procedures.
8  *
9  * \file
10  */
11 #ifndef RRTS_RRTS_H
12 #define RRTS_RRTS_H
13
14 #include <chrono>
15 #include <functional>
16 #include <json/json.h>
17 #include <random>
18 #include <vector>
19 #include "bcar.hh"
20
21 namespace rrts {
22 using namespace bcar;
23
24 /*! Compute elapsed time class. */
25 class Ter {
26 private:
27         std::chrono::high_resolution_clock::time_point _tstart;
28 public:
29         void start();
30         double scnt() const;
31 };
32
33 /*! Store RRT node. */
34 class RRTNode : public virtual Pose, public virtual CarMove {
35 private:
36         double _c = 0.0;
37         double _cc = 0.0;
38         RRTNode* _p;
39         unsigned int _cusp = 0;
40         int _segment_type = 0;  // 0 ~ straight, 1 ~ left, -1 right
41 public:
42         /*! Get cost to parent. */
43         double c() const;
44
45         /*! Set cost to parent. */
46         void c(double c);
47
48         /*! Get cumulative cost from root. */
49         double cc() const;
50
51         /*! Get parent node. */
52         RRTNode* p() const;
53
54         /*! Set parent node. */
55         void p(RRTNode& p);
56
57         /*! Get number of backward-forward direction changes. */
58         unsigned int cusp() const;
59
60         /*! Set number of backward-forward direction changes. */
61         void cusp(RRTNode const& p);
62
63         /*! \brief Get Reeds & Shepp segment type.
64          *
65          * Segment type is:
66          * - 0 for straight,
67          * - 1 for turning left,
68          * - and -1 for turning right.
69          */
70         int st(void);
71
72         /*! Set Reeds & Shepp segment type. */
73         void st(int st);
74
75         bool operator==(RRTNode const& n);
76 };
77
78 class RRTGoal : public virtual RRTNode, public virtual PoseRange {
79 public:
80         using PoseRange::PoseRange;
81 };
82
83 /*! RRT* algorithm basic class. */
84 class RRTS {
85 protected:
86         BicycleCar bc_;
87         RRTGoal goal_;
88         unsigned int icnt_ = 0;
89         unsigned int _imax = 1000;
90         Ter ter_;
91         std::default_random_engine gen_;
92         std::vector<RRTNode> nodes_;
93         std::vector<RRTNode> steered_;
94         std::vector<RRTNode*> path_;
95         RRTNode* nn_ = nullptr;
96         std::vector<RRTNode*> nv_;
97         double cost_ = 0.0;
98         double eta_ = 0.5;
99         double time_ = 0.0;
100         double last_goal_cc_ = 0.0;
101         std::vector<RRTNode> last_path_;
102         void recompute_cc(RRTNode* g);
103         void recompute_path_cc();
104         double min_gamma_eta() const;
105         bool should_continue() const;
106         void join_steered(RRTNode* f);
107         RRTNode& nn();
108         bool connect();
109         void rewire();
110         bool goal_drivable_from(RRTNode const& f);
111         virtual void store(RRTNode n);
112         virtual double cost_build(RRTNode const& f, RRTNode const& t) const;
113         virtual double cost_search(RRTNode const& f, RRTNode const& t) const;
114         virtual void find_nn(RRTNode const& t);
115         virtual void find_nv(RRTNode const& t);
116         virtual void compute_path();
117         virtual void steer(RRTNode const& f, RRTNode const& t) = 0;
118         virtual bool collide_steered() = 0;
119         virtual RRTNode sample() = 0;
120         virtual bool should_finish() const = 0;
121 public:
122         RRTS();
123
124         /*! Set internal bicycle car. */
125         BicycleCar &bc();
126
127         /*! Set maximum number of iterations before reset. */
128         void set_imax_reset(unsigned int i);
129
130         /*! Set goal. */
131         void set_goal(double x, double y, double b, double e);
132
133         /*! Set start. */
134         void set_start(double x, double y, double h);
135
136         /*! Get path. */
137         std::vector<Pose> get_path() const;
138
139         /*! Get path cost. */
140         double get_path_cost() const;
141
142         /*! Get iterations counter. */
143         unsigned int icnt() const;
144
145         /*! Set iterations counter. */
146         void icnt(unsigned int i);
147
148         /*! Return elapsed time. */
149         double scnt() const;
150
151         double eta() const;
152         void eta(double e);
153
154         /*! Generate JSON output. */
155         Json::Value json() const;
156
157         /*! Load JSON input. */
158         void json(Json::Value jvi);
159
160         /*! Run next RRT* iteration. */
161         virtual bool next();
162
163         /*! Reset the algorithm. */
164         virtual void reset();
165 };
166
167 } // namespace rrts
168 #endif /* RRTS_RRTS_H */