Skip to content

Commit 7e0304b

Browse files
committed
Refactor alignment importance scaling
1 parent e02620d commit 7e0304b

5 files changed

Lines changed: 34 additions & 58 deletions

File tree

crates/step_planning/src/cost_fields/target_orientation.rs

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,17 @@ use crate::{
77

88
pub struct TargetOrientationField {
99
pub target_orientation: Angle<f32>,
10-
pub alignment_start_distance: f32,
11-
pub ramp_width: f32,
1210
}
1311

1412
impl TargetOrientationField {
15-
pub fn cost(&self, pose: Pose<f32>, progress: f32, path_length: f32) -> f32 {
16-
let distance_to_target = path_length - progress;
17-
13+
pub fn cost(&self, pose: Pose<f32>) -> f32 {
1814
angle_penalty(pose.orientation, self.target_orientation)
19-
* self.importance(distance_to_target)
2015
}
2116

22-
pub fn grad(&self, pose: Pose<f32>, progress: f32, path_length: f32) -> PoseGradient<f32> {
23-
let distance_to_target = path_length - progress;
24-
17+
pub fn grad(&self, pose: Pose<f32>) -> PoseGradient<f32> {
2518
PoseGradient {
2619
position: Vector2::zeros(),
27-
orientation: angle_penalty_derivative(pose.orientation, self.target_orientation)
28-
* self.importance(distance_to_target),
29-
}
30-
}
31-
}
32-
33-
impl TargetOrientationField {
34-
fn importance(&self, distance_to_target: f32) -> f32 {
35-
if distance_to_target > self.alignment_start_distance - self.ramp_width {
36-
0.0
37-
} else if distance_to_target < self.alignment_start_distance + self.ramp_width {
38-
1.0
39-
} else {
40-
(distance_to_target - (self.alignment_start_distance - self.ramp_width))
41-
/ (2.0 * self.ramp_width)
20+
orientation: angle_penalty_derivative(pose.orientation, self.target_orientation),
4221
}
4322
}
4423
}
@@ -49,25 +28,17 @@ mod tests {
4928

5029
use linear_algebra::point;
5130
use proptest::proptest;
52-
use types::planned_path::Path;
5331

5432
use crate::{
5533
cost_fields::target_orientation::TargetOrientationField,
5634
geometry::{angle::Angle, pose::Pose},
57-
test_utils::test_path,
58-
traits::{Length, PathProgress},
5935
};
6036

6137
proptest!(
6238
#[test]
6339
fn verify_gradient(x in -2.0f32..5.0, y in -2.0f32..5.0, orientation in 0.0..TAU) {
6440
let cost_field = TargetOrientationField {
6541
target_orientation: Angle(PI),
66-
alignment_start_distance: 1.0,
67-
ramp_width: 0.5,
68-
};
69-
let path = Path {
70-
segments: &test_path(),
7142
};
7243

7344
let position = point![x, y];
@@ -79,18 +50,8 @@ mod tests {
7950
};
8051

8152
crate::test_utils::verify_gradient::verify_gradient(
82-
&|p: Pose<f32>| {
83-
let progress = path.progress(p.position);
84-
let path_length = path.length();
85-
86-
cost_field.cost(p, progress, path_length)
87-
},
88-
&|p| {
89-
let progress = path.progress(p.position);
90-
let path_length = path.length();
91-
92-
cost_field.grad(p, progress, path_length)
93-
},
53+
&|p| cost_field.cost(p),
54+
&|p| cost_field.grad(p),
9455
0.05,
9556
pose,
9657
)

crates/step_planning/src/step_plan.rs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,20 @@ impl StepPlanning<'_> {
7171

7272
let progress = self.path.progress(pose.position);
7373
let path_length = self.path.length();
74+
let distance_to_target = path_length - progress;
75+
let target_alignment_importance = self.target_alignment_importance(distance_to_target);
76+
let walk_alignment_importance = 1.0 - target_alignment_importance;
7477

7578
let path_progress_cost =
7679
self.path_progress().cost(progress, path_length) * cost_factors.path_progress;
7780
let path_distance_cost =
7881
self.path_distance().cost(pose.position) * cost_factors.path_distance;
79-
let walk_orientation_cost =
80-
self.walk_orientation().cost(pose.clone()) * cost_factors.walk_orientation;
81-
let target_orientation_cost = self.target_orientation().cost(pose, progress, path_length)
82-
* cost_factors.target_orientation;
82+
let walk_orientation_cost = self.walk_orientation().cost(pose.clone())
83+
* cost_factors.walk_orientation
84+
* walk_alignment_importance;
85+
let target_orientation_cost = self.target_orientation().cost(pose)
86+
* cost_factors.target_orientation
87+
* target_alignment_importance;
8388

8489
path_progress_cost + path_distance_cost + walk_orientation_cost + target_orientation_cost
8590
}
@@ -90,16 +95,20 @@ impl StepPlanning<'_> {
9095
let progress = self.path.progress(pose.position);
9196
let forward = self.path.forward(pose.position);
9297
let path_length = self.path.length();
98+
let distance_to_target = path_length - progress;
99+
let target_alignment_importance = self.target_alignment_importance(distance_to_target);
100+
let walk_alignment_importance = 1.0 - target_alignment_importance;
93101

94102
let path_progress_gradient =
95103
self.path_progress().grad(progress, forward, path_length) * cost_factors.path_progress;
96104
let path_distance_gradient =
97105
self.path_distance().grad(pose.position) * cost_factors.path_distance;
98-
let walk_orientation_gradient =
99-
self.walk_orientation().grad(pose.clone()) * cost_factors.walk_orientation;
100-
let target_orientation_gradient =
101-
self.target_orientation().grad(pose, progress, path_length)
102-
* cost_factors.target_orientation;
106+
let walk_orientation_gradient = self.walk_orientation().grad(pose.clone())
107+
* cost_factors.walk_orientation
108+
* walk_alignment_importance;
109+
let target_orientation_gradient = self.target_orientation().grad(pose)
110+
* cost_factors.target_orientation
111+
* target_alignment_importance;
103112

104113
PoseGradient {
105114
position: path_progress_gradient + path_distance_gradient,
@@ -108,6 +117,14 @@ impl StepPlanning<'_> {
108117
+ target_orientation_gradient
109118
}
110119

120+
// https://www.desmos.com/calculator/mzuvbmrxym
121+
fn target_alignment_importance(&self, distance_to_target: f32) -> f32 {
122+
(1.0 - f32::tanh(
123+
(distance_to_target - self.parameters.alignment_start_distance)
124+
* self.parameters.alignment_ramp_steepness,
125+
)) / 2.0
126+
}
127+
111128
fn path_distance(&self) -> PathDistanceField<'_> {
112129
PathDistanceField { path: self.path }
113130
}
@@ -127,8 +144,6 @@ impl StepPlanning<'_> {
127144
fn target_orientation(&self) -> TargetOrientationField {
128145
TargetOrientationField {
129146
target_orientation: Angle(self.target_orientation.angle()),
130-
alignment_start_distance: self.parameters.alignment_start_distance,
131-
ramp_width: self.parameters.alignment_start_smoothness,
132147
}
133148
}
134149
}

crates/step_planning_solver/benches/mpc_step_planning.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ fn plan_steps(path: Path) -> Vec<Step> {
3535
},
3636
path_progress_smoothness: 0.05,
3737
alignment_start_distance: 0.1,
38-
alignment_start_smoothness: 0.05,
38+
alignment_ramp_steepness: 50.0,
3939
};
4040

4141
let (step_plan, _, _) = step_planning_solver::plan_steps(

crates/types/src/parameters.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,5 +475,5 @@ pub struct StepPlanningOptimizationParameters {
475475
pub cost_factors: StepPlanningCostFactors,
476476
pub path_progress_smoothness: f32,
477477
pub alignment_start_distance: f32,
478-
pub alignment_start_smoothness: f32,
478+
pub alignment_ramp_steepness: f32,
479479
}

etc/parameters/default.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@
406406
},
407407
"path_progress_smoothness": 0.05,
408408
"alignment_start_distance": 0.1,
409-
"alignment_start_smoothness": 0.05
409+
"alignment_ramp_steepness": 50
410410
}
411411
},
412412
"zero_moment_point": {

0 commit comments

Comments
 (0)