Might.190 2024-04-28 21:40 采纳率: 86.2%
浏览 1
已结题

输入这段代码,为什么文件不生成呢?

在ros2下输入ros2 pkg create service --build-type ament_cmake --dependencies rclcpp base_interfaces_demo 本应该自动生成一个
base_interfaces_demo文件和service文件的,可是并没有生成,只生成了service文件,请问能不能自己创建一个base_interfaces_demo文件,有没有同样的作用。

  • 写回答

6条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-04-28 21:40
    关注

    引自免费微信小程序:皆我百晓生

    看起来你在尝试使用ROS 2的包管理工具ros2 pkg来创建一个新的服务包,并且期望它能自动生成包含一个名为base_interfaces_demo的目录以及一个服务文件。然而,根据你的描述,只有服务文件被创建了,而预期的目录并未生成。

    这里可能的原因是命令格式有误,或者是在执行命令时遇到了一些未明确的问题。首先,让我们确认一下创建服务包的基本命令结构:

    # 创建一个名为"my_service_package"的服务包,依赖于'rclcpp'和'base_interfaces_demo'
    ros2 pkg create my_service_package --build-type ament_cmake --dependencies rclcpp base_interfaces_demo
    

    从你的描述来看,似乎base_interfaces_demo既是你想要创建的新包名,又是一个你想要作为依赖项的库。在ROS 2中,包名称通常不会与已存在的库名称冲突,因此这可能是导致问题的一个原因。

    如果你确实想创建一个名为base_interfaces_demo的包,并且在这个包内添加一个服务,那么你应该先确保这个包不存在。然后,你可以手动创建这个包及其内部结构。以下是手动创建包和服务的步骤:

    1. 在你的工作空间(workspace)根目录下创建新的包:

      mkdir -p src/base_interfaces_demo
      
    2. 进入新创建的包目录:

      cd src/base_interfaces_demo
      
    3. 创建必要的文件夹结构:

      mkdir -p include base_interfaces_demo src
      
    4. 编辑CMakeLists.txt文件(位于src/目录),添加依赖项:

      cmake_minimum_required(VERSION 3.5)
      project(base_interfaces_demo)
      
      find_package(rclcpp REQUIRED)
      add_dependencies(base_interfaces_demo ${rclcpp_dependencies})
      
      include_directories(include ${rclcpp_INCLUDE_DIRS})
      
    5. 创建服务接口定义文件,在include/base_interfaces_demo目录下创建一个.srv文件,例如hello_world.srv

      # hello_world.srv
      string Hello
      
    6. 创建服务实现文件,在src/目录下创建hello_world_server.cpp

      ```cpp
      #include <rclcpp/rclcpp.hpp>
      #include "base_interfaces_demo/hello_world.h"
      
      class HelloWorldServer : public rclcpp::Node
      {
      public:
        HelloWorldServer()
          : Node("hello_world_server"),
            publisher_(this, "hello_world", &HelloWorld::msg_type::msg.dds_, 10)
        {
          this->declare_parameter("message", std::string("Hello World!"));
          auto message = this->get_parameter("message").as_string();
          RCLCPP_INFO(this->get_logger(), "%s", message.c_str());
          timer_ = this->create_wall_timer(
              std::chrono::seconds(1),
              [this]() { publisher_->publish(HelloWorld(message)); });
        }
      
      private:
        rclcpp::Publisher<HelloWorld>::SharedPtr publisher_;
        rclcpp::TimerBase::SharedPtr timer_;
      };
      
      RCLCPP_COMPONENTS_REGISTER_NODE(HelloWorldServer)
      
    7. 创建客户端实现文件,在src/目录下创建hello_world_client.cpp

      ```cpp
      #include <rclcpp/rclcpp.hpp>
      #include "base_interfaces_demo/hello_world.h"
      
      class HelloWorldClient : public rclcpp::Node
      {
      public:
        HelloWorldClient()
          : Node("hello_world_client")
        {
          client_ = this->create_client<HelloWorld>(rclcpp::node_name_ + "/hello_world");
          while (!client_->wait_for_service(std::chrono::seconds(1))) {
            RCLCPP_INFO(get_logger(), "Service not available, waiting again...");
          }
          send_request();
        }
      
      private:
        void send_request()
        {
          auto future = client_->async_send_request(HelloWorld());
          future.add_done_callback([this](std::shared_future<rclcpp::Response<HelloWorld>> fut) {
            if (auto resp = fut.get()) {
              RCLCPP_INFO(get_logger(), "Received: %s", resp->message().c_str());
            } else {
              RCLCPP_ERROR(get_logger(), "Request failed");
            }
          });
        }
      
        rclcpp::Client<HelloWorld>::SharedPtr client_;
      };
      
      RCLCPP_COMPONENTS_REGISTER_NODE(HelloWorldClient)
      
    8. 更新CMakeLists.txt,添加对服务的编译:

      # ...
      add_executable(hello_world_server src/hello_world_server.cpp)
      target_include_directories(hello_world_server PUBLIC include)
      
      add_executable(hello_world_client src/hello_world_client.cpp)
      target_include_directories(hello_world_client PUBLIC include)
      
    9. 保存并返回到工作空间根目录:

      cd ..
      
    10. 将新包添加到工作空间配置文件中:

      echo 'package: base_interfaces_demo' >> packages.yaml
      
    11. 构建你的工作空间:

      colcon build
      

    这样,你就成功地创建了一个名为base_interfaces_demo的包,并包含了服务的服务器端和客户端实现。通过这种方式创建的包将与使用ros2 pkg create命令创建的包具有相同的作用。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(5条)

报告相同问题?

问题事件

  • 系统已结题 5月6日
  • 已采纳回答 4月28日
  • 创建了问题 4月28日

悬赏问题

  • ¥15 无源定位系统的时差估计误差标准差
  • ¥15 请问这个代码哪里有问题啊
  • ¥20 python--version在命令端输入结果Python is not defined怎么办?还有pip不是exe格式是不是没安装成功?
  • ¥15 通过GaussianView进行结构微调消除虚频
  • ¥15 调用transformers库
  • ¥15 由于导出的数据名字中带有/,导致Matlab打不开,怎么办?
  • ¥15 新硬盘安装的程序总是崩溃,提示遇到错误
  • ¥15 openpcdet自制数据集评估bev精度和3d精度相同
  • ¥15 excel 上下按钮 显示行
  • ¥20 云卓h12pro 数传问题