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

@@ -34,7 +34,7 @@ NavigationServer::NavigationServer()
// ngay từ lúc dựng — trước cả initialize().
nav_feedback_ = std::make_shared<robot::move_base_core::NavFeedback>();
nav_feedback_->navigation_state = robot::move_base_core::State::PENDING;
nav_feedback_->feed_back_str = "chưa khởi tạo";
nav_feedback_->feed_back_str = "not initialized";
nav_feedback_->goal_checked = false;
nav_feedback_->is_ready = false;
}
@@ -60,7 +60,7 @@ bool NavigationServer::startControlThread(double frequency)
{
if (!loop_.initialized())
{
robot::log_error("[move_base2] startControlThread() trước khi control loop được cấu hình.\n");
robot::log_error("[move_base2] startControlThread() before the control loop was configured.\n");
return false;
}
if (control_thread_running_.load())
@@ -69,23 +69,36 @@ bool NavigationServer::startControlThread(double frequency)
}
if (!(frequency > 0.0))
{
robot::log_error("[move_base2] controller_frequency phải > 0 [Hz], nhận %.3f.\n", frequency);
robot::log_error("[move_base2] controller_frequency must be > 0 [Hz], got %.3f.\n", frequency);
return false;
}
control_thread_running_.store(true);
control_thread_ = std::thread([this, frequency]() {
if (stats_ != nullptr)
{
stats_->registerCurrentThread("move_base2/control");
}
robot::Rate rate(frequency);
while (control_thread_running_.load())
{
// Bỏ qua giá trị trả về: false chỉ nghĩa là yêu cầu hiện tại vừa kết thúc, không phải lý do
// dừng vòng lặp — thread phải sống để nhận goal kế tiếp.
spinOnce();
// In bảng thống kê nằm NGOÀI phần đo của cycle: chi phí của chính công cụ đo không được tính
// vào chi phí của runtime, nếu không mỗi lần in lại thành một đỉnh giả trong cột "đỉnh [ms]".
if (stats_ != nullptr)
{
stats_->tick();
}
rate.sleep();
}
});
robot::log_info("[move_base2] control thread chạy ở %.2f Hz.\n", frequency);
robot::log_info("[move_base2] control thread running at %.2f Hz.\n", frequency);
return true;
}
@@ -108,7 +121,7 @@ bool NavigationServer::configureLoop(const ControlLoopConfig& config, const Cont
if (!loop_.configure(config, deps, error))
{
nav_feedback_->is_ready = false;
nav_feedback_->feed_back_str = "cấu hình lỗi: " + error;
nav_feedback_->feed_back_str = "config error: " + error;
return false;
}
@@ -116,7 +129,7 @@ bool NavigationServer::configureLoop(const ControlLoopConfig& config, const Cont
nav_feedback_->is_ready = true;
nav_feedback_->feed_back_str = "sẵn sàng";
nav_feedback_->feed_back_str = "ready";
refreshFeedback();
return true;
}
@@ -156,9 +169,14 @@ void NavigationServer::attachCostmaps(robot_costmap_2d::LayeredCostmap* global,
bool NavigationServer::spinOnce()
{
// Bao trọn thân hàm: đây là chi phí một cycle điều khiển, đối chiếu trực tiếp được với
// controller_frequency để biết control loop còn giữ được nhịp hay không.
ScopedSection cycle_timer(stats_, section_cycle_);
// Trước khi tính lệnh: đẩy xuống controller những gì host đã đặt từ thread của nó. Đặt ở đây chứ
// không ở cuối cycle để trần vận tốc có hiệu lực ngay trong chính cycle này — chậm một cycle
// nghĩa là một chu kỳ nữa chạy quá tốc độ mà tầng an toàn vừa yêu cầu hạ.
applyPendingFootprint();
pushHostInputsToController();
drainLifecycleRequests();
@@ -169,9 +187,16 @@ bool NavigationServer::spinOnce()
runtime_->mission().pumpPendingRequest();
}
const bool running = loop_.step();
bool running = false;
{
ScopedSection step_timer(stats_, section_step_);
running = loop_.step();
}
publishCommand();
cachePlans();
{
ScopedSection cache_timer(stats_, section_cache_plans_);
cachePlans();
}
refreshFeedback();
return running;
}
@@ -253,9 +278,31 @@ robot::move_base_core::State NavigationServer::toHostState(NavigationState state
return HostState::LOST;
}
bool NavigationServer::missionHasPendingWork() const
{
return runtime_ != nullptr && runtime_->missionLayer().started() &&
runtime_->mission().hasActiveMission();
}
void NavigationServer::refreshFeedback()
{
nav_feedback_->navigation_state = toHostState(loop_.state());
robot::move_base_core::State host_state = toHostState(loop_.state());
// Một order nhiều chặng: lõi về SUCCEEDED sau MỖI chặng, còn order thì chưa xong. Host suy "order
// hoàn thành" thẳng từ SUCCEEDED (amr_vda_5050_client_api.cpp:1062, 1092), nên báo nguyên trạng
// là báo cho fleet master rằng robot đã tới node cuối trong khi nó mới đi được nửa tuyến.
//
// ACTIVE là ánh xạ đúng cho khoảng giữa hai chặng: "yêu cầu đang được xử lý, chưa xong" — và
// KHÔNG phải CONTROLLING, vì robot lúc này đứng yên chờ chặng kế tiếp (host suy `driving` từ
// CONTROLLING, :1233). PENDING cũng phải che: lõi rơi về IDLE trong đúng cycle trước khi chặng
// sau được đẩy xuống.
if (missionHasPendingWork() && (host_state == robot::move_base_core::State::SUCCEEDED ||
host_state == robot::move_base_core::State::PENDING))
{
host_state = robot::move_base_core::State::ACTIVE;
}
nav_feedback_->navigation_state = host_state;
const char* reason = loop_.lastReason();
if (reason != nullptr && reason[0] != '\0')
@@ -293,17 +340,35 @@ void NavigationServer::initialize(robot::TFListenerPtr tf)
if (!runtime_->buildCostmaps(tf_, error))
{
runtime_.reset();
stats_ = nullptr;
nav_feedback_->is_ready = false;
nav_feedback_->feed_back_str = "không dựng được costmap: " + error;
robot::log_error("[move_base2] initialize() thất bại: %s\n", error.c_str());
nav_feedback_->feed_back_str = "could not build costmap: " + error;
robot::log_error("[move_base2] initialize() failed: %s\n", error.c_str());
return;
}
// Telemetry đã tồn tại từ buildCostmaps (nó phải có mặt trước costmap để chụp được thread của
// costmap). Mượn con trỏ và đăng ký các đoạn công việc của chính server ở đây, một lần.
stats_ = runtime_->stats();
if (stats_ != nullptr && stats_->enabled())
{
section_cycle_ = stats_->section("control.cycle");
section_step_ = stats_->section("control.step");
section_cache_plans_ = stats_->section("control.cachePlans");
robot::log_info("[move_base2] telemetry ON — printing stats every %.2f s.\n",
runtime_->config().runtime_stats_period);
}
// Đường nạp cảm biến chạy trên thread callback của host; đo bằng đoạn công việc mới tách được nó
// ra khỏi phần "(không đăng ký)".
sensors_.attachTelemetry(stats_);
if (!configureSensors(runtime_->config().sensors, error))
{
runtime_.reset();
stats_ = nullptr;
nav_feedback_->is_ready = false;
nav_feedback_->feed_back_str = "cấu hình cảm biến lỗi: " + error;
nav_feedback_->feed_back_str = "sensor config error: " + error;
return;
}
@@ -324,20 +389,27 @@ void NavigationServer::initialize(robot::TFListenerPtr tf)
attachCostmaps(runtime_->globalCostmap()->getLayeredCostmap(),
runtime_->localCostmap()->getLayeredCostmap());
// Host có thể đã đặt footprint ngay sau khi tạo BaseNavigation nhưng trước initialize(). Áp nó
// vào cả hai costmap TRƯỚC khi dựng runner để local planner đầu tiên snapshot đúng hình robot,
// thay vì khởi tạo với footprint YAML rồi lập tức phải recreate trong cycle đầu tiên.
applyPendingFootprint();
// --- Pha 2: nạp planner, controller, recovery, action ----------------------------------------
if (!runtime_->buildRunners(error))
{
runtime_.reset();
stats_ = nullptr;
nav_feedback_->is_ready = false;
nav_feedback_->feed_back_str = "không nạp được runtime: " + error;
robot::log_error("[move_base2] initialize() thất bại: %s\n", error.c_str());
nav_feedback_->feed_back_str = "could not load runtime: " + error;
robot::log_error("[move_base2] initialize() failed: %s\n", error.c_str());
return;
}
if (!configureLoop(runtime_->config().toControlLoopConfig(), runtime_->deps(), error))
{
runtime_.reset();
robot::log_error("[move_base2] initialize() không cấu hình được control loop: %s\n",
stats_ = nullptr;
robot::log_error("[move_base2] initialize() could not configure the control loop: %s\n",
error.c_str());
return;
}
@@ -349,12 +421,20 @@ void NavigationServer::initialize(robot::TFListenerPtr tf)
if (!loop_.submit(request, reason))
{
last_reject_reason_ = reason;
robot::log_error("[move_base2] từ chối chặng mission %llu: %s\n",
robot::log_error("[move_base2] rejecting mission leg %llu: %s\n",
static_cast<unsigned long long>(request.mission_sequence_id),
reason.c_str());
// Bắt buộc báo ngược: chặng bị lõi từ chối sẽ không bao giờ sinh ra outcome theo đường bình
// thường, mà mission layer đã chuyển sang RUNNING lúc giao nó. Im lặng ở đây là cả order treo
// vĩnh viễn ở chặng đó — fleet master chờ một node không bao giờ tới.
runtime_->mission().reportOutcome(request.mission_sequence_id, NavigationOutcome::kFailed);
}
});
runtime_->mission().setCancelCallback([this]() { cancel(); });
// CHỈ huỷ chặng đang chạy: yêu cầu này vừa đi ra từ chính mission layer, gọi cancel() đầy đủ sẽ
// vòng ngược lên xoá hàng đợi của nó.
runtime_->mission().setCancelCallback([this]() { requestLoopCancel(); });
// start() sau cùng: cho thread cập nhật costmap chạy khi mọi thứ khác đã lắp xong.
runtime_->start();
@@ -364,13 +444,14 @@ void NavigationServer::initialize(robot::TFListenerPtr tf)
if (!startControlThread(runtime_->config().controller_frequency))
{
runtime_.reset();
stats_ = nullptr;
nav_feedback_->is_ready = false;
nav_feedback_->feed_back_str = "không khởi động được control thread";
nav_feedback_->feed_back_str = "could not start the control thread";
return;
}
nav_feedback_->is_ready = true;
nav_feedback_->feed_back_str = "sẵn sàng";
nav_feedback_->feed_back_str = "ready";
refreshFeedback();
}
@@ -382,6 +463,7 @@ void NavigationServer::setRobotFootprint(const std::vector<robot_geometry_msgs::
{
std::lock_guard<std::mutex> lock(data_mutex_);
footprint_ = fprt;
footprint_pending_ = true;
}
std::vector<robot_geometry_msgs::Point> NavigationServer::getRobotFootprint()
@@ -390,6 +472,31 @@ std::vector<robot_geometry_msgs::Point> NavigationServer::getRobotFootprint()
return footprint_;
}
void NavigationServer::applyPendingFootprint()
{
if (runtime_ == nullptr)
{
return;
}
std::vector<robot_geometry_msgs::Point> footprint;
{
std::lock_guard<std::mutex> lock(data_mutex_);
if (!footprint_pending_)
{
return;
}
footprint = footprint_;
footprint_pending_ = false;
}
if (!runtime_->setRobotFootprint(footprint))
{
robot::log_error("[move_base2] NavigationServer: footprint update was not fully applied to "
"the navigation runtime.\n");
}
}
// ================================================================================================
// Nhận dữ liệu sensor
// ================================================================================================
@@ -595,18 +702,33 @@ bool NavigationServer::submit(const NavigationRequest& request)
}
last_reject_reason_ = reason;
nav_feedback_->feed_back_str = "từ chối yêu cầu: " + reason;
nav_feedback_->feed_back_str = "request rejected: " + reason;
return false;
}
bool NavigationServer::moveTo(const robot_geometry_msgs::PoseStamped& goal,
double xy_goal_tolerance, double yaw_goal_tolerance)
{
(void)xy_goal_tolerance;
(void)yaw_goal_tolerance;
// Goal đơn lẻ từ host (RViz /move_base_simple/goal, OPC-UA, ...) cũng là một nguồn mission.
// Nếu bỏ qua GoalSourceAdapter thì request vào ControlLoop có mission_sequence_id == 0 và mất
// lifecycle/cancel thống nhất với VDA5050. GoalSourceAdapter chịu trách nhiệm validate pose, tạo
// SIMPLE_GOAL và gán profile `position`; MissionManager cấp mission id khác 0 trước khi bridge
// giao chặng xuống đây.
//
// Fallback trực tiếp chỉ dành cho cấu hình tương thích cũ: mission layer bị tắt, chưa khởi động,
// hoặc không nạp GoalSourceAdapter. Không được coi false là goal đã được layer nhận.
if (runtime_ != nullptr && runtime_->missionLayer().submitGoal(goal))
{
last_reject_reason_.clear();
return true;
}
NavigationRequest request;
request.profile = MotionProfile::kPosition;
request.goal = goal;
request.tolerance.xy = xy_goal_tolerance;
request.tolerance.yaw = yaw_goal_tolerance;
return submit(request);
}
@@ -614,11 +736,23 @@ bool NavigationServer::moveTo(const robot_protocol_msgs::Order& msg,
const robot_geometry_msgs::PoseStamped& goal,
double xy_goal_tolerance, double yaw_goal_tolerance)
{
(void)xy_goal_tolerance;
(void)yaw_goal_tolerance;
// Order đi qua mission layer khi layer đang chạy: chỉ ở đó order mới được cắt thành từng chặng
// tại node có action, lọc theo `released`, và nối tiếp được khi fleet master release thêm horizon.
// Đẩy thẳng xuống lõi là dồn cả order thành MỘT goal — action ở node giữa đường không có chỗ chạy.
//
// `submitOrder` trả false khi layer tắt hoặc không có nguồn nhận schema `vda5050.order`; lúc đó
// rơi xuống đường trực tiếp bên dưới, đúng hành vi đã chạy được trên sim.
if (runtime_ != nullptr && runtime_->missionLayer().submitOrder(msg))
{
last_reject_reason_.clear();
return true;
}
NavigationRequest request;
request.profile = MotionProfile::kPosition;
request.goal = goal;
request.tolerance.xy = xy_goal_tolerance;
request.tolerance.yaw = yaw_goal_tolerance;
request.order = std::make_shared<robot_protocol_msgs::Order>(msg);
return submit(request);
}
@@ -627,11 +761,11 @@ bool NavigationServer::dockTo(const std::string& maker,
const robot_geometry_msgs::PoseStamped& goal,
double xy_goal_tolerance, double yaw_goal_tolerance)
{
(void)xy_goal_tolerance;
(void)yaw_goal_tolerance;
NavigationRequest request;
request.profile = MotionProfile::kDocking;
request.goal = goal;
request.tolerance.xy = xy_goal_tolerance;
request.tolerance.yaw = yaw_goal_tolerance;
request.marker = maker;
return submit(request);
}
@@ -640,11 +774,11 @@ bool NavigationServer::dockTo(const robot_protocol_msgs::Order& msg, const std::
const robot_geometry_msgs::PoseStamped& goal,
double xy_goal_tolerance, double yaw_goal_tolerance)
{
(void)xy_goal_tolerance;
(void)yaw_goal_tolerance;
NavigationRequest request;
request.profile = MotionProfile::kDocking;
request.goal = goal;
request.tolerance.xy = xy_goal_tolerance;
request.tolerance.yaw = yaw_goal_tolerance;
request.marker = marker;
request.order = std::make_shared<robot_protocol_msgs::Order>(msg);
return submit(request);
@@ -653,20 +787,20 @@ bool NavigationServer::dockTo(const robot_protocol_msgs::Order& msg, const std::
bool NavigationServer::moveStraightTo(const robot_geometry_msgs::PoseStamped& goal,
double xy_goal_tolerance)
{
(void)xy_goal_tolerance;
NavigationRequest request;
request.profile = MotionProfile::kGoStraight;
request.goal = goal;
request.tolerance.xy = xy_goal_tolerance;
return submit(request);
}
bool NavigationServer::rotateTo(const robot_geometry_msgs::PoseStamped& goal,
double yaw_goal_tolerance)
{
(void)yaw_goal_tolerance;
NavigationRequest request;
request.profile = MotionProfile::kRotate;
request.goal = goal;
request.tolerance.yaw = yaw_goal_tolerance;
return submit(request);
}
@@ -694,6 +828,16 @@ void NavigationServer::resume()
}
void NavigationServer::cancel()
{
std::lock_guard<std::mutex> lock(data_mutex_);
cancel_requested_ = true;
// Huỷ từ host là "bỏ cả order", không chỉ chặng đang chạy: không xoá hàng đợi thì mission layer
// giao chặng kế tiếp ngay sau khi chặng này dừng, và robot chạy tiếp một tuyến vừa bị huỷ.
mission_cancel_requested_ = true;
}
void NavigationServer::requestLoopCancel()
{
std::lock_guard<std::mutex> lock(data_mutex_);
cancel_requested_ = true;
@@ -704,15 +848,18 @@ void NavigationServer::drainLifecycleRequests()
bool pause = false;
bool resume = false;
bool cancel = false;
bool mission_cancel = false;
{
std::lock_guard<std::mutex> lock(data_mutex_);
pause = pause_requested_;
resume = resume_requested_;
cancel = cancel_requested_;
mission_cancel = mission_cancel_requested_;
pause_requested_ = false;
resume_requested_ = false;
cancel_requested_ = false;
mission_cancel_requested_ = false;
}
// Huỷ trước: nó thắng mọi thứ khác. Tạm dừng rồi huỷ và huỷ rồi tạm dừng phải cho cùng kết quả.
@@ -728,6 +875,19 @@ void NavigationServer::drainLifecycleRequests()
{
loop_.requestResume();
}
// Huỷ lan tới cả hàng đợi mission. Đi sau lời gọi xuống lõi vì đây là đường bất đồng bộ (xếp vào
// event bus, xử lý trên thread sự kiện) — lõi phải phản ứng trước, hàng đợi theo sau.
if (mission_cancel && runtime_ != nullptr)
{
runtime_->missionLayer().cancel();
}
// `pause`/`resume` CỐ Ý không lan xuống mission layer. Chặng đang chạy đã bị chính control loop
// giữ lại, nên hàng đợi không giao chặng mới trong lúc tạm dừng dù manager không biết gì.
// Ngược lại, đẩy manager sang PAUSED mở ra một đường hỏng thật: `onNavigationDone` chỉ được nhận
// khi manager đang RUNNING, nên một chặng kết thúc đúng lúc lệnh pause tới sẽ bị **bỏ mất
// outcome** — resume xong cả order treo vĩnh viễn ở chặng đó, không lỗi, không log.
}
bool NavigationServer::setTwistLinear(const robot_geometry_msgs::Vector3& linear)