当前位置:网站首页>Service creation and operation example of ROS
Service creation and operation example of ROS
2022-06-13 01:38:00 【lainegates】
Reference resources
ROS Official website Wiki— establish ROS Messages and services
ROS Official website Wiki— Write simple services and clients (C++)
ROS Official website Wiki— Verify simple services and clients
In front of
ROS Logical view of
ROS Working space for
List of articles
The sample code is intuitive , Go straight to the code
Definition of service message
First, enter the created package
$ roscd beginner_tutorials
$ mkdir srv
Define the service file
Define service messages srv file ``, The content is as follows ,--- Above is request (Request) Content ,--- The following is the response (Response) Content .
int64 A
int64 B
---
int64 Sum
This file is available from rospy_tutorials Copy
# roscp [package_name] [file_to_copy_path] [copy_path]
$ roscp rospy_tutorials AddTwoInts.srv srv/AddTwoInts.srv
Set up srv File compilation information
stay packages.xml Add the configuration
<build_depend>message_generation</build_depend> # Compile time dependent Libraries
<exec_depend>message_runtime</exec_depend> # Runtime dependent Libraries
stay CMakeLists.txt Make the following settings in
# add to ROS Dependency Library
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs # Standard information , Basic data supported by the system , Such as int,float,...
message_generation # take *.srv The file to C++/Python Code
# stay packages.xml Has added
)
# Add just written *.srv file
add_service_files(
FILES
AddTwoInts.srv
)
View the generated srv Information
# rossrv show <service type>
$ rossrv show beginner_tutorials/AddTwoInts
Publisher code
#include "ros/ros.h" // ROS System header file
#include "beginner_tutorials/AddTwoInts.h" // The code generated by the previous service definition
// Callback function
bool add(beginner_tutorials::AddTwoInts::Request &req,
beginner_tutorials::AddTwoInts::Response &res)
{
res.sum = req.a + req.b;
ROS_INFO("request: x=%ld, y=%ld", (long int)req.a, (long int)req.b);
ROS_INFO("sending back response: [%ld]", (long int)res.sum);
return true;
}
int main(int argc, char **argv)
{
// Parameters 1、2: Command line arguments
// Parameters 3: The node name , Node names cannot conflict !!!
ros::init(argc, argv, "add_two_ints_server");
// ROS System handle
// The first to apply for resources , The last handle destroys all requested resources
ros::NodeHandle n;
// Create a "add_two_ints" Service for , The callback function is "add(...)"
ros::ServiceServer service = n.advertiseService("add_two_ints", add);
ROS_INFO("Ready to add two ints.");
// Loop response request , until ros::ok() return false
ros::spin();
return 0;
}
Client code
#include "ros/ros.h"
#include "beginner_tutorials/AddTwoInts.h" // The code generated by the previous service definition
#include <cstdlib>
int main(int argc, char **argv)
{
ros::init(argc, argv, "add_two_ints_client");
if (argc != 3)
{
ROS_INFO("usage: add_two_ints_client X Y");
return 1;
}
ros::NodeHandle n;
// establish client End , The message type of the publish request is "beginner_tutorials::AddTwoInts"
// The service to be requested is named "add_two_ints", See the server code above
ros::ServiceClient client = n.serviceClient<beginner_tutorials::AddTwoInts>("add_two_ints");
// Create message object , And fill in the data
beginner_tutorials::AddTwoInts srv;
srv.request.a = atoll(argv[1]);
srv.request.b = atoll(argv[2]);
// Request service
if (client.call(srv))
{
ROS_INFO("Sum: %ld", (long int)srv.response.sum);
}
else
{
ROS_ERROR("Failed to call service add_two_ints");
return 1;
}
return 0;
}
Building a node
edit beginner_tutorials Inside CMakeLists.txt file , The file is located in ~/catkin_ws/src/beginner_tutorials/CMakeLists.txt.
add_executable(add_two_ints_server src/add_two_ints_server.cpp)
target_link_libraries(add_two_ints_server ${catkin_LIBRARIES})
add_dependencies(add_two_ints_server beginner_tutorials_gencpp)
add_executable(add_two_ints_client src/add_two_ints_client.cpp)
target_link_libraries(add_two_ints_client ${catkin_LIBRARIES})
add_dependencies(add_two_ints_client beginner_tutorials_gencpp)
Built executable add_two_ints_server and add_two_ints_client By default, it is placed in the package directory devel In the space , namely ~/catkin_ws/devel/lib/<package name>.
You can directly call to generate an executable file , You can also use rosrun To call .
Inspection node
Operation service
$ rosrun beginner_tutorials add_two_ints_server # (C++)
Running client
$ rosrun beginner_tutorials add_two_ints_client 1 3 # (C++)
Running results
Requesting 1+3
1 + 3 = 4
边栏推荐
- pycharm add configutions
- TensorFlow2的Conv1D, Conv2D,Conv3D机器对应的MaxPooling详解
- Record the VMware installation process of VMware Tools and some problems encountered
- MySQL download and installation
- About retrieving ignored files in cornerstone
- Note: common gadgets in project architecture
- [learn FPGA programming from scratch -22]: Advanced chapter - Architecture - Design and modeling of FPGA internal hardware circuit
- Leetcode question brushing 03 stack
- leetcode743. 网络延迟时间(中等, dijkstra)
- Golang learning essay
猜你喜欢

开发者来稿|AMD赛灵思中文论坛分享 - 提问的智慧

C language implementation of the classic eight queens problem

Rasa dialogue robot helpdesk (III)

Crypto JS reports uglifyjs error

MySQL download and installation

Camera model_

一种不带CPU的DPU架构:Hyperion

September 3, 2021 visual notes

三、上传织物图片至SQL Server并提供name进行展示织物照片
![[wsl2]wsl2 migrate virtual disk file ext4 vhdx](/img/e9/4e08e07c2de2f99c2938e79f7f1c44.png)
[wsl2]wsl2 migrate virtual disk file ext4 vhdx
随机推荐
[soft test] software designer knowledge points sorting (to be updated)
FLIP动画实现思路
Page optimization - Notes
Exercise 5.14 input n strings, arrange them in alphabetical order and output them.
Happy string
Use koa to mock data and set cross domain issues
Introduction to common activation functions
Wildcard usage of go standard library FMT
项目实训(十七)---个人工作总结
Traversal of binary tree - first order traversal, middle order traversal, and second order traversal
MySQL performance optimization
Leetcode find duplicates
Uuid/guid introduction, generation rules and generation codes
csdn涨薪技术之Jmeter接口测试数据库断言的实现与设计
#pragma comment(lib,“urlmon.lib“)
On February 26, 2022, the latest news of national oil price adjustment today
About inquirerjs
Redis usage optimization summary learning
[Andoid][踩坑]CTS 11_r3开始出现的testBootClassPathAndSystemServerClasspath_nonDuplicateClasses FAIL问题分析
Idea installation tutorial