From 1a9fc5f7e1df4869194749d3459fdbe09643e53d Mon Sep 17 00:00:00 2001 From: Sucan126 <632190820@qq.com> Date: Sun, 21 Sep 2025 15:15:09 +0800 Subject: [PATCH] =?UTF-8?q?refactor(navigation):=20=E9=87=8D=E6=9E=84Navig?= =?UTF-8?q?ation=E7=B1=BB=E7=BB=A7=E6=89=BFunitree::robot::Client?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 移除不必要的client_成员变量,直接继承unitree::robot::Client类 简化初始化逻辑,更新相关文档说明 --- README.md | 4 ++-- include/navigation.hpp | 3 +-- src/navigation.cpp | 12 +++--------- 3 files changed, 6 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 965dce7..16fe266 100644 --- a/README.md +++ b/README.md @@ -429,7 +429,7 @@ unitree-go2/ │ ├── custom_robot.hpp # Main orchestrator │ ├── logger.hpp # Logging system │ ├── mqtt.hpp # MQTT client -│ ├── navigation.hpp # Navigation and SLAM +│ ├── navigation.hpp # Navigation and SLAM (inherits from unitree::robot::Client) │ └── nlohmann/ # JSON library ├── src/ # Source files │ ├── config.cpp # Configuration implementation @@ -438,7 +438,7 @@ unitree-go2/ │ ├── logger.cpp # Logging system implementation │ ├── main.cpp # Entry point │ ├── mqtt.cpp # MQTT client implementation -│ └── navigation.cpp # Navigation and SLAM implementation +│ └── navigation.cpp # Navigation and SLAM implementation (inherits from unitree::robot::Client) ├── scripts/ # Utility scripts │ ├── install_deps.sh # Install dependencies │ └── run_robot.sh # Run with optimal settings diff --git a/include/navigation.hpp b/include/navigation.hpp index 7cbec67..feb5f6f 100644 --- a/include/navigation.hpp +++ b/include/navigation.hpp @@ -8,7 +8,7 @@ namespace custom { -class Navigation { +class Navigation : public unitree::robot::Client { public: Navigation(); ~Navigation(); @@ -24,7 +24,6 @@ public: bool closeSlam(); private: - std::unique_ptr client_; bool callSlamService(int api_id, const nlohmann::json& data); }; diff --git a/src/navigation.cpp b/src/navigation.cpp index 38f8ff1..ea57524 100644 --- a/src/navigation.cpp +++ b/src/navigation.cpp @@ -7,14 +7,13 @@ const std::string SLAM_SERVICE_NAME = "slam_operate"; namespace custom { -Navigation::Navigation() : client_(nullptr) {} +Navigation::Navigation() : unitree::robot::Client(SLAM_SERVICE_NAME) {} Navigation::~Navigation() {} void Navigation::Init() { try { - client_ = std::make_unique(SLAM_SERVICE_NAME); - client_->Init(); + unitree::robot::Client::Init(); LOG_INFO("Navigation client for service '" + SLAM_SERVICE_NAME + "' initialized."); } catch (const std::exception& e) { LOG_ERROR("Failed to initialize navigation client: " + std::string(e.what())); @@ -22,11 +21,6 @@ void Navigation::Init() { } bool Navigation::callSlamService(int api_id, const nlohmann::json& data) { - if (!client_) { - LOG_ERROR("Navigation client not initialized."); - return false; - } - nlohmann::json request_json; request_json["data"] = data; std::string request_str = request_json.dump(); @@ -34,7 +28,7 @@ bool Navigation::callSlamService(int api_id, const nlohmann::json& data) { LOG_INFO("Calling slam service api_id: " + std::to_string(api_id) + ", request: " + request_str); - int ret = client_->Call(api_id, request_str, response_str); + int ret = this->Call(api_id, request_str, response_str); if (ret != 0) { LOG_ERROR("Slam service request failed with SDK error code: " + std::to_string(ret));