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

i am new to ros and i want to know how to write a publisher subscriber code in the same node in python (can i get a basic hello world code)

asked 2021-01-25 05:42:41 -0500

ar89 gravatar image

updated 2021-01-27 01:03:01 -0500

!/usr/bin/env python

import rospy from std_msgs.msg import String

pub = rospy.Publisher('chatter', String, queue_size=10) rospy.init_node('chatternode', anonymous=False)

def callback(data): rospy.loginfo(rospy.get_caller_id() + 'I heard %s', data.data)

def chatternode(): rospy.Subscriber('chatter', String, callback) rate = rospy.Rate(10) # 10hz while not rospy.is_shutdown(): hello_str = "Hello World %s" % rospy.get_time() rospy.loginfo(hello_str) pub.publish(hello_str) rate.sleep() pub.publish(hello_str) rospy.spin()

if __name__ == '__main__': try: chatternode() except rospy.ROSInterruptException: pass

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-01-26 11:56:03 -0500

fruitbot gravatar image

updated 2021-01-26 13:05:00 -0500

You should look into this tutorial: http://wiki.ros.org/ROS/Tutorials/Wri...

In the tutorial, the publisher and subscriber are in separate nodes, but you should be able to integrate the two.

EDIT: Here is an example:

#!/usr/bin/env python
# license removed for brevity
import rospy
from std_msgs.msg import String

def callback(data):
    rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)

def talker():
    pub = rospy.Publisher('chatter', String, queue_size=10)
    rate = rospy.Rate(10) # 10hz
    hello_str = "hello world %s" % rospy.get_time()
    rospy.loginfo(hello_str)
    pub.publish(hello_str)
    rate.sleep()

def listener():

    rospy.Subscriber("chatter", String, callback)
    rate = rospy.Rate(10)
    rate.sleep()

if __name__ == '__main__':
    rospy.init_node('talker_and_listener', anonymous=True)

    while not rospy.is_shutdown():
        talker()
        listener()
edit flag offensive delete link more

Comments

I am not able to thats the problem!!!

ar89 gravatar image ar89  ( 2021-01-26 12:21:26 -0500 )edit

Do you want the publisher and subscriber to publish/subscribe to the same topic?

fruitbot gravatar image fruitbot  ( 2021-01-26 12:41:54 -0500 )edit

Yes same topic

ar89 gravatar image ar89  ( 2021-01-26 12:42:57 -0500 )edit

Where were you getting stuck? I might be able to help if I can see your code or a specific issue.

fruitbot gravatar image fruitbot  ( 2021-01-26 12:54:46 -0500 )edit

I added an example in the answer.

fruitbot gravatar image fruitbot  ( 2021-01-26 13:07:29 -0500 )edit

can i add another publisher in that???

ar89 gravatar image ar89  ( 2021-01-27 00:57:33 -0500 )edit

i added my code also

ar89 gravatar image ar89  ( 2021-01-27 01:03:13 -0500 )edit

Yes, you can add another publisher. If you understand the tutorial, you should be able to figure out how to add another publisher on your own. I recommend reading and digesting the tutorial. There is even a section that explains the sample code, line by line.

As for the code you shared, it is very difficult to read. You should insert new lines where they belong. Additionally, you should format it with the code button at the top of your editor. Anyway, I ran your script and it successfully published and subscribed.

fruitbot gravatar image fruitbot  ( 2021-01-27 11:36:20 -0500 )edit

Question Tools

Stats

Asked: 2021-01-25 05:42:41 -0500

Seen: 131 times

Last updated: Jan 27 '21