编写简单的服务和客户端 (C++)
**目标:**使用 C++ 创建和运行服务和客户机节点。
**教程级别:**初学者
**时间:**20分钟
内容
背景
当节点使用服务进行通信时,发送数据请求的节点称为客户端节点,响应请求的节点称为服务节点。 请求和响应的结构由文件确定。.srv
这里使用的示例是一个简单的整数加法系统;一个节点请求两个整数的总和,另一个节点响应结果。
先决条件
任务
1 创建包
打开一个新终端并获取您的 ROS 2 安装,以便命令正常工作。ros2
导航到在上一教程中创建的目录。ros2_ws
回想一下,包应该在目录中创建,而不是在工作区的根目录中创建。 导航到并创建一个新包:src``ros2_ws/src
ros2 pkg create --build-type ament_cmake --license Apache-2.0 cpp_srvcli --dependencies rclcpp example_interfaces
您的终端将返回一条消息,验证您的包及其所有必要文件和文件夹的创建。cpp_srvcli
该参数会自动向 和 添加必要的依赖行。 是包含构建请求和响应所需的 .srv 文件的包:--dependencies``package.xml``CMakeLists.txt``example_interfaces
int64 a
int64 b
---
int64 sum
前两行是请求的参数,破折号下方是响应。
1.1 更新package.xml
由于您在包创建过程中使用了该选项,因此不必手动向 or 添加依赖项。--dependencies``package.xml``CMakeLists.txt
但是,与往常一样,请确保将描述、维护者电子邮件和名称以及许可证信息添加到 .package.xml
<description>C++ client server tutorial</description>
<maintainer email="[email protected]">Your Name</maintainer>
<license>Apache License 2.0</license>
2 编写服务节点
在目录中,创建一个名为的新文件,并将以下代码粘贴到其中:ros2_ws/src/cpp_srvcli/src``add_two_ints_server.cpp
#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 Examine the code
The first two statements are your package dependencies.#include
The function adds two integers from the request and gives the sum to the response, while notifying the console of its status using logs.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);
}
The function accomplishes the following, line by line:main
-
Initializes ROS 2 C++ client library:
rclcpp::init(argc, argv);
-
Creates a node named :
add_two_ints_server
std::shared_ptr<rclcpp::Node> node = rclcpp::Node::make_shared("add_two_ints_server");
-
Creates a service named for that node and automatically advertises it over the networks with the method:
add_two_ints``&add
rclcpp::Service<example_interfaces::srv::AddTwoInts>::SharedPtr service = node->create_service<example_interfaces::srv::AddTwoInts>("add_two_ints", &add);
-
Prints a log message when it’s ready:
RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "Ready to add two ints.");
-
Spins the node, making the service available.
rclcpp::spin(node);
2.2 Add executable
The macro generates an executable you can run using . Add the following code block to to create an executable named :add_executable``ros2 run``CMakeLists.txt``server
add_executable(server src/add_two_ints_server.cpp)
ament_target_dependencies(server rclcpp example_interfaces)
So can find the executable, add the following lines to the end of the file, right before :ros2 run``ament_package()
install(TARGETS
server
DESTINATION lib/${PROJECT_NAME})
You could build your package now, source the local setup files, and run it, but let’s create the client node first so you can see the full system at work.
3 Write the client node
Inside the directory, create a new file called and paste the following code within:ros2_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 Examine the code
Similar to the service node, the following lines of code create the node and then create the client for that node:
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");
Next, the request is created. Its structure is defined by the file mentioned earlier..srv
auto request = std::make_shared<example_interfaces::srv::AddTwoInts::Request>();
request->a = atoll(argv[1]);
request->b = atoll(argv[2]);
该循环为客户端提供 1 秒钟的时间来搜索网络中的服务节点。 如果它找不到任何,它将继续等待。while
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.");
然后客户端发送其请求,节点旋转,直到收到响应或失败。
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 构建和运行
在构建之前,最好在工作区 () 的根目录中运行以检查缺少的依赖项:rosdep``ros2_ws
Linux操作系统macOS 操作系统窗户
rosdep install -i --from-path src --rosdistro humble -y
导航回工作区的根目录,并生成新包:Navigate back to the root of your workspace, and build your new package:ros2_ws
Linux操作系统macOS 操作系统窗户
colcon build --packages-select cpp_srvcli
打开一个新终端,导航到 ,并获取安装文件:ros2_ws
Linux操作系统macOS 操作系统窗户
source install/setup.bash
现在运行服务节点:
ros2 run cpp_srvcli server
终端应返回以下消息,然后等待:
[INFO] [rclcpp]: Ready to add two ints.
打开另一个终端,再次从内部获取设置文件。 启动客户机节点,后跟用空格分隔的任意两个整数:ros2_ws
ros2 run cpp_srvcli client 2 3
例如,如果选择 和 ,客户端将收到如下响应:2``3
[INFO] [rclcpp]: Sum: 5
返回到运行服务节点的终端。 您将看到它在收到请求和收到的数据以及发回的响应时发布了日志消息:
[INFO] [rclcpp]: Incoming request
a: 2 b: 3
[INFO] [rclcpp]: sending back response: [5]
进入服务器终端,停止节点旋转。Ctrl+C
总结
您创建了两个节点来通过服务请求和响应数据。 已将其依赖项和可执行文件添加到包配置文件中,以便可以生成和运行它们,并查看正在运行的服务/客户端系统。
源码分析
目录:
主要文件代码如下:
CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
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()
package.xml
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>cpp_srvcli</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="[email protected]">a</maintainer>
<license>Apache-2.0</license>
<buildtool_depend>ament_cmake</buildtool_depend>
<depend>rclcpp</depend>
<depend>example_interfaces</depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
<export>
<build_type>ament_cmake</build_type>
</export>
</package>
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;
}
add_two_ints_server.cpp
#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();
}
像这里运行即可:
结果如下: