0
$\begingroup$

Rosanswers logo

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!


Originally posted by diegomex on ROS Answers with karma: 13 on 2014-09-29

Post score: 0


Original comments

Comment by BennyRe on 2014-09-30:
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.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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
rospy.init_node("One_node_to_rule_them_all")
pub = rospy.Publisher("echo", String)

def callback(input):
        pub.publish(input.data)
        # 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


Originally posted by Rabe with karma: 683 on 2014-09-29

This answer was ACCEPTED on the original site

Post score: 3


Original comments

Comment by diegomex on 2014-09-29:
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

$\endgroup$

Your Answer

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