optimal & fix file cmake

This commit is contained in:
2026-08-03 22:41:32 +07:00
parent d8babff20b
commit 701d25f952
70 changed files with 5572 additions and 1146 deletions

View File

@@ -9,6 +9,7 @@
#include <sstream>
#include <robot/robot.h>
#include <yaml-cpp/yaml.h>
namespace move_base2
{
@@ -20,7 +21,7 @@ void readDouble(robot::NodeHandle& nh, const std::string& key, double& value)
{
if (!nh.hasParam(key))
{
robot::log_warning("[move_base2] thiếu param '%s', dùng default %.4f", key.c_str(), value);
robot::log_warning("[move_base2] missing param '%s', using default %.4f", key.c_str(), value);
return;
}
nh.param(key, value, value);
@@ -30,7 +31,7 @@ void readInt(robot::NodeHandle& nh, const std::string& key, int& value)
{
if (!nh.hasParam(key))
{
robot::log_warning("[move_base2] thiếu param '%s', dùng default %d", key.c_str(), value);
robot::log_warning("[move_base2] missing param '%s', using default %d", key.c_str(), value);
return;
}
nh.param(key, value, value);
@@ -40,7 +41,7 @@ void readBool(robot::NodeHandle& nh, const std::string& key, bool& value)
{
if (!nh.hasParam(key))
{
robot::log_warning("[move_base2] thiếu param '%s', dùng default %s", key.c_str(),
robot::log_warning("[move_base2] missing param '%s', using default %s", key.c_str(),
value ? "true" : "false");
return;
}
@@ -51,7 +52,7 @@ void readString(robot::NodeHandle& nh, const std::string& key, std::string& valu
{
if (!nh.hasParam(key))
{
robot::log_warning("[move_base2] thiếu param '%s', dùng default '%s'", key.c_str(),
robot::log_warning("[move_base2] missing param '%s', using default '%s'", key.c_str(),
value.c_str());
return;
}
@@ -64,28 +65,121 @@ void readBinding(robot::NodeHandle& nh, const std::string& ns, ProfileBinding& b
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);
readDouble(profile_nh, "xy_goal_tolerance", binding.default_xy_tolerance);
readDouble(profile_nh, "yaw_goal_tolerance", binding.default_yaw_tolerance);
}
void readSensors(robot::NodeHandle& nh, SensorGatewayConfig& sensors);
/// Đọ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();
}
}
/// 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, "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);
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);
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 đó. Nhưng nếu đã đặt planner thì
// sai số phải hợp lệ, vì chúng đi thẳng vào điều kiện dừng.
// Không đặt là hợp lệ: deployment có thể không dùng profile đó.
return true;
}
if (!std::isfinite(binding.default_xy_tolerance) || binding.default_xy_tolerance <= 0.0)
{
error = std::string(name) + ".xy_goal_tolerance phải > 0 [m]";
return false;
}
if (!std::isfinite(binding.default_yaw_tolerance) || binding.default_yaw_tolerance <= 0.0)
{
error = std::string(name) + ".yaw_goal_tolerance phải > 0 [rad]";
return false;
}
return true;
}
@@ -101,134 +195,89 @@ void readSensors(robot::NodeHandle& nh, SensorGatewayConfig& sensors)
void describeBinding(std::ostringstream& out, const char* name, const ProfileBinding& binding)
{
out << " " << name << ": global='" << binding.global_planner_name << "' local='"
<< binding.local_planner_name << "' xy=" << binding.default_xy_tolerance
<< " m yaw=" << binding.default_yaw_tolerance << " rad\n";
}
/// Dịch patience gen-1 sang gen-2. Gen-1: mốc + patience luôn ở quá khứ khi patience <= 0, tức là
/// "fail -> recovery NGAY". Gen-2: <= 0 nghĩa là TẮT đồng hồ — ngược nghĩa hoàn toàn. Giữ hành vi
/// cũ bằng cách dịch thành đúng một chu kỳ điều khiển (gen-1 cũng chỉ phản ứng theo cycle).
double legacyPatience(double value, double control_period_s, const char* key)
{
if (value > 0.0)
{
return value;
}
robot::log_warning(
"[move_base2] legacy %s = %.3f: gen-1 hiểu là 'fail -> recovery ngay', gen-2 hiểu là 'tắt "
"đồng hồ'. Dịch thành một chu kỳ điều khiển (%.4f s) để giữ hành vi cũ.",
key, value, control_period_s);
return control_period_s;
}
/// Đọc binding của một profile theo schema gen-1: tên local planner ở khoá `<profile>_planner_name`
/// tại root, global planner ở section con mang TÊN planner đó (thiếu thì dùng global mặc định).
void readLegacyBinding(robot::NodeHandle& nh, const std::string& name_key,
const std::string& default_global, double xy_tolerance,
double yaw_tolerance, ProfileBinding& binding)
{
binding.default_xy_tolerance = xy_tolerance;
binding.default_yaw_tolerance = yaw_tolerance;
binding.global_planner_name = default_global;
// Default để RỖNG chứ không lấy default gen-1 ("mkt_algorithm/..."): các plugin đó không tồn tại
// trong workspace, và profile không khai coi như không dùng — validate sẽ chặn nếu cả bốn rỗng.
std::string local_name;
nh.param(name_key, local_name, std::string(""));
if (local_name.empty())
{
robot::log_warning("[move_base2] legacy: thiếu '%s' — profile này bị tắt", name_key.c_str());
return;
}
binding.local_planner_name = local_name;
robot::NodeHandle planner_nh(nh, local_name);
if (planner_nh.hasParam("base_global_planner"))
{
planner_nh.param("base_global_planner", binding.global_planner_name,
binding.global_planner_name);
}
robot::log_info("[move_base2] legacy: %s='%s' -> local='%s' global='%s'", name_key.c_str(),
local_name.c_str(), binding.local_planner_name.c_str(),
binding.global_planner_name.c_str());
<< binding.local_planner_name << "'\n";
}
} // namespace
void MoveBase2Config::fromNodeHandle(robot::NodeHandle& nh)
{
readDouble(nh, "controller_frequency", controller_frequency);
readDouble(nh, "planner_frequency", planner_frequency);
readDouble(nh, "planner_timeout", planner_timeout);
readDouble(nh, "planner_patience", state_machine.planner_patience);
readDouble(nh, "controller_patience", state_machine.controller_patience);
readDouble(nh, "oscillation_timeout", state_machine.oscillation_timeout);
readDouble(nh, "oscillation_distance", state_machine.oscillation_distance);
readDouble(nh, "action_patience", state_machine.action_patience);
readInt(nh, "max_planning_retries", state_machine.max_planning_retries);
readBool(nh, "recovery_behavior_enabled", state_machine.recovery_enabled);
readDouble(nh, "max_vel_x", velocity.max_vel_x);
readDouble(nh, "min_vel_x", velocity.min_vel_x);
readDouble(nh, "max_vel_theta", velocity.max_vel_theta);
readDouble(nh, "acc_lim_x", velocity.max_accel_x);
readDouble(nh, "acc_lim_theta", velocity.max_accel_theta);
readSensors(nh, sensors);
readMoveBase2Fields(nh, *this);
readBinding(nh, "position", position);
readBinding(nh, "docking", docking);
readBinding(nh, "go_straight", go_straight);
readBinding(nh, "rotate", rotate);
readString(nh, "recovery_namespace", recovery_namespace);
readString(nh, "action_namespace", action_namespace);
readString(nh, "mission_namespace", mission_namespace);
readString(nh, "global_frame", global_frame);
readString(nh, "robot_base_frame", robot_base_frame);
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 phải > 0 [Hz]";
error = "controller_frequency must be > 0 [Hz]";
return false;
}
if (controller_frequency > 200.0)
{
error = "controller_frequency > 200 Hz — nhịp này không thực tế cho một control loop có costmap";
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 phải >= 0 [Hz] (0 = chỉ lập plan khi cần)";
error = "planner_frequency must be >= 0 [Hz] (0 = plan only when needed)";
return false;
}
if (!std::isfinite(planner_timeout))
{
error = "planner_timeout không hữu hạn [s]";
error = "planner_timeout is not finite [s]";
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 rỗng";
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 và robot_base_frame không được rỗng";
error = "global_frame and robot_base_frame must not be empty";
return false;
}
if (global_frame == robot_base_frame)
{
error = "global_frame trùng robot_base_frame — pose robot sẽ luôn là gốc toạ độ";
error = "global_frame equals robot_base_frame — the robot pose would always be the origin";
return false;
}
@@ -243,7 +292,7 @@ bool MoveBase2Config::validate(std::string& error) const
if (position.local_planner_name.empty() && docking.local_planner_name.empty() &&
go_straight.local_planner_name.empty() && rotate.local_planner_name.empty())
{
error = "không profile nào có base_local_planner — runtime sẽ từ chối mọi yêu cầu";
error = "no profile has base_local_planner — the runtime would reject every request";
return false;
}
@@ -278,13 +327,26 @@ std::string MoveBase2Config::describe() const
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();
@@ -298,14 +360,14 @@ std::string MoveBase2Config::describe() const
void MoveBase2Config::fromLegacyNodeHandle(robot::NodeHandle& nh)
{
// Default của gen-1 khác gen-2 ở hai chỗ; chế độ legacy giữ default gen-1 để không đổi hành vi
// của một hệ đang chạy chỉ vì đổi runtime.
// 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";
position.default_xy_tolerance = 0.2; // [m]
position.default_yaw_tolerance = 0.2; // [rad]
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, "oscillation_timeout", state_machine.oscillation_timeout);
@@ -315,12 +377,16 @@ void MoveBase2Config::fromLegacyNodeHandle(robot::NodeHandle& nh)
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);
// Sai số ở root là default chung cho cả bốn profile.
double xy = position.default_xy_tolerance;
double yaw = position.default_yaw_tolerance;
readDouble(nh, "xy_goal_tolerance", xy);
readDouble(nh, "yaw_goal_tolerance", yaw);
// 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);
@@ -332,8 +398,9 @@ void MoveBase2Config::fromLegacyNodeHandle(robot::NodeHandle& nh)
// `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: bỏ qua base_local_planner='%s' — move_base2 gọi "
"thẳng local planner, không qua adapter.", adapter.c_str());
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
@@ -350,12 +417,10 @@ void MoveBase2Config::fromLegacyNodeHandle(robot::NodeHandle& nh)
for (const LegacyProfile& profile : profiles)
{
profile.binding->default_xy_tolerance = xy;
profile.binding->default_yaw_tolerance = yaw;
if (!nh.hasParam(profile.key))
{
robot::log_warning("[move_base2] schema gen-1: thiếu '%s', profile này sẽ từ chối mọi yêu cầu",
robot::log_warning("[move_base2] schema gen-1: missing '%s', this profile will reject every "
"request",
profile.key);
continue;
}
@@ -385,14 +450,14 @@ void MoveBase2Config::fromLegacyNodeHandle(robot::NodeHandle& nh)
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 được dịch thành %.3f s "
"(một chu kỳ điều khiển), không phải 'tắt'.", one_cycle);
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 được dịch thành %.3f s "
"(một chu kỳ điều khiển), không phải 'tắt'.", one_cycle);
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;
}
}
@@ -407,21 +472,29 @@ MoveBase2Config MoveBase2Config::load(robot::NodeHandle& root_nh)
robot::NodeHandle modern_nh(root_nh, "move_base2");
if (modern_nh.hasParam("controller_frequency"))
{
robot::log_info("[move_base2] dùng schema mới (namespace 'move_base2').");
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] không thấy namespace 'move_base2'; đọc theo schema gen-1 của "
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] không tìm thấy cấu hình nào — chạy với toàn bộ giá trị mặc định. "
"Kiểm PNKX_NAV_CORE_CONFIG_DIR và sự tồn tại của file 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;
}
@@ -433,10 +506,14 @@ ControlLoopConfig MoveBase2Config::toControlLoopConfig() const
config.nominal_control_period =
controller_frequency > 0.0 ? 1.0 / controller_frequency : 0.05; // [s]
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;
return config;
}