0
$\begingroup$

Rosanswers logo

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..


Originally posted by mutreras on ROS Answers with karma: 1 on 2013-05-07

Post score: 4

$\endgroup$

3 Answers 3

0
$\begingroup$

Rosanswers logo

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.


Originally posted by ryeakle with karma: 36 on 2013-05-07

This answer was ACCEPTED on the original site

Post score: 2


Original comments

Comment by amarbanerjee23 on 2017-06-22:
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 ?

$\endgroup$
0
$\begingroup$

Rosanswers logo

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. :)


Originally posted by Ben_S with karma: 2510 on 2013-05-07

This answer was NOT ACCEPTED on the original site

Post score: 1


Original comments

Comment by mutreras on 2013-05-08:
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

Comment by ryeakle on 2013-05-08:
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.

$\endgroup$
0
$\begingroup$

Rosanswers logo

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')

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

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

    def run(self):
        r = rospy.Rate(10)
        while not rospy.is_shutdown():
            self.pub.publish(self.value)
            r.sleep()

Originally posted by Ernest with karma: 341 on 2013-05-08

This answer was NOT ACCEPTED on the original site

Post score: 3


Original comments

Comment by Michdo93 on 2020-07-23:
how can i run it?

Comment by Hugo on 2021-03-02:
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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.