feat(lane_route): add lane_route_core and lane_route_planner (R0 + R1)
lane_route_core plans on a precomputed coarse graph (Voronoi ridges and direction-zone lanes) and, when enabled, falls back to a weighted A* search over an SE(2) lattice that enforces forward motion, the minimum turn radius, end-only spins, the footprint gap and direction zones. - R0: route_planner.cpp split into graph search, path building and the graph tier; regress7 output identical to the previous build. - R1: lattice tier (lazy packed C-space tiles, direction-aware heuristic field, analytic Dubins and point connectors, end-gap floors counting unknown cells). Off by default (lattice_enabled = false). - lane_route_eval takes an optional tier mode and LANE_ROUTE_EVAL_SET overrides for offline tuning. Local snapshot for history only; not pushed.
This commit is contained in:
354
tools/lane_route_eval.cpp
Normal file
354
tools/lane_route_eval.cpp
Normal file
@@ -0,0 +1,354 @@
|
||||
/** Offline batch runner: plans scenes from text files and writes one JSON line per query, for comparison
|
||||
* with the Python prototype. Usage:
|
||||
* lane_route_eval <scenes.txt> <out.jsonl> <profile sehc|kit|hook_150> <deviations, e.g. 90,45> <ladder, e.g. 0.01 or none> [graph|fallback|lattice]
|
||||
* Each scenes.txt line: id grid_file variant live_file|- has_start_yaw sx sy syaw has_goal_yaw gx gy gyaw box_count {x0 y0 x1 y1}
|
||||
* Grid files are kept in memory, so scenes that share one map also share its graphs, as on the robot. */
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include <lane_route_core/grid_checks.h>
|
||||
#include <lane_route_core/route_planner.h>
|
||||
|
||||
#include <lane_route_core/costmap_backend.h>
|
||||
|
||||
using namespace lane_route_core;
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
using Clock = std::chrono::steady_clock;
|
||||
|
||||
double msSince(Clock::time_point begin) { return std::chrono::duration<double, std::milli>(Clock::now() - begin).count(); }
|
||||
|
||||
std::vector<uint8_t> readRuns(std::istream& in, const std::string& name)
|
||||
{
|
||||
std::string key;
|
||||
std::size_t runs = 0;
|
||||
in >> key >> runs;
|
||||
if (key != name)
|
||||
throw std::runtime_error("expected " + name + ", got " + key);
|
||||
std::vector<uint8_t> out;
|
||||
for (std::size_t i = 0; i < runs; ++i)
|
||||
{
|
||||
int value = 0;
|
||||
std::size_t count = 0;
|
||||
in >> value >> count;
|
||||
out.insert(out.end(), count, static_cast<uint8_t>(value));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** One map: compile grid, live grid, direction grids and the cached graphs per direction grid and limit. */
|
||||
struct MapData
|
||||
{
|
||||
int32_t width = 0, height = 0;
|
||||
double resolution = 0.0, origin_x = 0.0, origin_y = 0.0;
|
||||
std::vector<uint8_t> static_cost; // No inflation, as the static layer provides.
|
||||
std::vector<uint8_t> live_cost; // Master: with the inscribed ring and soft values.
|
||||
std::vector<std::vector<uint8_t>> directions;
|
||||
std::vector<uint16_t> clearance_mm;
|
||||
double step1_ms = 0.0;
|
||||
std::map<int, CoarseGraph> coarse;
|
||||
std::map<std::pair<int, double>, std::shared_ptr<const CompiledGraph>> graphs;
|
||||
};
|
||||
|
||||
std::shared_ptr<MapData> loadMap(const std::string& path, const robot_costmap_2d::ReferenceGraphParams& ridge_params)
|
||||
{
|
||||
std::ifstream in(path);
|
||||
if (!in)
|
||||
throw std::runtime_error("cannot open " + path);
|
||||
auto map = std::make_shared<MapData>();
|
||||
std::string key;
|
||||
int version = 0, variants = 0;
|
||||
in >> key >> version;
|
||||
in >> key >> map->width >> map->height;
|
||||
in >> key >> map->resolution;
|
||||
in >> key >> map->origin_x >> map->origin_y;
|
||||
map->live_cost = readRuns(in, "cost");
|
||||
in >> key >> variants;
|
||||
for (int i = 0; i < variants; ++i)
|
||||
map->directions.push_back(readRuns(in, "direction"));
|
||||
if (!in)
|
||||
throw std::runtime_error("truncated grid " + path);
|
||||
map->static_cost = map->live_cost;
|
||||
for (uint8_t& value : map->static_cost)
|
||||
if (value == kInscribedCost)
|
||||
value = kFreeSpace;
|
||||
|
||||
GridRef grid;
|
||||
grid.width = map->width;
|
||||
grid.height = map->height;
|
||||
grid.resolution = map->resolution;
|
||||
grid.origin_x = map->origin_x;
|
||||
grid.origin_y = map->origin_y;
|
||||
grid.cost = map->static_cost.data();
|
||||
const Clock::time_point began = Clock::now();
|
||||
robot_costmap_2d::ReferenceGridView view;
|
||||
view.cost = grid.cost;
|
||||
view.geometry = geometryOf(grid);
|
||||
map->clearance_mm = robot_costmap_2d::computeVoronoiSkeleton(view, ridge_params).clearance_mm;
|
||||
map->step1_ms = msSince(began);
|
||||
return map;
|
||||
}
|
||||
|
||||
GridRef gridOf(const MapData& map, const std::vector<uint8_t>& cost, const uint16_t* clearance, int variant)
|
||||
{
|
||||
GridRef grid;
|
||||
grid.width = map.width;
|
||||
grid.height = map.height;
|
||||
grid.resolution = map.resolution;
|
||||
grid.origin_x = map.origin_x;
|
||||
grid.origin_y = map.origin_y;
|
||||
grid.cost = cost.data();
|
||||
grid.clearance_mm = clearance;
|
||||
grid.direction = map.directions[static_cast<std::size_t>(variant)].data();
|
||||
return grid;
|
||||
}
|
||||
|
||||
/** Offline tuning without a rebuild: LANE_ROUTE_EVAL_SET="name=value,name=value" overrides lattice and fallback fields.
|
||||
* Unknown names are an error, so a typo cannot silently run the defaults. */
|
||||
void applyOverrides(PlannerConfig* config)
|
||||
{
|
||||
const char* text = std::getenv("LANE_ROUTE_EVAL_SET");
|
||||
if (text == nullptr || *text == '\0')
|
||||
return;
|
||||
const std::map<std::string, double*> doubles{
|
||||
{ "lattice_closed_resolution_m", &config->lattice_closed_resolution_m },
|
||||
{ "lattice_heuristic_weight", &config->lattice_heuristic_weight },
|
||||
{ "lattice_bucket_width_m", &config->lattice_bucket_width_m },
|
||||
{ "lattice_field_clearance_weight_m", &config->lattice_field_clearance_weight_m },
|
||||
{ "lattice_wall_weight_m", &config->lattice_wall_weight_m },
|
||||
{ "lattice_switch_cost_m", &config->lattice_switch_cost_m },
|
||||
{ "lattice_steer_cost_m", &config->lattice_steer_cost_m },
|
||||
{ "lattice_analytic_radius_m", &config->lattice_analytic_radius_m },
|
||||
{ "lattice_analytic_near_goal_m", &config->lattice_analytic_near_goal_m },
|
||||
{ "lattice_field_time_fraction", &config->lattice_field_time_fraction },
|
||||
{ "fallback_time_limit_s", &config->fallback_time_limit_s },
|
||||
{ "lattice_end_relax_radius_m", &config->lattice_end_relax_radius_m },
|
||||
};
|
||||
const std::map<std::string, int*> ints{
|
||||
{ "lattice_heading_bins", &config->lattice_heading_bins },
|
||||
{ "lattice_primitive_multiple_2", &config->lattice_primitive_multiple_2 },
|
||||
{ "lattice_primitive_multiple_3", &config->lattice_primitive_multiple_3 },
|
||||
{ "lattice_analytic_interval", &config->lattice_analytic_interval },
|
||||
{ "lattice_analytic_goal_headings", &config->lattice_analytic_goal_headings },
|
||||
{ "lattice_field_cell_factor", &config->lattice_field_cell_factor },
|
||||
};
|
||||
const std::map<std::string, int64_t*> longs{
|
||||
{ "lattice_field_max_cells", &config->lattice_field_max_cells },
|
||||
{ "lattice_max_memory_mb", &config->lattice_max_memory_mb },
|
||||
};
|
||||
std::stringstream stream(text);
|
||||
std::string item;
|
||||
while (std::getline(stream, item, ','))
|
||||
{
|
||||
const std::size_t equals = item.find('=');
|
||||
if (equals == std::string::npos)
|
||||
throw std::runtime_error("bad LANE_ROUTE_EVAL_SET item: " + item);
|
||||
const std::string key = item.substr(0, equals);
|
||||
const std::string value = item.substr(equals + 1);
|
||||
if (doubles.count(key) != 0)
|
||||
*doubles.at(key) = std::stod(value);
|
||||
else if (ints.count(key) != 0)
|
||||
*ints.at(key) = std::stoi(value);
|
||||
else if (longs.count(key) != 0)
|
||||
*longs.at(key) = std::stoll(value);
|
||||
else
|
||||
throw std::runtime_error("unknown LANE_ROUTE_EVAL_SET key: " + key);
|
||||
std::cerr << "override " << key << "=" << value << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<double> parseList(const std::string& text)
|
||||
{
|
||||
std::vector<double> out;
|
||||
if (text == "none")
|
||||
return out;
|
||||
std::stringstream stream(text);
|
||||
std::string item;
|
||||
while (std::getline(stream, item, ','))
|
||||
out.push_back(std::stod(item));
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc != 6 && argc != 7)
|
||||
{
|
||||
std::cerr << "usage: lane_route_eval <scenes.txt> <out.jsonl> <sehc|kit|hook_150> <deviations> <ladder|none> [graph|fallback|lattice]" << std::endl;
|
||||
return 2;
|
||||
}
|
||||
const std::string mode = argc == 7 ? argv[6] : "graph";
|
||||
if (mode != "graph" && mode != "fallback" && mode != "lattice")
|
||||
{
|
||||
std::cerr << "invalid tier mode: " << mode << std::endl;
|
||||
return 2;
|
||||
}
|
||||
PlannerConfig config;
|
||||
config.lattice_enabled = mode != "graph";
|
||||
if (std::string(argv[3]) == "kit")
|
||||
{
|
||||
config.half_x_m = 0.325;
|
||||
config.half_y_m = 0.345;
|
||||
config.clearance_hard_m = 0.36;
|
||||
config.clearance_min_m = 0.36;
|
||||
config.clearance_preferred_m = 0.625;
|
||||
config.collision_check_distance_m = 0.53;
|
||||
}
|
||||
else if (std::string(argv[3]) == "hook_150")
|
||||
{
|
||||
config.half_x_m = 0.511;
|
||||
config.half_y_m = 0.1955;
|
||||
config.clearance_hard_m = 0.2305;
|
||||
config.clearance_min_m = 0.2305;
|
||||
config.clearance_preferred_m = 0.4955;
|
||||
config.collision_check_distance_m = 0.603;
|
||||
}
|
||||
robot_costmap_2d::ReferenceGraphParams ridge_params = ridgeParams(config);
|
||||
if (std::string(argv[3]) == "hook_150")
|
||||
{
|
||||
ridge_params.min_clearance_m = 0.335;
|
||||
ridge_params.ridge_min_separation_m = 0.7;
|
||||
}
|
||||
config.deviation_ladder_deg = parseList(argv[5]);
|
||||
const std::vector<double> deviations = parseList(argv[4]);
|
||||
applyOverrides(&config);
|
||||
const RidgeBackend backend = makeRidgeBackend(config, ridge_params);
|
||||
|
||||
std::ifstream scenes(argv[1]);
|
||||
std::ofstream out(argv[2]);
|
||||
if (!scenes || !out)
|
||||
{
|
||||
std::cerr << "cannot open input or output" << std::endl;
|
||||
return 2;
|
||||
}
|
||||
out.precision(17);
|
||||
|
||||
std::map<std::string, std::shared_ptr<MapData>> maps;
|
||||
std::string line;
|
||||
while (std::getline(scenes, line))
|
||||
{
|
||||
if (line.empty())
|
||||
continue;
|
||||
std::istringstream row(line);
|
||||
std::string id, grid_file, live_file;
|
||||
int variant = 0, has_start = 0, has_goal = 0, boxes = 0;
|
||||
RouteRequest request;
|
||||
request.lattice_only = mode == "lattice";
|
||||
row >> id >> grid_file >> variant >> live_file >> has_start >> request.start.x >> request.start.y >> request.start.yaw >> has_goal >>
|
||||
request.goal.x >> request.goal.y >> request.goal.yaw >> boxes;
|
||||
request.start.has_yaw = has_start != 0;
|
||||
request.goal.has_yaw = has_goal != 0;
|
||||
|
||||
auto& map = maps[grid_file];
|
||||
if (map == nullptr)
|
||||
map = loadMap(grid_file, ridge_params);
|
||||
|
||||
// Live master: a recorded merged grid, or the map with boxes painted lethal.
|
||||
std::vector<uint8_t> live_cost = map->live_cost;
|
||||
if (live_file != "-")
|
||||
{
|
||||
std::ifstream live(live_file);
|
||||
live_cost = readRuns(live, "cost");
|
||||
}
|
||||
const double resolution = map->resolution;
|
||||
for (int b = 0; b < boxes; ++b)
|
||||
{
|
||||
double x_min, y_min, x_max, y_max;
|
||||
row >> x_min >> y_min >> x_max >> y_max;
|
||||
const int32_t x0 = std::max<int32_t>(0, static_cast<int32_t>(std::floor((x_min - map->origin_x) / resolution)));
|
||||
const int32_t x1 = std::min<int32_t>(map->width, static_cast<int32_t>(std::ceil((x_max - map->origin_x) / resolution)));
|
||||
const int32_t y0 = std::max<int32_t>(0, static_cast<int32_t>(std::floor((y_min - map->origin_y) / resolution)));
|
||||
const int32_t y1 = std::min<int32_t>(map->height, static_cast<int32_t>(std::ceil((y_max - map->origin_y) / resolution)));
|
||||
for (int32_t y = y0; y < y1; ++y)
|
||||
for (int32_t x = x0; x < x1; ++x)
|
||||
live_cost[static_cast<std::size_t>(y * map->width + x)] = kLethalCost;
|
||||
}
|
||||
|
||||
const GridRef compile_grid = gridOf(*map, map->static_cost, map->clearance_mm.data(), variant);
|
||||
const Clock::time_point update_began = Clock::now();
|
||||
const GridRef unpatched = gridOf(*map, live_cost, map->clearance_mm.data(), variant);
|
||||
request.changed_boxes = changedBoxes(map->static_cost.data(), unpatched);
|
||||
std::vector<uint16_t> patched;
|
||||
const uint16_t* live_clearance = map->clearance_mm.data();
|
||||
if (!request.changed_boxes.empty())
|
||||
{
|
||||
patchClearance(map->static_cost.data(), map->clearance_mm.data(), unpatched, 1.5, &patched);
|
||||
live_clearance = patched.data();
|
||||
}
|
||||
const double update_ms = msSince(update_began);
|
||||
const GridRef live_grid = gridOf(*map, live_cost, live_clearance, variant);
|
||||
|
||||
double coarse_ms = 0.0;
|
||||
const CoarseGraph* coarse = nullptr;
|
||||
if (mode != "lattice")
|
||||
{
|
||||
auto found = map->coarse.find(variant);
|
||||
if (found == map->coarse.end())
|
||||
{
|
||||
const Clock::time_point began = Clock::now();
|
||||
found = map->coarse.emplace(variant, buildCoarseGraph(compile_grid, config, ridge_params)).first;
|
||||
coarse_ms = msSince(began);
|
||||
}
|
||||
coarse = &found->second;
|
||||
}
|
||||
|
||||
for (double deviation : deviations)
|
||||
{
|
||||
config.max_deviation_deg = deviation;
|
||||
const GraphProvider provider = [&](double limit) -> std::shared_ptr<const CompiledGraph> {
|
||||
if (coarse == nullptr) return {};
|
||||
auto& slot = map->graphs[std::make_pair(variant, limit)];
|
||||
if (slot == nullptr)
|
||||
slot = std::make_shared<const CompiledGraph>(compileGraph(*coarse, compile_grid, compile_grid.cost, limit, config, backend));
|
||||
return slot;
|
||||
};
|
||||
const PlanResult result = planRoute(provider, request, live_grid, config, backend);
|
||||
const PlanInfo& info = result.info;
|
||||
out << "{\"scene\":\"" << id << "\",\"dev\":" << deviation << ",\"ok\":" << (result.ok ? 1 : 0) << ",\"reason\":\"" << info.reason
|
||||
<< "\",\"query_ms\":" << info.query_ms << ",\"compile_ms\":" << info.compile_ms << ",\"coarse_ms\":" << coarse_ms
|
||||
<< ",\"update_ms\":" << update_ms << ",\"search_ms\":" << info.search_ms << ",\"build_ms\":" << info.build_ms
|
||||
<< ",\"verify_ms\":" << info.verify_ms << ",\"live_ridge_ms\":" << info.live_ridge_ms << ",\"live_ridge\":\"" << info.live_ridge
|
||||
<< "\",\"rung\":" << info.rung_used_deg << ",\"retries\":" << info.retries << ",\"gap_failures\":" << info.gap_failures
|
||||
<< ",\"soft_ignored\":" << (info.soft_cost_ignored ? 1 : 0) << ",\"raise\":" << info.gap_shortcut_raise_m
|
||||
<< ",\"swing_windows\":" << info.swing_windows << ",\"swing_checks\":" << info.swing_line_checks
|
||||
<< ",\"swing_builds\":" << info.swing_builds << ",\"swing_selected\":" << info.swing_selected;
|
||||
if (result.ok)
|
||||
{
|
||||
out << ",\"min_gap\":" << info.min_gap_m << ",\"exempt\":" << (info.gap_exemption_used ? 1 : 0) << ",\"kinks\":" << result.check.kinks
|
||||
<< ",\"crossings\":" << result.check.self_crossings << ",\"zone_violations\":" << result.check.zone_violations
|
||||
<< ",\"collisions\":" << result.check.footprint_collisions << ",\"path\":[";
|
||||
for (std::size_t i = 0; i < result.path.size(); ++i)
|
||||
{
|
||||
const PathPoint& p = result.path[i];
|
||||
out << (i > 0 ? "," : "") << "[" << p.x << "," << p.y << "," << p.yaw << "]";
|
||||
}
|
||||
out << "]";
|
||||
}
|
||||
out << ",\"tier\":\"" << info.tier_used << "\",\"graph_reason\":\"" << info.graph_reason
|
||||
<< "\",\"lattice_reason\":\"" << info.lattice_reason << "\",\"lattice_states_expanded\":" << info.lattice_states_expanded
|
||||
<< ",\"lattice_pose_checks\":" << info.lattice_pose_checks << ",\"lattice_field_cells\":" << info.lattice_field_cells
|
||||
<< ",\"lattice_tiles_built\":" << info.lattice_tiles_built << ",\"lattice_peak_bytes\":" << info.lattice_peak_bytes
|
||||
<< ",\"lattice_crossing_rejects\":" << info.lattice_crossing_rejects
|
||||
<< ",\"lattice_field_ms\":" << info.lattice_field_ms << ",\"lattice_search_ms\":" << info.lattice_search_ms
|
||||
<< ",\"lattice_verify_ms\":" << info.lattice_verify_ms
|
||||
<< ",\"lattice_ms\":" << info.lattice_ms << "}\n";
|
||||
std::cout << id << " " << deviation << " " << info.reason << " " << info.query_ms << " ms" << std::endl;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user