diff --git a/config/runtime/move_base_common_params.yaml b/config/runtime/move_base_common_params.yaml index 2db6d9b..aed7db5 100644 --- a/config/runtime/move_base_common_params.yaml +++ b/config/runtime/move_base_common_params.yaml @@ -303,3 +303,27 @@ mission_layer_enabled: true # nguyên nhân phổ biến nhất của lỗi "plugin build xong nhưng runtime báo không tìm thấy". MoveBase: library_path: libmove_base2 + +# --- Công tắc log chẩn đoán ---------------------------------------------------------------------- +# +# Ba nhóm dưới đây gom toàn bộ dòng `[move_base2][debug]`. Trước đây chúng in VÔ ĐIỀU KIỆN, không +# có cách nào tắt — trên robot chạy cả ca thì phần lớn là tiếng ồn, và tiếng ồn thường trực làm +# dòng thật sự bất thường trôi mất giữa hàng nghìn dòng giống hệt nhau. +# +# ► Mặc định BẬT cả ba. Tắt là lựa chọn có ý thức, không phải thứ đổi sau lưng người đang dựa vào +# log để debug. +debug: + # Chuyển trạng thái của state machine: `state -> CONTROLLING: valid plan available (...)`. + # ► Chỉ in khi ĐỔI trạng thái, không phải mỗi chu kỳ — rẻ, và thường là dòng đáng giữ nhất vì nó + # nói TẠI SAO đổi cùng trạng thái của từng runner lúc đó. Giữ bật trừ khi thật sự cần log sạch. + state: true + + # Controller nhận/từ chối plan, và path do recovery sinh ra. + # ► Một dòng mỗi lần lập plan. Tắt khi chạy mission dài và không quan tâm tuyến đi. + plan: true + + # Vì sao controller bị bỏ qua trong lúc CONTROLLING: thiếu pose, costmap quá hạn, hoặc controller + # không trả về lệnh hợp lệ. + # ► Nhóm ỒN NHẤT — có thể in mỗi chu kỳ khi có gì đó sai kéo dài. Nhưng cũng chính là nhóm trả + # lời câu "robot đứng im mà không tầng nào báo lỗi", nên ĐỪNG tắt khi đang truy lỗi. + skip: true diff --git a/include/move_base2/config/move_base2_config.h b/include/move_base2/config/move_base2_config.h index a62366e..b38d8ac 100644 --- a/include/move_base2/config/move_base2_config.h +++ b/include/move_base2/config/move_base2_config.h @@ -99,6 +99,32 @@ struct MoveBase2Config */ double sensor_store_report_period = 0.0; + // --- Công tắc log chẩn đoán ------------------------------------------------------------------- + // + // Mọi dòng `[move_base2][debug]` trước đây in VÔ ĐIỀU KIỆN. Trên robot chạy cả ca, phần lớn + // chúng là tiếng ồn: chúng mô tả đường đi BÌNH THƯỜNG, và tiếng ồn thường trực làm dòng thật sự + // bất thường trôi mất giữa hàng nghìn dòng giống hệt nhau. + // + // Mặc định BẬT cả ba, để bật/tắt là lựa chọn có ý thức chứ không phải thứ đổi sau lưng người + // đang dựa vào log để debug. Tắt bớt khi đã hiểu hệ thống và cần log sạch. + + /// Chuyển trạng thái của state machine (`state -> CONTROLLING: ...`). + /// + /// In khi ĐỔI trạng thái, không phải mỗi chu kỳ — nên nó rẻ và thường là dòng đáng giữ nhất: + /// nó nói tại sao đổi, và trạng thái của từng runner lúc đó. + bool debug_state = true; + + /// Nhận/từ chối plan, và path do recovery sinh ra. + /// + /// Một dòng cho mỗi lần lập plan. Tắt khi chạy mission dài và không quan tâm tuyến đi. + bool debug_plan = true; + + /// Vì sao controller bị bỏ qua trong lúc CONTROLLING (thiếu pose, costmap cũ, không có lệnh hợp lệ). + /// + /// Nhóm ồn nhất trong ba nhóm — nó có thể in mỗi chu kỳ khi có gì đó sai kéo dài. Nhưng cũng + /// chính là nhóm trả lời câu "robot đứng im mà không ai báo lỗi", nên đừng tắt khi đang truy lỗi. + bool debug_skip = true; + // --- Ánh xạ profile -> planner ---------------------------------------------------------------- ProfileBinding position; diff --git a/include/move_base2/control_loop.h b/include/move_base2/control_loop.h index 24bb0ee..cfe3bcc 100644 --- a/include/move_base2/control_loop.h +++ b/include/move_base2/control_loop.h @@ -95,6 +95,11 @@ struct ControlLoopConfig /// [s] Chu kỳ danh nghĩa, chỉ dùng cho dt của cycle ĐẦU TIÊN khi chưa có mốc thời gian trước đó. double nominal_control_period = 0.05; + /// Công tắc log chẩn đoán. Xem `MoveBase2Config::debug_*` để biết từng nhóm in cái gì. + bool debug_state = true; + bool debug_plan = true; + bool debug_skip = true; + /// Frame gắn với thân robot. Lệnh vận tốc phát ra được đóng dấu bằng frame này — cmd_vel là vận /// tốc trong hệ thân xe, không phải hệ bản đồ hay odom. std::string robot_base_frame = "base_link"; diff --git a/src/config/move_base2_config.cpp b/src/config/move_base2_config.cpp index 56607ac..1d339a7 100644 --- a/src/config/move_base2_config.cpp +++ b/src/config/move_base2_config.cpp @@ -68,6 +68,8 @@ void readBinding(robot::NodeHandle& nh, const std::string& ns, ProfileBinding& b } void readSensors(robot::NodeHandle& nh, SensorGatewayConfig& sensors); +void readDebug(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. @@ -234,6 +236,7 @@ void readMoveBase2Fields(robot::NodeHandle& nh, MoveBase2Config& config) readSensors(nh, config.sensors); readSensorStore(nh, config); + readDebug(nh, config); readPathMonitor(nh, config.path_monitor); readBool(nh, "docking_requires_marker", config.docking_requires_marker); @@ -277,6 +280,15 @@ void readSensorStore(robot::NodeHandle& nh, MoveBase2Config& config) 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); +} + /// Đọc cấu hình đường vào cảm biến từ namespace con `sensors`. void readSensors(robot::NodeHandle& nh, SensorGatewayConfig& sensors) { @@ -503,6 +515,9 @@ std::string MoveBase2Config::describe() const 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(); @@ -666,6 +681,9 @@ MoveBase2Config MoveBase2Config::load(robot::NodeHandle& root_nh) 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.velocity = velocity; config.nominal_control_period = diff --git a/src/control_loop.cpp b/src/control_loop.cpp index a5d248d..d5171cf 100644 --- a/src/control_loop.cpp +++ b/src/control_loop.cpp @@ -831,11 +831,14 @@ bool ControlLoop::step() if (output.state_changed) { last_reason_ = output.reason; - robot::log_info("[move_base2][debug] state -> %s: %s " - "(planner=%s controller=%s recovery=%s pose=%s active_request=%s).\n", - toString(output.state), output.reason != nullptr ? output.reason : "", - toString(input.planner), toString(input.controller), toString(input.recovery), - pose_available ? "yes" : "no", has_active_request_ ? "yes" : "no"); + if (config_.debug_state) + { + robot::log_info("[move_base2][debug] state -> %s: %s " + "(planner=%s controller=%s recovery=%s pose=%s active_request=%s).\n", + toString(output.state), output.reason != nullptr ? output.reason : "", + toString(input.planner), toString(input.controller), toString(input.recovery), + pose_available ? "yes" : "no", has_active_request_ ? "yes" : "no"); + } } // Cờ một-lần đã được state machine tiêu thụ xong ở lời gọi trên. @@ -911,13 +914,16 @@ bool ControlLoop::step() { // Controller từ chối plan: coi như lần lập plan này hỏng, để chu kỳ kiên nhẫn tiếp tục chạy. planner_feedback_ = PlannerFeedback::kFailed; - robot::log_error("[move_base2][debug] controller rejected plan with %zu pose(s); " - "first=(%.3f, %.3f, frame=%s), last=(%.3f, %.3f, frame=%s).\n", - latest_plan_.size(), latest_plan_.front().pose.position.x, - latest_plan_.front().pose.position.y, - latest_plan_.front().header.frame_id.c_str(), - latest_plan_.back().pose.position.x, latest_plan_.back().pose.position.y, - latest_plan_.back().header.frame_id.c_str()); + if (config_.debug_plan) + { + robot::log_error("[move_base2][debug] controller rejected plan with %zu pose(s); " + "first=(%.3f, %.3f, frame=%s), last=(%.3f, %.3f, frame=%s).\n", + latest_plan_.size(), latest_plan_.front().pose.position.x, + latest_plan_.front().pose.position.y, + latest_plan_.front().header.frame_id.c_str(), + latest_plan_.back().pose.position.x, latest_plan_.back().pose.position.y, + latest_plan_.back().header.frame_id.c_str()); + } } else { @@ -926,13 +932,16 @@ bool ControlLoop::step() last_plan_applied_ = now; has_last_plan_applied_ = true; - robot::log_info("[move_base2][debug] controller accepted plan with %zu pose(s); " - "first=(%.3f, %.3f, frame=%s), last=(%.3f, %.3f, frame=%s).\n", - latest_plan_.size(), latest_plan_.front().pose.position.x, - latest_plan_.front().pose.position.y, - latest_plan_.front().header.frame_id.c_str(), - latest_plan_.back().pose.position.x, latest_plan_.back().pose.position.y, - latest_plan_.back().header.frame_id.c_str()); + if (config_.debug_plan) + { + robot::log_info("[move_base2][debug] controller accepted plan with %zu pose(s); " + "first=(%.3f, %.3f, frame=%s), last=(%.3f, %.3f, frame=%s).\n", + latest_plan_.size(), latest_plan_.front().pose.position.x, + latest_plan_.front().pose.position.y, + latest_plan_.front().header.frame_id.c_str(), + latest_plan_.back().pose.position.x, latest_plan_.back().pose.position.y, + latest_plan_.back().header.frame_id.c_str()); + } } } @@ -989,13 +998,16 @@ bool ControlLoop::step() // Họ recovery sinh lại đường đi: coi kết quả như một plan mới, chờ state machine đẩy xuống. latest_plan_ = tick.path; planner_feedback_ = PlannerFeedback::kPlanReady; - robot::log_info("[move_base2][debug] recovery produced path with %zu pose(s); " - "first=(%.3f, %.3f, frame=%s), last=(%.3f, %.3f, frame=%s).\n", - latest_plan_.size(), latest_plan_.front().pose.position.x, - latest_plan_.front().pose.position.y, - latest_plan_.front().header.frame_id.c_str(), - latest_plan_.back().pose.position.x, latest_plan_.back().pose.position.y, - latest_plan_.back().header.frame_id.c_str()); + if (config_.debug_plan) + { + robot::log_info("[move_base2][debug] recovery produced path with %zu pose(s); " + "first=(%.3f, %.3f, frame=%s), last=(%.3f, %.3f, frame=%s).\n", + latest_plan_.size(), latest_plan_.front().pose.position.x, + latest_plan_.front().pose.position.y, + latest_plan_.front().header.frame_id.c_str(), + latest_plan_.back().pose.position.x, latest_plan_.back().pose.position.y, + latest_plan_.back().header.frame_id.c_str()); + } } } @@ -1071,22 +1083,28 @@ bool ControlLoop::step() if (output.state == NavigationState::kControlling && !pose_available) { - robot::log_warning_throttle( - kDebugControlLogThrottle, - "[move_base2][debug] controller skipped while CONTROLLING: pose unavailable " - "(reason=%s, controller_feedback_in=%s, recovery_cursor=%zu/%zu).\n", - output.reason != nullptr ? output.reason : "", toString(input.controller), - output.recovery_route_cursor, output.recovery_route_size); + if (config_.debug_skip) + { + robot::log_warning_throttle( + kDebugControlLogThrottle, + "[move_base2][debug] controller skipped while CONTROLLING: pose unavailable " + "(reason=%s, controller_feedback_in=%s, recovery_cursor=%zu/%zu).\n", + output.reason != nullptr ? output.reason : "", toString(input.controller), + output.recovery_route_cursor, output.recovery_route_size); + } } if (output.run_controller && costmap_stale) { - robot::log_warning_throttle( - kDebugControlLogThrottle, - "[move_base2][debug] controller skipped while CONTROLLING: costmap stale " - "(reason=%s, controller_feedback_in=%s, recovery_cursor=%zu/%zu).\n", - output.reason != nullptr ? output.reason : "", toString(input.controller), - output.recovery_route_cursor, output.recovery_route_size); + if (config_.debug_skip) + { + robot::log_warning_throttle( + kDebugControlLogThrottle, + "[move_base2][debug] controller skipped while CONTROLLING: costmap stale " + "(reason=%s, controller_feedback_in=%s, recovery_cursor=%zu/%zu).\n", + output.reason != nullptr ? output.reason : "", toString(input.controller), + output.recovery_route_cursor, output.recovery_route_size); + } } if (output.run_controller && !costmap_stale) @@ -1094,14 +1112,17 @@ bool ControlLoop::step() runController(candidate); if (controller_feedback_ == ControllerFeedback::kNoValidCommand) { - robot::log_warning_throttle( - kDebugControlLogThrottle, - "[move_base2][debug] controller returned no valid command " - "(reason=%s, planner_in=%s, controller_in=%s, recovery_in=%s, pose=%s, " - "recovery_cursor=%zu/%zu).\n", - output.reason != nullptr ? output.reason : "", toString(input.planner), - toString(input.controller), toString(input.recovery), pose_available ? "yes" : "no", - output.recovery_route_cursor, output.recovery_route_size); + if (config_.debug_skip) + { + robot::log_warning_throttle( + kDebugControlLogThrottle, + "[move_base2][debug] controller returned no valid command " + "(reason=%s, planner_in=%s, controller_in=%s, recovery_in=%s, pose=%s, " + "recovery_cursor=%zu/%zu).\n", + output.reason != nullptr ? output.reason : "", toString(input.planner), + toString(input.controller), toString(input.recovery), pose_available ? "yes" : "no", + output.recovery_route_cursor, output.recovery_route_size); + } } // else if (controller_feedback_ == ControllerFeedback::kCommandValid) // { @@ -1182,7 +1203,10 @@ bool ControlLoop::step() if (output.reason != nullptr && std::strcmp(output.reason, "no recovery behavior left") == 0) { - robot::log_error("[move_base2][debug] abort detail: recovery route exhausted " + // KHÔNG gắn thẻ [debug] và KHÔNG cho tắt: đây là LÝ DO mission hỏng, in đúng một lần + // mỗi lần abort. Công tắc `debug/*` dành cho log mô tả đường đi bình thường; tắt được + // một dòng lỗi nghĩa là có cấu hình khiến robot hỏng trong im lặng. + robot::log_error("[move_base2] abort detail: recovery route exhausted " "(trigger=%s cursor=%zu size=%zu, planner_in=%s, controller_in=%s, " "recovery_in=%s, pose=%s, costmap_current=%s).\n", toString(output.recovery_trigger), output.recovery_route_cursor,