[8] ROS2初探之通信机制学习(自定义服务接口)


1. 创立功用包

# 创立作业空间   
mkdir -p ~/dev_ws/src   
cd ~/dev_ws/src   
# 创立功用包 
ros2 pkg create cpp_mysrv --build-type ament_cmake --dependencies rclcpp    
# 创立服务端和客户端程序cpp文件   
cd ~/dev_ws/src/cpp_mysrv/src   
touch add_two_nums_server.cpp   
touch add_two_nums_client.cpp 

2. 创立服务接口

cd ~/dev_ws/src/cpp_mysrv
mkdir srv && cd srv
touch AddNum.srv
float32 x
float32 y
float32 total

3. 装备接口自动化文件(CMakeLists.txtpackage.xml

find_package(rosidl_default_generators REQUIRED)
# 增加音讯文件和依靠

4. 编译服务接口

colcon build --packages-select cpp_mysrv

[8] R<span class=接口crc错误计数OS2初探之通讯机制学习(自定义服务接口)” src=”https://www.6h接口文档u.cc/wp-content/uploads/2022/07/37afc9bbe52511af996727335a15接口测试c90f.png”>

5. 编写服务端程序

#include "rclcpp/rclcpp.hpp"
#include "cpp_mysrv/srv/add_num.hpp" 
#include <memory>
void add(const std::shared_ptr<cpp_mysrv::srv::AddNum::Request> request,
          std::shared_ptr<cpp_mysrv::srv::AddNum::Response>      response)
  // Adds two integers from the request and gives the sum to the response.
  response->total = request->x + request->y;
  // Notifies the console of its status using logs.
  RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "Incoming requestnx: %.2f" " y: %.2f",
                request->x, request->y);
  RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "sending back response: [%.2f", (float)response->total);
int main(int argc, char **argv)
  // Initialize the ROS 2 C++ Client Library
  rclcpp::init(argc, argv);
  // Create a node named add_two_server
  std::shared_ptr<rclcpp::Node> node = rclcpp::Node::make_shared("add_two_nums_server");
  // Create a service named add_two_num and advertise it over the network (i.e. &add method)
  rclcpp::Service<cpp_mysrv::srv::AddNum>::SharedPtr service =
    node->create_service<cpp_mysrv::srv::AddNum>("add_two_nums", &add);
  // Display a log message when the service is ready
  RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "Ready to add two nums.");
  // Make the service available.
  // Call shutdown procedure when we are done

注意:头文件#in接口clude "cpp_mysrv/srv/add_num.hpp"

6. 编写客接口crc错误计数户端程序

#include "rclcpp/rclcpp.hpp"
#include "cpp_mysrv/srv/add_num.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_nums_client X Y");
      return 1;
  // Create the node
  std::shared_ptr<rclcpp::Node> node = rclcpp::Node::make_shared("add_two_nums_client");
  // Create the client for the node
  rclcpp::Client<cpp_mysrv::srv::AddNum>::SharedPtr client =
  // Make the request
  auto request = std::make_shared<cpp_mysrv::srv::AddNum::Request>();
  request->x = atoll(argv[1]);
  request->y = atoll(argv[2]);
  while (!client->wait_for_service(1s)) {
    if (!rclcpp::ok()) {
      // Show an error if the user types CTRL + C
      RCLCPP_ERROR(rclcpp::get_logger("rclcpp"), "Interrupted while waiting for the service. Exiting.");
      return 0;
    // Search for service nodes in the network
    RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "service not available, waiting again...");
  // Send a request
  auto result = client->async_send_request(request);
  // Wait for the result.
  if (rclcpp::spin_until_future_complete(node, result) ==
    RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "total: %.2f", result.get()->total);
  } else {
    RCLCPP_ERROR(rclcpp::get_logger("rclcpp"), "Failed to call service add_two_nums");
  return 0;

注意:头文件#include "cpp_mysrv/srv/add_num.hpp"

7. 装备CMakeLists.txt文件

find_package(cpp_mysrv REQUIRED)
add_executable(myserver src/add_two_nums_server.cpp)
ament_target_dependencies(myserver rclcpp cpp_mysrv)
add_executable(myclient src/add_two_nums_client.cpp)
ament_target_dependencies(myclient rclcpp cpp_mysrv) 

8接口测试. 编译验证

colcon build --packages-select cpp_mysrv
ros2 run cpp_mysrv myserver
ros2 run cpp_mysrv myclient 1.0 1.0

[8] ROS2初探之通讯机制学习(自定义服务接口)接口英文rc=”https://www.6h接口是什么u.cc/wp接口类型-cont接口类型ent/uploads/2022/07/6ae91acc0285bf9a6de272f5c358a82e.png”>



立即查看 了解详情