Skip to main content

sampling_planning/
lib.rs

1//! # Sampling-based motion planning
2//!
3//! Sampling-based motion planners for continuous configuration spaces: RRT, RRT*, RRT#,
4//! RRT-Connect, PRM, PRM*, SPARS, SPARS2, FMT*, BFMT*, BIT*, AIT*, SST, SST* and KPIECE.
5//! The crate is `#![no_std]` with `alloc`.
6//!
7//! This is a **motion planning** crate. It is named `sampling-planning` because the crate name `motion-planning` was already taken on crates.io.
8//!
9//! ## Building blocks
10//!
11//! A [`Problem`] bundles everything a planner needs:
12//!
13//! - a [`StateSpace`] with a metric and interpolation, for example [`Euclidean`], [`So2`],
14//!   [`Se2`], or the car-like [`Dubins`] and [`ReedsShepp`];
15//! - a [`Sampler`] that draws states, for example [`UniformBox`];
16//! - a [`ValidityChecker`] for states and motions, which answers with a three-valued
17//!   [`Validity`]. Planners accept only [`Validity::Valid`]; [`Validity::Unknown`] is rejected
18//!   like [`Validity::Invalid`];
19//! - a [`Steering`] function, for example [`StraightLine`];
20//! - a start state and a [`GoalRegion`].
21//!
22//! Planners keep their nodes in a [`NearestNeighbors`] structure, [`LinearScan`] by default;
23//! [`KdTree`] is much faster in low-dimensional Euclidean spaces and returns identical results.
24//! Randomness always comes from a caller-provided [`rand_core::Rng`], so a seeded RNG makes a
25//! run fully reproducible. Float math goes through `libm`, so results are also identical across
26//! platforms.
27//!
28//! ## Example
29//!
30//! ```
31//! use rand_core::SeedableRng;
32//! use sampling_planning::{
33//!     Euclidean, GoalRegion, Problem, StraightLine, UniformBox, Validity, ValidityChecker,
34//!     trees::{RrtStar, RrtStarConfig},
35//! };
36//!
37//! /// Free space everywhere.
38//! struct Free;
39//!
40//! impl ValidityChecker<Euclidean<2>> for Free {
41//!     fn check_state(&self, _: &[f64; 2]) -> Validity {
42//!         Validity::Valid
43//!     }
44//!
45//!     fn check_motion(&self, _: &Euclidean<2>, _: &[f64; 2], _: &[f64; 2]) -> Validity {
46//!         Validity::Valid
47//!     }
48//! }
49//!
50//! let problem = Problem {
51//!     space: Euclidean::<2>,
52//!     sampler: UniformBox::new([0.0, 0.0], [10.0, 10.0]),
53//!     checker: Free,
54//!     steering: StraightLine::new(1.0),
55//!     start: [1.0, 1.0],
56//!     goal: GoalRegion::new([9.0, 9.0], 0.5),
57//! };
58//!
59//! let mut rng = rand_pcg::Pcg64Mcg::seed_from_u64(7);
60//! let gamma = 1.5 * RrtStarConfig::gamma_lower_bound(2, 100.0);
61//! let mut planner = RrtStar::new(problem, RrtStarConfig::new(gamma, 1.0)).expect("valid problem");
62//! planner.run(2000, &mut rng);
63//!
64//! let path = planner.best_path().expect("free space has a path");
65//! assert!(path.cost < 12.0);
66//! ```
67//!
68//! ## Planners
69//!
70//! - [`trees`]: [`Rrt`](trees::Rrt), [`RrtStar`](trees::RrtStar) (radius or k-nearest,
71//!   optionally informed), [`RrtSharp`](trees::RrtSharp) and the bidirectional
72//!   [`RrtConnect`](trees::RrtConnect), all over a [`Problem`].
73//! - [`roadmaps`]: the multi-query [`Prm`](roadmaps::Prm) (PRM, PRM*, lazy PRM) and
74//!   the sparse roadmap spanners [`Spars`](roadmaps::Spars) and [`Spars2`](roadmaps::Spars2), and the batch
75//!   planners [`FmtStar`](roadmaps::FmtStar) and [`BfmtStar`](roadmaps::BfmtStar). They need no
76//!   steering function.
77//! - [`informed`]: the batch-informed [`BitStar`](informed::BitStar) and
78//!   [`AitStar`](informed::AitStar).
79//! - [`kinodynamic`]: [`Sst`](kinodynamic::Sst) (SST and SST*) and
80//!   [`Kpiece`](kinodynamic::Kpiece) over a [`Propagator`](kinodynamic::Propagator) that
81//!   forward-simulates controls.
82//!
83//! [`shortcut()`] and [`shortcut_random`] post-process any [`Path`].
84//!
85//! Not yet implemented: ABIT*, EIT*, multi-level KPIECE, and the SE(3) and compound state
86//! spaces.
87//!
88//! Repository: <https://github.com/sunsided/sampling-planning>
89
90#![no_std]
91#![forbid(unsafe_code)]
92
93extern crate alloc;
94
95mod goal;
96mod graph;
97pub mod informed;
98pub mod kinodynamic;
99mod nearest;
100mod problem;
101mod radius;
102pub mod roadmaps;
103mod sampler;
104mod shortcut;
105mod space;
106mod steering;
107pub mod trees;
108mod validity;
109
110pub use goal::GoalRegion;
111pub use nearest::{KdTree, LinearScan, NearestNeighbors, RemovableNeighbors};
112pub use problem::{Path, Problem};
113pub use rand_core;
114pub use sampler::{INFORMED_BOX_ATTEMPTS, ProlateHyperspheroid, Sampler, UniformBox, unit_f64};
115pub use shortcut::{shortcut, shortcut_random};
116pub use space::{
117    Dubins, DubinsPath, DubinsWord, Euclidean, ReedsShepp, ReedsSheppPath, Se2, Se2State, Segment,
118    SegmentKind, So2, StateSpace, UniformAngle, UniformSe2, angle_difference, wrap_angle,
119};
120pub use steering::{Steering, StraightLine};
121pub use validity::{Validity, ValidityChecker, discretized_motion};