ros2 跟着官方教学从零开始 CS
ros2 从零开始10 服务者和消费者C/S前言上节课介绍写了简单的Topic订阅模型。本章我们将要学习C/S模型即服务者和消费者模型背景前面服务概念时提到过服务是ROS2 节点的另一种通信方式。服务基于调用与响应模型而非发布者-订阅者主题模型。服务就是我们常用的C/S模型Client 客户端请求而Service 服务端提供相应的响应。相较于发布者-订阅者主题模型调用与响应模型是点对点的通信。服务的通信数据结构用.srv文件来描述如下图srv文件用---来分隔上面是请求数据结构下面是响应数据结构如果数据结构为空则相应位置也为空。后面会有章节来介绍srv先按下不表。int64 a int64 b --- int64 sum实践创建一个包进入工作区的src目录用如下指令创建我们的包ros2 pkg create --build-type ament_cmake --license Apache-2.0 cpp_srvcli --dependencies rclcpp example_interfaces。--dependencies后面代表相关的依赖项rclcpp、example_interfaces命令会自动把依赖信息添加到package.xml和CMakeLists.txt里面。rootbc2bf85b2e4a:~/ros2_ws/src# ros2 pkg create --build-type ament_cmake --license Apache-2.0 cpp_srvcli --dependencies rclcpp example_interfaces going to create a new package package name: cpp_srvcli destination directory: /root/ros2_ws/src package format: 3 version: 0.0.0 description: TODO: Package description maintainer: [root xianlutinnove.com.cn] licenses: [Apache-2.0] 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 rootbc2bf85b2e4a:~/ros2_ws/src# ls cpp_srvcli example_interfaces examples mypackage以上命令ros2帮我们自动生成了cpp_srvcli。还记得example_interfaces吗example_interfaces是 ROS 2 中的一个消息接口包它包含了一系列示例消息类型主要用于测试、演示和学习目的。package.xml里面可以维护描述、作者信息、License等descriptionC client server tutorial/description maintainer emailyouemail.comYour Name/maintainer licenseApache License 2.0/license创建我们将设计2个节点执行程序server负责提供服务接口client负责调用client提供的服务接口开始吧2.1 在src里面创建一个cpp文件add_two_ints_server.cpp其内容如下#include rclcpp/rclcpp.hpp #include example_interfaces/srv/add_two_ints.hpp #include memory void add(const std::shared_ptrexample_interfaces::srv::AddTwoInts::Request request, std::shared_ptrexample_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_ptrrclcpp::Node node rclcpp::Node::make_shared(add_two_ints_server); rclcpp::Serviceexample_interfaces::srv::AddTwoInts::SharedPtr service node-create_serviceexample_interfaces::srv::AddTwoInts(add_two_ints, add); RCLCPP_INFO(rclcpp::get_logger(rclcpp), Ready to add two ints.); rclcpp::spin(node); rclcpp::shutdown(); }解析 下面代码创建节点add_two_ints_server 并接下来创建 rclcpp::Service对象, 而 rclcpp::Service对象创建时把实现函数add当成参数初始化了。在创建完成对象会自动通过在网络上进行广播些服务。rclcpp::spin(node)会阻塞直到节点退出后才返回。std::shared_ptrrclcpp::Node node rclcpp::Node::make_shared(add_two_ints_server); rclcpp::Serviceexample_interfaces::srv::AddTwoInts::SharedPtr service node-create_serviceexample_interfaces::srv::AddTwoInts(add_two_ints, add); RCLCPP_INFO(rclcpp::get_logger(rclcpp), Ready to add two ints.); rclcpp::spin(node);关于add函数入参分别是std::shared_ptrexample_interfaces::srv::AddTwoInts::Request和std::shared_ptrexample_interfaces::srv::AddTwoInts::Response这2个定义的实现由前面提到的srv文件编译自动生成得到的。void add(const std::shared_ptrexample_interfaces::srv::AddTwoInts::Request request, std::shared_ptrexample_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); }2.2 在src里面一个cpp文件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_ptrrclcpp::Node node rclcpp::Node::make_shared(add_two_ints_client); rclcpp::Clientexample_interfaces::srv::AddTwoInts::SharedPtr client node-create_clientexample_interfaces::srv::AddTwoInts(add_two_ints); auto request std::make_sharedexample_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; }解析 像server一样, 下面代码创建节点node, 并且创建rclcpp::Client对象(对应rclcpp::Service对象)。std::shared_ptrrclcpp::Node node rclcpp::Node::make_shared(add_two_ints_client); rclcpp::Clientexample_interfaces::srv::AddTwoInts::SharedPtr client node-create_clientexample_interfaces::srv::AddTwoInts(add_two_ints);接下来创建请求入参request。并且给具体的值具体值a和b 是我们node节点启动时的参数1、参数2。auto request std::make_sharedexample_interfaces::srv::AddTwoInts::Request(); request-a atoll(argv[1]); request-b atoll(argv[2]);接下来查找服务client-wait_for_service(1s)如果找到就返回true否则1s超时返回。auto result client-async_send_request(request);就是调用服务。2.3 修改CMakeLists.txt在ament_package()的前一行新增如下信息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})完整的CMakeLists.txt如下cmake_minimum_required(VERSION 3.8) project(cpp_srvcli) if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES Clang) add_compile_options(-Wall -Wextra -Wpedantic) endif() # find dependencies find_package(ament_cmake REQUIRED) find_package(rclcpp REQUIRED) find_package(example_interfaces REQUIRED) if(BUILD_TESTING) find_package(ament_lint_auto REQUIRED) # the following line skips the linter which checks for copyrights # comment the line when a copyright and license is added to all source files set(ament_cmake_copyright_FOUND TRUE) # the following line skips cpplint (only works in a git repo) # comment the line when this package is in a git repo and when # a copyright and license is added to all source files set(ament_cmake_cpplint_FOUND TRUE) ament_lint_auto_find_test_dependencies() endif() 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()开始编译进入工作区用colcon build等待编译完成。rootbc2bf85b2e4a:/# cd ~/ros2_ws rootbc2bf85b2e4a:~/ros2_ws# colcon build --packages-select cpp_srvcli Starting cpp_srvcli Finished cpp_srvcli [5.22s] Summary: 1 package finished [5.57s] rootbc2bf85b2e4a:~/ros2_ws#运行编译完成后我们需要安装后才能运行使用source install/setup.sh打开一个终端输入如下命令启动serverrootbc2bf85b2e4a:~/ros2_ws# source install/setup.sh rootbc2bf85b2e4a:~/ros2_ws# ros2 run cpp_srvcli server [INFO] [1774875183.179833701] [rclcpp]: Ready to add two ints.另开一个终端输入如下命令启动clientrootbc2bf85b2e4a:~/ros2_ws# cd ~/ros2_ws/ rootbc2bf85b2e4a:~/ros2_ws# source install/setup.sh rootbc2bf85b2e4a:~/ros2_ws# ros2 run cpp_srvcli client 300 300 [INFO] [1774875293.803476695] [rclcpp]: Sum: 600 rootbc2bf85b2e4a:~/ros2_ws#可以看到server计算2个数相加并把结果返回给client。总结在最近几个教程中你一直在使用主题和服务传递数据。 接下来我们学习如何自定义消息数据接口srv。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2466407.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!