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:

  1. int8, int16, int32, int64 (plus uint*)
  2. float32, float64
  3. string
  4. time, duration
  5. other msg files
  6. 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:

  1. 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.
  2. 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)

  1. 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.
  2. 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
原创粉丝点击