rospy overview: Initialization and Shutdown | Messages | Publishers and Subscribers | Services | Parameter Server | Logging | Names and Node Information | Time | Exceptions

Publishing to a topic

See also: rospy.Publisher Code API

You can create a handle to publish messages to a topic using the rospy.Publisher class. The most common usage for this is to provide the name of the topic and the message class/type of the topic. You can then call publish() on that handle to publish a message, e.g.:

pub = rospy.Publisher('topic_name', std_msgs.msg.String)
pub.publish(std_msgs.msg.String("foo"))

rospy.Publisher initialization

rospy.Publisher(topic_name, msg_class)

  • The only required arguments to create a rospy.Publisher are the topic name and the Message class, e.g.

    pub = rospy.Publisher('topic_name', std_msgs.msg.String)

    There are additional advanced options that allow you to configure the Publisher:

    • subscriber_listener=rospy.SubscribeListener

      • Receive callbacks via a rospy.SubscribeListener instance when new subscribers connect and disconnect.

      tcp_nodelay=True

      • Enable TCP_NODELAY on publisher’s socket, which disables Nagle algorithm on TCPROS connnections. This results in lower latency publishing at the cost of efficiency.

      latch=True

      • Enable 'latching' on the connection. When a connection is latched, the last message published is saved and sent to any future subscribers that connect. This is useful for slow-changing or static data like a map.

Publisher.publish()

There are three different ways of calling publish() ranging from an explicit style, where you provide a Message instance, to two implicit styles that create the Message instance on the fly.

Explicit style

  • The explicit style is simple: you create your own Message instance and pass it to publish, e.g.:

    pub.publish(std_msgs.msg.String("hello world"))

Implicit style with in-order arguments

  • In the in-order style, a new Message instance will be created with the arguments provided, in order. The argument order is the same as the order of the fields in the Message, and you must provide a value for all of the fields. For example, std_msgs.msg.String only has a single string field, so you can call:

    pub.publish("hello world")

    std_msgs.msg.ColorRGBA has four fields (r, g, b, a), so we could call:

    pub.publish(255.0, 255.0, 255.0, 128.0)

    which would create a ColorRGBA instance with r, g, and b set to 255.0 and a set to 128.0.

Implicit style with keyword arguments

  • In the keyword style, you only initialize the fields that you wish to provide values for. The rest receive default values (e.g. 0, empty string, etc...). For std_msgs.msg.String, the name of its lone field is data, so you can call:

    pub.publish(data="hello world")

    std_msgs.msg.ColorRGBA has four fields (r, g, b, a), so we could call:

    pub.publish(b=255)

    which would publish a ColorRGBA instance with b=255 and the rest of the fields set to 0.0.

Complete example

   1 import rospy
   2 from std_msgs.msg import String
   3 
   4 pub = rospy.Publisher('topic_name', String)
   5 rospy.init_node('node_name')
   6 r = rospy.Rate(10) # 10hz
   7 while not rospy.is_shutdown():
   8    pub.publish("hello world")
   9    r.sleep()

Subscribing to a topic

   1 import rospy
   2 from std_msgs.msg import String
   3 
   4 def callback(data):
   5     rospy.loginfo("I heard %s",data.data)
   6     
   7 def listener():
   8     rospy.init_node('node_name')
   9     rospy.Subscriber("chatter", String, callback)
  10     # spin() simply keeps python from exiting until this node is stopped
  11     rospy.spin()

Connection Information

A subscriber can get access to a "connection header", which includes debugging information such as who sent the message, as well information like whether or not a message was latched. This data is stored as the _connection_header field of a received message.

e.g.

print m._connection_header
{'callerid': '/talker_38321_1284999593611',
 'latching': '0',
 'md5sum': '992ce8a1687cec8c8bd883ec73ca41d1',
 'message_definition': 'string data\n\n',
 'topic': '/chatter',
 'type': 'std_msgs/String'}

We do not recommend using callerid information beyond debugging purposes, as it can lead to brittle code in an anonymous publish/subscribe architecture.

Wiki: rospy/Overview/Publishers and Subscribers (last edited 2010-09-20 16:28:25 by KenConley)