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

Revision history [back]

click to hide/show revision 1
initial version

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

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

import rospy
from std_msgs.msg import String
rospy.init_node("One_node_to_rule_them_all")
pub = rospy.Publisher("chatter", String)

def callback(data):
        print data

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

pub.publish("Hello to myself")

>>> data: Hello to myself

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("chatter", rospy.Publisher("echo", String)

def callback(data):
callback(input):
 print 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)

pub.publish("Hello to myself")

>>> data: Hello to myself
pub2 = rospy.Publisher("chatter", String)
pub2.publish("'Stop repeating everything I say")

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