def _prepareROSMaster(cls, masteruri):
        if masteruri is None:
            masteruri = masteruri_from_ros()
        # start roscore, if needed
        try:
            if not os.path.isdir(screen.LOG_PATH):
                os.makedirs(screen.LOG_PATH)
            socket.setdefaulttimeout(3)
            master = xmlrpcclient.ServerProxy(masteruri)
            master.getUri(rospy.get_name())
            # restart ROSCORE on different masteruri?, not now...
#      master_uri = master.getUri(rospy.get_name())
#      if masteruri != master_uri[2]:
#        # kill the local roscore...
#        raise
        except Exception:
            # run a roscore
            master_host = get_hostname(masteruri)
            if nm.is_local(master_host, True):
                master_port = get_port(masteruri)
                new_env = dict(os.environ)
                new_env['ROS_MASTER_URI'] = masteruri
                ros_hostname = nmdhost.get_ros_hostname(masteruri)
                if ros_hostname:
                    new_env['ROS_HOSTNAME'] = ros_hostname
                cmd_args = '%s roscore --port %d' % (screen.get_cmd('/roscore--%d' % master_port), master_port)
                for n in [1, 2, 3, 4]:
                    try:
                        if n == 1:
                            print("Launch ROS Master in screen  ... %s" % (cmd_args))
                            SupervisedPopen(shlex.split(cmd_args), env=new_env, object_id="ROSCORE", description="Start roscore")
                        elif n == 2:
                            print("ROS Master takes too long for start, wait for next 10 sec ...")
                        elif n == 3:
                            print("A really slow start, wait for last 10 sec ...")
                        # wait for roscore to avoid connection problems while init_node
                        result = -1
                        count = 1
                        while result == -1 and count < 11:
                            try:
                                master = xmlrpcclient.ServerProxy(masteruri)
                                result, _, _ = master.getUri(rospy.get_name())  # _:=uri, msg
                                return
                            except Exception:
                                time.sleep(1)
                                count += 1
                        if n == 4 and count >= 11:
                            raise StartException('Cannot connect to ROS-Master: %s\n--> please run "roscore" manually!' % utf8(masteruri))
                    except Exception as e:
                        raise Exception("Error while call '%s': %s" % (cmd_args, utf8(e)))
            else:
                raise Exception("ROS master '%s' is not reachable" % masteruri)
        finally:
            socket.setdefaulttimeout(None)
Exemple #2
0
 def _prepareROSMaster(cls, masteruri):
     if not masteruri:
         masteruri = masteruri_from_ros()
     # start roscore, if needed
     try:
         if not os.path.isdir(screen.LOG_PATH):
             os.makedirs(screen.LOG_PATH)
         socket.setdefaulttimeout(3)
         master = xmlrpcclient.ServerProxy(masteruri)
         master.getUri(rospy.get_name())
     except Exception:
         # run a roscore
         screen.test_screen()
         master_host = get_hostname(masteruri)
         if cls.is_local(master_host, True):
             print("Start ROS-Master with %s ..." % masteruri)
             master_port = get_port(masteruri)
             new_env = dict(os.environ)
             new_env['ROS_MASTER_URI'] = masteruri
             ros_hostname = nmdhost.get_ros_hostname(masteruri)
             if ros_hostname:
                 new_env['ROS_HOSTNAME'] = ros_hostname
             cmd_args = '%s roscore --port %d' % (screen.get_cmd(
                 '/roscore--%d' % master_port), master_port)
             try:
                 subprocess.Popen(shlex.split(cmd_args), env=new_env)
                 # wait for roscore to avoid connection problems while init_node
                 result = -1
                 count = 1
                 while result == -1 and count < 11:
                     try:
                         print("  retry connect to ROS master %d/10" %
                               count)
                         master = xmlrpcclient.ServerProxy(masteruri)
                         result, _, _ = master.getUri(
                             rospy.get_name())  # _:=uri, msg
                     except Exception:
                         time.sleep(1)
                         count += 1
                 if count >= 11:
                     raise StartException(
                         'Cannot connect to the ROS-Master: ' +
                         str(masteruri))
             except Exception as e:
                 import sys
                 sys.stderr.write("%s\n" % e)
                 raise
         else:
             raise Exception("ROS master '%s' is not reachable" % masteruri)
     finally:
         socket.setdefaulttimeout(None)