ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

How to make a python listener&talker on the same node?

asked 2014-09-29 09:22:09 -0600

diegomex gravatar image

updated 2014-09-29 09:32:02 -0600

Hi, I would like to make a listener/talker in python. I already looked at the tutorials. However, these examples implement two separated codes, a listener and a talker, and I would like to implement them on the same node:

  1. Some external node (TalkerA) shouts some mesagges through a topic TopicA (NO PROBLEM, it's a simple talker).
  2. A second node (ListenerTalker) subscribes to TopicA and shouts something in TopicB.

For example, TalkerA shouts "Hello", and ListenerTalker listens TopicA and shouts "HelloWorld"...

How can I do this? It's a simple question, but it's not clear for me. Cheers!

edit retag flag offensive close merge delete


Do you want to do something else with your TopicA? Or is this data useful for another node? I'm asking because this looks a little bit like a bad design.

BennyRe gravatar image BennyRe  ( 2014-09-30 00:30:05 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2014-09-29 09:49:20 -0600

Rabe gravatar image

updated 2014-09-29 10:40:02 -0600

There is no magic trick to make this work, you just initialize both after you have called rospy.init_node(...) (Edited in response to your comment)

import rospy
from std_msgs.msg import String
pub = rospy.Publisher("echo", String)

def callback(input):
        # The message has 2 parts, a header and the data. We only want to use the actual data

sub = rospy.Subscriber("chatter", String, callback)

pub2 = rospy.Publisher("chatter", String)
pub2.publish("'Stop repeating everything I say")

When you run rostopic echo /echo It will show the desired output

edit flag offensive delete link more


Hi Rabe, thank you for your answer. What I want to do is to publish the received "data" in a new topic... I supose that maybe this should be done inside the callback that received "data", but I really dont know. "Data" is only avalable inside the callback local context, not for the publisher. Cheers

diegomex gravatar image diegomex  ( 2014-09-29 10:22:05 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2014-09-29 09:22:09 -0600

Seen: 1,298 times

Last updated: Sep 29 '14