Skip to main content

sampling_planning/
kinodynamic.rs

1//! Kinodynamic planners: SST, SST* and KPIECE.
2//!
3//! Kinodynamic planners cannot steer exactly between two states. They grow a tree by forward
4//! simulation instead: a [`Propagator`] applies a control for some time. Edges are therefore
5//! trajectories, solutions are [`Trajectory`] values with the controls that produce them, and
6//! the cost is the total control duration.
7
8mod kpiece;
9mod sst;
10
11use alloc::vec::Vec;
12use core::fmt;
13
14use rand_core::Rng;
15
16pub use kpiece::{Kpiece, KpieceConfig, Projection};
17pub use sst::{Sst, SstConfig, SstStarSchedule};
18
19use crate::{GoalRegion, StateSpace, Validity};
20
21/// Forward-simulates a system: the kinodynamic counterpart of [`Steering`](crate::Steering).
22pub trait Propagator<Sp: StateSpace> {
23    /// A control input, for example a velocity or an acceleration vector.
24    type Control: Clone;
25
26    /// The number of control dimensions; feeds the SST* epoch schedule.
27    fn control_dimension(&self) -> usize;
28
29    /// Draws a random admissible control.
30    fn sample_control<R: Rng + ?Sized>(&mut self, rng: &mut R) -> Self::Control;
31
32    /// The state reached from `from` by applying `control` for `duration` time units.
33    ///
34    /// Planners call this with short durations (one integration step) and check every step
35    /// with [`ValidityChecker::check_motion`](crate::ValidityChecker::check_motion), so it must
36    /// be deterministic: the same arguments always yield the same state.
37    fn propagate(
38        &self,
39        space: &Sp,
40        from: &Sp::State,
41        control: &Self::Control,
42        duration: f64,
43    ) -> Sp::State;
44}
45
46/// Everything a kinodynamic planner needs to know about the world.
47#[derive(Debug, Clone)]
48pub struct KinodynamicProblem<Sp: StateSpace, Sa, C, P> {
49    /// The state space, also used as the metric for neighbor queries and the goal region.
50    pub space: Sp,
51    /// Draws the states the planner grows toward.
52    pub sampler: Sa,
53    /// Decides which states and motions are allowed.
54    pub checker: C,
55    /// Simulates the system.
56    pub propagator: P,
57    /// The initial state. Must be [`Validity::Valid`].
58    pub start: Sp::State,
59    /// Where the trajectory has to end.
60    pub goal: GoalRegion<Sp::State>,
61}
62
63/// A solution trajectory: states and the controls that connect them.
64#[derive(Debug, Clone, PartialEq)]
65pub struct Trajectory<S, U> {
66    /// The states from the start to a state in the goal region. Never empty.
67    pub states: Vec<S>,
68    /// `controls[i]` is the control and its duration that take `states[i]` to `states[i + 1]`.
69    /// One shorter than `states`.
70    pub controls: Vec<(U, f64)>,
71    /// The total duration of all controls.
72    pub cost: f64,
73}
74
75/// Why a kinodynamic planner could not be created.
76#[derive(Debug, Clone, Copy, PartialEq, Eq)]
77pub enum Error {
78    /// The start state did not check as [`Validity::Valid`]; carries the actual result.
79    InvalidStart(Validity),
80    /// The goal bias is not in `[0, 1]`.
81    GoalBiasOutOfRange,
82    /// A radius or KPIECE cell size is not positive and finite.
83    InvalidRadius,
84    /// The integration step is not positive and finite, or the step count is zero.
85    InvalidPropagation,
86    /// The SST* shrink factor is not in `(0, 1)` or the first epoch is empty; or a KPIECE score
87    /// factor is not in `(0, 1]` or its exterior bias not in `[0, 1]`.
88    InvalidSchedule,
89}
90
91impl fmt::Display for Error {
92    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93        match self {
94            Self::InvalidStart(validity) => write!(f, "start state is not valid ({validity:?})"),
95            Self::GoalBiasOutOfRange => f.write_str("goal bias must be in [0, 1]"),
96            Self::InvalidRadius => f.write_str("radii and cell sizes must be positive and finite"),
97            Self::InvalidPropagation => {
98                f.write_str("step size must be positive and finite, step count at least one")
99            }
100            Self::InvalidSchedule => f.write_str(
101                "shrink factor must be in (0, 1) and first epoch at least one iteration, \
102                     or score factors in (0, 1] and exterior bias in [0, 1]",
103            ),
104        }
105    }
106}
107
108impl core::error::Error for Error {}