当前位置:网站首页>ROS notes (08) - definition and use of service data
ROS notes (08) - definition and use of service data
2022-06-28 08:03:00 【wohu1104】
1. Service model
The topic model we want to implement is as follows :
2. Custom service implementation
2.1 Definition srv file
In Feature Pack service_client Create one in the directory srv Folder , Create a... In this folder Person.srv file , As shown in the figure below :
Person.srv There is our custom message format in
string name
uint8 age
uint8 sex
uint8 unknown = 0
uint8 male = 1
uint8 female = 2
---
string result
2.2 Add Feature Pack dependencies
stay package.xml Add the following exec_depend Operational dependency :
<exec_depend>message_generation</exec_depend>
<exec_depend>message_runtime</exec_depend>
2.3 Add compile options

find_package(catkin REQUIRED COMPONENTS
geometry_msgs
rospy
std_msgs
turtlesim
message_generation # The new content
add_message_files(
FILES
Person.srv
)
generate_messages(
DEPENDENCIES
std_msgs
)
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES topic_demo
CATKIN_DEPENDS geometry_msgs rospy std_msgs turtlesim message_runtime
# DEPENDS system_lib
)
2.4 Compile and generate language related files
To the root directory of the project catkin_make command
$ catkin_make
Base path: /home/wohu/project/ros/ros_demo
Source space: /home/wohu/project/ros/ros_demo/src
Build space: /home/wohu/project/ros/ros_demo/build
Devel space: /home/wohu/project/ros/ros_demo/devel
Install space: /home/wohu/project/ros/ros_demo/install
####
#### Running command: "make cmake_check_build_system" in "/home/wohu/project/ros/ros_demo/build"
####
####
#### Running command: "make -j12 -l12" in "/home/wohu/project/ros/ros_demo/build"
####
[ 0%] Built target std_msgs_generate_messages_nodejs
[ 0%] Built target std_msgs_generate_messages_lisp
[ 0%] Built target std_msgs_generate_messages_eus
[ 0%] Built target std_msgs_generate_messages_cpp
[ 0%] Built target std_msgs_generate_messages_py
[ 0%] Built target _topic_demo_generate_messages_check_deps_Person
[ 0%] Built target _service_client_generate_messages_check_deps_Person
[ 21%] Generating C++ code from service_client/Person.srv
[ 21%] Built target topic_demo_generate_messages_py
[ 28%] Generating Lisp code from service_client/Person.srv
[ 35%] Generating Python code from SRV service_client/Person
[ 42%] Built target topic_demo_generate_messages_lisp
[ 50%] Generating Javascript code from service_client/Person.srv
[ 57%] Generating EusLisp code from service_client/Person.srv
[ 71%] Built target topic_demo_generate_messages_eus
[ 78%] Built target topic_demo_generate_messages_nodejs
[ 85%] Built target topic_demo_generate_messages_cpp
[ 85%] Built target topic_demo_generate_messages
[ 85%] Built target service_client_generate_messages_lisp
[ 85%] Built target service_client_generate_messages_nodejs
[ 92%] Built target service_client_generate_messages_eus
[100%] Generating Python srv __init__.py for service_client
[100%] Built target service_client_generate_messages_cpp
[100%] Built target service_client_generate_messages_py
[100%] Built target service_client_generate_messages
You can see that code files in various languages will be generated . among Python The relevant code files are in
/devel/lib/python2.7/dist-packages
Under the table of contents 
2.5 Run code
Client code person_client.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# The routine will request /show_person service , Service data type learning_service::Person
import sys
import rospy
from service_client.srv import Person, PersonRequest
def person_client():
# ROS Node initialization
rospy.init_node('person_client')
# Find out /spawn After service , Create a service client , The connection name is /spawn Of service
rospy.wait_for_service('/show_person')
try:
person_client = rospy.ServiceProxy('/show_person', Person)
# Request service call , Enter request data
response = person_client("Tom", 20, PersonRequest.male)
return response.result
except rospy.ServiceException, e:
print "Service call failed: %s"%e
if __name__ == "__main__":
# The service calls and displays the call result
print "Show person result : %s" %(person_client())
Server code person_server.py Code
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# The routine will execute /show_person service , Service data type learning_service::Person
import rospy
from service_client.srv import Person, PersonResponse
def personCallback(req):
# Display request data
rospy.loginfo("Person: name:%s age:%d sex:%d", req.name, req.age, req.sex)
# Feedback data
return PersonResponse("OK")
def person_server():
# ROS Node initialization
rospy.init_node('person_server')
# Create a file called /show_person Of server, Register callback function personCallback
s = rospy.Service('/show_person', Person, personCallback)
# Loop waiting for callback function
print "Ready to show person informtion."
rospy.spin()
if __name__ == "__main__":
person_server()
Run the code command :
Server code :
$ cd ~/catkin_ws
$ catkin_make
$ source ./devel/setup.bash
$ rescore
$ rosrun service_client person_server.py
Ready to show person informtion.
[INFO] [1655723144.945655]: Person: name:Tom age:20 sex:1
[INFO] [1655723149.379382]: Person: name:Tom age:20 sex:1
[INFO] [1655723150.891466]: Person: name:Tom age:20 sex:1
Client code :
$ rosrun service_client person_client.py
response is result: "OK"
Show person result : OK
边栏推荐
- HJ成绩排序
- ACM notes
- 股票炒股注册开户靠谱吗?安全吗?
- Analyze 5 indicators of NFT project
- Soft test -- software designer -- database design of afternoon questions
- 【js】-【DFS、BFS应用】-学习笔记
- Generation and verification of JWT token
- Recommended system series (Lecture 5): Optimization Practice of sorting model
- ROS 笔记(08)— 服务数据的定义与使用
- Study notes 22/1/18
猜你喜欢

Soft exam -- software designer -- afternoon question data flow diagram DFD

Kubernetes theoretical basis

sql分析(查询截取分析做sql优化)

Do you know TCP protocol (1)?

Redis master-slave structure and application scenarios

Online WPS tool

你了解TCP協議嗎(二)?

Disposition Flex

Section 8: DMA of zynq

MySQL two table connection principle (understand join buf)
随机推荐
Study notes 22/1/19 and 22/1/20
Ambari (IX) --- use expect to realize no interaction in ambri server setup phase (valid for personal test)
How to use redis to solve concurrency problems
Estimation of SQL execution cost by MySQL query optimizer
Configuring MySQL multi instance master-slave synchronization for Linux
Redis uses sentinel master-slave switching. What should the program do?
Leetcode learning records
Section 8: DMA of zynq
Co process, asyncio, asynchronous programming
asp. Net error "/" server error in the application. String or binary data would be truncated. The statement...
SQL Master slave Replication Build
HJ character count
SOC clock configuration
Static resource compression reduces bandwidth pressure and increases access speed
Sword finger offer|: linked list (simple)
异或的应用。(提取出数字中最右侧的1,面试中经常用的到)
Trigonometric transformation formula
Software testing and quality final review
券商注册开户靠谱吗?安全吗?
SQL master-slave replication setup