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

I modified the talker.py to
(1) Use get_param() to test the existence of roscore. Though I am wondering if there is a formal way to test the existence of roscore.
(2) when disconnected, restart from scratch (init_node, publish ...)

import roslib; roslib.load_manifest('rospy_tutorials')
import rospy from std_msgs.msg import String

def talker():
    pub = rospy.Publisher('chatter', String)
    rospy.init_node('talker', anonymous=True, log_level=rospy.DEBUG)
    r = rospy.Rate(10) # 10hz
    while not rospy.is_shutdown():
        str = "hello world %s"%rospy.get_time()
        rospy.loginfo(str)
        pub.publish(str) 
        try:
            print rospy.get_param('run_id');
        except Exception,(e,n):
            print e,n
            return
        r.sleep()
         if __name__ == '__main__':
    try:
        import time
        while 1:
            talker()
            time.sleep(1)
    except rospy.ROSInterruptException: pass

I modified the talker.py to
(1) Use get_param() to test the existence of roscore. Though I am wondering if there is a formal way to test the existence of roscore.
(2) when disconnected, restart from scratch (init_node, publish ...)

import roslib; roslib.load_manifest('rospy_tutorials')
import rospy from std_msgs.msg import String

def talker():
    pub = rospy.Publisher('chatter', String)
    rospy.init_node('talker', anonymous=True, log_level=rospy.DEBUG)
    r = rospy.Rate(10) # 10hz
    while not rospy.is_shutdown():
        str = "hello world %s"%rospy.get_time()
        rospy.loginfo(str)
        pub.publish(str) 
         try:
            print "Checking rosparam"
            print rospy.get_param('run_id');
        except Exception,(e,n):
            print "connection failed, bailing out.."
            print e,n
            return
         r.sleep()
         if __name__ == '__main__':
    try:
        import time
        while 1:
            talker()
            time.sleep(1)
    except rospy.ROSInterruptException: pass

I modified the talker.py to
(1) Use get_param() to test the existence of roscore. Though I am wondering if there is a formal way to test the existence of roscore.
(2) when disconnected, restart from scratch (init_node, publish ...)

import roslib; roslib.load_manifest('rospy_tutorials')
import rospy from std_msgs.msg import String

def talker():
    pub = rospy.Publisher('chatter', String)
    rospy.init_node('talker', anonymous=True, log_level=rospy.DEBUG)
    r = rospy.Rate(10) # 10hz
    while not rospy.is_shutdown():
        str = "hello world %s"%rospy.get_time()
        rospy.loginfo(str)
        pub.publish(str) 


        try:
            print "Checking rosparam"
            print rospy.get_param('run_id');
        except Exception,(e,n):
            print "connection failed, bailing out.."
            print e,n
            return


        r.sleep()
  if __name__ == '__main__':
    try:
        import time
        while 1:
            talker()
            time.sleep(1)
    except rospy.ROSInterruptException: pass

I modified the talker.py to
(1) Use get_param() get_system_state to test the existence of roscore. Though I am wondering if there is a formal way to test the existence of roscore.
(2) when disconnected, restart from scratch (init_node, publish ...)

import roslib; roslib.load_manifest('rospy_tutorials')
import rospy 
import rospy
from std_msgs.msg import String

def talker():
    pub = rospy.Publisher('chatter', String)
    rospy.init_node('talker', anonymous=True, log_level=rospy.DEBUG)
    r = rospy.Rate(10) # 10hz
    while not rospy.is_shutdown():

        #query rosmaster status, which will raise socket.error when failure
        rospy.get_master().getSystemState()
        #end-of-checking

        str = "hello world %s"%rospy.get_time()
        rospy.loginfo(str)
        pub.publish(str) 


        try:
            print "Checking rosparam"
            print rospy.get_param('run_id');
        except Exception,(e,n):
            print "connection failed, bailing out.."
            print e,n
            return


pub.publish(str)
        r.sleep()

if __name__ == '__main__':
    try:
        import time
    import socket
    while 1:
        try:
            talker()
            break; #normal termination, such as shutdown/ctrl-c
        except socket.error:
            time.sleep(1)
    except rospy.ROSInterruptException: pass
        print "reconnecting"