- SensorGateway::pushStaticMap also dispatches to DIRECTIONAL_LAYER on the global costmap, before STATIC_LAYER: with a same-geometry map the directional layer goes PENDING before StaticLayer can report current, so a plan cannot pair the new map with the old zones. - Static maps are replayed in arrival order instead of topic order, so a direction grid is never replayed before its static map. - PlannerRunner checks every enabled global layer under the same lock as the snapshot. While a layer is not current it keeps the goal, reports waiting_for_costmap, logs the stale layers (throttled) and spends no planner_patience or watchdog budget. After costmap_ready_timeout [s] (default 10, 0 = legacy behaviour) the attempt fails as before. The key is added to config/runtime/move_base_common_params.yaml. - Test fixture destroys the server before its fake ports (the control thread could call a destroyed port). Full history: branch feature/direction-zone.
764 lines
31 KiB
C++
764 lines
31 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 readDebug(robot::NodeHandle& nh, MoveBase2Config& config);
|
|
void readEnvMonitor(robot::NodeHandle& nh, MoveBase2Config& config);
|
|
|
|
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();
|
|
}
|
|
}
|
|
|
|
/// 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, "costmap_ready_timeout", config.costmap_ready_timeout);
|
|
readDouble(nh, "runtime_stats_period", config.runtime_stats_period);
|
|
readDouble(nh, "watchdog_period", config.watchdog.period);
|
|
readDouble(nh, "local_planner_call_budget", config.watchdog.local_planner_call_budget);
|
|
readDouble(nh, "recovery_stall_budget", config.watchdog.recovery_stall_budget);
|
|
readDouble(nh, "action_stall_budget", config.watchdog.action_stall_budget);
|
|
readDouble(nh, "control_loop_stall_budget", config.watchdog.control_loop_stall_budget);
|
|
config.watchdog.planner_timeout = config.planner_timeout;
|
|
|
|
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, "recovery_reset_min_travel", config.state_machine.recovery_reset_min_travel);
|
|
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, "settle_timeout", config.state_machine.settle_timeout);
|
|
readDouble(nh, "stopped_measured_linear_eps", config.state_machine.stopped_measured_linear_eps);
|
|
readDouble(nh, "stopped_measured_angular_eps", config.state_machine.stopped_measured_angular_eps);
|
|
|
|
// 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);
|
|
readDebug(nh, config);
|
|
readEnvMonitor(nh, config);
|
|
|
|
readString(nh, "recovery_namespace", config.recovery_namespace);
|
|
readString(nh, "action_namespace", config.action_namespace);
|
|
readString(nh, "mission_namespace", config.mission_namespace);
|
|
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ông tắc log chẩn đoán từ namespace con `debug`.
|
|
void readDebug(robot::NodeHandle& nh, MoveBase2Config& config)
|
|
{
|
|
robot::NodeHandle debug_nh(nh, "debug");
|
|
readBool(debug_nh, "state", config.debug_state);
|
|
readBool(debug_nh, "plan", config.debug_plan);
|
|
readBool(debug_nh, "skip", config.debug_skip);
|
|
readBool(debug_nh, "env", config.debug_env);
|
|
}
|
|
|
|
/// Đọc khối `env_monitor` (xem config/runtime/env_monitor_params.yaml).
|
|
void readEnvMonitor(robot::NodeHandle& nh, MoveBase2Config& config)
|
|
{
|
|
robot::NodeHandle env_nh(nh, "env_monitor");
|
|
EnvMonitorConfig& m = config.env_monitor;
|
|
EnvSamplingConfig& s = config.env_sampling;
|
|
|
|
readBool(env_nh, "enabled", m.enabled);
|
|
readDouble(env_nh, "grace_after_new_plan", m.grace_after_new_plan);
|
|
readDouble(env_nh, "check_period", s.check_period);
|
|
|
|
robot::NodeHandle body_nh(env_nh, "body");
|
|
readDouble(body_nh, "front_offset", s.body_front);
|
|
readDouble(body_nh, "rear_offset", s.body_rear);
|
|
readDouble(body_nh, "side_offset", s.body_side);
|
|
readDouble(body_nh, "padding", s.padding);
|
|
readInt(body_nh, "lethal_threshold", s.lethal_threshold);
|
|
|
|
robot::NodeHandle geo_nh(env_nh, "geometry");
|
|
readDouble(geo_nh, "horizon", s.horizon);
|
|
readDouble(geo_nh, "corridor_margin", s.corridor_margin);
|
|
readDouble(geo_nh, "corridor_cap", s.corridor_cap);
|
|
readDouble(geo_nh, "target_backoff", s.target_backoff);
|
|
readDouble(geo_nh, "robot_snap", s.robot_snap);
|
|
readDouble(geo_nh, "track_back", s.track_back);
|
|
readDouble(geo_nh, "track_ahead", s.track_ahead);
|
|
readDouble(geo_nh, "exit_ahead", s.exit_ahead);
|
|
readDouble(geo_nh, "target_snap", s.target_snap);
|
|
|
|
robot::NodeHandle stall_nh(env_nh, "stall");
|
|
readBool(stall_nh, "enabled", m.stall.enabled);
|
|
readString(stall_nh, "action", m.stall.action);
|
|
readDouble(stall_nh, "window_s", m.stall.window_s);
|
|
readDouble(stall_nh, "window_m", m.stall.window_m);
|
|
readDouble(stall_nh, "min_commanded", m.stall.min_commanded);
|
|
readDouble(stall_nh, "eff_on", m.stall.eff_on);
|
|
readDouble(stall_nh, "eff_off", m.stall.eff_off);
|
|
readDouble(stall_nh, "confirm", m.stall.confirm);
|
|
readDouble(stall_nh, "goal_zone", m.stall.goal_zone);
|
|
readDouble(stall_nh, "slip_on", m.stall.slip_on);
|
|
readDouble(stall_nh, "slip_time", m.stall.slip_time);
|
|
readDouble(stall_nh, "min_commanded_since_goal", m.stall.min_commanded_since_goal);
|
|
readDouble(stall_nh, "retry_backoff", m.stall.retry_backoff);
|
|
readDouble(stall_nh, "jump_reject", m.stall.jump_reject);
|
|
readDouble(stall_nh, "disp_window_s", m.stall.disp_window_s);
|
|
readDouble(stall_nh, "disp_min", m.stall.disp_min);
|
|
readDouble(stall_nh, "disp_min_commanded", m.stall.disp_min_commanded);
|
|
readDouble(stall_nh, "idle_window_s", m.stall.idle_window_s);
|
|
readDouble(stall_nh, "confine_window_s", m.stall.confine_window_s);
|
|
readDouble(stall_nh, "confine_radius", m.stall.confine_radius);
|
|
readDouble(stall_nh, "confine_cmd_factor", m.stall.confine_cmd_factor);
|
|
|
|
robot::NodeHandle unreach_nh(env_nh, "unreachable");
|
|
readBool(unreach_nh, "enabled", m.unreachable.enabled);
|
|
readString(unreach_nh, "action", m.unreachable.action);
|
|
readDouble(unreach_nh, "static_on", m.unreachable.static_on);
|
|
readDouble(unreach_nh, "clean_start_margin", m.unreachable.clean_start_margin);
|
|
readDouble(unreach_nh, "retry_backoff", m.unreachable.retry_backoff);
|
|
|
|
robot::NodeHandle contact_nh(env_nh, "contact");
|
|
readBool(contact_nh, "enabled", m.contact.enabled);
|
|
readString(contact_nh, "action", m.contact.action);
|
|
readBool(contact_nh, "apply_command", m.contact.apply_command);
|
|
readString(contact_nh, "use_local_plan", s.use_local_plan);
|
|
readDouble(contact_nh, "local_plan_start_tol", s.local_plan_start_tol);
|
|
readDouble(contact_nh, "lookahead", s.contact_lookahead);
|
|
readInt(contact_nh, "confirm_cycles", m.contact.confirm_cycles);
|
|
readDouble(contact_nh, "trigger_static_time", m.contact.trigger_static_time);
|
|
readDouble(contact_nh, "clean_start_margin", m.contact.clean_start_margin);
|
|
readDouble(contact_nh, "brake_decel", m.contact.brake_decel);
|
|
readDouble(contact_nh, "min_speed", m.contact.min_speed);
|
|
readDouble(contact_nh, "min_check_speed", m.contact.min_check_speed);
|
|
readDouble(contact_nh, "release_hysteresis", m.contact.release_hysteresis);
|
|
readInt(contact_nh, "release_confirm_cycles", m.contact.release_confirm_cycles);
|
|
readDouble(contact_nh, "envelope_margin", m.contact.envelope_margin);
|
|
readDouble(contact_nh, "standdown_min_travel", m.contact.standdown_min_travel);
|
|
readDouble(contact_nh, "standdown_travel", m.contact.standdown_travel);
|
|
readBool(contact_nh, "envelope", m.contact.envelope);
|
|
|
|
robot::NodeHandle budget_nh(env_nh, "replan_budget");
|
|
readDouble(budget_nh, "min_travel", m.replan.min_travel);
|
|
readDouble(budget_nh, "escalate_factor", m.replan.escalate_factor);
|
|
readDouble(budget_nh, "escalate_window_m", m.replan.escalate_window_m);
|
|
readDouble(budget_nh, "escalate_max", m.replan.escalate_max);
|
|
|
|
robot::NodeHandle logger_nh(env_nh, "logger");
|
|
readBool(logger_nh, "enabled", config.env_logger.enabled);
|
|
readString(logger_nh, "log_dir", config.env_logger.log_dir);
|
|
readDouble(logger_nh, "flush_period", config.env_logger.flush_period);
|
|
|
|
std::vector<std::string> names;
|
|
env_nh.param("profiles", names, std::vector<std::string>{});
|
|
if (!names.empty())
|
|
{
|
|
s.profiles.clear();
|
|
for (const std::string& name : names)
|
|
{
|
|
if (name == "position")
|
|
{
|
|
s.profiles.push_back(MotionProfile::kPosition);
|
|
}
|
|
else if (name == "docking")
|
|
{
|
|
s.profiles.push_back(MotionProfile::kDocking);
|
|
}
|
|
else if (name == "go_straight")
|
|
{
|
|
s.profiles.push_back(MotionProfile::kGoStraight);
|
|
}
|
|
else if (name == "rotate")
|
|
{
|
|
s.profiles.push_back(MotionProfile::kRotate);
|
|
}
|
|
else
|
|
{
|
|
robot::log_warning("[move_base2] env_monitor/profiles: unknown profile '%s', ignored.\n",
|
|
name.c_str());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Đọ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 (!std::isfinite(costmap_ready_timeout) || costmap_ready_timeout < 0.0)
|
|
{
|
|
error = "costmap_ready_timeout must be finite and >= 0 [s] (0 = legacy behavior)";
|
|
return false;
|
|
}
|
|
if (!watchdog.validate(error))
|
|
{
|
|
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_namespace.empty())
|
|
{
|
|
error = "mission_namespace is empty — there would be no namespace to read `mission_sources` "
|
|
"from, and the mission layer is always built";
|
|
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 (env_monitor.enabled)
|
|
{
|
|
if (!env_monitor.validate(error) || !env_sampling.validate(error) || !env_logger.validate(error))
|
|
{
|
|
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 << " costmap_ready_timeout: " << costmap_ready_timeout << " s"
|
|
<< (costmap_ready_timeout == 0.0 ? " (readiness check off)\n" : "\n");
|
|
out << " runtime_stats_period: " << runtime_stats_period << " s"
|
|
<< (runtime_stats_period > 0.0 ? "\n" : " (off)\n");
|
|
out << watchdog.describe();
|
|
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";
|
|
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);
|
|
}
|
|
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 << " debug: state=" << (debug_state ? "on" : "off")
|
|
<< " plan=" << (debug_plan ? "on" : "off")
|
|
<< " skip=" << (debug_skip ? "on" : "off") << "\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);
|
|
readDouble(nh, "costmap_ready_timeout", costmap_ready_timeout);
|
|
// 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);
|
|
// Watchdog (2026-09-11) — không có trong schema gen-1; thiếu khoá thì default (bật, 0.1 s).
|
|
readDouble(nh, "watchdog_period", watchdog.period);
|
|
readDouble(nh, "local_planner_call_budget", watchdog.local_planner_call_budget);
|
|
readDouble(nh, "recovery_stall_budget", watchdog.recovery_stall_budget);
|
|
readDouble(nh, "action_stall_budget", watchdog.action_stall_budget);
|
|
readDouble(nh, "control_loop_stall_budget", watchdog.control_loop_stall_budget);
|
|
watchdog.planner_timeout = planner_timeout;
|
|
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);
|
|
// Không có trong schema gen-1 (bản cũ báo CANCELLED ngay khi lệnh về 0) — đọc để cây config cũ
|
|
// vẫn bật được chờ quiescent; thiếu khoá thì dùng default (bật, 2 s).
|
|
readDouble(nh, "settle_timeout", state_machine.settle_timeout);
|
|
readDouble(nh, "stopped_measured_linear_eps", state_machine.stopped_measured_linear_eps);
|
|
readDouble(nh, "stopped_measured_angular_eps", state_machine.stopped_measured_angular_eps);
|
|
|
|
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);
|
|
|
|
// 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.
|
|
readString(nh, "mission_namespace", mission_namespace);
|
|
|
|
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.debug_state = debug_state;
|
|
config.debug_plan = debug_plan;
|
|
config.debug_skip = debug_skip;
|
|
config.state_machine = state_machine;
|
|
config.costmap_ready_timeout = costmap_ready_timeout;
|
|
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.env_monitor = env_monitor;
|
|
config.env_sampling = env_sampling;
|
|
config.env_logger = env_logger;
|
|
config.debug_env = debug_env;
|
|
return config;
|
|
}
|
|
|
|
} // namespace move_base2
|