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

publisher can only in While? no, maybe other reason.

asked 2018-06-11 05:10:34 -0500

suoxd123 gravatar image

updated 2018-06-11 22:32:42 -0500

After execute the following code, the subscriber CANNOT receive msg. I start this publisher by : python demo_pub.py or rosrun demo demo_pub.py. And start the Subscriber in terminal by : rostopic echo /chatter

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

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

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

When I show the rqt_graph, it shows that they have been get linked.

But when I add while loop like this, it works well.

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

def talker():
    pub = rospy.Publisher('chatter', String, queue_size=10)
    rospy.init_node('talker')
    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()

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

Also, I need to restart the subscriber when I restart the demo_pub file. And how can I publish a message only once?


The code has nothing wrong.

I figure out the reason but cannot get the answer.

After I change the master_urI value within .bashrc to other IP address, and then run " roscore" in the terminal of device with that IP, then the strange problem reappeared again.

So, each time I change the master of roscore, I restart my ubuntu system will help.

I just run "source .bashrc", and restart the terminal before.

edit retag flag offensive close merge delete

Comments

Even if I add the following code after publish,the subscriber still cannot get msg.

rate = rospy.Rate(10)
while not rospy.is_shutdown():
     rate.sleep()
suoxd123 gravatar image suoxd123  ( 2018-06-11 05:11:46 -0500 )edit

It's look fine, can you add the code of your Subscriber ?

lmathieu gravatar image lmathieu  ( 2018-06-11 09:00:45 -0500 )edit

Thanks, I just check it on terminal by

rostopic echo /chatter
suoxd123 gravatar image suoxd123  ( 2018-06-11 09:31:41 -0500 )edit

And Even for the while code, when I restart the publish file , the Subscriber need to be restart for receiving msg.

suoxd123 gravatar image suoxd123  ( 2018-06-11 09:34:34 -0500 )edit

Do you start the rostopic echo /chatter before or after the node ? I can't reprodruce any problem with your code

lmathieu gravatar image lmathieu  ( 2018-06-11 09:42:27 -0500 )edit

Thanks, I have exec it on terminal before I run the demo_pub.py.

suoxd123 gravatar image suoxd123  ( 2018-06-11 09:49:02 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2018-06-11 20:55:20 -0500

suoxd123 gravatar image

updated 2018-06-11 22:30:49 -0500

The code has nothing wrong.

my master on raspberry and nodes on ubuntu system of PC.

Last night I recharge the raspberry's li battery and this morning the code works fine.

The problem cannot reappear again, I guess there is 2 possible reason.

  1. Battery is under voltage, so it works well after recharged.
  2. I restart the raspberry device and Ubuntu system, so the ros system is reset also.

After I change the master uri, the problem happened. And I restart my ubuntu system, the problem disappeared.

edit flag offensive delete link more

Comments

Please don't use an answer to provide more information about your question. This isn't a forum. Please update your question with this information instead.

jayess gravatar image jayess  ( 2018-06-11 21:28:21 -0500 )edit

ok, thanks.

suoxd123 gravatar image suoxd123  ( 2018-06-11 21:45:24 -0500 )edit
1

answered 2018-06-11 10:04:40 -0500

I think I got it :

There is 2 possible problem here :

  1. You didn't close your subscriber : If you registered your subscriber (in your case : rosptopic echo /chatter) with a roscore, then you closed this roscore and open a new one, the subscriber will not receive any message from the new roscore. You can check it by :

    • Open a roscore
    • Send a message to /chatter
    • Open your subscriber to /chatter
    • Close your roscore
    • Open your roscore again
    • Send a message to /chatter, nothing will happen
  2. Your publisher didn't have the time to send the message. Ros need some time to setup the publisher/Subscriber : https://github.com/ros/ros_comm/issue... or https://answers.ros.org/question/1116...

Try to properly restart your roscore and subscriber and then try to add a little wait after your rospy.Publisher() to see if this works now.

edit flag offensive delete link more

Comments

Thank you so much for the detail explainning.

  1. I didnot close or restart roscore.
  2. I add the loop sleep, but cannot work as well.
suoxd123 gravatar image suoxd123  ( 2018-06-11 20:33:21 -0500 )edit

Question Tools

3 followers

Stats

Asked: 2018-06-11 05:10:34 -0500

Seen: 254 times

Last updated: Jun 11 '18