写一个学习笔记方便自己以后回查
写一个服务节点
我们写一个服务节点,请求两个int, 返回这两个int的和
roscd beginner_tutorials
在此之前请确保你搞过之前那个srv文件和msg文件的教程
源码
在 scripts/add_two_ints_server.py
文件下粘贴上下面的源码
#!/usr/bin/env python
from __future__ import print_function
from beginner_tutorials.srv import AddTwoInts,AddTwoIntsResponse
import rospy
def handle_add_two_ints(req):
print("Returning [%s + %s = %s]"%(req.a, req.b, (req.a + req.b)))
return AddTwoIntsResponse(req.a + req.b)
def add_two_ints_server():
rospy.init_node('add_two_ints_server')
s = rospy.Service('add_two_ints', AddTwoInts, handle_add_two_ints)
print("Ready to add two ints.")
rospy.spin()
if __name__ == "__main__":
add_two_ints_server()
#!/usr/bin/env python
from future import print_function
import sys
import rospy
from beginner_tutorials.srv import *
def add_two_ints_client(x, y):
rospy.wait_for_service(‘add_two_ints’)
try:
add_two_ints = rospy.ServiceProxy(‘add_two_ints’, AddTwoInts)
resp1 = add_two_ints(x, y)
return resp1.sum
except rospy.ServiceException as e:
print(“Service call failed: %s”%e)
def usage():
return “%s [x y]”%sys.argv[0]
if name == “main”:
if len(sys.argv) == 3:
x = int(sys.argv[1])
y = int(sys.argv[2])
else:#!/usr/bin/env python
from future import print_function
import sys
import rospy
from beginner_tutorials.srv import *
def add_two_ints_client(x, y):
rospy.wait_for_service(‘add_two_ints’)
try:
add_two_ints = rospy.ServiceProxy(‘add_two_ints’, AddTwoInts)
resp1 = add_two_ints(x, y)
return resp1.sum
except rospy.ServiceException as e:
print(“Service call failed: %s”%e)
def usage():
return “%s [x y]”%sys.argv[0]
if name == “main”:
if len(sys.argv) == 3:
x = int(sys.argv[1])
y = int(sys.argv[2])
else:
print(usage())
sys.exit(1)
print(“Requesting %s+%s”%(x, y))
print("%s + %s = %s"%(x, y, add_two_ints_client(x, y)))
print(usage())
sys.exit(1)
print(“Requesting %s+%s”%(x, y))
print("%s + %s = %s"%(x, y, add_two_ints_client(x, y)))最后别忘了改变py文件的执行权限
scripts/add_two_ints_server.py
并且在CMakeLists.txt里增加修改代码片段如下
catkin_install_python(PROGRAMS scripts/add_two_ints_server.py
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)
代码的解释在下面注释
#!/usr/bin/env python
from __future__ import print_function
from beginner_tutorials.srv import AddTwoInts,AddTwoIntsResponse # 这个包是真的没看懂
import rospy
def handle_add_two_ints(req):
print("Returning [%s + %s = %s]"%(req.a, req.b, (req.a + req.b)))
return AddTwoIntsResponse(req.a + req.b) # 返回一个值
def add_two_ints_server():
rospy.init_node('add_two_ints_server') # 初始化这个节点名字叫 add_two_ints_server
s = rospy.Service('add_two_ints', AddTwoInts, handle_add_two_ints) # 运行一个服务
# 名字叫 add_two_ints 接收参数为AddTwoInts,执行
# handle_add_two_ints()
print("Ready to add two ints.")
rospy.spin()
if __name__ == "__main__":
add_two_ints_server()
写一个客户端节点
源码
在scripts/add_two_ints_client.py
文件下粘贴上下面的源码:
#!/usr/bin/env python
from __future__ import print_function
import sys
import rospy
from beginner_tutorials.srv import *
def add_two_ints_client(x, y):
rospy.wait_for_service('add_two_ints')
try:
add_two_ints = rospy.ServiceProxy('add_two_ints', AddTwoInts)
resp1 = add_two_ints(x, y)
return resp1.sum
except rospy.ServiceException as e:
print("Service call failed: %s"%e)
def usage():
return "%s [x y]"%sys.argv[0]
if __name__ == "__main__":
if len(sys.argv) == 3:
x = int(sys.argv[1])
y = int(sys.argv[2])
else:
print(usage())
sys.exit(1)
print("Requesting %s+%s"%(x, y))
print("%s + %s = %s"%(x, y, add_two_ints_client(x, y)))
最后别忘了改变py文件的执行权限
scripts/add_two_ints_client.py
并且在CMakeLists.txt里增加修改代码片段如下
catkin_install_python(PROGRAMS scripts/add_two_ints_server.py /script/add_two_ints_server.py
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)
测试自己写的服务端和客户端节点
- 先setup 工作空间
- catkin_make编译
rosrun beginner_tutorials add_two_ints_server.py
确认代码执行成功,启动服务节点
这里要你启动rosmaster, 那么我们新终端启动核心roscore
它就ready 了
-
新终端
rosrun beginner_tutorials add_two_ints_client.py
确认代码执行成功,启动客户端节点
-
rosrun beginner_tutorials add_two_ints_client.py 4 5
发送请求
-
查看客户端接受到请求
- 查看服务端表示响应了请求