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

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

asked 2014-09-29 09:22:09 -0500

diegomex gravatar image

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

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

Comments

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 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
3

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

Rabe gravatar image

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

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

edit flag offensive delete link more

Comments

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 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2014-09-29 09:22:09 -0500

Seen: 1,473 times

Last updated: Sep 29 '14