robust_planning/steer.rs
1//! Steering: how a planner picks the control for one edge.
2//!
3//! Steering is split from [`Reach`] so that the same reachability model can be driven by
4//! different steering functions. A model that knows how to steer itself implements both
5//! traits. A model that only propagates sets is paired with a steering function through
6//! [`WithSteering`].
7
8use polyzonotope::{Polytope, SparsePolyZonotope};
9use sampling_planning::{Euclidean, Steering, StraightLine};
10
11use crate::Reach;
12
13/// Picks the control of type `C` that moves a nominal state toward a target.
14///
15/// Planners never trust steering for safety: every control is propagated by a [`Reach`]
16/// model and checked against the obstacles, so a poor steering function costs progress, not
17/// soundness.
18pub trait Steer<const N: usize, C> {
19 /// Picks the control that moves the nominal state `from` toward `toward`.
20 ///
21 /// Returns the control and the nominal end state, which may stop short of `toward`.
22 fn steer(&self, from: &[f64; N], toward: &[f64; N]) -> (C, [f64; N]);
23}
24
25/// Steers toward the target in a straight line and hands the end point to the model as its
26/// control.
27///
28/// Fits models whose control is a setpoint, that is, models whose [`Reach::Control`] is the
29/// nominal end state `[f64; N]` and which track it with their own controller. The end point
30/// is `toward` itself when it is at most [`StraightLine::max_step`] away, so edges toward
31/// near targets end exactly at the target.
32impl<const N: usize> Steer<N, [f64; N]> for StraightLine {
33 fn steer(&self, from: &[f64; N], toward: &[f64; N]) -> ([f64; N], [f64; N]) {
34 let end = Steering::steer(self, &Euclidean::<N>, from, toward);
35
36 (end, end)
37 }
38}
39
40/// A reachability model paired with a separate steering function.
41///
42/// Implements [`Reach`] by delegating to [`WithSteering::reach`] and [`Steer`] by delegating
43/// to [`WithSteering::steering`], so it can be used wherever a planner expects a model that
44/// steers itself.
45///
46/// # Examples
47///
48/// ```
49/// use robust_planning::{Reach, Steer, WithSteering};
50/// use robust_planning::polyzonotope::SparsePolyZonotope;
51/// use robust_planning::sampling_planning::StraightLine;
52///
53/// /// A model whose control is the nominal end state.
54/// struct Setpoint;
55///
56/// impl Reach<2> for Setpoint {
57/// type Control = [f64; 2];
58///
59/// fn propagate(
60/// &self,
61/// initial: &SparsePolyZonotope,
62/// _: &[f64; 2],
63/// cover: &mut Vec<SparsePolyZonotope>,
64/// ) -> SparsePolyZonotope {
65/// cover.push(initial.clone());
66/// initial.clone()
67/// }
68/// }
69///
70/// let model = WithSteering::new(Setpoint, StraightLine::new(1.0));
71/// let (control, end) = model.steer(&[0.0, 0.0], &[3.0, 4.0]);
72///
73/// assert_eq!(control, end);
74/// assert!((end[0] - 0.6).abs() < 1e-12 && (end[1] - 0.8).abs() < 1e-12);
75/// ```
76#[derive(Debug, Clone, PartialEq)]
77pub struct WithSteering<R, S> {
78 /// The model that propagates reachable sets.
79 pub reach: R,
80 /// The steering function that picks each edge's control.
81 pub steering: S,
82}
83
84impl<R, S> WithSteering<R, S> {
85 /// Pairs `reach` with `steering`.
86 #[must_use]
87 pub const fn new(reach: R, steering: S) -> Self {
88 Self { reach, steering }
89 }
90}
91
92impl<R: Reach<N>, S, const N: usize> Reach<N> for WithSteering<R, S> {
93 /// The control of the wrapped model.
94 type Control = R::Control;
95
96 fn propagate(
97 &self,
98 initial: &SparsePolyZonotope,
99 control: &Self::Control,
100 cover: &mut Vec<SparsePolyZonotope>,
101 ) -> SparsePolyZonotope {
102 self.reach.propagate(initial, control, cover)
103 }
104
105 /// Forwards to the wrapped model, so pairing a model with a steering function keeps its
106 /// collision certificates.
107 fn certify_collision(
108 &self,
109 initial: &SparsePolyZonotope,
110 control: &Self::Control,
111 set: &SparsePolyZonotope,
112 obstacle: &Polytope,
113 ) -> Option<Vec<f64>> {
114 self.reach
115 .certify_collision(initial, control, set, obstacle)
116 }
117}
118
119impl<R, S: Steer<N, C>, C, const N: usize> Steer<N, C> for WithSteering<R, S> {
120 fn steer(&self, from: &[f64; N], toward: &[f64; N]) -> (C, [f64; N]) {
121 self.steering.steer(from, toward)
122 }
123}
124
125#[cfg(test)]
126mod tests {
127 use super::*;
128
129 #[test]
130 fn straight_line_reaches_near_targets_exactly_and_clamps_far_ones() {
131 let steering = StraightLine::new(1.0);
132
133 assert_eq!(
134 Steer::steer(&steering, &[0.0, 0.0], &[0.6, 0.8]),
135 ([0.6, 0.8], [0.6, 0.8])
136 );
137
138 let (control, end) = Steer::steer(&steering, &[0.0, 0.0], &[30.0, 40.0]);
139 assert_eq!(control, end);
140 assert!((end[0] - 0.6).abs() < 1e-12 && (end[1] - 0.8).abs() < 1e-12);
141 }
142}