ROS Kinetic: msg & srv
来源:互联网 发布:驱动器加密软件 编辑:程序博客网 时间:2024/06/05 11:24
- msg: msg files are simple text files that describe the fields of a ROS message. They are used to generate source code for messages in different languages.
- srv: an srv file describes a service. It is composed of two parts: a request and a response.
Field Type
msg files are just simple text files with a field type and field name per line. The field types you can use are:
- int8, int16, int32, int64 (plus uint*)
- float32, float64
- string
- time, duration
- other msg files
- variable-length array[] and fixed-length array[C]
There is also a special type in ROS: Header, the header contains a timestamp and coordinate frame information that are commonly used in ROS.
srv files are just like msg files, except they contain two parts: a request and a response. The two parts are separated by a ‘—’ line.
Making msg
Linux command:
roscd PACKAGE_NAMEmkdir msgcd msg
Create a msg file named Num.msg (‘Num’ is not necessary, choose the name you prefer) and add:
int64 Num
Now you have created a msg file. Go back to your package directory, edit the following to files:
- package.xml:
Add “message_generation” to your “build_depend” receptacle. (Just uncomment it!)
Add “message_runtime” to your “run_depend” receptacle. (Just uncomment it!)
Save & Exit. - CMakeLists.txt
Add “message_generation” to “find_package” command components.
Add “message_runtime” to “catkin_package” command components after the CATKIN_DEPENDS item. (Never forget to uncomment the CATKIN_DEPENDS item)
Uncomment “add_message_files” command and add “Num.msg“(Your msg) to its components.
Uncomment “generate_messages” command.
Save & Exit.
Now a new message has been added into your package. You can see it when calling:
rosmsg show Num.msg
Finally, you need to run:
roscd PACKAGE_NAMEcd ../..catkin_make installcd -
Making srv
Linux command:
roscd PACKAGE_NAMEmkdir srvcd srv
Create a srv file named AddTwoInts.srv (Choose your favorite name) and add:
int64 aint64 b---int64 sum
Now you have created a srv file. Go back to your package directory, edit the following to files: (Note that there are many steps same to those in Making msg section, you DO NOT need to repeat them once you have added a msg file)
- package.xml:
Add “message_generation” to your “build_depend” receptacle. (Just uncomment it!)
Add “message_runtime” to your “run_depend” receptacle. (Just uncomment it!)
Save & Exit. - CMakeLists.txt
Add “message_generation” to “find_package” command components.
Add “message_runtime” to “catkin_package” command components after the CATKIN_DEPENDS item. (Never forget to uncomment the CATKIN_DEPENDS item)
Uncomment “add_service_files” command and add “AddTwoInts.srv“(Your msg) to its components.
Uncomment “generate_messages” command.
Save & Exit.
Now a new service has been added into your package. You can see it when calling:
rossrv show AddTwoInts
Finally, you need to run (You have to run them despite that you can done in Making msg secion):
roscd PACKAGE_NAMEcd ../..catkin_make installcd -
Making Publisher & Subscriber
Linux command:
roscd PACKAGE_NAMEmkdir scriptscd scripts
Create two python files: (Choose your favorite names)
(1) talker.py
#!/usr/bin/env python# license removed for brevityimport rospyfrom std_msgs.msg import Stringdef talker(): pub = rospy.Publisher('chatter', String, queue_size=10) #components are "topic name","message type","queue size"(not necessary) rospy.init_node('talker', anonymous=True) #initialize a new node rate = rospy.Rate(10) # 10hz while not rospy.is_shutdown(): hello_str = "hello world %s" % rospy.get_time() rospy.loginfo(hello_str) #print on the screen, save to the log file and input to /rosout node pub.publish(hello_str) rate.sleep()if __name__ == '__main__': try: talker() except rospy.ROSInterruptException: pass
(2) listener.py
#!/usr/bin/env pythonimport rospyfrom std_msgs.msg import Stringdef callback(data): rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)def listener(): # In ROS, nodes are uniquely named. If two nodes with the same # node are launched, the previous one is kicked off. The # anonymous=True flag means that rospy will choose a unique # name for our 'listener' node so that multiple listeners can # run simultaneously. rospy.init_node('listener', anonymous=True) rospy.Subscriber("chatter", String, callback) # spin() simply keeps python from exiting until this node is stopped rospy.spin()if __name__ == '__main__': listener()
Never forget to make these python files executable.
chmod +x talker.pychmod +x listener.py
Then build the nodes:
cd ~/CATKIN_WORKSPACEcatkin_make
Wanna examine them? Run:
roscorerosrun PACKAGE_NAME talker.pyrosrun PACKAGE_NAME listener.py
Making Service & Client
linux command:
roscd PACKAGE_NAMEmkdir scriptscd scripts
Create two python files:
(1) add_two_ints_server.py
#!/usr/bin/env pythonfrom PACKAGE_NAME.srv import *import 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 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()
(2) add_two_ints_client.py
#!/usr/bin/env pythonimport sysimport rospyfrom PACKAGE_NAME.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, e: print "Service 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]) else: print usage() sys.exit(1) print "Requesting %s+%s"%(x, y) print "%s + %s = %s"%(x, y, add_two_ints_client(x, y))
Never forget to make them executable:
chmod +x add_two_ints_server.pychmod +x add_two_ints_client.py
Then build them: (Note that in add_two_ints_client.py you DO NOT need to initialize a node)
cd ~/CATKIN_WORKSPACEcatkin_make
Wanna examine them? Run:
roscorerosrun PACKAGE_NAME add_two_ints_server.pyrosrun PACKAGE_NAME add_two_ints_client.py 1 3
- ROS Kinetic: msg & srv
- 创建ROS msg 和 srv
- ROS的msg和srv
- (四)创建ROS msg 和 srv
- 创建ROS的msg和srv
- (四)创建ROS msg 和 srv
- 7、创建ROS msg和srv
- ros 创建自己的msg srv 注意
- Creating a ROS msg and srv
- ROS的学习(十一)ROS的msg和srv
- ROS学习手记 - 7 创建ROS msg & srv
- ROS学习笔记八:创建ROS msg和srv
- ROS(二) 创建一个ROS的msg和srv
- 一步步学ROS(3)建立msg和srv
- ROS学习笔记(消息msg和服务srv)
- ROS系统下创建msg与srv文件过程
- ROS学习第四弹 (ROS编辑器及配置/ROS msg/srv 消息和服务)
- [跟我学ROS]10.生成ROS msg文件及srv文件
- TagFlowLayout(新手搬运完整版) 简单的多选tag布局
- 来聊聊WWDC 苹果大会上的那些黑科技
- collect2 ../
- shell脚本 Permission denied,解决方法
- 用正则表达式实现中英文标点符号的替换
- ROS Kinetic: msg & srv
- iOS逆向工程---OS工具(Theos)
- 整合SpringMVC+Spring+Mybatis+mysql的maven配置文件pom.xml
- logback logback.xml常用配置详解(一)<configuration> and <logger>
- linux几种关机命令总结
- lua继承小记
- 设计模式---组合模式
- WWDC2017全球开发者大会汇总
- React Native 嵌入原生项目混合开发-多个RN Activity