This commit is contained in:
2026-08-06 15:17:59 +07:00
parent 701d25f952
commit 5375a9da9e
33 changed files with 3199 additions and 137 deletions

View File

@@ -15,6 +15,7 @@
#include <boost/dll/import.hpp>
#include <boost/system/system_error.hpp>
#include <boost/thread/locks.hpp>
#include <robot/plugin_loader_helper.h>
#include <robot/robot.h>
@@ -331,6 +332,13 @@ bool ControllerRunner::setPlan(const std::vector<robot_geometry_msgs::PoseStampe
try
{
// Plugin có thể chiếu plan lên costmap ngay trong setPlan — khoá như hai đường gọi còn lại.
boost::unique_lock<robot_costmap_2d::Costmap2D::mutex_t> costmap_lock;
if (costmap_lock_enabled_)
{
costmap_lock = boost::unique_lock<robot_costmap_2d::Costmap2D::mutex_t>(
*(costmap_->getCostmap()->getMutex()));
}
// Gen-2 tách goal khỏi plan: `setGoalPose` phải gọi TRƯỚC `setPlan`, đúng như
// `LocalPlannerAdapter` làm. Goal là pose CUỐI của plan.
const robot_nav_2d_msgs::Path2D path = robot_nav_2d_utils::posesToPath2D(plan);
@@ -475,6 +483,15 @@ bool ControllerRunner::computeVelocityCommands(robot_geometry_msgs::Twist& cmd)
// Đo đúng lời gọi vào plugin, không đo cả hàm: phần còn lại (tra pose, kiểm NaN) là chi phí của
// move_base2, còn đây mới là chi phí của local planner đang cấu hình.
ScopedSection timer(stats_, section_compute_);
// Khoá lưới local costmap trong lúc plugin tính lệnh — gen-1 giữ đúng lock này quanh mọi
// computeVelocityCommands (move_base.cpp:1078). Thread cập nhật costmap ghi lưới dưới cùng
// mutex; không khoá thì cost function đọc dữ liệu đang bị ghi dở.
boost::unique_lock<robot_costmap_2d::Costmap2D::mutex_t> costmap_lock;
if (costmap_lock_enabled_)
{
costmap_lock = boost::unique_lock<robot_costmap_2d::Costmap2D::mutex_t>(
*(costmap_->getCostmap()->getMutex()));
}
// Gen-2 trả THẲNG lệnh (không có cờ thành công/thất bại) và ném exception khi không tính được —
// ngược với gen-1. Vì vậy nhánh "không có lệnh hợp lệ" ở đây là nhánh catch.
const robot_nav_2d_msgs::Twist2DStamped cmd_2d =
@@ -524,6 +541,13 @@ bool ControllerRunner::isGoalReached()
try
{
// Cùng lý do với computeVelocityCommands: plugin có thể dò costmap khi kiểm đích.
boost::unique_lock<robot_costmap_2d::Costmap2D::mutex_t> costmap_lock;
if (costmap_lock_enabled_)
{
costmap_lock = boost::unique_lock<robot_costmap_2d::Costmap2D::mutex_t>(
*(costmap_->getCostmap()->getMutex()));
}
const bool reached =
active_->isGoalReached(pose, robot_nav_2d_utils::twist3Dto2D(measured_velocity_));
if (reached)

View File

@@ -14,9 +14,11 @@
#include <boost/dll/import.hpp>
#include <boost/system/system_error.hpp>
#include <boost/thread/locks.hpp>
#include <robot/plugin_loader_helper.h>
#include <robot/robot.h>
#include <robot_costmap_2d/costmap_2d_robot.h>
namespace move_base2
{
@@ -364,6 +366,17 @@ void PlannerRunner::threadBody()
try
{
ScopedSection timer(stats_, section_make_plan_);
// Khoá lưới costmap suốt makePlan — cùng giao thức với gen-1 (move_base.cpp:1921,1959):
// LayeredCostmap::updateMap ghi lưới, và resizeMap REALLOC cả mảng, dưới đúng mutex này.
// Không khoá thì planner đọc cost đang bị ghi dở, hoặc tệ hơn là đọc vào vùng nhớ vừa bị
// resize giải phóng khi static map được nhận lại. Lấy getCostmap() mỗi lượt, không cache —
// con trỏ đó có thể bị thay (bài học double-free của workspace).
boost::unique_lock<robot_costmap_2d::Costmap2D::mutex_t> costmap_lock;
if (costmap_lock_enabled_)
{
costmap_lock = boost::unique_lock<robot_costmap_2d::Costmap2D::mutex_t>(
*(costmap_->getCostmap()->getMutex()));
}
// Hai overload của interface gốc gộp lại: "có Order hay không" là một nhánh, không phải hai
// contract. Plugin nào không hiểu Order thì overload mặc định của nó tự lo.
ok = (order != nullptr) ? planner->makePlan(*order, start, goal, planning_)

View File

@@ -6,7 +6,6 @@
#include <move_base2/runners/recovery_runner.h>
#include <map>
#include <set>
#include <utility>
#include <robot/robot.h>
@@ -29,6 +28,10 @@ recovery_core::RecoveryTrigger toCoreTrigger(RecoveryTrigger trigger)
return recovery_core::RecoveryTrigger::kControllingFailed;
case RecoveryTrigger::kOscillation:
return recovery_core::RecoveryTrigger::kOscillation;
case RecoveryTrigger::kPathBlocked:
return recovery_core::RecoveryTrigger::kPathBlocked;
case RecoveryTrigger::kOffPath:
return recovery_core::RecoveryTrigger::kOffPath;
}
return recovery_core::RecoveryTrigger::kUnspecified;
}
@@ -69,6 +72,11 @@ void RecoveryRunner::setNamespace(const std::string& ns)
namespace_ = ns;
}
void RecoveryRunner::setReferencePlanSource(PlanSource source)
{
reference_plan_bridge_.setSource(std::move(source));
}
void RecoveryRunner::setPlanSource(PlanSource source)
{
plan_bridge_.setSource(std::move(source));
@@ -84,8 +92,10 @@ void RecoveryRunner::refreshContext()
ctx_.pose = &pose_bridge_;
ctx_.collision = &collision_;
ctx_.plan = &plan_bridge_;
ctx_.reference_plan = &reference_plan_bridge_;
ctx_.local_costmap = deps_.local_costmap;
ctx_.global_costmap = deps_.global_costmap;
ctx_.tf = deps_.tf;
}
bool RecoveryRunner::configure(robot::NodeHandle& nh)
@@ -178,13 +188,20 @@ bool RecoveryRunner::configureRoutes(robot::NodeHandle& nh, std::string& error)
return false;
}
// Ba route GỐC bắt buộc phải có; hai route giám sát tuyến là TUỲ CHỌN — thiếu chúng nghĩa là
// cơ chế giám sát tắt, và mọi config đang chạy (viết trước 2026-08-04d) vẫn boot được.
const std::pair<const char*, RecoveryTrigger> expected_routes[] = {
{"planning_failed", RecoveryTrigger::kPlanningFailed},
{"controlling_failed", RecoveryTrigger::kControllingFailed},
{"oscillation", RecoveryTrigger::kOscillation},
};
const auto is_expected_key = [&expected_routes](const std::string& key) {
const std::pair<const char*, RecoveryTrigger> optional_routes[] = {
{"path_blocked", RecoveryTrigger::kPathBlocked},
{"off_path", RecoveryTrigger::kOffPath},
};
const auto is_expected_key = [&expected_routes, &optional_routes](const std::string& key) {
for (const auto& expected : expected_routes)
{
if (key == expected.first)
@@ -192,6 +209,13 @@ bool RecoveryRunner::configureRoutes(robot::NodeHandle& nh, std::string& error)
return true;
}
}
for (const auto& optional : optional_routes)
{
if (key == optional.first)
{
return true;
}
}
return false;
};
@@ -210,32 +234,36 @@ bool RecoveryRunner::configureRoutes(robot::NodeHandle& nh, std::string& error)
}
}
for (const auto& expected : expected_routes)
{
const std::string trigger_name(expected.first);
const YAML::Node route_node = configured_routes[trigger_name];
if (!route_node || !route_node.IsSequence())
{
error = routes_key + "/" + trigger_name + " must be a YAML sequence";
return false;
}
std::vector<std::size_t>* resolved = nullptr;
switch (expected.second)
const auto route_for = [this](RecoveryTrigger trigger) -> std::vector<std::size_t>* {
switch (trigger)
{
case RecoveryTrigger::kPlanningFailed:
resolved = &routes_.planning_failed;
break;
return &routes_.planning_failed;
case RecoveryTrigger::kControllingFailed:
resolved = &routes_.controlling_failed;
break;
return &routes_.controlling_failed;
case RecoveryTrigger::kOscillation:
resolved = &routes_.oscillation;
break;
return &routes_.oscillation;
case RecoveryTrigger::kPathBlocked:
return &routes_.path_blocked;
case RecoveryTrigger::kOffPath:
return &routes_.off_path;
}
return &routes_.planning_failed;
};
// Resolve tên instance -> index registry cho MỘT route. Trả false kèm `error` khi schema sai;
// tên không nạp được thì bỏ qua kèm cảnh báo (cho phép commit schema trước plugin).
//
// Một behavior ĐƯỢC PHÉP xuất hiện nhiều lần trong cùng route: mỗi lần xuất hiện là một lượt thử.
// Đây là cách duy nhất cấp ngân sách cho vòng "thử detour — chờ — nhích lại gần — thử lại"
// (2026-08-05): cursor chỉ reset khi robot đi được quá oscillation_distance, nhưng giữa hai lần
// monitor bắn robot chỉ nhích được ~0.1-0.15 m, nên route không lặp thì cạn ngay sau một cặp
// [detour, wait] dù vật cản sắp vào tầm. Route vẫn hữu hạn — bế tắc thật vẫn ABORT được.
const auto resolve_route = [&](const char* trigger_name, RecoveryTrigger trigger,
const YAML::Node& route_node) {
std::vector<std::size_t>* resolved = route_for(trigger);
resolved->clear();
std::set<std::size_t> seen;
for (const YAML::Node& behavior_node : route_node)
{
if (!behavior_node.IsScalar())
@@ -248,33 +276,75 @@ bool RecoveryRunner::configureRoutes(robot::NodeHandle& nh, std::string& error)
const auto found = loaded.find(behavior_name);
if (found == loaded.end())
{
// Đây là điểm cho phép commit schema trước plugin. Không tạo index giả: StateMachine chỉ
// được thấy những index registry thực sự nạp được.
// Không tạo index giả: StateMachine chỉ được thấy index registry thực sự nạp được.
robot::log_warning("[move_base2] RecoveryRunner: route '%s' skips '%s' because that "
"behavior was not loaded.",
trigger_name.c_str(), behavior_name.c_str());
trigger_name, behavior_name.c_str());
continue;
}
if (!seen.insert(found->second).second)
{
error = routes_key + "/" + trigger_name + " repeats behavior '" + behavior_name + "'";
return false;
}
resolved->push_back(found->second);
}
return true;
};
if (resolved->empty())
for (const auto& expected : expected_routes)
{
const std::string trigger_name(expected.first);
const YAML::Node route_node = configured_routes[trigger_name];
if (!route_node || !route_node.IsSequence())
{
error = routes_key + "/" + trigger_name +
" has no behavior that was successfully loaded";
error = routes_key + "/" + trigger_name + " must be a YAML sequence";
return false;
}
if (!resolve_route(expected.first, expected.second, route_node))
{
return false;
}
if (route_for(expected.second)->empty())
{
error = routes_key + "/" + trigger_name + " has no behavior that was successfully loaded";
return false;
}
}
for (const auto& optional : optional_routes)
{
const std::string trigger_name(optional.first);
const YAML::Node route_node = configured_routes[trigger_name];
if (!route_node)
{
// Không khai = cơ chế giám sát tuyến tắt. Đây là mặc định và là đường lùi.
continue;
}
if (!route_node.IsSequence())
{
error = routes_key + "/" + trigger_name + " must be a YAML sequence";
return false;
}
if (!resolve_route(optional.first, optional.second, route_node))
{
return false;
}
if (route_for(optional.second)->empty())
{
// Khai route nhưng không behavior nào nạp được: KHÔNG chặn khởi động (navigation vẫn phải
// chạy), nhưng phải hét lên — im lặng ở đây là người vận hành tưởng giám sát đang bật.
robot::log_error("[move_base2] RecoveryRunner: route '%s' was configured but none of its "
"behaviors loaded — that monitor stays DISABLED.",
trigger_name.c_str());
}
}
robot::log_info("[move_base2] RecoveryRunner: routes resolved: planning=%zu controlling=%zu "
"oscillation=%zu.",
"oscillation=%zu path_blocked=%zu off_path=%zu.",
routes_.planning_failed.size(), routes_.controlling_failed.size(),
routes_.oscillation.size());
routes_.oscillation.size(), routes_.path_blocked.size(),
routes_.off_path.size());
return true;
}
@@ -333,6 +403,8 @@ bool RecoveryRunner::start(std::size_t index, RecoveryTrigger trigger)
}
active_ = behavior;
active_name_ = registry_.nameAt(index);
active_trigger_ = trigger;
// Log tại SƯỜN (một dòng cho mỗi lượt khởi động, không nằm trên đường tick). Không có dòng này
// thì trên robot/sim không cách nào biết đang chạy behavior nào: các plugin chỉ log khi chúng TỪ
@@ -358,7 +430,30 @@ RecoveryTick RecoveryRunner::update()
}
refreshContext();
return toTick(active_->update(deps_.clock->now()));
const RecoveryTick result = toTick(active_->update(deps_.clock->now()));
// Log tại SƯỜN KẾT THÚC, một dòng cho mỗi lượt (không nằm trên đường tick: kRunning không log).
//
// Không có nó thì một behavior CHẠY RỒI HỎNG hoàn toàn im lặng: bên gọi chỉ thấy state machine
// ghi `recovery=failed` lẫn giữa dòng chuyển state, còn `message` mà behavior đặt sẵn (vd
// "no reachable rejoin point on the plan after 3 attempt(s)") thì không ai in ra. Sim 2026-08-05:
// detour hỏng cả 3 attempt -> route cạn -> ABORTED, và trong log không có chữ "fail" nào của
// recovery. Nhánh TỪ CHỐI KHỞI ĐỘNG đã có log riêng ở start().
if (result.status == RecoveryTick::Status::kFailed)
{
robot::log_warning("[move_base2] RecoveryRunner: behavior '%s' FAILED (trigger %s): %s",
active_name_.c_str(), toString(active_trigger_),
result.message.empty() ? "no reason given" : result.message.c_str());
}
else if (result.status == RecoveryTick::Status::kSucceeded)
{
robot::log_info("[move_base2] RecoveryRunner: behavior '%s' succeeded (trigger %s)%s: %s",
active_name_.c_str(), toString(active_trigger_),
result.has_path ? " with a new path" : "",
result.message.empty() ? "done" : result.message.c_str());
}
return result;
}
void RecoveryRunner::cancel()