ROS2与C++入门教程-创建服务(srv)文件
ROS2与C++入门教程-创建服务(srv)文件
说明:
- 介绍如何创建服务srv文件
步骤:
- 使用已经建好的工作空间dev_ws
- 新建包,如果已经建好,忽略这行
cd ~/dev-ws/src
ros2 pkg create --build-type ament_cmake tutorial_interfaces
- 新建目录mkdir srv
cd ~/dev-ws/src/tutorial_interfaces
mkdir srv
- 增加msg定义文件
cd ~/dev-ws/src/tutorial_interfaces/srv
vim AddThreeInts.srv
- 内容如下:
int64 a
int64 b
int64 c
---
int64 sum
- 定义一个a b c三个输入,输出sum,都是整数
- 编辑CMakeLists.txt,增加如下行
find_package(rosidl_default_generators REQUIRED)
rosidl_generate_interfaces(${PROJECT_NAME}
"srv/AddThreeInts.srv"
)
- 编辑package.xml,增加如下行
<build_depend>rosidl_default_generators</build_depend>
<exec_depend>rosidl_default_runtime</exec_depend>
<member_of_group>rosidl_interface_packages</member_of_group>
- 编译包
cd ~/dev_ws/
colcon build --symlink-install --packages-select tutorial_interfaces
- 接着重新加载工作空间
. install/setup.bash
- 查询消息接口
ros2 interface show tutorial_interfaces/srv/AddThreeInts
- 效果如下:
int64 a
int64 b
int64 c
---
int64 sum
测试:
- 使用之前的cpp_pubsub包的发布
- 修改服务端,进入srv目录,新建文件add_two_ints_server_srv.cpp
vim add_two_ints_server_srv.cpp
- 内容如下:
#include "rclcpp/rclcpp.hpp"
#include "tutorial_interfaces/srv/add_three_ints.hpp" // CHANGE
#include <memory>
void add(const std::shared_ptr<tutorial_interfaces::srv::AddThreeInts::Request> request, // CHANGE
std::shared_ptr<tutorial_interfaces::srv::AddThreeInts::Response> response) // CHANGE
{
response->sum = request->a + request->b + request->c; // CHANGE
RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "Incoming request\na: %ld" " b: %ld" " c: %ld", // CHANGE
request->a, request->b, request->c); // CHANGE
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_three_ints_server"); // CHANGE
rclcpp::Service<tutorial_interfaces::srv::AddThreeInts>::SharedPtr service = // CHANGE
node->create_service<tutorial_interfaces::srv::AddThreeInts>("add_three_ints", &add); // CHANGE
RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "Ready to add three ints."); // CHANGE
rclcpp::spin(node);
rclcpp::shutdown();
}
- 修改客户端,进入srv目录,新建文件add_two_ints_client_srv.cpp
- 如下内容:
#include "rclcpp/rclcpp.hpp"
#include "tutorial_interfaces/srv/add_three_ints.hpp" // CHANGE
#include <chrono>
#include <cstdlib>
#include <memory>
using namespace std::chrono_literals;
int main(int argc, char **argv)
{
rclcpp::init(argc, argv);
if (argc != 4) { // CHANGE
RCLCPP_INFO(rclcpp::get_logger("rclcpp"), "usage: add_three_ints_client X Y Z"); // CHANGE
return 1;
}
std::shared_ptr<rclcpp::Node> node = rclcpp::Node::make_shared("add_three_ints_client"); // CHANGE
rclcpp::Client<tutorial_interfaces::srv::AddThreeInts>::SharedPtr client = // CHANGE
node->create_client<tutorial_interfaces::srv::AddThreeInts>("add_three_ints"); // CHANGE
auto request = std::make_shared<tutorial_interfaces::srv::AddThreeInts::Request>(); // CHANGE
request->a = atoll(argv[1]);
request->b = atoll(argv[2]);
request->c = atoll(argv[3]); // CHANGE
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::executor::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_three_ints"); // CHANGE
}
rclcpp::shutdown();
return 0;
}
- 修改CMakelist.txt 如下
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(tutorial_interfaces REQUIRED) # CHANGE
add_executable(server_srv src/add_two_ints_server_srv.cpp)
ament_target_dependencies(server_srv
rclcpp tutorial_interfaces) #CHANGE
add_executable(client_srv src/add_two_ints_client_srv.cpp)
ament_target_dependencies(client_srv
rclcpp tutorial_interfaces) #CHANGE
install(TARGETS
server_srv
client_srv
DESTINATION lib/${PROJECT_NAME})
ament_package()
- 修改package.xml 增加依赖
<depend>tutorial_interfaces</depend>
- 编译包
colcon build --packages-select cpp_srvcli
- 新开终端,启动服务端
ros2 run cpp_srvcli server_srv
- 新开终端,启动客户端
ros2 run cpp_srvcli client_srv 2 3 1
获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号