2 * SPDX-FileCopyrightText: 2021 Jiri Vlasak <jiri.vlasak.2@cvut.cz>
4 * SPDX-License-Identifier: GPL-3.0-only
7 /*! \brief RRT* structure and default procedures.
16 #include <json/json.h>
24 /*! Compute elapsed time class. */
27 std::chrono::high_resolution_clock::time_point _tstart;
33 /*! Store RRT node. */
34 class RRTNode : public virtual Pose, public virtual CarMove {
38 RRTNode* _p = nullptr;
39 unsigned int _cusp_cnt = 0;
40 int _segment_type = 0; // 0 ~ straight, 1 ~ left, -1 right
41 bool _p_is_cusp = false;
43 /*! Get cost to parent. */
46 /*! Set cost to parent. */
49 /*! Get cumulative cost from root. */
52 /*! Get parent node. */
55 /*! Set parent node. */
58 /*! Get number of backward-forward direction changes.
60 * The parent node may be cusp to this node, i.e. parent may have sp ==
61 * 0 or sgn(sp) differs from sgn(this->sp). In such a situation, the
62 * number of this->cusp_cnt is incremented by one.
64 unsigned int cusp_cnt() const;
66 /*! Set number of backward-forward direction changes. */
67 void cusp_cnt(RRTNode const& p);
69 /*! \brief Get Reeds & Shepp segment type.
73 * - 1 for turning left,
74 * - and -1 for turning right.
78 /*! Set Reeds & Shepp segment type. */
81 /*! Return true if the parent is cusp node and false otherwise. */
82 bool p_is_cusp(void) const;
84 /*! Set if the parent node is cusp (direction changed from parent). */
85 void p_is_cusp(bool isit);
87 bool operator==(RRTNode const& n);
90 class RRTGoal : public virtual RRTNode, public virtual PoseRange {
92 using PoseRange::PoseRange;
95 /*! RRT* algorithm basic class. */
100 unsigned int _icnt = 0;
101 unsigned int _icnt_max = 1000;
103 std::default_random_engine _gen;
104 std::vector<RRTNode> _nodes;
105 std::vector<RRTNode> _steered;
106 std::vector<RRTNode*> _path;
107 RRTNode* _nn = nullptr;
108 std::vector<RRTNode*> _nv;
112 std::vector<std::vector<RRTNode>> _logged_paths;
114 double min_gamma_eta(void) const;
115 bool should_continue(void) const;
116 void recompute_cc_for_predecessors_and(RRTNode* g);
117 void recompute_path_cc();
118 void join_steered(RRTNode* f);
121 bool goal_drivable_from(RRTNode const& f);
123 virtual void store(RRTNode n);
124 virtual double cost_build(RRTNode const& f, RRTNode const& t) const;
125 virtual double cost_search(RRTNode const& f, RRTNode const& t) const;
126 virtual void find_nn(RRTNode const& t);
127 virtual void find_nv(RRTNode const& t);
128 virtual void compute_path();
130 /*! \brief Return nodes from f to t inclusive.
132 * The "inclusive" means that f is at the same pose (x, y, h) as
133 * this->_steered.front() and t is at the same pose (x, y, h) as
134 * this->_steered.back().
136 virtual void steer(RRTNode const& f, RRTNode const& t) = 0;
137 virtual bool collide_steered() = 0;
138 virtual RRTNode sample() = 0;
139 virtual bool should_finish() const = 0;
143 /*! Set pose of the bicycle car used in the planner. */
144 void set_bc_pose_to(Pose const& p);
146 /*! Set bicycle car dimensions. */
147 void set_bc_to_become(std::string what);
150 RRTGoal const& goal(void) const;
153 void goal(double x, double y, double b, double e);
155 /*! Get number of iterations. */
156 unsigned int icnt(void) const;
158 /*! Set number of iterations. */
159 void icnt(unsigned int i);
161 /*! Get maximum number of iterations before reset. */
162 unsigned int icnt_max(void) const;
164 /*! Set maximum number of iterations before reset. */
165 void icnt_max(unsigned int i);
167 /*! Start elapsed time counter. */
170 /*! Return elapsed time. */
173 /*! Set init pose. */
174 void set_init_pose_to(Pose const& p);
177 std::vector<Pose> path() const;
179 /*! Get path cost. */
180 double path_cost() const;
182 /*! Get cost of the last path. */
183 double last_path_cost(void) const;
185 /*! Get eta, the RRT* constant used in near vertices and steering. */
191 /*! Generate JSON output. */
192 virtual Json::Value json(void) const;
194 /*! Load JSON input. */
195 virtual void json(Json::Value jvi);
197 /*! Run next RRT* iteration. Return True if should continue. */
200 /*! Reset the algorithm. */
201 virtual void reset();
205 #endif /* RRTS_RRTS_H */