Python on ROS
来源:互联网 发布:优化教育怎么样 编辑:程序博客网 时间:2024/05/14 21:31
- WIKI
Services:
ROSServices are defined bysrv files, which contains a request message and a response message. These are identical to the messages used with ROSTopics (see rospy message overview). rospy converts these srv files into Python source code and creates three classes that you need to be familiar with: service definitions, request messages, and response messages. The names of these classes come directly from thesrv filename:
my_package/srv/Foo.srv →my_package.srv.Foo
my_package/srv/Foo.srv →my_package.srv.FooRequest
my_package/srv/Foo.srv → my_package.srv.FooResponse
srv : http://wiki.ros.org/srv
A service description file consists of a request and aresponsemsg type, separated by'---'. Any two.msg files concatenated together with a'---' are a legal service description.
Client Library Support
In Python, the generated Python service type file (e.g.foo_srvs.srv.Foo) provides nearly all the information you might want about a.srv file. You can examine the __slots__ and_slot_types and other fields to introspect information about the request and reply Messages. Foradvanced users, theroslib.srvs module in theroslib Package provides basic support for parsing.srv files in Python. This library is only recommended for those implementing service generators.Creating a ROS msg and srv
Reference article: 如何创建使用ros的service?Python版
- Notes
- Files we need to create
- filename.srv file; This file is used to define the input(request message) and output(response message) for this service class. rospy converts thesesrv files into Python source code and creates three classes that you need to be familiar with: service definitions, request messages, and response messages. The names of these classes come directly from thesrv filename.
- server.py file; This file is to create a service.
- client.py file; This file is to produce a request for the service.
- filename.srv file; This file is used to define the input(request message) and output(response message) for this service class. rospy converts thesesrv files into Python source code and creates three classes that you need to be familiar with: service definitions, request messages, and response messages. The names of these classes come directly from thesrv filename.
- Files we need to create
- SRV file (e.g WordCount.srv and AddTwoInts.srv in the package of 'py_package')
- This file usually locate in the 'srv' folder of your package. So when we use this srv, we can using following code in the server.py to import three classes (WordCount,WordCountRequest,WordCountResponse) (if pyLint prompt error for this line that can not find the py_package.srv, we ignore it. If using python command to run this file and an error of 'ImportError: No module named py_package.srv',then try with'source devel/setup.bash' at first in your terminal where running the python command, if error still shows again ,then try catkin_make to rebuild again not just with roboware's builder tool because it sometimes does not work well . Here the py_pacakge.srv is the path of srv folder rather than a srv file)
from py_package.srv import AddTwoInts,AddTwoIntsResponse,WordCount,WordCountResponse
- AddTwoInts.srv
int64 aint64 b---int64 sum
- WordCount.srv
string words---uint32 count
- server.py
from py_package.srv import AddTwoInts,AddTwoIntsResponse,WordCount,WordCountResponseimport rospydef 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 handle_word_count(req):print "Return [%s] "% (req.words)return WordCountResponse(len(req.words.split()))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."w = rospy.Service('word_count',WordCount,handle_word_count)print "Ready to count the length."rospy.spin()if __name__ == "__main__":add_two_ints_server()
- client.py
#!/usr/bin/env pythonimport sysimport rospyfrom py_package.srv import AddTwoInts,WordCountdef 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.sumexcept rospy.ServiceException, e:print "Service add_two_ints call failed: %s"%edef count_length_words(words):rospy.wait_for_service('word_count')try:word_counter = rospy.ServiceProxy('word_count',WordCount)word_count = word_counter(words)print words,'->',word_count.countreturn word_count.countexcept rospy.ServiceException,e:print "Service word_count call failed: %s"%edef 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])words = ' '.join(sys.argv[3:])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 "The count of \' %s \'is %s "%(words,count_length_words(words))
- Python on ROS
- ROS on ARM pcduino
- Android on ROS
- ccny_vision compiling on ros-fuerte
- DARwin-OP on the ROS
- installing ROS on Ubuntu 14.04
- ROS笔记:Python
- ros 编译 Python 文件
- ros, python初识
- ROS与python学习
- ROS on ARM--RK3066上本地编译ROS Groovy
- Running Bumblebee2 on ROS: tutorial[ROS下安装bumblebee2相机]
- 转载:ROS on ARM--RK3066上本地编译ROS Groovy
- ROS on ARMRK3066上本地编译ROS Groovy
- Howto Install and Configure ROS on Ubuntu
- All reinstallation of ROS on Ubuntu
- ros-rt-wmp compile on laptop
- PX4 Offboard Control Using MAVROS on ROS
- hadoop
- 23种设计模式之抽象工厂模式
- ASP从HTML标签中提取中文
- SQLServer存储过程实现单条件分页
- JS分段传输数据
- Python on ROS
- 从C++类创建后,自动创建的函数有哪些开始谈起
- JS表单提交
- ASP获取上月本月下月的第一天和最后一天
- Unity3d UGUI缩放循环拖动展示卡牌效果
- coding.net网站项目成员下载Maven项目并构建eclipse项目
- ASP跳出FOR循环
- java常用的数据类型
- ScrollView包裹RecyclerView导致滑动缓慢