refactor(navigation): 重构Navigation类继承unitree::robot::Client

移除不必要的client_成员变量,直接继承unitree::robot::Client类
简化初始化逻辑,更新相关文档说明
This commit is contained in:
2025-09-21 15:15:09 +08:00
parent 61d206e948
commit 1a9fc5f7e1
3 changed files with 6 additions and 13 deletions

View File

@@ -429,7 +429,7 @@ unitree-go2/
│ ├── custom_robot.hpp # Main orchestrator │ ├── custom_robot.hpp # Main orchestrator
│ ├── logger.hpp # Logging system │ ├── logger.hpp # Logging system
│ ├── mqtt.hpp # MQTT client │ ├── mqtt.hpp # MQTT client
│ ├── navigation.hpp # Navigation and SLAM │ ├── navigation.hpp # Navigation and SLAM (inherits from unitree::robot::Client)
│ └── nlohmann/ # JSON library │ └── nlohmann/ # JSON library
├── src/ # Source files ├── src/ # Source files
│ ├── config.cpp # Configuration implementation │ ├── config.cpp # Configuration implementation
@@ -438,7 +438,7 @@ unitree-go2/
│ ├── logger.cpp # Logging system implementation │ ├── logger.cpp # Logging system implementation
│ ├── main.cpp # Entry point │ ├── main.cpp # Entry point
│ ├── mqtt.cpp # MQTT client implementation │ ├── 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 ├── scripts/ # Utility scripts
│ ├── install_deps.sh # Install dependencies │ ├── install_deps.sh # Install dependencies
│ └── run_robot.sh # Run with optimal settings │ └── run_robot.sh # Run with optimal settings

View File

@@ -8,7 +8,7 @@
namespace custom { namespace custom {
class Navigation { class Navigation : public unitree::robot::Client {
public: public:
Navigation(); Navigation();
~Navigation(); ~Navigation();
@@ -24,7 +24,6 @@ public:
bool closeSlam(); bool closeSlam();
private: private:
std::unique_ptr<unitree::robot::Client> client_;
bool callSlamService(int api_id, const nlohmann::json& data); bool callSlamService(int api_id, const nlohmann::json& data);
}; };

View File

@@ -7,14 +7,13 @@ const std::string SLAM_SERVICE_NAME = "slam_operate";
namespace custom { namespace custom {
Navigation::Navigation() : client_(nullptr) {} Navigation::Navigation() : unitree::robot::Client(SLAM_SERVICE_NAME) {}
Navigation::~Navigation() {} Navigation::~Navigation() {}
void Navigation::Init() { void Navigation::Init() {
try { try {
client_ = std::make_unique<unitree::robot::Client>(SLAM_SERVICE_NAME); unitree::robot::Client::Init();
client_->Init();
LOG_INFO("Navigation client for service '" + SLAM_SERVICE_NAME + "' initialized."); LOG_INFO("Navigation client for service '" + SLAM_SERVICE_NAME + "' initialized.");
} catch (const std::exception& e) { } catch (const std::exception& e) {
LOG_ERROR("Failed to initialize navigation client: " + std::string(e.what())); 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) { bool Navigation::callSlamService(int api_id, const nlohmann::json& data) {
if (!client_) {
LOG_ERROR("Navigation client not initialized.");
return false;
}
nlohmann::json request_json; nlohmann::json request_json;
request_json["data"] = data; request_json["data"] = data;
std::string request_str = request_json.dump(); 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); 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) { if (ret != 0) {
LOG_ERROR("Slam service request failed with SDK error code: " + std::to_string(ret)); LOG_ERROR("Slam service request failed with SDK error code: " + std::to_string(ret));