ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To make a node both a publisher and a subscriber, you need to define both a publisher object (e.g. pub = rospy.Publisher('topic name', type)) and a subscriber (e.g. rospy.Subscriber('topic name', type, callback)) AND a callback for the subscriber object.

Each node will need to call rospy.spin() in order to check for callbacks.

To get a better intuitive feel for how ROS nodes work, here's a good way to think about it. Publishers and subscribers are objects that nodes can have and use. They're the tools the node uses to connect to and publish topics. A node can have anywhere from zero to many of publishers and subscribers. How many of each the node has is however many you declare. 3 different publishers (e.g. pub1, pub2, pub3) will let your node publish to 3 different topics. 2 subscribers and 1 publisher will let your node listen to 2 topics and publish to 1.

click to hide/show revision 2
changed 'it' to 'publishers and subscribers' for improved readability.

To make a node both a publisher and a subscriber, you need to define both a publisher object (e.g. pub = rospy.Publisher('topic name', type)) and a subscriber (e.g. rospy.Subscriber('topic name', type, callback)) AND a callback for the subscriber object.

Each node will need to call rospy.spin() in order to check for callbacks.

To get a better intuitive feel for how ROS nodes work, here's a good way to think about it. publishers and subscribers. Publishers and subscribers are objects that nodes can have and use. They're the tools the node uses to connect to and publish topics. A node can have anywhere from zero to many of publishers and subscribers. How many of each the node has is however many you declare. 3 different publishers (e.g. pub1, pub2, pub3) will let your node publish to 3 different topics. 2 subscribers and 1 publisher will let your node listen to 2 topics and publish to 1.

click to hide/show revision 3
update based on changes in question

To make a node both a publisher and a subscriber, you need to define both a publisher object (e.g. pub = rospy.Publisher('topic name', type)) and a subscriber (e.g. rospy.Subscriber('topic name', type, callback)) AND a callback for the subscriber object.

Each node will need to call rospy.spin() in order to check for callbacks.

To get a better intuitive feel for how ROS nodes work, here's a good way to think about publishers and subscribers. Publishers and subscribers are objects that nodes can have and use. They're the tools the node uses to connect to and publish topics. A node can have anywhere from zero to many of publishers and subscribers. How many of each the node has is however many you declare. 3 different publishers (e.g. pub1, pub2, pub3) will let your node publish to 3 different topics. 2 subscribers and 1 publisher will let your node listen to 2 topics and publish to 1.

(EDIT) For one node in C++, one node in Python: The same principle still applies to making a node in both C++ and python. To make a node both a publisher and subscriber, declare both publisher and subscriber objects in the same node.