Files
move_base2/src/config/move_base2_config.cpp
duongtd b102391fb8 Keep raw sensor readings so plugins can reach what the costmap threw away
Sensor data entered this runtime and disappeared into the costmap. At 0.05 m
with inflation the costmap has destroyed ray order and object shape, so a dock
detector fitting lines to find a V plate cannot reconstruct what the laser
saw - two 0.2 m wings on that grid are one blob. A reinforcement-learning
local planner wants the raw scan for the same reason. Neither had a route:
ActionContext held only TF, and LocalPlanner::initialize takes only a costmap.

NavigationServer now owns a SensorStore and feeds it from all five doors,
handing a read-only port to the action registry and to the controller runner.

The store gets the reading BEFORE the filter. Anything that wants filtering
filters upstream; the store does not know a filter exists. With laser SOR on,
the store and the costmap deliberately see different data - one wants outliers
gone, the other wants the true shape - and the comment at addLaserScan now
says so, because the one it replaces claimed the opposite.

remove* mirrors into the store, dropping readings while keeping the topic
declared. A host that has disowned data while the store still serves it means
the two disagree about what the robot can currently see, and max_age only
covers the tail of that.

Reaching a local planner without breaking every other one: widening
LocalPlanner::initialize would break hybrid, mppi, priest, stanley and
deep_mpc at once for the sake of one planner. A planner that wants sensor data
inherits SensorConsumer instead, and ControllerRunner finds out with a
dynamic_cast after loading it, at all three sites that build an instance and
always before initialize(). Planners that do not inherit it cast to null and
nothing happens, so nothing needs rebuilding. That cast crosses a .so boundary
and depends on matching RTTI, which a green build does not prove - hence the
log line, and hence TestControllerSensorProbe, dlopen'd like a real plugin, to
check it actually matches.

sensor_store/report_period prints what the store holds every N seconds, off by
default. Until a consumer exists it is the only way to see whether data
arrives at all, and it is what separates a mistyped topic from a dead sensor -
without it the two look identical. It reads age from the control loop's clock,
not robot::Time::now(), which is the wall clock while a simulated scan is not.

Config is refused at boot, not at first use: an unknown data_type, a short name
two packages share, a duplicate topic, a capacity below 1, a max_age that is
not positive. Declaring no sensor at all is valid and leaves the store empty
and silent - a runtime with no consumer of raw data holds nothing, which is an
answer rather than a reason to refuse to start.

robot_nav_msgs/OccupancyGrid is registered here rather than in
robot_sensor_ports, which ships only its own package's types; pulling
robot_nav_msgs into every translation unit that touches the store would be the
cost of doing otherwise.
2026-08-26 11:35:17 +07:00

688 lines
27 KiB
C++

/*********************************************************************
* move_base2 — đọc và validate cấu hình runtime.
*
* Author: DuongTD
*********************************************************************/
#include <move_base2/config/move_base2_config.h>
#include <cmath>
#include <sstream>
#include <robot/robot.h>
#include <yaml-cpp/yaml.h>
namespace move_base2
{
namespace
{
/// Đọc một khoá double; thiếu khoá thì giữ nguyên default và nói rõ khoá nào bị thiếu.
void readDouble(robot::NodeHandle& nh, const std::string& key, double& value)
{
if (!nh.hasParam(key))
{
robot::log_warning("[move_base2] missing param '%s', using default %.4f", key.c_str(), value);
return;
}
nh.param(key, value, value);
}
void readInt(robot::NodeHandle& nh, const std::string& key, int& value)
{
if (!nh.hasParam(key))
{
robot::log_warning("[move_base2] missing param '%s', using default %d", key.c_str(), value);
return;
}
nh.param(key, value, value);
}
void readBool(robot::NodeHandle& nh, const std::string& key, bool& value)
{
if (!nh.hasParam(key))
{
robot::log_warning("[move_base2] missing param '%s', using default %s", key.c_str(),
value ? "true" : "false");
return;
}
nh.param(key, value, value);
}
void readString(robot::NodeHandle& nh, const std::string& key, std::string& value)
{
if (!nh.hasParam(key))
{
robot::log_warning("[move_base2] missing param '%s', using default '%s'", key.c_str(),
value.c_str());
return;
}
nh.param(key, value, value);
}
/// Đọc một binding profile từ namespace con cùng tên.
void readBinding(robot::NodeHandle& nh, const std::string& ns, ProfileBinding& binding)
{
robot::NodeHandle profile_nh(nh, ns);
readString(profile_nh, "base_global_planner", binding.global_planner_name);
readString(profile_nh, "base_local_planner", binding.local_planner_name);
}
void readSensors(robot::NodeHandle& nh, SensorGatewayConfig& sensors);
void readSensorStore(robot::NodeHandle& nh, MoveBase2Config& config);
/// Đọc cặp planner runtime trực tiếp, không có adapter gen-1 ở giữa.
void readRootProfileBinding(robot::NodeHandle& nh, const std::string& ns, ProfileBinding& binding)
{
robot::NodeHandle profile_nh(nh, ns);
readString(profile_nh, "global_planner", binding.global_planner_name);
readString(profile_nh, "local_planner", binding.local_planner_name);
}
void readDockingMarkerProfiles(robot::NodeHandle& nh, DockingMarkerProfiles& profiles,
std::string& error)
{
profiles.clear();
error.clear();
if (!nh.hasParam("docking_marker_profiles"))
{
return; // Optional: the default docking binding remains authoritative.
}
const YAML::Node table = nh.getParamValue("docking_marker_profiles");
if (!table || !table.IsDefined())
{
error = "docking_marker_profiles is declared but cannot be read";
return;
}
if (!table.IsMap())
{
error = "docking_marker_profiles must be a map of marker names";
return;
}
try
{
for (auto entry = table.begin(); entry != table.end(); ++entry)
{
const std::string marker = entry->first.as<std::string>();
const YAML::Node value = entry->second;
if (marker.empty() || !value.IsMap())
{
error = "docking_marker_profiles has an empty marker name or a non-map entry";
return;
}
if (!value["global_planner"] || !value["local_planner"] ||
!value["global_planner"].IsScalar() || !value["local_planner"].IsScalar())
{
error = "docking_marker_profiles/'" + marker +
"' must set scalar global_planner and local_planner";
return;
}
ProfileBinding binding;
binding.global_planner_name = value["global_planner"].as<std::string>();
binding.local_planner_name = value["local_planner"].as<std::string>();
if (binding.global_planner_name.empty() || binding.local_planner_name.empty())
{
error = "docking_marker_profiles/'" + marker +
"' has an empty global_planner or local_planner";
return;
}
profiles.emplace(marker, std::move(binding));
}
}
catch (const YAML::Exception& ex)
{
error = std::string("docking_marker_profiles is malformed: ") + ex.what();
}
}
/**
* @brief Đọc khối `path_monitor`.
*
* Khối vắng mặt = giám sát tuyến TẮT, hành vi y hệt trước 2026-08-04d. Đây là đường lùi thứ nhất
* (thứ hai là route `path_blocked`/`off_path` rỗng) — hai nút độc lập, cố ý.
*/
void readPathMonitor(robot::NodeHandle& nh, ControlLoopConfig::PathMonitorConfig& monitor)
{
robot::NodeHandle monitor_nh(nh, "path_monitor");
readBool(monitor_nh, "enabled", monitor.enabled);
readBool(monitor_nh, "path_blocked_enabled", monitor.path_blocked_enabled);
readBool(monitor_nh, "off_path_enabled", monitor.off_path_enabled);
readDouble(monitor_nh, "check_period", monitor.check_period);
readDouble(monitor_nh, "confirm_time", monitor.confirm_time);
readDouble(monitor_nh, "horizon", monitor.horizon);
readDouble(monitor_nh, "min_detour_distance", monitor.min_detour_distance);
readDouble(monitor_nh, "max_cross_track", monitor.max_cross_track);
readDouble(monitor_nh, "grace_after_new_plan", monitor.grace_after_new_plan);
std::vector<std::string> names;
monitor_nh.param("profiles", names, std::vector<std::string>{});
if (names.empty())
{
return; // Giữ mặc định: chỉ profile position.
}
monitor.profiles.clear();
for (const std::string& name : names)
{
if (name == "position")
{
monitor.profiles.push_back(MotionProfile::kPosition);
}
else if (name == "docking")
{
monitor.profiles.push_back(MotionProfile::kDocking);
}
else if (name == "go_straight")
{
monitor.profiles.push_back(MotionProfile::kGoStraight);
}
else if (name == "rotate")
{
monitor.profiles.push_back(MotionProfile::kRotate);
}
else
{
// Không đoán: tên lạ mà im lặng bỏ qua thì người vận hành tưởng đã bật giám sát cho profile
// đó. Cảnh báo rồi bỏ — profile lạ không thể tự sinh ra hành vi nào.
robot::log_warning("[move_base2] path_monitor/profiles: unknown profile '%s', ignored.\n",
name.c_str());
}
}
}
/// Các tham số chung của move_base2, dùng chung cho schema namespace và schema root-profile.
void readMoveBase2Fields(robot::NodeHandle& nh, MoveBase2Config& config)
{
readDouble(nh, "controller_frequency", config.controller_frequency);
readDouble(nh, "planner_frequency", config.planner_frequency);
readDouble(nh, "planner_timeout", config.planner_timeout);
readDouble(nh, "runtime_stats_period", config.runtime_stats_period);
readDouble(nh, "planner_patience", config.state_machine.planner_patience);
readDouble(nh, "controller_patience", config.state_machine.controller_patience);
readDouble(nh, "sensor_patience", config.state_machine.sensor_patience);
readDouble(nh, "oscillation_timeout", config.state_machine.oscillation_timeout);
readDouble(nh, "oscillation_distance", config.state_machine.oscillation_distance);
readDouble(nh, "action_patience", config.state_machine.action_patience);
readInt(nh, "max_planning_retries", config.state_machine.max_planning_retries);
readBool(nh, "recovery_behavior_enabled", config.state_machine.recovery_enabled);
// Nghĩa của Twist đi qua arbiter — quyết định đơn vị của cả cụm giới hạn bên dưới. "steering"
// (linear.x = tốc độ bánh steer, angular.z = GÓC lái) là convention của robot lái một bánh
// trong workspace; để mặc định "twist" cho robot vi sai. Giá trị lạ giữ nguyên mặc định và
// cảnh báo, không chết boot — nhưng validate() của VelocityLimits sẽ chặn tổ hợp vô nghĩa.
{
std::string semantics = toString(config.velocity.semantics);
readString(nh, "command_semantics", semantics);
if (semantics == "steering")
config.velocity.semantics = CommandSemantics::kSteering;
else if (semantics == "twist")
config.velocity.semantics = CommandSemantics::kTwist;
else
robot::log_warning("[move_base2] unknown command_semantics '%s', keeping '%s'",
semantics.c_str(), toString(config.velocity.semantics));
}
readDouble(nh, "max_vel_x", config.velocity.max_vel_x);
readDouble(nh, "min_vel_x", config.velocity.min_vel_x);
readDouble(nh, "max_vel_theta", config.velocity.max_vel_theta);
readDouble(nh, "acc_lim_x", config.velocity.max_accel_x);
readDouble(nh, "acc_lim_theta", config.velocity.max_accel_theta);
readSensors(nh, config.sensors);
readSensorStore(nh, config);
readPathMonitor(nh, config.path_monitor);
readBool(nh, "docking_requires_marker", config.docking_requires_marker);
readString(nh, "recovery_namespace", config.recovery_namespace);
readString(nh, "action_namespace", config.action_namespace);
readString(nh, "mission_namespace", config.mission_namespace);
readBool(nh, "mission_layer_enabled", config.mission_layer_enabled);
readString(nh, "backup_global_planner", config.backup_global_planner_name);
readString(nh, "global_frame", config.global_frame);
readString(nh, "robot_base_frame", config.robot_base_frame);
readBool(nh, "require_current_costmap", config.require_current_costmap);
}
bool validateBinding(const ProfileBinding& binding, const char* name, std::string& error)
{
(void)name;
(void)error;
if (binding.local_planner_name.empty())
{
// Không đặt là hợp lệ: deployment có thể không dùng profile đó.
return true;
}
return true;
}
/// Đọc khai báo kho cảm biến thô từ namespace con `sensor_store`.
///
/// Lỗi được CẤT chứ không ném: `fromNodeHandle` là void và được gọi trên nhiều schema khác nhau,
/// nên chỗ duy nhất chặn boot được là `validate()`. Cùng khuôn với `docking_marker_profiles_error`.
void readSensorStore(robot::NodeHandle& nh, MoveBase2Config& config)
{
config.sensor_store_error.clear();
if (!robot_sensor_ports::SensorStoreConfig::load(nh, "sensor_store", config.sensor_store,
config.sensor_store_error))
{
config.sensor_store.sensors.clear();
}
robot::NodeHandle store_nh(nh, "sensor_store");
readDouble(store_nh, "report_period", config.sensor_store_report_period);
}
/// Đọc cấu hình đường vào cảm biến từ namespace con `sensors`.
void readSensors(robot::NodeHandle& nh, SensorGatewayConfig& sensors)
{
robot::NodeHandle sensors_nh(nh, "sensors");
readBool(sensors_nh, "laser_sor_enabled", sensors.laser_sor_enabled);
readInt(sensors_nh, "laser_sor_mean_k", sensors.laser_sor_mean_k);
readDouble(sensors_nh, "laser_sor_stddev_mul", sensors.laser_sor_stddev_mul);
}
void describeBinding(std::ostringstream& out, const char* name, const ProfileBinding& binding)
{
out << " " << name << ": global='" << binding.global_planner_name << "' local='"
<< binding.local_planner_name << "'\n";
}
} // namespace
void MoveBase2Config::fromNodeHandle(robot::NodeHandle& nh)
{
readMoveBase2Fields(nh, *this);
readBinding(nh, "position", position);
readBinding(nh, "docking", docking);
readBinding(nh, "go_straight", go_straight);
readBinding(nh, "rotate", rotate);
readDockingMarkerProfiles(nh, docking_marker_profiles, docking_marker_profiles_error);
// recovery_behavior_count KHÔNG đọc từ YAML: nó là số behavior thực sự nạp được, do
// RecoveryRunner báo lại sau khi configure. Đọc từ config thì một behavior hỏng sẽ khiến state
// machine tin là vẫn còn đường phục hồi.
}
void MoveBase2Config::fromRootProfileNodeHandle(robot::NodeHandle& nh)
{
readMoveBase2Fields(nh, *this);
readRootProfileBinding(nh, "position", position);
readRootProfileBinding(nh, "docking", docking);
readRootProfileBinding(nh, "go_straight", go_straight);
readRootProfileBinding(nh, "rotate", rotate);
readDockingMarkerProfiles(nh, docking_marker_profiles, docking_marker_profiles_error);
}
bool MoveBase2Config::validate(std::string& error) const
{
if (!docking_marker_profiles_error.empty())
{
error = docking_marker_profiles_error;
return false;
}
if (!std::isfinite(controller_frequency) || controller_frequency <= 0.0)
{
error = "controller_frequency must be > 0 [Hz]";
return false;
}
if (controller_frequency > 200.0)
{
error = "controller_frequency > 200 Hz — unrealistic rate for a control loop that owns a "
"costmap";
return false;
}
if (!std::isfinite(planner_frequency) || planner_frequency < 0.0)
{
error = "planner_frequency must be >= 0 [Hz] (0 = plan only when needed)";
return false;
}
if (!std::isfinite(planner_timeout))
{
error = "planner_timeout is not finite [s]";
return false;
}
if (path_monitor.enabled)
{
// Chỉ kiểm khi BẬT: tham số vô nghĩa trong một khối đang tắt không có lý do gì để chặn boot.
if (!std::isfinite(path_monitor.check_period) || path_monitor.check_period <= 0.0)
{
error = "path_monitor/check_period must be > 0 [s]";
return false;
}
if (!std::isfinite(path_monitor.confirm_time) || path_monitor.confirm_time < 0.0)
{
error = "path_monitor/confirm_time must be >= 0 [s]";
return false;
}
if (!std::isfinite(path_monitor.horizon) || path_monitor.horizon < 0.0)
{
error = "path_monitor/horizon must be >= 0 [m] (0 = the blocked-path check is off)";
return false;
}
if (!std::isfinite(path_monitor.min_detour_distance) || path_monitor.min_detour_distance < 0.0)
{
error = "path_monitor/min_detour_distance must be >= 0 [m]";
return false;
}
if (!std::isfinite(path_monitor.max_cross_track) || path_monitor.max_cross_track < 0.0)
{
error = "path_monitor/max_cross_track must be >= 0 [m] (0 = the off-path check is off)";
return false;
}
if (!std::isfinite(path_monitor.grace_after_new_plan) ||
path_monitor.grace_after_new_plan < 0.0)
{
error = "path_monitor/grace_after_new_plan must be >= 0 [s]";
return false;
}
if (!(path_monitor.horizon > 0.0) && !(path_monitor.max_cross_track > 0.0))
{
// Bật khối mà tắt cả hai phép đo là mâu thuẫn: người khai tưởng giám sát đang chạy.
error = "path_monitor is enabled but both checks are off; set horizon > 0 [m] and/or "
"max_cross_track > 0 [m], or set enabled: false";
return false;
}
if (path_monitor.profiles.empty())
{
error = "path_monitor/profiles is empty; no leg would ever be monitored";
return false;
}
}
if (!std::isfinite(runtime_stats_period) || runtime_stats_period < 0.0)
{
error = "runtime_stats_period must be >= 0 [s] (0 = telemetry off)";
return false;
}
if (recovery_namespace.empty())
{
error = "recovery_namespace is empty";
return false;
}
if (mission_layer_enabled && mission_namespace.empty())
{
error = "mission_layer_enabled is true but mission_namespace is empty — there would be no "
"namespace to read `mission_sources` from";
return false;
}
if (global_frame.empty() || robot_base_frame.empty())
{
error = "global_frame and robot_base_frame must not be empty";
return false;
}
if (global_frame == robot_base_frame)
{
error = "global_frame equals robot_base_frame — the robot pose would always be the origin";
return false;
}
if (!validateBinding(position, "position", error) ||
!validateBinding(docking, "docking", error) ||
!validateBinding(go_straight, "go_straight", error) ||
!validateBinding(rotate, "rotate", error))
{
return false;
}
if (position.local_planner_name.empty() && docking.local_planner_name.empty() &&
go_straight.local_planner_name.empty() && rotate.local_planner_name.empty())
{
error = "no profile has base_local_planner — the runtime would reject every request";
return false;
}
if (!velocity.validate(error))
{
return false;
}
if (!sensors.validate(error))
{
return false;
}
// Khai kho sai thì chặn boot, không im lặng chạy với kho rỗng: consumer nào cần dữ liệu thô sẽ
// chờ mãi không có gì, và triệu chứng giống hệt cảm biến chết.
if (!sensor_store_error.empty())
{
error = "sensor_store: " + sensor_store_error;
return false;
}
if (!std::isfinite(sensor_store_report_period) || sensor_store_report_period < 0.0)
{
error = "sensor_store/report_period must be a finite value >= 0 (0 = off)";
return false;
}
// Sau cùng: struct con. Thứ tự này có chủ đích — báo lỗi ở tầng cụ thể nhất trước, để thông báo
// nói đúng khoá YAML mà người vận hành cần sửa, chứ không phải một ràng buộc phái sinh.
//
// Lưu ý ràng buộc thứ tự KHỞI TẠO: `state_machine.recovery_behavior_count` KHÔNG đến từ YAML mà
// là số behavior RecoveryRunner nạp được thật. Bên gọi phải điền nó trước khi gọi hàm này —
// xem @ref MoveBase2Config::validate trong header.
if (!state_machine.validate(error))
{
return false;
}
return true;
}
std::string MoveBase2Config::describe() const
{
std::ostringstream out;
out << "move_base2 config:\n";
out << " controller_frequency: " << controller_frequency << " Hz\n";
out << " planner_frequency: " << planner_frequency << " Hz\n";
out << " planner_timeout: " << planner_timeout << " s\n";
out << " runtime_stats_period: " << runtime_stats_period << " s"
<< (runtime_stats_period > 0.0 ? "\n" : " (off)\n");
out << " frames: global='" << global_frame << "' base='" << robot_base_frame << "'\n";
out << " require_current_costmap: " << (require_current_costmap ? "true" : "false") << "\n";
out << " namespaces: recovery='" << recovery_namespace << "' actions='" << action_namespace
<< "' mission='" << mission_namespace << "'\n";
out << " mission_layer_enabled: " << (mission_layer_enabled ? "true" : "false")
<< (mission_layer_enabled ? " (orders are split into legs)\n"
: " (orders go straight down as one goal)\n");
describeBinding(out, "position", position);
describeBinding(out, "docking", docking);
for (const auto& entry : docking_marker_profiles)
{
describeBinding(out, ("docking marker '" + entry.first + "'").c_str(), entry.second);
}
out << " docking_requires_marker: " << (docking_requires_marker ? "true" : "false") << '\n';
describeBinding(out, "go_straight", go_straight);
describeBinding(out, "rotate", rotate);
out << " backup_global_planner: '" << backup_global_planner_name << "'"
<< (backup_global_planner_name.empty() ? " (off)\n" : "\n");
out << state_machine.describe();
out << velocity.describe();
out << sensors.describe();
out << " sensor_store: " << sensor_store.sensors.size() << " topic(s)"
<< (sensor_store.sensors.empty() ? " (off)" : "") << ", report_period: "
<< sensor_store_report_period << " s" << (sensor_store_report_period > 0.0 ? "\n" : " (off)\n");
for (const robot_sensor_ports::SensorEntryConfig& entry : sensor_store.sensors)
{
out << " '" << entry.topic << "' " << entry.data_type << " capacity=" << entry.capacity
<< " max_age=" << entry.max_age << " s\n";
}
return out.str();
}
// ================================================================================================
// Đọc theo schema gen-1
// ================================================================================================
void MoveBase2Config::fromLegacyNodeHandle(robot::NodeHandle& nh)
{
// Schema gen-1 dùng base_footprint; giữ frame cũ để không đổi hành vi của hệ đang chạy.
robot_base_frame = "base_footprint";
readDouble(nh, "controller_frequency", controller_frequency);
readDouble(nh, "planner_frequency", planner_frequency);
// Khoá này không có trong schema gen-1 — nó là công cụ chẩn đoán của move_base2. Vẫn đọc ở đây
// để bật được telemetry mà không phải chuyển cả cây config sang schema mới.
readDouble(nh, "runtime_stats_period", runtime_stats_period);
readDouble(nh, "planner_patience", state_machine.planner_patience);
readDouble(nh, "controller_patience", state_machine.controller_patience);
readDouble(nh, "sensor_patience", state_machine.sensor_patience);
readDouble(nh, "oscillation_timeout", state_machine.oscillation_timeout);
readDouble(nh, "oscillation_distance", state_machine.oscillation_distance);
readInt(nh, "max_planning_retries", state_machine.max_planning_retries);
readBool(nh, "recovery_behavior_enabled", state_machine.recovery_enabled);
readString(nh, "global_frame", global_frame);
readString(nh, "robot_base_frame", robot_base_frame);
// Không có trong schema gen-1 (bản cũ hard-code guard này, không cho tắt) — đọc để có đường tắt
// khi observation buffer bị cấu hình sai, mặc định vẫn là bật.
readBool(nh, "require_current_costmap", require_current_costmap);
// Cũng không có trong schema gen-1: gen-1 không có mission layer nên không có khoá tương ứng để
// dịch. Đọc ở đây để tắt/bật được ngay trên cây config đang chạy mà không phải chuyển schema —
// đây là đường lùi khi mission layer gây vấn đề trên hiện trường.
readBool(nh, "mission_layer_enabled", mission_layer_enabled);
readString(nh, "mission_namespace", mission_namespace);
readBool(nh, "docking_requires_marker", docking_requires_marker);
std::string root_global_planner;
readString(nh, "base_global_planner", root_global_planner);
if (nh.hasParam("base_local_planner"))
{
std::string adapter;
nh.param("base_local_planner", adapter, adapter);
// `LocalPlannerAdapter` là cầu nhúng planner gen-2 vào move_base gen-1. move_base2 gọi thẳng
// interface gen-2 qua ControllerPort nên không cần cầu đó — bỏ qua CÓ LOG, để không ai tưởng
// khoá này vẫn đang có hiệu lực.
robot::log_warning("[move_base2] schema gen-1: ignoring base_local_planner='%s' — move_base2 "
"calls the local planner directly, not through an adapter.",
adapter.c_str());
}
struct LegacyProfile
{
const char* key;
ProfileBinding* binding;
};
const LegacyProfile profiles[] = {
{ "position_planner_name", &position },
{ "docking_planner_name", &docking },
{ "go_straight_planner_name", &go_straight },
{ "rotate_planner_name", &rotate },
};
for (const LegacyProfile& profile : profiles)
{
if (!nh.hasParam(profile.key))
{
robot::log_warning("[move_base2] schema gen-1: missing '%s', this profile will reject every "
"request",
profile.key);
continue;
}
std::string local_planner;
nh.param(profile.key, local_planner, local_planner);
profile.binding->local_planner_name = local_planner;
// Global planner riêng của profile nằm trong namespace mang tên chính planner đó; thiếu thì
// rơi về khoá ở root. Đúng cách bản cũ tra (`NodeHandle(private_nh_, position_planner_name_)`).
profile.binding->global_planner_name = root_global_planner;
if (!local_planner.empty())
{
robot::NodeHandle planner_nh(nh, local_planner);
if (planner_nh.hasParam("base_global_planner"))
{
std::string profile_global;
planner_nh.param("base_global_planner", profile_global, profile_global);
profile.binding->global_planner_name = profile_global;
}
}
}
// Dịch tường minh khác biệt NGỮ NGHĨA của patience. Gen-1: 0 nghĩa là "fail -> recovery NGAY"
// (mốc + 0 luôn ở quá khứ). Gen-2: 0 nghĩa là "tắt đồng hồ" — ngược hẳn. Dịch thành đúng MỘT chu
// kỳ điều khiển: gen-1 cũng chỉ phản ứng được ở độ phân giải cycle nên hành vi giữ nguyên.
const double one_cycle = controller_frequency > 0.0 ? 1.0 / controller_frequency : 0.05; // [s]
if (state_machine.planner_patience <= 0.0)
{
robot::log_warning("[move_base2] schema gen-1: planner_patience <= 0 is translated into %.3f s "
"(one control cycle), not into 'off'.", one_cycle);
state_machine.planner_patience = one_cycle;
}
if (state_machine.controller_patience <= 0.0)
{
robot::log_warning("[move_base2] schema gen-1: controller_patience <= 0 is translated into "
"%.3f s (one control cycle), not into 'off'.", one_cycle);
state_machine.controller_patience = one_cycle;
}
}
MoveBase2Config MoveBase2Config::load(robot::NodeHandle& root_nh)
{
MoveBase2Config config;
// Nhận diện schema bằng một khoá bắt buộc phải có ở cả hai. KHÔNG trộn từng khoá giữa hai schema:
// hai nguồn cùng có hiệu lực cho một tham số là đúng kiểu lỗi "sửa config mãi không ăn" mà hai cây
// config trùng tên của workspace này đã gây ra một lần.
robot::NodeHandle modern_nh(root_nh, "move_base2");
if (modern_nh.hasParam("controller_frequency"))
{
robot::log_info("[move_base2] using the new schema (namespace 'move_base2').");
config.fromNodeHandle(modern_nh);
return config;
}
robot::NodeHandle position_nh(root_nh, "position");
if (position_nh.hasParam("local_planner"))
{
robot::log_info("[move_base2] using the root profile schema.");
config.fromRootProfileNodeHandle(root_nh);
return config;
}
if (root_nh.hasParam("controller_frequency") || root_nh.hasParam("base_global_planner"))
{
robot::log_warning("[move_base2] namespace 'move_base2' not found; reading the gen-1 schema of "
"move_base_common_params.yaml.");
config.fromLegacyNodeHandle(root_nh);
return config;
}
robot::log_error("[move_base2] no configuration found — running with all default values. Check "
"PNKX_NAV_CORE_CONFIG_DIR and that the config file exists.");
return config;
}
ControlLoopConfig MoveBase2Config::toControlLoopConfig() const
{
ControlLoopConfig config;
config.state_machine = state_machine;
config.velocity = velocity;
config.nominal_control_period =
controller_frequency > 0.0 ? 1.0 / controller_frequency : 0.05; // [s]
config.planner_frequency = planner_frequency; // [Hz] replan nền khi kControlling
config.robot_base_frame = robot_base_frame;
config.require_current_costmap = require_current_costmap;
config.position = position;
config.docking = docking;
config.docking_marker_profiles = docking_marker_profiles;
config.go_straight = go_straight;
config.rotate = rotate;
config.backup_global_planner_name = backup_global_planner_name;
config.docking_requires_marker = docking_requires_marker;
config.path_monitor = path_monitor;
return config;
}
} // namespace move_base2