publisher can only in While? no, maybe other reason.
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.
Even if I add the following code after publish,the subscriber still cannot get msg.
It's look fine, can you add the code of your Subscriber ?
Thanks, I just check it on terminal by
And Even for the while code, when I restart the publish file , the Subscriber need to be restart for receiving msg.
Do you start the
rostopic echo /chatter
before or after the node ? I can't reprodruce any problem with your codeThanks, I have exec it on terminal before I run the demo_pub.py.