ROS2學習之旅(15)——編寫簡單的服務和客戶節點(C++)


當節點使用服務進行通信時,發送數據請求的節點稱為客戶節點,響應請求的節點稱為服務節點。請求和響應的結構由.srv文件決定。

本文的例子是一個簡單的整數加法系統:一個節點請求兩個整數的和,另一個節點響應結果。

1.創建功能包

在開始之前,確保ROS2的環境變量正確配置。

其次,包應該在src目錄下,不是在工作空間的根目錄下。所以,導航到dev_ws下,並創建一個新的包:

ros2 pkg create --build-type ament_cmake cpp_srvcli --dependencies rclcpp example_interfaces

終端將返回一條消息,驗證cpp_srvcli包及其所有必需的文件和文件夾的創建。

going to create a new package
package name: cpp_srvcli
destination directory: /home/**/dev_ws/src
package format: 3
version: 0.0.0
description: TODO: Package description
maintainer: ['** <**@todo.todo>']
licenses: ['TODO: License declaration']
build type: ament_cmake
dependencies: ['rclcpp', 'example_interfaces']
creating folder ./cpp_srvcli
creating ./cpp_srvcli/package.xml
creating source and include folder
creating folder ./cpp_srvcli/src
creating folder ./cpp_srvcli/include/cpp_srvcli
creating ./cpp_srvcli/CMakeLists.txt

--dependencies參數將自動將必要的依賴項添加到package.xmlCMakeLists.txtexample_interfaces是包含.srv文件的包,通過此來構造請求和響應:

int64 a
int64 b
---
int64 sum

前兩行是請求的參數,破折號下面是響應的參數。在以后的項目中,往往會自己編寫.srv文件。

1.1更新package.xml

由於在包創建過程中使用了--dependencies選項,所以不必手動向package.xmlCMakeLists.txt添加依賴項。

但是,與往常一樣,請確保將描述、維護人員電子郵件和名稱以及許可信息添加到package.xml

<description>C++ client server tutorial</description>
<maintainer email="you@email.com">Your Name</maintainer>
<license>Apache License 2.0</license>

2.編寫服務節點

dev_ws/src/cpp_srvcli/src文件夾下,創建一個名為add_two_ints_server.cpp的新文件(可以使用touch命令),並將以下代碼粘貼到其中:

#include "rclcpp/rclcpp.hpp"
#include "example_interfaces/srv/add_two_ints.hpp"

#include <memory>

void add(const std::shared_ptr<example_interfaces::srv::AddTwoInts::Request> request,
          std::shared_ptr<example_interfaces::srv::AddTwoInts::Response>      response)
{
  response->sum = request->a + request->b;
  RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "Incoming request\na: %ld" " b: %ld",
                request->a, request->b);
  RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "sending back response: [%ld]", (long int)response->sum);
}

int main(int argc, char **argv)
{
  rclcpp::init(argc, argv);

  std::shared_ptr<rclcpp::Node> node = rclcpp::Node::make_shared("add_two_ints_server");

  rclcpp::Service<example_interfaces::srv::AddTwoInts>::SharedPtr service =
    node->create_service<example_interfaces::srv::AddTwoInts>("add_two_ints", &add);

  RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "Ready to add two ints.");

  rclcpp::spin(node);
  rclcpp::shutdown();
}

2.1審閱代碼

前兩個#include語句是包的依賴項。

add函數從請求中獲取兩個整數,並將其和提供給響應,同時使用日志通知控制台其狀態。

void add(const std::shared_ptr<example_interfaces::srv::AddTwoInts::Request> request,
         std::shared_ptr<example_interfaces::srv::AddTwoInts::Response>      response)
{
    response->sum = request->a + request->b;
    RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "Incoming request\na: %ld" " b: %ld",
        request->a, request->b);
    RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "sending back response: [%ld]", (long int)response->sum);
}

main函數的實現如下,逐行執行:

  • 初識化ROS2 C++庫:
rclcpp::init(argc, argv);
  • 創建一個名為add_two_ints_server的節點:
std::shared_ptr<rclcpp::Node> node = rclcpp::Node::make_shared("add_two_ints_server");
  • 為該節點創建一個名為add_two_ints的服務,並通過&add方法自動在網絡上發布它:
rclcpp::Service<example_interfaces::srv::AddTwoInts>::SharedPtr service =
node->create_service<example_interfaces::srv::AddTwoInts>("add_two_ints", &add);
  • 當節點准備好時,打印log信息:
RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "Ready to add two ints.");
  • 啟動節點,使得節點可用:
rclcpp::spin(node);

2.2添加可執行程序

add_executable宏生成一個可以使用ros2 run運行的可執行文件。將以下代碼塊添加到CMakeLists.txt中,創建一個名為server的可執行文件:

add_executable(server src/add_two_ints_server.cpp)
ament_target_dependencies(server rclcpp example_interfaces)

為了讓ros2 run可以找到可執行文件,在文件末尾ament_package()之前添加以下代碼:

install(TARGETS
  server
  DESTINATION lib/${PROJECT_NAME})

至此,server創建完畢。

3.編寫客戶節點

dev_ws/src/cpp_srvcli/src文件夾中,創建一個名為add_two_ints_client.cpp的新文件,並將以下代碼粘貼到其中:

#include "rclcpp/rclcpp.hpp"
#include "example_interfaces/srv/add_two_ints.hpp"

#include <chrono>
#include <cstdlib>
#include <memory>

using namespace std::chrono_literals;

int main(int argc, char **argv)
{
  rclcpp::init(argc, argv);

  if (argc != 3) {
      RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "usage: add_two_ints_client X Y");
      return 1;
  }

  std::shared_ptr<rclcpp::Node> node = rclcpp::Node::make_shared("add_two_ints_client");
  rclcpp::Client<example_interfaces::srv::AddTwoInts>::SharedPtr client =
    node->create_client<example_interfaces::srv::AddTwoInts>("add_two_ints");

  auto request = std::make_shared<example_interfaces::srv::AddTwoInts::Request>();
  request->a = atoll(argv[1]);
  request->b = atoll(argv[2]);

  while (!client->wait_for_service(1s)) {
    if (!rclcpp::ok()) {
      RCLCPP_ERROR(rclcpp::get_logger("rclcpp"), "Interrupted while waiting for the service. Exiting.");
      return 0;
    }
    RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "service not available, waiting again...");
  }

  auto result = client->async_send_request(request);
  // Wait for the result.
  if (rclcpp::spin_until_future_complete(node, result) ==
    rclcpp::FutureReturnCode::SUCCESS)
  {
    RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "Sum: %ld", result.get()->sum);
  } else {
    RCLCPP_ERROR(rclcpp::get_logger("rclcpp"), "Failed to call service add_two_ints");
  }

  rclcpp::shutdown();
  return 0;
}

3.1審閱代碼

與服務節點類似,以下代碼行創建客戶節點:

std::shared_ptr<rclcpp::Node> node = rclcpp::Node::make_shared("add_two_ints_client");
rclcpp::Client<example_interfaces::srv::AddTwoInts>::SharedPtr client =
  node->create_client<example_interfaces::srv::AddTwoInts>("add_two_ints");

接下來,創建請求。它的結構由前面提到的.srv文件定義。

auto request = std::make_shared<example_interfaces::srv::AddTwoInts::Request>();
request->a = atoll(argv[1]);
request->b = atoll(argv[2]);

while循環給客戶節點1秒的時間來搜索網絡中的服務節點。如果找不到,它就會繼續等待。

RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "service not available, waiting again...");

如果客戶節點被取消(例如,在終端中輸入Ctrl+C),它將返回一個錯誤日志消息,說明它被中斷了。

RCLCPP_ERROR(rclcpp::get_logger("rclcpp"), "Interrupted while waiting for the service. Exiting.");
  return 0;

然后客戶節點發送它的請求,啟動節點直到收到響應,或者失敗。

3.2添加可行性程序

返回CMakeLists.txt為新節點添加可執行文件和目標。從自動生成的文件中刪除一些不必要的信息后,整體的CMakeLists.txt應該是這樣的:

cmake_minimum_required(VERSION 3.5)
project(cpp_srvcli)

find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(example_interfaces REQUIRED)

add_executable(server src/add_two_ints_server.cpp)
ament_target_dependencies(server
  rclcpp example_interfaces)

add_executable(client src/add_two_ints_client.cpp)
ament_target_dependencies(client
  rclcpp example_interfaces)

install(TARGETS
  server
  client
  DESTINATION lib/${PROJECT_NAME})

ament_package()

4.編譯和運行

回到工作空間的根目錄,並編譯包:

colcon build --packages-select cpp_srvcli

打開一個新的終端,導航到dev_ws,source配置文件:

. install/setup.bash

接下來運行服務節點:

ros2 run cpp_srvcli server

此時,終端返回:

[INFO] [rclcpp]: Ready to add two ints.

打開另一個終端,再次從dev_ws中source配置文件。啟動客戶端節點,后面跟着以空格分隔的任意兩個整數:

ros2 run cpp_srvcli client 2 3

此時,終端返回:

[INFO] [rclcpp]: Sum: 5

返回服務節點正在運行的終端,將看到收到請求以及它發回的響應:

[INFO] [rclcpp]: Incoming request
a: 2 b: 3
[INFO] [rclcpp]: sending back response: [5]

5.總結

本文創建了兩個節點來進行服務請求和響應數據,並將它們的依賴項和可執行文件添加到包配置文件中,以便編譯和運行它們,並查看工作中的服務/客戶機系統。

如果給您帶來幫助,希望能給點個關注,以后還會陸續更新有關機器人的內容,點個關注不迷路~歡迎大家一起交流學習。
都看到這了,點個推薦再走吧~
未經允許,禁止轉載。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM