bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
rrt.h
Go to the documentation of this file.
1#ifndef BITSIM_PLANNING_RRT_H
2#define BITSIM_PLANNING_RRT_H
3
5
6#include <array>
7#include <functional>
8#include <random>
9#include <vector>
10
11namespace bitsim
12{
13namespace planning
14{
15
30class RRT
31{
32 public:
33 using Point = std::array<real_t, 2>;
34
38 using FreeTest = std::function<bool(const Point &)>;
39
48
53 struct Node
54 {
57 };
58
66 RRT(const Point &lower, const Point &upper, FreeTest is_free, const Parameters &parameters,
67 uint_t seed = 42);
68
72 bool plan(const Point &start, const Point &goal);
73
77 const std::vector<Node> &tree() const noexcept { return tree_; }
78
83 std::vector<Point> path() const;
84
88 uint_t n_iterations() const noexcept { return n_iterations_; }
89
93 bool is_segment_free(const Point &a, const Point &b) const;
94
100 std::vector<Point> shortcut(const std::vector<Point> &path) const;
101
105 static real_t length(const std::vector<Point> &path);
106
107 private:
108 uint_t nearest_(const Point &p) const;
109
110 Point lower_;
111 Point upper_;
112 FreeTest is_free_;
113 Parameters p_;
114 std::mt19937 rng_;
115 std::vector<Node> tree_;
116 uint_t goal_node_{0};
117 bool reached_{false};
118 uint_t n_iterations_{0};
119};
120
121} // namespace planning
122} // namespace bitsim
123
124#endif // BITSIM_PLANNING_RRT_H
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
bool is_segment_free(const Point &a, const Point &b) const
Whether the straight segment from a to b is free, checked every collision_step.
Definition rrt.cpp:100
std::vector< Point > shortcut(const std::vector< Point > &path) const
A shorter path through the same free space: from each point of the path, go straight to the furthest ...
Definition rrt.cpp:115
RRT(const Point &lower, const Point &upper, FreeTest is_free, const Parameters &parameters, uint_t seed=42)
Definition rrt.cpp:20
static real_t length(const std::vector< Point > &path)
The length of a path [m].
Definition rrt.cpp:136
bool plan(const Point &start, const Point &goal)
Grow a tree from start to goal. Returns whether it reached the goal.
Definition rrt.cpp:35
std::array< real_t, 2 > Point
Definition rrt.h:33
std::vector< Point > path() const
The path of the last plan() from the start to the goal, both included; empty if the goal was not reac...
Definition rrt.cpp:81
std::function< bool(const Point &)> FreeTest
Whether a point is free of obstacles.
Definition rrt.h:38
uint_t n_iterations() const noexcept
The iterations the last plan() took.
Definition rrt.h:88
const std::vector< Node > & tree() const noexcept
The tree of the last plan().
Definition rrt.h:77
Definition a_star_search.cpp:8
Definition dc_motor_backend_base.h:9
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20
A node of the tree: where it is, and the index of its parent; the root is its own parent.
Definition rrt.h:54
uint_t parent
Definition rrt.h:56
Point p
Definition rrt.h:55
real_t goal_radius
close enough to the goal [m]
Definition rrt.h:44
real_t collision_step
how finely a branch is checked [m]
Definition rrt.h:46
real_t step
the longest branch [m]
Definition rrt.h:42
real_t goal_bias
the probability of growing towards the goal
Definition rrt.h:43
uint_t max_iterations
before giving up
Definition rrt.h:45