/
hold.rs
82 lines (73 loc) · 2.59 KB
/
hold.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* Copyright (C) 2022 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
use crate::{
node::Informed,
expander::traits::{Expander, Targeted, Goal},
motion::{
Duration, Waypoint, Trajectory,
trajectory::CostCalculator,
movable::{Movable, ArcMovable},
},
error::NoError,
};
use std::sync::Arc;
pub const DEFAULT_HOLD_SECS: i64 = 1;
pub struct Hold<W: Waypoint, C: CostCalculator<W>, N> {
cost_calculator: Arc<C>,
duration: Duration,
_ignore: std::marker::PhantomData<(W, N)>,
}
impl<W: Waypoint, C: CostCalculator<W>, N: Movable<W>> Hold<W, C, N> {
pub fn new(cost_calculator: Arc<C>) -> Self {
Self{
cost_calculator,
duration: Duration::from_secs(DEFAULT_HOLD_SECS),
_ignore: Default::default()
}
}
pub fn for_duration(mut self, duration: Duration) -> Result<Self, ()> {
if duration.nanos <= 0 {
return Err(());
}
self.duration = duration;
Ok(self)
}
}
impl<W: Waypoint, C: CostCalculator<W>, N> Expander for Hold<W, C, N> {
type Node = N;
}
impl<W: Waypoint, C: CostCalculator<W, Cost=N::Cost>, N: Informed + Movable<W>, G: Goal<N>> Targeted<G> for Hold<W, C, N> {
type TargetedError = NoError;
type TargetedExpansion<'a> = impl Iterator<Item=Result<Arc<N>, NoError>> where W: 'a, N: 'a, C: 'a, G: 'a ;
fn expand<'a>(
&'a self,
parent: &'a Arc<N>,
_: &'a G,
) -> Self::TargetedExpansion<'a> {
// SAFETY: The value check on for_duration makes sure that duration is
// always positive so the hold will always be valid.
let until_time = *parent.state().time() + self.duration;
let trajectory = Trajectory::hold(parent.state().clone(), until_time).unwrap();
let cost_from_parent = self.cost_calculator.compute_cost(&trajectory);
[Ok(parent.clone().moved_with(
parent.partial_key().cloned(),
cost_from_parent,
parent.remaining_cost_estimate(),
Some(trajectory),
))].into_iter()
}
}