def runNode(package, type, name, args, prefix=''): ''' Runs a ROS node. Starts a roscore if needed. ''' try: master = xmlrpclib.ServerProxy(roslib.rosenv.get_master_uri()) master.getUri('remote_nm') except: # run a roscore cmd_args = [nm.ScreenHandler.getSceenCmd('/roscore'), 'roscore'] subprocess.Popen(shlex.split(' '.join([str(c) for c in cmd_args]))) try: cmd = roslib.packages.find_node(package, type) except roslib.packages.ROSPkgException as e: # multiple nodes, invalid package raise nm.StartException(str(e)) # handle different result types str or array of string (electric / fuerte) import types if isinstance(cmd, types.StringTypes): cmd = [cmd] if cmd is None or len(cmd) == 0: raise nm.StartException(' '.join([ type, 'in package [', package, '] not found!\n\nThe package was created?\nIs the binary executable?\n' ])) cmd_args = [ nm.ScreenHandler.getSceenCmd(name), prefix, cmd[0], ' '.join([a for a in args[1:]]) ] print 'run on remote host:', ' '.join(cmd_args) subprocess.Popen(shlex.split(' '.join([str(c) for c in cmd_args])))
def runNode(package, type, name, args, prefix='', repawn=False, masteruri=None): ''' Runs a ROS node. Starts a roscore if needed. ''' if not masteruri: masteruri = nm.masteruri_from_ros() #start roscore, if needed nm.StartHandler._prepareROSMaster(masteruri) # start node try: cmd = roslib.packages.find_node(package, type) except roslib.packages.ROSPkgException as e: # multiple nodes, invalid package raise nm.StartException(str(e)) # handle different result types str or array of string (electric / fuerte) import types if isinstance(cmd, types.StringTypes): cmd = [cmd] if cmd is None or len(cmd) == 0: raise nm.StartException(' '.join([ type, 'in package [', package, '] not found!\n\nThe package was created?\nIs the binary executable?\n' ])) # create string for node parameter. Set arguments with spaces into "'". node_params = ' '.join(''.join(["'", a, "'"]) if a.find(' ') > -1 else a for a in args[1:]) cmd_args = [ nm.ScreenHandler.getSceenCmd(name), nm.RESPAWN_SCRIPT if repawn else '', prefix, cmd[0], node_params ] print 'run on remote host:', ' '.join(cmd_args) # determine the current working path arg_cwd = getCwdArg('__cwd', args) cwd = nm.get_ros_home() if not (arg_cwd is None): if arg_cwd == 'ROS_HOME': cwd = nm.get_ros_home() elif arg_cwd == 'node': cwd = os.path.dirname(cmd[0]) # set the masteruri to launch with other one master new_env = dict(os.environ) new_env['ROS_MASTER_URI'] = masteruri subprocess.Popen(shlex.split(str(' '.join(cmd_args))), cwd=cwd, env=new_env)
def kill(self, host, pid): ''' Kills the process with given process id on given host. @param host: the name or address of the host, where the process must be killed. @type host: C{str} @param pid: the process id @type pid: C{int} @raise StartException: on error @raise Exception: on errors while resolving host @see: L{node_manager_fkie.is_local()} ''' if nm.is_local(host): import signal os.kill(pid, signal.SIGKILL) rospy.loginfo("kill: %s", str(pid)) else: # kill on a remote machine cmd = ['kill -9', str(pid)] rospy.loginfo("kill remote: %s", ' '.join(cmd)) (stdin, stdout, stderr), ok = nm.ssh().ssh_exec(host, cmd) if ok: stdin.close() error = stderr.read() if error: rospy.logwarn("ERROR while kill %s: %s", str(pid), error) raise nm.StartException( str(''.join( ['The host "', host, '" reports:\n', error]))) output = stdout.read() if output: rospy.logdebug("STDOUT while kill %s: %s", str(pid), output)
def runNode(cls, node, launch_config): ''' Start the node with given name from the given configuration. @param node: the name of the node (with name space) @type node: C{str} @param launch_config: the configuration containing the node @type launch_config: L{LaunchConfig} @raise StartException: if the screen is not available on host. @raise Exception: on errors while resolving host @see: L{node_manager_fkie.is_local()} ''' n = launch_config.getNode(node) if n is None: raise StartException(''.join(["Node '", node, "' not found!"])) env = list(n.env_args) prefix = n.launch_prefix if not n.launch_prefix is None else '' # thus the parameters while the transfer are not separated if prefix: prefix = ''.join(['"', prefix, '"']) args = [ ''.join(['__ns:=', n.namespace]), ''.join(['__name:=', n.name]) ] if not (n.cwd is None): args.append(''.join(['__cwd:=', n.cwd])) # add remaps for remap in n.remap_args: args.append(''.join([remap[0], ':=', remap[1]])) # get host of the node host = launch_config.hostname env_loader = '' if n.machine_name: machine = launch_config.Roscfg.machines[n.machine_name] host = machine.address #TODO: env-loader support? # if hasattr(machine, "env_loader") and machine.env_loader: # env_loader = machine.env_loader masteruri = nm.nameres().getUri(host=host) # set the ROS_MASTER_URI if masteruri is None: env.append(('ROS_MASTER_URI', nm.masteruri_from_ros())) # set the global parameter if not masteruri is None and not masteruri in launch_config.global_param_done: global_node_names = cls.getGlobalParams(launch_config.Roscfg) rospy.loginfo("Register global parameter:\n%s", '\n'.join(global_node_names)) cls._load_parameters(masteruri, global_node_names, []) launch_config.global_param_done.append(masteruri) # add params if not masteruri is None: nodens = ''.join([n.namespace, n.name, '/']) params = dict() for param, value in launch_config.Roscfg.params.items(): if param.startswith(nodens): params[param] = value clear_params = [] for cparam in launch_config.Roscfg.clear_params: if cparam.startswith(nodens): clear_params.append(param) rospy.loginfo("Register parameter:\n%s", '\n'.join(params)) cls._load_parameters(masteruri, params, clear_params) if nm.is_local(host): nm.screen().testScreen() try: cmd = roslib.packages.find_node(n.package, n.type) except (Exception, roslib.packages.ROSPkgException) as e: # multiple nodes, invalid package raise StartException(''.join(["Can't find resource: ", str(e)])) # handle diferent result types str or array of string import types if isinstance(cmd, types.StringTypes): cmd = [cmd] cmd_type = '' if cmd is None or len(cmd) == 0: raise nm.StartException(' '.join([ n.type, 'in package [', n.package, '] not found!\n\nThe package was created?\nIs the binary executable?\n' ])) if len(cmd) > 1: # Open selection for executables try: from PySide import QtGui item, result = QtGui.QInputDialog.getItem( None, ' '.join( ['Multiple executables', n.type, 'in', n.package]), 'Select an executable', cmd, 0, False) if result: #open the selected screen cmd_type = item else: return except: raise nm.StartException( 'Multiple executables with same name in package found!' ) else: cmd_type = cmd[0] node_cmd = [prefix, cmd_type] cmd_args = [nm.screen().getSceenCmd(node)] cmd_args[len(cmd_args):] = node_cmd cmd_args.append(n.args) cmd_args[len(cmd_args):] = args rospy.loginfo("RUN: %s", ' '.join(cmd_args)) subprocess.Popen(shlex.split(str(' '.join(cmd_args)))) else: # start remote if launch_config.PackageName is None: raise StartException(''.join( ["Can't run remote without a valid package name!"])) # setup environment env_command = '' if env_loader: rospy.logwarn( "env_loader in machine tag currently not supported") raise nm.StartException( "env_loader in machine tag currently not supported") if env: env_command = "env " + ' '.join( ["%s=%s" % (k, v) for (k, v) in env]) startcmd = [ env_command, nm.STARTER_SCRIPT, '--package', str(n.package), '--node_type', str(n.type), '--node_name', str(node) ] if prefix: startcmd[len(startcmd):] = ['--prefix', prefix] startcmd[len(startcmd):] = n.args startcmd[len(startcmd):] = args rospy.loginfo("Run remote: %s", ' '.join(startcmd)) (stdin, stdout, stderr), ok = nm.ssh().ssh_exec(host, startcmd) if ok: stdin.close() # stderr.close() # stdout.close() error = stderr.read() if error: rospy.logwarn("ERROR while start '%s': %s", node, error) raise nm.StartException( str(''.join( ['The host "', host, '" reports:\n', error]))) output = stdout.read() if output: rospy.logdebug("STDOUT while start '%s': %s", node, output)
def runNodeWithoutConfig(cls, host, package, type, name, args=[]): ''' Start a node with using a launch configuration. @param host: the host or ip to run the node @type host: C{str} @param package: the ROS package containing the binary @type package: C{str} @param type: the binary of the node to execute @type type: C{str} @param name: the ROS name of the node (with name space) @type name: C{str} @param args: the list with arguments passed to the binary @type args: C{[str, ...]} @raise Exception: on errors while resolving host @see: L{node_manager_fkie.is_local()} ''' # create the name with namespace fullname = ''.join(['/', name]) for a in args: if a.startswith('__ns:='): fullname = ''.join( ['/', a.replace('__ns:=', '').strip('/ '), fullname]) fullname = fullname.replace('//', '/') args2 = list(args) args2.append(''.join(['__name:=', name])) # run on local host if nm.is_local(host): try: cmd = roslib.packages.find_node(package, type) except roslib.packages.ROSPkgException as e: # multiple nodes, invalid package raise StartException(str(e)) # handle diferent result types str or array of string import types if isinstance(cmd, types.StringTypes): cmd = [cmd] cmd_type = '' if cmd is None or len(cmd) == 0: raise nm.StartException(' '.join( [type, 'in package [', package, '] not found!'])) if len(cmd) > 1: # Open selection for executables try: from PySide import QtGui item, result = QtGui.QInputDialog.getItem( None, ' '.join(['Multiple executables', type, 'in', package]), 'Select an executable', cmd, 0, False) if result: #open the selected screen cmd_type = item else: return except: raise nm.StartException( 'Multiple executables with same name in package found!' ) else: cmd_type = cmd[0] cmd_str = str(' '.join( [nm.screen().getSceenCmd(fullname), cmd_type, ' '.join(args2)])) rospy.loginfo("Run without config: %s", cmd_str) subprocess.Popen(shlex.split(cmd_str)) else: # run on a remote machine startcmd = [ nm.STARTER_SCRIPT, '--package', str(package), '--node_type', str(type), '--node_name', str(fullname) ] startcmd[len(startcmd):] = args2 rospy.loginfo("Run remote: %s", ' '.join(startcmd)) (stdin, stdout, stderr), ok = nm.ssh().ssh_exec(host, startcmd) if ok: stdin.close() error = stderr.read() if error: rospy.logwarn("ERROR while start '%s': %s", name, error) from PySide import QtGui QtGui.QMessageBox.warning( None, 'Error while remote start %s' % str(name), str(''.join( ['The host "', host, '" reports:\n', error])), QtGui.QMessageBox.Ok) output = stdout.read() if output: rospy.logdebug("STDOUT while start '%s': %s", name, output)