feat: add a back-off pivot as the last-resort dock maneuver, with optional costmap checks

A differential robot that stood closer to the dock than the safe pivot point had no
maneuver at all. Direct entry lacked curve room, the single cusp exceeded
maximum_forward_extension, and straight-pivot-straight requires the vertex between the
robot and the dock. Two charger dockings on the KIT robot failed this way (start
0.579/0.292 m and 0.427/0.155 m).

- New shape backoff_pivot_straight, differential only. It is tried only after every
  existing shape failed. The robot drives straight along its current heading (no rotation
  at the start) until it is past the pivot vertex, then follows the existing
  straight-pivot-straight geometry. Its feasibility does not depend on the turning radius.
- maneuver/pivot_backoff_max_distance [m]: longest allowed back-off; 0 disables the shape.
- maneuver/pivot_check_costmap: rejects a pivot shape when a rotation sweep (circumscribed
  radius + pivot_safety_margin) or an approach-leg footprint covers a LETHAL cell. The
  terminal into the dock is not checked: it ends in contact with the dock by design.
- Defaults keep every existing plan unchanged. Tests cover the two robot failures, unchanged
  plans for the existing shapes, ackermann, the gates and the costmap callbacks.
This commit is contained in:
2026-10-29 15:17:24 +07:00
parent 1f04a95e1b
commit 7c969d037b
9 changed files with 1008 additions and 49 deletions

View File

@@ -50,6 +50,35 @@ bool near(double first, double second, double tolerance = 1e-8)
return std::fabs(first - second) <= tolerance;
}
bool samePoseExactly(const robot_geometry_msgs::Pose& first,
const robot_geometry_msgs::Pose& second)
{
return first.position.x == second.position.x &&
first.position.y == second.position.y &&
first.position.z == second.position.z &&
first.orientation.x == second.orientation.x &&
first.orientation.y == second.orientation.y &&
first.orientation.z == second.orientation.z &&
first.orientation.w == second.orientation.w;
}
bool pathsEqualExactly(const std::vector<robot_geometry_msgs::Pose>& first,
const std::vector<robot_geometry_msgs::Pose>& second)
{
if (first.size() != second.size())
{
return false;
}
for (std::size_t index = 0; index < first.size(); ++index)
{
if (!samePoseExactly(first[index], second[index]))
{
return false;
}
}
return true;
}
double normalizeAngle(double angle)
{
return std::remainder(angle, 2.0 * kPi);
@@ -347,6 +376,8 @@ void testConfig()
CHECK(config.validate(error));
CHECK(error.empty());
CHECK(std::isnan(config.minimumTurningRadius()));
CHECK(near(config.pivot_backoff_max_distance, 0.0));
CHECK(!config.pivot_check_costmap);
config.wheel_base = 1.3268;
CHECK(near(config.minimumTurningRadius(),
@@ -369,6 +400,18 @@ void testConfig()
config = dock_planner::DockPlannerConfig{};
config.pivot_safety_margin = -0.01;
CHECK(!config.validate(error));
config = dock_planner::DockPlannerConfig{};
config.pivot_backoff_max_distance = -0.01;
CHECK(!config.validate(error));
config = dock_planner::DockPlannerConfig{};
config.pivot_backoff_max_distance = 3.01;
CHECK(!config.validate(error));
config = dock_planner::DockPlannerConfig{};
config.pivot_backoff_max_distance = std::numeric_limits<double>::infinity();
CHECK(!config.validate(error));
config = dock_planner::DockPlannerConfig{};
config.pivot_backoff_max_distance = 3.0;
CHECK(config.validate(error));
// A differential base has no steered axle, so empty axle frames and a zero
// steering angle must NOT stop it loading -- validating them there would
@@ -407,6 +450,375 @@ std::vector<robot_geometry_msgs::Point> apmDockFootprint()
point(-0.50, 0.40), point(-0.50, -0.40)};
}
std::vector<robot_geometry_msgs::Point> robotFootprint065x069()
{
return {point(0.325, -0.345), point(0.325, 0.345),
point(-0.325, 0.345), point(-0.325, -0.345)};
}
dock_planner::DockPlannerConfig differentialBackoffConfig()
{
dock_planner::DockPlannerConfig config;
config.kinematic_model = dock_planner::KinematicModel::kDifferential;
config.minimum_turning_radius = 1.0;
config.maximum_forward_extension = 0.5;
config.direct_entry_min_curve_room = 0.3;
config.pivot_fallback_extension = 1.2;
config.pivot_safety_margin = 0.15;
config.straight_length = 0.4;
config.path_point_spacing = 0.05;
return config;
}
void checkBackoffPivotPath(const std::vector<robot_geometry_msgs::Pose>& path,
const robot_geometry_msgs::PoseStamped& start,
const robot_geometry_msgs::PoseStamped& goal,
double pivot_vertex_distance)
{
CHECK(path.size() > 12);
CHECK(samePoseExactly(path.front(), start.pose));
CHECK(samePoseExactly(path.back(), goal.pose));
const double start_yaw = data_convert::getYaw(start.pose.orientation);
std::size_t first_rotation_begin = path.size();
for (std::size_t index = 1; index < path.size(); ++index)
{
const bool same_position =
std::hypot(path[index].position.x - path[index - 1].position.x,
path[index].position.y - path[index - 1].position.y) <= 1e-9;
const double yaw_change = normalizeAngle(
data_convert::getYaw(path[index].orientation) -
data_convert::getYaw(path[index - 1].orientation));
if (same_position && std::fabs(yaw_change) > 1e-6)
{
first_rotation_begin = index - 1;
break;
}
}
CHECK(first_rotation_begin > 0);
CHECK(first_rotation_begin < path.size());
if (first_rotation_begin >= path.size())
{
return;
}
double previous_longitudinal = longitudinal(path.front(), goal.pose);
for (std::size_t index = 0; index <= first_rotation_begin; ++index)
{
CHECK(std::fabs(normalizeAngle(
data_convert::getYaw(path[index].orientation) - start_yaw)) < 1e-7);
const double current_longitudinal = longitudinal(path[index], goal.pose);
CHECK(current_longitudinal + 1e-8 >= previous_longitudinal);
previous_longitudinal = current_longitudinal;
}
CHECK(longitudinal(path[first_rotation_begin], goal.pose) >=
pivot_vertex_distance + 0.05 - 1e-7);
int rotation_blocks = 0;
bool in_rotation = false;
std::size_t last_rotation_end = 0;
for (std::size_t index = 1; index < path.size(); ++index)
{
const bool same_position =
std::hypot(path[index].position.x - path[index - 1].position.x,
path[index].position.y - path[index - 1].position.y) <= 1e-9;
const double yaw_change = normalizeAngle(
data_convert::getYaw(path[index].orientation) -
data_convert::getYaw(path[index - 1].orientation));
if (same_position && std::fabs(yaw_change) > 1e-6)
{
if (!in_rotation)
{
++rotation_blocks;
}
in_rotation = true;
last_rotation_end = index;
}
else if (!same_position)
{
in_rotation = false;
}
}
CHECK(rotation_blocks >= 2);
CHECK(last_rotation_end + 1 < path.size());
for (std::size_t index = last_rotation_end; index + 1 < path.size(); ++index)
{
CHECK(!edgeIsForward(path[index], path[index + 1]));
CHECK(std::fabs(lateral(path[index], goal.pose)) < 1e-7);
CHECK(std::fabs(normalizeAngle(
data_convert::getYaw(path[index].orientation) -
data_convert::getYaw(goal.pose.orientation))) < 1e-7);
}
}
void testBackoffPivotRobotFailures()
{
const auto robot_footprint = robotFootprint065x069();
const std::vector<robot_geometry_msgs::Point> no_dock_footprint;
const auto goal = pose(0.0, 0.0, 0.0);
const double pivot_vertex = std::hypot(0.325, 0.345) + 0.15;
const std::vector<robot_geometry_msgs::PoseStamped> starts = {
pose(0.579, 0.292, 0.0),
pose(0.427, 0.155, 0.0),
};
for (const auto& start : starts)
{
auto config = differentialBackoffConfig();
dock_planner::DockCalc disabled(config);
std::vector<robot_geometry_msgs::Pose> failed_path;
CHECK(!disabled.makePlanMoveToDock(start, goal, robot_footprint,
no_dock_footprint, failed_path));
CHECK(failed_path.empty());
config.pivot_backoff_max_distance = 1.0;
dock_planner::DockCalc enabled(config);
std::vector<robot_geometry_msgs::Pose> path;
CHECK(enabled.makePlanMoveToDock(start, goal, robot_footprint,
no_dock_footprint, path));
checkBackoffPivotPath(path, start, goal, pivot_vertex);
}
}
void testExistingPlansUnchangedByBackoff()
{
const auto robot_footprint = apmFootprint();
const auto dock_footprint = apmDockFootprint();
const auto goal = pose(0.0, 0.0, 0.0);
struct Case
{
dock_planner::DockPlannerConfig config;
robot_geometry_msgs::PoseStamped start;
};
std::vector<Case> cases;
auto cusp_config = apmConfig();
cases.push_back({cusp_config, pose(2.8, 0.6, -1.2)});
cases.push_back({cusp_config, pose(-2.8, 0.6, 0.0)});
auto direct_config = apmConfig();
direct_config.direct_entry_min_curve_room = 0.5;
cases.push_back({direct_config, pose(2.8, 0.6, -1.2)});
cases.push_back({direct_config, pose(-4.0, 0.6, 0.0)});
auto pivot_config = apmConfig();
pivot_config.pivot_fallback_extension = 1.0;
cases.push_back({pivot_config, pose(2.8, 0.6, -1.2)});
cases.push_back({pivot_config, pose(-2.8, 0.6, 0.0)});
for (const auto& test_case : cases)
{
auto baseline_config = test_case.config;
baseline_config.pivot_backoff_max_distance = 0.0;
baseline_config.pivot_check_costmap = false;
auto candidate_config = baseline_config;
candidate_config.pivot_backoff_max_distance = 1.0;
dock_planner::DockCalc baseline(baseline_config);
dock_planner::DockCalc candidate(candidate_config);
std::vector<robot_geometry_msgs::Pose> baseline_path;
std::vector<robot_geometry_msgs::Pose> candidate_path;
CHECK(baseline.makePlanMoveToDock(test_case.start, goal, robot_footprint,
dock_footprint, baseline_path));
CHECK(candidate.makePlanMoveToDock(test_case.start, goal, robot_footprint,
dock_footprint, candidate_path));
// A key-zero plan is the pre-feature behavior: the fallback is only
// reached after every old ladder candidate fails. Exact field equality
// catches even changes to sampling, quaternion signs, or endpoints.
CHECK(pathsEqualExactly(baseline_path, candidate_path));
}
}
void testBackoffPivotGates()
{
const auto robot_footprint = robotFootprint065x069();
const std::vector<robot_geometry_msgs::Point> no_dock_footprint;
const auto goal = pose(0.0, 0.0, 0.0);
auto config = differentialBackoffConfig();
config.pivot_backoff_max_distance = 1.0;
dock_planner::DockCalc perpendicular(config);
std::vector<robot_geometry_msgs::Pose> path;
CHECK(!perpendicular.makePlanMoveToDock(
pose(0.579, 0.292, kPi / 2.0), goal, robot_footprint,
no_dock_footprint, path));
CHECK(path.empty());
config.pivot_backoff_max_distance = 0.3;
dock_planner::DockCalc too_far(config);
CHECK(!too_far.makePlanMoveToDock(
pose(0.1, 0.4, 0.0), goal, robot_footprint,
no_dock_footprint, path));
CHECK(path.empty());
auto ackermann_config = config;
ackermann_config.kinematic_model = dock_planner::KinematicModel::kAckermann;
ackermann_config.wheel_base = std::tan(0.5);
ackermann_config.max_steering_angle = 0.5;
dock_planner::DockCalc ackermann(ackermann_config);
CHECK(!ackermann.makePlanMoveToDock(
pose(0.579, 0.292, 0.0), goal, robot_footprint,
no_dock_footprint, path));
CHECK(path.empty());
}
void testCostmapCheckCallbacks()
{
const auto robot_footprint = robotFootprint065x069();
const std::vector<robot_geometry_msgs::Point> no_dock_footprint;
const auto goal = pose(0.0, 0.0, 0.0);
const auto start = pose(0.579, 0.292, 0.0);
auto config = differentialBackoffConfig();
config.pivot_backoff_max_distance = 1.0;
config.pivot_check_costmap = true;
const double expected_rotation_radius =
std::hypot(0.325, 0.345) + config.pivot_safety_margin;
int rotation_calls = 0;
dock_planner::DockCalc rotation_blocked(config);
rotation_blocked.setCostmapCheckCallbacks(
[&rotation_calls, expected_rotation_radius](
const robot_geometry_msgs::Pose&, double radius)
{
++rotation_calls;
CHECK(near(radius, expected_rotation_radius));
return false;
},
[](const robot_geometry_msgs::Pose&) { return true; });
std::vector<robot_geometry_msgs::Pose> path;
CHECK(!rotation_blocked.makePlanMoveToDock(
start, goal, robot_footprint, no_dock_footprint, path));
CHECK(path.empty());
CHECK(rotation_calls > 0);
int footprint_calls = 0;
dock_planner::DockCalc footprint_blocked(config);
footprint_blocked.setCostmapCheckCallbacks(
[](const robot_geometry_msgs::Pose&, double) { return true; },
[&footprint_calls](const robot_geometry_msgs::Pose& sample)
{
++footprint_calls;
return !(sample.position.x > 0.60 && sample.position.x < 0.67 &&
sample.position.y > 0.25 && sample.position.y < 0.33);
});
CHECK(!footprint_blocked.makePlanMoveToDock(
start, goal, robot_footprint, no_dock_footprint, path));
CHECK(path.empty());
CHECK(footprint_calls > 0);
int start_pose_checks = 0;
dock_planner::DockCalc start_pose_skipped(config);
start_pose_skipped.setCostmapCheckCallbacks(
[](const robot_geometry_msgs::Pose&, double) { return true; },
[&start_pose_checks, &start](const robot_geometry_msgs::Pose& sample)
{
if (near(sample.position.x, start.pose.position.x) &&
near(sample.position.y, start.pose.position.y))
{
++start_pose_checks;
return false;
}
return true;
});
CHECK(start_pose_skipped.makePlanMoveToDock(
start, goal, robot_footprint, no_dock_footprint, path));
CHECK(start_pose_checks == 0);
// A blocked old pivot falls through to the old cusp when that cusp remains
// feasible, proving the check also gates straight_pivot_straight.
auto old_pivot_config = apmConfig();
old_pivot_config.wheel_base = std::tan(0.5);
old_pivot_config.max_steering_angle = 0.5;
old_pivot_config.maximum_forward_extension = 3.0;
old_pivot_config.pivot_fallback_extension = 0.5;
old_pivot_config.pivot_check_costmap = true;
dock_planner::DockCalc old_pivot(old_pivot_config);
old_pivot.setCostmapCheckCallbacks(
[](const robot_geometry_msgs::Pose&, double) { return false; },
[](const robot_geometry_msgs::Pose&) { return true; });
const auto old_pivot_path = generatePath(
old_pivot, pose(2.8, 0.6, -1.2), goal, apmFootprint(),
apmDockFootprint());
checkSingleCuspPath(old_pivot_path, goal.pose, 1.0, false);
// A lethal cell in the terminal corridor is expected at the charger/dock
// interface. Pivot approach legs and rotation remain clear, so the old
// pivot plan must stay exactly the same as with the check switched off.
auto terminal_pivot_config = apmConfig();
terminal_pivot_config.kinematic_model =
dock_planner::KinematicModel::kDifferential;
terminal_pivot_config.minimum_turning_radius = 0.0;
const auto terminal_pivot_start = pose(2.8, 0.6, -1.2);
dock_planner::DockCalc terminal_pivot_unchecked(terminal_pivot_config);
std::vector<robot_geometry_msgs::Pose> terminal_pivot_baseline;
CHECK(terminal_pivot_unchecked.makePlanMoveToDock(
terminal_pivot_start, goal, apmFootprint(), apmDockFootprint(),
terminal_pivot_baseline));
checkPivotPath(terminal_pivot_baseline, goal.pose, false,
std::hypot(1.65, 0.40) + 0.50 +
terminal_pivot_config.pivot_safety_margin);
terminal_pivot_config.pivot_check_costmap = true;
const double terminal_pivot_distance = std::max(
0.35 + 0.50 + terminal_pivot_config.reverse_distance_padding,
terminal_pivot_config.straight_length);
dock_planner::DockCalc terminal_pivot_checked(terminal_pivot_config);
terminal_pivot_checked.setCostmapCheckCallbacks(
[](const robot_geometry_msgs::Pose&, double) { return true; },
[&goal, terminal_pivot_distance](const robot_geometry_msgs::Pose& sample)
{
return std::hypot(sample.position.x - goal.pose.position.x,
sample.position.y - goal.pose.position.y) >
terminal_pivot_distance;
});
std::vector<robot_geometry_msgs::Pose> terminal_pivot_path;
CHECK(terminal_pivot_checked.makePlanMoveToDock(
terminal_pivot_start, goal, apmFootprint(), apmDockFootprint(),
terminal_pivot_path));
checkPivotPath(terminal_pivot_path, goal.pose, false,
std::hypot(1.65, 0.40) + 0.50 +
terminal_pivot_config.pivot_safety_margin);
CHECK(pathsEqualExactly(terminal_pivot_baseline, terminal_pivot_path));
auto check_off_config = differentialBackoffConfig();
check_off_config.pivot_backoff_max_distance = 1.0;
dock_planner::DockCalc no_callbacks(check_off_config);
dock_planner::DockCalc ignored_callbacks(check_off_config);
ignored_callbacks.setCostmapCheckCallbacks(
[](const robot_geometry_msgs::Pose&, double) { return false; },
[](const robot_geometry_msgs::Pose&) { return false; });
std::vector<robot_geometry_msgs::Pose> no_callback_path;
std::vector<robot_geometry_msgs::Pose> ignored_callback_path;
CHECK(no_callbacks.makePlanMoveToDock(start, goal, robot_footprint,
no_dock_footprint, no_callback_path));
CHECK(ignored_callbacks.makePlanMoveToDock(
start, goal, robot_footprint, no_dock_footprint,
ignored_callback_path));
CHECK(pathsEqualExactly(no_callback_path, ignored_callback_path));
// The back-off fallback also ignores cells in the terminal corridor while
// still checking its rotation and both approach legs.
dock_planner::DockCalc terminal_corridor_blocked(config);
const double backoff_terminal_distance = config.straight_length;
terminal_corridor_blocked.setCostmapCheckCallbacks(
[](const robot_geometry_msgs::Pose&, double) { return true; },
[&goal, backoff_terminal_distance](const robot_geometry_msgs::Pose& sample)
{
return std::hypot(sample.position.x - goal.pose.position.x,
sample.position.y - goal.pose.position.y) >
backoff_terminal_distance;
});
std::vector<robot_geometry_msgs::Pose> terminal_corridor_backoff_path;
CHECK(terminal_corridor_blocked.makePlanMoveToDock(
start, goal, robot_footprint, no_dock_footprint,
terminal_corridor_backoff_path));
checkBackoffPivotPath(terminal_corridor_backoff_path, start, goal,
std::hypot(0.325, 0.345) + config.pivot_safety_margin);
CHECK(pathsEqualExactly(ignored_callback_path,
terminal_corridor_backoff_path));
}
/**
* Where the rotation has to happen for the footprints above. The swept circle
* is the binding constraint, not the mating extent: hypot(1.65, 0.40) is
@@ -759,6 +1171,10 @@ void testDockAnchor()
int main()
{
testConfig();
testBackoffPivotRobotFailures();
testExistingPlansUnchangedByBackoff();
testBackoffPivotGates();
testCostmapCheckCallbacks();
testPathGeneration();
testDirectEntry();
testPivotFallback();