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 = 0;
40 int _segment_type = 0; // 0 ~ straight, 1 ~ left, -1 right
42 /*! Get cost to parent. */
45 /*! Set cost to parent. */
48 /*! Get cumulative cost from root. */
51 /*! Get parent node. */
54 /*! Set parent node. */
57 /*! Get number of backward-forward direction changes. */
58 unsigned int cusp() const;
60 /*! Set number of backward-forward direction changes. */
61 void cusp(RRTNode const& p);
63 /*! \brief Get Reeds & Shepp segment type.
67 * - 1 for turning left,
68 * - and -1 for turning right.
72 /*! Set Reeds & Shepp segment type. */
75 bool operator==(RRTNode const& n);
78 class RRTGoal : public virtual RRTNode, public virtual PoseRange {
80 using PoseRange::PoseRange;
83 /*! RRT* algorithm basic class. */
88 unsigned int _icnt = 0;
89 unsigned int _icnt_max = 1000;
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;
100 std::vector<std::vector<RRTNode>> _logged_paths;
102 double min_gamma_eta(void) const;
103 bool should_continue(void) const;
104 void recompute_cc_for_predecessors_and(RRTNode* g);
105 void recompute_path_cc();
106 void join_steered(RRTNode* f);
109 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();
118 virtual void steer(RRTNode const& f, RRTNode const& t) = 0;
119 virtual bool collide_steered() = 0;
120 virtual RRTNode sample() = 0;
121 virtual bool should_finish() const = 0;
125 /*! Set pose of the bicycle car used in the planner. */
126 void set_bc_pose_to(Pose const& p);
129 RRTGoal const& goal(void) const;
132 void goal(double x, double y, double b, double e);
134 /*! Get number of iterations. */
135 unsigned int icnt(void) const;
137 /*! Set number of iterations. */
138 void icnt(unsigned int i);
140 /*! Get maximum number of iterations before reset. */
141 unsigned int icnt_max(void) const;
143 /*! Set maximum number of iterations before reset. */
144 void icnt_max(unsigned int i);
146 /*! Start elapsed time counter. */
149 /*! Return elapsed time. */
152 /*! Set init pose. */
153 void set_init_pose_to(Pose const& p);
156 std::vector<Pose> path() const;
158 /*! Get path cost. */
159 double path_cost() const;
161 /*! Get cost of the last path. */
162 double last_path_cost(void) const;
164 /*! Get eta, the RRT* constant used in near vertices and steering. */
170 /*! Generate JSON output. */
171 virtual Json::Value json(void) const;
173 /*! Load JSON input. */
174 virtual void json(Json::Value jvi);
176 /*! Run next RRT* iteration. Return True if should continue. */
179 /*! Reset the algorithm. */
180 virtual void reset();
184 #endif /* RRTS_RRTS_H */