本文已参加「新人创造礼」活动,一同敞开创造之路。

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)
# 增加音讯文件和依靠
rosidl_generate_interfaces(${PROJECT_NAME}
"srv/AddNum.srv"
)
  <build_depend>rosidl_default_generators</build_depend>
  <exec_depend>rosidl_default_runtime</exec_depend>
  <member_of_group>rosidl_interface_packages</member_of_group>

4. 编译服务接口

colcon build --packages-select cpp_mysrv

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.
  rclcpp::spin(node);
  // Call shutdown procedure when we are done
  rclcpp::shutdown();
}

注意:头文件#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 =
    node->create_client<cpp_mysrv::srv::AddNum>("add_two_nums");
  // 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::FutureReturnCode::SUCCESS)
  {
    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");
  }
  rclcpp::shutdown();
  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) 
install(TARGETS
  myserver
  myclient
  DESTINATION lib/${PROJECT_NAME}
)

8接口测试. 编译验证

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