定义srv服务
roscd beginner_tutorials
mkdir srv
cd srv
touch AddTwoInts.srv
rosed beginner_tutorials AddTwoInts.srv (使用rosed需要安装vim)
srv文件分为请求和响应两部分,由'---‘分隔。在AddTwoInts.srv中输入代码:
int64 A
int64 B
---
int64 Sum
打开文件rosed beginner_tutorials package.xml,增加依赖,
<build_depend>message_generation</build_depend>
<run_depend>message_runtime</run_depend>
打开文件rosed beginner_tutorials CMakeLists.txt,增加依赖,
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
message_generation
)
add_service_files(
FILES
AddTwoInts.srv
)
generate_messages(
DEPENDENCIES
std_msgs
)
cd ~/catkin_ws
catkin_make
rossrv show beginner_tutorials/AddTwoInts (检查服务)
编写服务端节点
roscd beginner_tutorials/scripts
touch add_two_ints_server.py
chmod +x add_two_ints_server.py
rosed beginner_tutorials add_two_ints_server.py
在add_two_ints_server.py输入:
#!/usr/bin/env python
NAME = 'add_two_ints_server'
# import the AddTwoInts service
from beginner_tutorials.srv import *
import rospy
def add_two_ints(req):
print("Returning [%s + %s = %s]" % (req.a, req.b, (req.a + req.b)))
sum = req.a + req.b
return AddTwoIntsResponse(sum)
def add_two_ints_server():
rospy.init_node(NAME)
s = rospy.Service('add_two_ints', AddTwoInts, add_two_ints)
print "Ready to add Two Ints"
# spin() keeps Python from exiting until node is shutdown
rospy.spin()
if __name__ == "__main__":
add_two_ints_server()
cd ~/catkin_ws/
catkin_make
rosrun beginner_tutorials add_two_ints_server.py (新终端)
rosservice list (列出服务)
rosservice args /add_two_ints (查看服务类型) (出错)
rosservice call /add_two_ints 1 2 (调用服务) (出错)
编写客户端节点
roscd beginner_tutorials/scripts
touch add_two_ints_client.py
chmod +x add_two_ints_client.py
rosed beginner_tutorials add_two_ints_client.py
在add_two_ints_client.py输入:
#!/usr/bin/env python
import sys
import os
import rospy
# imports the AddTwoInts service
from rospy_tutorials.srv import *
import sys
import os
import rospy
# imports the AddTwoInts service
from rospy_tutorials.srv import *
## add two numbers using the add_two_ints service
## @param x int: first number to add
## @param y int: second number to add
def add_two_ints_client(x, y):
# NOTE: you don't have to call rospy.init_node() to make calls against
# a service. This is because service clients do not have to be
# nodes.
# block until the add_two_ints service is available
# you can optionally specify a timeout
rospy.wait_for_service('add_two_ints')
try:
# create a handle to the add_two_ints service
add_two_ints = rospy.ServiceProxy('add_two_ints', AddTwoInts)
print "Requesting %s+%s"%(x, y)
# simplified style
resp1 = add_two_ints(x, y)
# formal style
resp2 = add_two_ints.call(AddTwoIntsRequest(x, y))
if not resp1.sum == (x + y):
raise Exception("test failure, returned sum was %s"%resp1.sum)
if not resp2.sum == (x + y):
raise Exception("test failure, returned sum was %s"%resp2.sum)
return resp1.sum
except rospy.ServiceException, e:
print "Service call failed: %s"%e
def usage():
return "%s [x y]"%sys.argv[0]
if __name__ == "__main__":
argv = rospy.myargv()
if len(argv) == 1:
import random
x = random.randint(-50000, 50000)
y = random.randint(-50000, 50000)
elif len(argv) == 3:
try:
x = int(argv[1])
y = int(argv[2])
except:
print usage()
sys.exit(1)
else:
print usage()
sys.exit(1)
print "%s + %s = %s"%(x, y, add_two_ints_client(x, y))
cd ~/catkin_ws/
catkin_make
rosrun beginner_tutorials add_two_ints_client.py 4 5
编写客户端节点
roscd beginner_tutorials/scripts
touch add_two_ints_client.py