当前位置:网站首页>ROS service communication programming
ROS service communication programming
2022-06-30 06:45:00 【Running on the road】
1 ROS-Service Communication programming
1.1 Service model
- Client client (person_client) Release request (Request) Send information to the service .
- Sever Server side (person_sever) Received request and data content , In the initial response to it (Response)
- service service (show_person)
Pictured :
1.2 Basic steps
- 1、 Create feature packs and compile
- 2、 Definition srv file
- 3、 modify package.xml file
- 4、 modify CMakeList.txt file
- 5、 Programming
- 6、 Compile operation
1.3 The detailed steps
1.3.1 Create feature packs and compile
- 1、 Enter the workspace src in .
cd ~/catkin_ws/src/
- 2、 Create Feature Pack
[email protected]:~/catkin_ws/src$ catkin_create_pkg learning_sevice roscpp rospy std_msgs geometry_msgs turtlesim
Created file learning_sevice/package.xml
Created file learning_sevice/CMakeLists.txt
Created folder learning_sevice/include/learning_sevice
Created folder learning_sevice/src
Successfully created files in /home/ubuntu/catkin_ws/src/learning_sevice. Please adjust the values in package.xml.
- 3、 Compile workspace
- You can edit it and compile it . Those who dislike trouble can skip .
[email protected]:~/catkin_ws$ cd ~/catkin_ws/
[email protected]:~/catkin_ws$ catkin_make
1.3.2 Definition srv file
- 1、 Open the function package path , Create folders and files
[email protected]:~/catkin_ws/src$ cd ~/catkin_ws/src/learning_sevice/
[email protected]:~/catkin_ws/src/learning_sevice$ mkdir srv
[email protected]:~/catkin_ws/src/learning_sevice$ cd srv/
[email protected]:~/catkin_ws/src/learning_sevice/srv$ ls
[email protected]:~/catkin_ws/src/learning_sevice/srv$ touch Person.srv
[email protected]:~/catkin_ws/src/learning_sevice/srv$ ls
Person.srv
- 2、 To write srv file
[email protected]:~/catkin_ws/src/learning_sevice/srv$ gedit Person.srv
- Write the following
string name
uint8 age
uint8 sex
uint8 unknow = 0
uint8 male = 1
uint8 female = 2
---
string result
- The top of which is Request Definition of the type of , Here is Response The type definitions of are separated by three horizontal lines .
1.3.3 modify package.xml file
- 1、 Enter the corresponding file path , Open file
[email protected]:~/catkin_ws/src/learning_sevice/srv$ cd ../
[email protected]:~/catkin_ws/src/learning_sevice$ ls
CMakeLists.txt include package.xml src srv
[email protected]:~/catkin_ws/src/learning_sevice$ gedit package.xml
- 2、 Enter the following , Save and exit
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
1.3.4 modify CMakeList.txt file
- 1、 Open the corresponding file path and open the file
[email protected]:~/catkin_ws/src/learning_sevice$ gedit package.xml
[email protected]:~/catkin_ws/src/learning_sevice$ gedit CMakeLists.txt
- 2、 newly added message_generation Dependency package
find_package(catkin REQUIRED COMPONENTS
...
message_generation
)
- 3、 Add compilation dependency and execution dependency
add_service_files(FILES Person.srv)
generate_messages(DEPENDENCIES std_msgs)
- 4、 Open the function package required for compilation
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES learning_sevice
CATKIN_DEPENDS geometry_msgs roscpp rospy std_msgs turtlesim message_runtime
# DEPENDS system_lib
)
- 5、 Compile the , Check if there is any problem with the addition . Confident people can skip .
[email protected]:~/catkin_ws$ cd ~/catkin_ws/
[email protected]:~/catkin_ws$ catkin_make
1.3.5 Programming
- 1、 stay learning_sevice The next path src Create files and write person_server.cpp Program
// The header file
#include <ros/ros.h>
#include "learning_service/Person.h"
// service Callback function , Input parameters req, Output parameters res
bool personCallback(learning_service::Person::Request &req,
learning_service::Person::Response &res)
{
// Display request data
ROS_INFO("Person: name:%s age:%d sex:%d", req.name.c_str(), req.age, req.sex);
// Set feedback data
res.result = "OK";
return true;
}
int main(int argc, char **argv)
{
// ROS Node initialization
ros::init(argc, argv, "person_server");
// Create node handle
ros::NodeHandle n;
// Create a file called /show_person Of server, Register callback function personCallback
ros::ServiceServer person_service = n.advertiseService("/show_person", personCallback);
// Loop waiting for callback function
ROS_INFO("Ready to show person informtion.");
ros::spin();
return 0;
}
- 2、 stay learning_sevice The next path src Create files and write person_client.cpp Program
#include <ros/ros.h>
#include "learning_service/Person.h"
int main(int argc, char** argv)
{
// initialization ROS node
ros::init(argc, argv, "person_client");
// Create node handle
ros::NodeHandle node;
// Find out /spawn After service , Create a service client , The connection name is /spawn Of service
ros::service::waitForService("/show_person");
ros::ServiceClient person_client = node.serviceClient<learning_service::Person>("/show_person");
// initialization learning_service::Person Request data for
learning_service::Person srv;
srv.request.name = "Tom";
srv.request.age = 20;
srv.request.sex = learning_service::Person::Request::male;
// Request service call
ROS_INFO("Call service to show person[name:%s, age:%d, sex:%d]",
srv.request.name.c_str(), srv.request.age, srv.request.sex);
person_client.call(srv);
// Display the service call result
ROS_INFO("Show person result : %s", srv.response.result.c_str());
return 0;
};
1.3.6 Compile operation
[email protected]:~/catkin_ws$ cd ~/catkin_ws/
[email protected]:~/catkin_ws$ catkin_make
边栏推荐
- 原理:WebMvcConfigurer 与 WebMvcConfigurationSupport避坑指南
- Centos8 install redis
- C language final experiment report (student achievement management system) source code
- Arrangement of in-depth learning materials
- 程序猿入门攻略(十一)——结构体
- [untitled]
- 手机开户一般哪个证券公司好?还有,在线开户安全么?
- Never forget the original intention, and be lazy if you can: C # operate word files
- Switch must be better than if Else fast
- InnoDB engine in MySQL
猜你喜欢
随机推荐
Rising posture series: fancy debugging information
Introduction to programming ape (11) -- structure
2020-10-06
IO stream (file class introduction)
与MQTT的初定情缘
The 40g high-efficiency cloud disk purchased by Alibaba cloud is only 20g attached
1.7 - CPU performance indicators
RT thread Kernel Implementation (III): implementation of idle threads and blocking delay
Unable to access the Internet at win10 /11 hotspot
When to use redis
Px4 control mode summary
Four tips in numpy
Bat usage details 2
明天!“移动云杯”大赛空宣会开播!
1.8 - 多级存储
First experience of Galaxy Kirin
1.3 - Code System
Static routing job
Fastapi learning Day1
1.5 - 逻辑运算








