feat: dai hanh lang theo route graph (Voronoi) cho goal xa, anytime ARA* dung ReplanParams, smoother path

- Anytime dung nghia (pha 0): replan() qua ReplanParams thay vi replan(double) - nghiem dau o
  initial_epsilon, ha dan eps_step toi final_epsilon trong repair_time; return_first_solution
  de bench. eps=1.0 (A* thuan) tren map rong gan chac chan het gio.
- Route graph corridor: nhan robot_sensor_ports::SensorConsumer, doc RouteGraph tu kho (host
  bien dich Voronoi tu map, day qua addRouteGraph), doi chieu map_hash voi static layer song.
  Goal >= min_range_m + co graph -> gan start/goal (BFS), Dijkstra ra tuyen, dong dau mask quanh
  tuyen; o NGOAI mask nhan cost inscribed (hard) hoac bi phat (soft) - search + heuristic 2D chi
  no trong dai (~3% map thay 100% tren sehc). Khong graph/lech/gan that bai = chay full nhu cu,
  log [debug/corridor] noi ly do; "route graph REJECTED" la LOI khong tat duoc. Nguong nghen ->
  noi rong (widen_factor, toi da max_widen) -> bo dai chay full voi thoi gian con lai.
- Path smoother (distance_map.h/.cpp, path_smoother.h/.cpp): keo day doan lattice di thang duoc
  tren costmap SONG (distance-transform giu khoang cach voi LETHAL, khong chi nhin cost), bo goc
  bang cung ban kinh co dinh khop bo mprim - giu nguyen doan lui (cusp). Do tren sehc: path
  lattice dai hon toi uu 2D 12%, 11 lan doi chieu re; smoother giam duoc phan lon.
- Log "metrics: ... detour= turn_deg= arcs= wiggles= plan_ms= corridor= corridor_ms= smooth=
  raw_m= smooth_ms= anchors= fillets=" moi lan plan thanh cong - so dai on/off, hard/soft, eps
  qua script tools/eval_plan_bag.py.
- Test moi: test/path_smoother_test.cpp (path_smoother la hinh hoc thuan, khong SBPL/costmap,
  chay duoc moi noi co gtest). CMakeLists them target catkin_add_gtest + nhanh standalone.
- Phu thuoc build moi: robot_sensor_ports, route_graph (repo rieng, da co remote).
- YAML: schema initial_epsilon/final_epsilon/eps_step/repair_time/return_first_solution,
  khoi voronoi_corridor (12 khoa) va smooth_path (7 khoa) trong cfg/sbpl_global_params.yaml
  (dong bo tu pnkx_nav_core c52cbe5 / move_base2 f4f9965).
This commit is contained in:
2026-09-22 15:51:26 +07:00
parent 34c74cb541
commit 3fff3b15cd
13 changed files with 2510 additions and 100 deletions

276
test/path_smoother_test.cpp Normal file
View File

@@ -0,0 +1,276 @@
// Unit tests for PathSmoother on synthetic worlds (no costmap, no SBPL).
#include <cmath>
#include <vector>
#include <gtest/gtest.h>
#include "sbpl_lattice_planner/distance_map.h"
#include "sbpl_lattice_planner/path_smoother.h"
using sbpl_lattice_planner::CellClass;
using sbpl_lattice_planner::PathSmoother;
using sbpl_lattice_planner::SmoothPose;
using sbpl_lattice_planner::SmootherConfig;
using sbpl_lattice_planner::SmootherStats;
namespace
{
struct Rect
{
double x0, y0, x1, y1;
bool contains(double x, double y, double margin) const
{
return x >= x0 - margin && x <= x1 + margin && y >= y0 - margin && y <= y1 + margin;
}
};
/// World of axis-aligned obstacles. Centre blocked within `inscribed` of a rectangle, footprint
/// check required within `circumscribed`; footprint = 1.17 x 0.96 m box (T800) tested by corners.
struct World
{
std::vector<Rect> obstacles;
double inscribed = 0.48;
double circumscribed = 0.76;
CellClass cell(double x, double y) const
{
CellClass c = CellClass::kFree;
for (const Rect& r : obstacles) {
if (r.contains(x, y, inscribed)) return CellClass::kBlocked;
if (r.contains(x, y, circumscribed)) c = CellClass::kNearObstacle;
}
return c;
}
bool pose(double x, double y, double th) const
{
const double hl = 0.583, hw = 0.48;
const double c = std::cos(th), s = std::sin(th);
const double cx[4] = {hl, hl, -hl, -hl};
const double cy[4] = {-hw, hw, hw, -hw};
for (int i = 0; i < 4; ++i) {
const double px = x + cx[i] * c - cy[i] * s;
const double py = y + cx[i] * s + cy[i] * c;
for (const Rect& r : obstacles)
if (r.contains(px, py, 0.0)) return false;
}
return true;
}
PathSmoother smoother(SmootherConfig cfg = SmootherConfig()) const
{
cfg.enabled = true;
return PathSmoother(cfg, [this](double x, double y) { return cell(x, y); },
[this](double x, double y, double th) { return pose(x, y, th); });
}
/// Every output pose must pass the same rule the smoother promised to honour.
void expectFree(const std::vector<SmoothPose>& p) const
{
for (const SmoothPose& q : p) {
const CellClass c = cell(q.x, q.y);
EXPECT_NE(c, CellClass::kBlocked) << "blocked at (" << q.x << "," << q.y << ")";
if (c == CellClass::kNearObstacle) {
EXPECT_TRUE(pose(q.x, q.y, q.theta)) << "footprint at (" << q.x << "," << q.y << ")";
}
}
}
};
/// Dense polyline through `v` at 5 cm, heading = segment direction (what the lattice gives).
std::vector<SmoothPose> dense(const std::vector<std::pair<double, double>>& v, double step = 0.05)
{
std::vector<SmoothPose> out;
for (std::size_t i = 1; i < v.size(); ++i) {
const double dx = v[i].first - v[i - 1].first, dy = v[i].second - v[i - 1].second;
const double len = std::hypot(dx, dy), th = std::atan2(dy, dx);
const int n = std::max(1, static_cast<int>(std::ceil(len / step)));
for (int s = 0; s < n; ++s)
out.push_back({v[i - 1].first + dx * s / n, v[i - 1].second + dy * s / n, th});
}
const auto& e = v.back();
out.push_back({e.first, e.second, out.empty() ? 0.0 : out.back().theta});
return out;
}
double maxStep(const std::vector<SmoothPose>& p)
{
double m = 0.0;
for (std::size_t i = 1; i < p.size(); ++i) m = std::max(m, std::hypot(p[i].x - p[i - 1].x, p[i].y - p[i - 1].y));
return m;
}
double maxHeadingJump(const std::vector<SmoothPose>& p)
{
double m = 0.0;
for (std::size_t i = 1; i < p.size(); ++i) m = std::max(m, std::fabs(std::remainder(p[i].theta - p[i - 1].theta, 2 * M_PI)));
return m;
}
} // namespace
TEST(PathSmoother, ZigzagInOpenSpaceBecomesOneSegment)
{
World w; // nothing in it
std::vector<std::pair<double, double>> v;
double x = 0, y = 0;
v.push_back({x, y});
for (int k = 0; k < 20; ++k) { // 0 / 22.5 deg alternating, 0.5 m legs — the lattice staircase
const double th = k % 2 ? 22.5 * M_PI / 180.0 : 0.0;
x += 0.5 * std::cos(th);
y += 0.5 * std::sin(th);
v.push_back({x, y});
}
auto in = dense(v);
in.back().theta = std::atan2(y, x); // goal yaw along the chord, so one heading end to end
std::vector<SmoothPose> out;
SmootherStats st;
ASSERT_TRUE(w.smoother().smooth(in, out, &st));
EXPECT_EQ(st.anchors, 2);
EXPECT_EQ(st.fillets, 0);
EXPECT_NEAR(st.out_len_m, std::hypot(x, y), 1e-6);
EXPECT_LT(st.out_len_m, st.in_len_m);
EXPECT_LE(maxStep(out), 0.05 + 1e-9);
EXPECT_LT(maxHeadingJump(out), 1e-9); // one heading all the way
EXPECT_NEAR(out.front().x, 0.0, 1e-9);
EXPECT_NEAR(out.back().x, x, 1e-9);
EXPECT_NEAR(out.back().y, y, 1e-9);
w.expectFree(out);
}
TEST(PathSmoother, WallForcesCornerWithTangentFillet)
{
World w;
w.obstacles.push_back({2.0, 1.0, 8.0, 9.0});
const auto in = dense({{0, 0}, {10, 0}, {10, 10}});
std::vector<SmoothPose> out;
SmootherStats st;
ASSERT_TRUE(w.smoother().smooth(in, out, &st));
EXPECT_EQ(st.anchors, 3);
EXPECT_EQ(st.fillets, 1);
EXPECT_EQ(st.fillets_rejected, 0);
EXPECT_LT(st.out_len_m, st.in_len_m);
// Tangent-continuous: no heading jump larger than one arc sample (step / R).
EXPECT_LT(maxHeadingJump(out), 0.05 / 1.06 + 1e-6);
EXPECT_LE(maxStep(out), 0.05 + 1e-6);
EXPECT_NEAR(out.back().x, 10.0, 1e-9);
EXPECT_NEAR(out.back().y, 10.0, 1e-9);
w.expectFree(out);
}
TEST(PathSmoother, FilletThatCollidesFallsBackToSharpCorner)
{
World w;
w.obstacles.push_back({2.0, 1.0, 8.0, 9.0});
w.obstacles.push_back({9.0, 0.7, 9.4, 1.2}); // post inside the corner the arc would cut
const auto in = dense({{0, 0}, {10, 0}, {10, 10}});
std::vector<SmoothPose> out;
SmootherStats st;
ASSERT_TRUE(w.smoother().smooth(in, out, &st));
EXPECT_EQ(st.fillets, 0);
EXPECT_GE(st.fillets_rejected, 1);
// Sharp corner = duplicate position carrying two headings (turn in place for the tracker).
bool dup = false;
for (std::size_t i = 1; i < out.size(); ++i)
if (std::hypot(out[i].x - out[i - 1].x, out[i].y - out[i - 1].y) < 1e-9 &&
std::fabs(std::remainder(out[i].theta - out[i - 1].theta, 2 * M_PI)) > 0.5)
dup = true;
EXPECT_TRUE(dup);
w.expectFree(out);
}
TEST(PathSmoother, ReverseMotionLeavesPathUntouched)
{
World w;
auto in = dense({{0, 0}, {5, 0}});
for (SmoothPose& p : in) p.theta = M_PI; // heading opposite to travel: driving backwards
std::vector<SmoothPose> out;
EXPECT_FALSE(w.smoother().smooth(in, out));
EXPECT_TRUE(out.empty());
}
TEST(PathSmoother, EndpointsAndGoalYawPreserved)
{
World w;
auto in = dense({{0, 0}, {3, 4}, {6, 0}});
in.back().theta = 1.234; // goal yaw
std::vector<SmoothPose> out;
ASSERT_TRUE(w.smoother().smooth(in, out));
EXPECT_NEAR(out.front().x, 0.0, 1e-9);
EXPECT_NEAR(out.front().y, 0.0, 1e-9);
EXPECT_NEAR(out.front().theta, 0.0, 1e-9); // straight to (6,0): open space, one segment
EXPECT_NEAR(out.back().x, 6.0, 1e-9);
EXPECT_NEAR(out.back().y, 0.0, 1e-9);
EXPECT_NEAR(out.back().theta, 1.234, 1e-9);
}
TEST(PathSmoother, FootprintCheckBlocksShortcutTheCentreWouldAllow)
{
// Narrow slot: centre fits (kNearObstacle) but the 0.96 m wide footprint does not when driving
// along y = 0 between two posts 0.7 m apart. The detour above them must survive.
World w;
w.inscribed = 0.3; // posts 0.7 m apart leave the centre line unblocked
w.circumscribed = 0.8;
w.obstacles.push_back({4.0, -2.0, 6.0, -0.35});
w.obstacles.push_back({4.0, 0.35, 6.0, 2.0});
const auto in = dense({{0, 0}, {3, 0}, {3, 3.5}, {7, 3.5}, {7, 0}, {10, 0}});
std::vector<SmoothPose> out;
SmootherStats st;
ASSERT_TRUE(w.smoother().smooth(in, out, &st));
EXPECT_GT(st.out_len_m, 10.0 + 1.0); // did not cut straight through the slot
EXPECT_LT(st.out_len_m, st.in_len_m); // but did shortcut the rectangular detour
w.expectFree(out);
}
TEST(PathSmoother, JunctionPrefersLatticeAlignedEndPoint)
{
// East leg (0,0)->(10,0), north leg (10,0)->(10,5) between a wall at x >= 11.2 and a block at
// x <= 9.3 above y = 2.5. From (0,0) the farthest visible point is (10, ~2.4) on the north leg,
// but that chord meets the north heading at ~77 deg. With the junction rule the smoother stops
// at (10,0) (chord along the east heading, then a fillet); with the rule off it takes the
// farthest visible point and the first output heading is the 13 deg chord.
World w;
w.obstacles.push_back({11.2, -1.0, 12.5, 3.0});
w.obstacles.push_back({8.0, 2.5, 9.3, 6.0});
const auto in = dense({{0, 0}, {10, 0}, {10, 5}});
SmootherConfig cfg;
cfg.max_junction_deg = 20.0;
std::vector<SmoothPose> out;
SmootherStats st;
ASSERT_TRUE(w.smoother(cfg).smooth(in, out, &st));
EXPECT_EQ(st.anchors, 3);
EXPECT_EQ(st.fillets, 1);
EXPECT_NEAR(out.front().theta, 0.0, 1e-6);
w.expectFree(out);
cfg.max_junction_deg = 0.0;
ASSERT_TRUE(w.smoother(cfg).smooth(in, out, &st));
EXPECT_GT(out.front().theta, 0.15); // chord straight to the north leg
w.expectFree(out);
}
TEST(DistanceMap, SquaredEuclideanToNearestObstacle)
{
// 7x5 grid, obstacles at (1,1) and (5,3): every cell must report the true squared distance.
const int w = 7, h = 5;
std::vector<uint8_t> obs(w * h, 0);
obs[1 * w + 1] = 1;
obs[3 * w + 5] = 1;
std::vector<float> d2;
sbpl_lattice_planner::squaredDistanceTransform(obs, w, h, d2);
ASSERT_EQ(d2.size(), static_cast<std::size_t>(w * h));
for (int y = 0; y < h; ++y)
for (int x = 0; x < w; ++x) {
const float a = static_cast<float>((x - 1) * (x - 1) + (y - 1) * (y - 1));
const float b = static_cast<float>((x - 5) * (x - 5) + (y - 3) * (y - 3));
EXPECT_FLOAT_EQ(d2[y * w + x], std::min(a, b)) << "at (" << x << "," << y << ")";
}
// No obstacle at all: everything far away.
std::fill(obs.begin(), obs.end(), 0);
sbpl_lattice_planner::squaredDistanceTransform(obs, w, h, d2);
for (float v : d2) EXPECT_GE(v, 1.0e6f);
}
int main(int argc, char** argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}