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

how to create a combining subscriber and publisher node in python??

asked 2013-05-07 07:40:50 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

I need a combined subscriber and publisher node in python... but a don't know how i can do that...

my project has 2 nodes.. one for a camera and one for two motors.. I need communicate those nodes... cameras node(in C) has to take a picture when motors node says "I moved" and cameras has to say "I already take a picture" so the motors node(in PYTHON) has to move again..

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2013-05-07 21:03:19 -0500

ryeakle gravatar image

updated 2013-05-08 11:08:37 -0500

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.

edit flag offensive delete link more


Hi, I am trying to create multiple publishers in python, but the continuous while loop blocks the other publisher loop to be executed. Could you please suggest of there is any ros functionality available so as to spawn multiple publishers, services and action servers in one single node ?

amarbanerjee23 gravatar image amarbanerjee23  ( 2017-06-22 03:29:54 -0500 )edit

answered 2013-05-08 16:30:12 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

From @isherman's answer to this (very similar) question, here is some pseudocode:

import rospy
from std_msgs.msg import Int32

class Echo(object):
    def __init__(self):
        self.value = 0

        rospy.init_node('echoer') = rospy.Publisher('/out_value', Int32, latch=True)
        rospy.Subscriber('/in_value', Int32, self.update_value)

    def update_value(self, msg):
        self.value =

    def run(self):
        r = rospy.Rate(10)
        while not rospy.is_shutdown():
edit flag offensive delete link more


how can i run it?

Michdo93 gravatar image Michdo93  ( 2020-07-23 16:39:10 -0500 )edit

I tried to use this template but it runs the subscriber once then stops in while loop holding the first value published. The workaround I used was remove the while loop and put a rospy.spin() after the subscriber. @Michdo93, to run it, just call Echo() inside your main function and use rosrun to execute it.

Hugo gravatar image Hugo  ( 2021-03-02 05:02:39 -0500 )edit

answered 2013-05-07 21:59:48 -0500

Ben_S gravatar image

Independent of how to achieve this with python (i dont know that very well, have a look at the answer from @ryeakle), i would advise you to implement a service in your camera node, that gets called from your motor controller. That way you have all the control you need and its easy to give feedback, if the picture was taken successfully. Also synchronisation becomes much easier. :)

edit flag offensive delete link more


my camera node is in C... and I don't understand very well the srv structure in C.. because it calls a whatever.h so i don't have that whatever.h... if you want i could send you the code

mutreras gravatar image mutreras  ( 2013-05-08 08:31:24 -0500 )edit

The whatever.h file is generated when you compile the project with either catkin or rosmake. If you follow the procedure in the ROS tutorials for building a service, the build system will generate the .h file for you when you build the project.

ryeakle gravatar image ryeakle  ( 2013-05-08 11:04:41 -0500 )edit

Question Tools


Asked: 2013-05-07 07:40:50 -0500

Seen: 23,962 times

Last updated: May 08 '13