use nalgebra::{DMatrix, DVector};
use polyzonotope::{Interval, IntervalBox, Polytope, SparsePolyZonotope, Zonotope};
use polyzonotope_reach::{NonlinearSettings, Polynomial};
use rand_pcg::Pcg64Mcg;
use robust_planning::{
Obstacles, PolytopeGoal, Steer, WithSteering,
models::Nonlinear,
robust_rrt::{Config, Problem, RobustRrt},
};
use sampling_planning::{UniformBox, rand_core::SeedableRng};
const CURRENT: f64 = 0.02;
const STEP: f64 = 0.25;
const INPUT_ERROR: f64 = 0.005;
struct Compensate {
speed: f64,
max_steps: u32,
}
impl Steer<2, Vec<Zonotope>> for Compensate {
fn steer(&self, from: &[f64; 2], toward: &[f64; 2]) -> (Vec<Zonotope>, [f64; 2]) {
let (dx, dy) = (toward[0] - from[0], toward[1] - from[1]);
let distance = dx.hypot(dy);
let steps = steps_for(distance / (self.speed * STEP), self.max_steps);
let duration = f64::from(steps) * STEP;
let scale = (self.speed * duration / distance).min(1.0);
let end = [from[0] + dx * scale, from[1] + dy * scale];
let velocity = [(end[0] - from[0]) / duration, (end[1] - from[1]) / duration];
let inputs = (0..steps)
.map(|k| {
let a = from[0] + velocity[0] * STEP * f64::from(k);
let b = a + velocity[0] * STEP;
let push = CURRENT * (a * a + a * b + b * b) / 3.0;
input_set([velocity[0], velocity[1] - push])
})
.collect();
(inputs, end)
}
}
fn steps_for(ratio: f64, max_steps: u32) -> u32 {
(1..=max_steps)
.find(|&k| f64::from(k) >= ratio)
.unwrap_or(max_steps)
}
fn input_set(u: [f64; 2]) -> Zonotope {
Zonotope::new(
DVector::from_column_slice(&u),
DMatrix::from_diagonal_element(2, 2, INPUT_ERROR),
)
}
fn current() -> Polynomial {
Polynomial::new(2, 2)
.term(0, 1.0, &[0, 0, 1, 0])
.term(1, 1.0, &[0, 0, 0, 1])
.term(1, CURRENT, &[2, 0, 0, 0])
}
fn rect(x0: f64, x1: f64, y0: f64, y1: f64) -> IntervalBox {
IntervalBox(vec![Interval::new(x0, x1), Interval::new(y0, y1)])
}
fn main() {
let walls = [rect(4.0, 5.0, 0.0, 7.0), rect(6.0, 7.0, 3.0, 10.0)];
let start = [1.0, 1.0];
let start_box = rect(0.98, 1.02, 0.98, 1.02);
let model = Nonlinear::new(current(), NonlinearSettings::new(STEP));
let steering = Compensate {
speed: 1.0,
max_steps: 4,
};
let problem = Problem {
reach: WithSteering::new(model, steering),
obstacles: Obstacles::new(walls.iter().map(Polytope::from_box).collect(), 4),
sampler: UniformBox::new([0.0, 0.0], [10.0, 10.0]),
start,
start_set: SparsePolyZonotope::from_zonotope(&Zonotope::from_box(&start_box)),
goal: PolytopeGoal::from_box(&rect(8.0, 10.0, 8.0, 10.0), 8),
};
let mut planner = RobustRrt::new(problem, Config::default()).expect("the start set is safe");
let path = planner.solve(3000, &mut Pcg64Mcg::seed_from_u64(7));
println!("{} nodes", planner.nodes().len());
let Some(path) = path else {
println!("no proven-safe path found");
return;
};
println!("path of nominal length {:.2}:", path.cost);
let goal = planner.goal_node().expect("a path ends at the goal node");
for id in planner.branch(goal) {
let node = &planner.nodes()[id];
let hull = node.set.interval_hull();
println!(
" ({:5.2}, {:5.2}) set {:.3} x {:.3}",
node.nominal[0],
node.nominal[1],
hull.0[0].width(),
hull.0[1].width()
);
}
}