def start_master(use_sim_time=True):
    _logger.info('Launching ROS master')
    ros_proc = utils.new_process(['roscore'], output_to_console=False)
    time.sleep(2)
    if use_sim_time:
        param_set_proc = utils.new_process(
            ['rosparam', 'set', 'use_sim_time', 'true'])
        param_set_proc.communicate()
    return ros_proc
def start_recording_bag(bag_path, topics=None):
    _logger.info('Starting bag recording')
    if topics is None:
        topics = ['-a']
    record_proc = utils.new_process(['rosbag', 'record'] + topics +
                                    ['-O', bag_path])
    return record_proc
def run_node(package, node, namespace=None, argc=None, argv=None):
    if namespace is not None:
        command = [
            'export',
            'ROS_NAMESPACE=%s' % namespace, '&&', 'rosrun', package, node
        ]
    else:
        command = ['rosrun', package, node]
    if argc is not None:
        command += argc
    elif argv is not None:
        command += ['%s:=%s' % (arg, value) for arg, value in argv.items()]
    node_proc = utils.new_process(command)
    return node_proc
def launch(**kwargs):
    if kwargs.has_key('direct_path'):
        command = ['roslaunch', kwargs.get('direct_path')]
    else:
        command = [
            'roslaunch',
            kwargs.get('package'),
            kwargs.get('launch_file')
        ]
    if kwargs.has_key('argv'):
        command += [
            '%s:=%s' % (arg, value)
            for arg, value in kwargs.get('argv').items()
        ]
    launch_proc = utils.new_process(command)
    return launch_proc
def play_bag(bag_path, use_clock=True, start_time=0):
    _logger.info('Starting bag playing')
    if type(bag_path) == tuple:
        bags = [rosbag.Bag(single_bag_path) for single_bag_path in bag_path]
        duration_in_seconds = sum(
            [bag.get_end_time() - bag.get_start_time() for bag in bags])
        path_for_command_line = list(bag_path)
    else:
        bag = rosbag.Bag(bag_path)
        duration_in_seconds = bag.get_end_time() - bag.get_start_time()
        path_for_command_line = [bag_path]
    if start_time == 0:
        command = ['rosbag', 'play'] + path_for_command_line
    else:
        command = ['rosbag', 'play', '-s',
                   str(start_time)] + path_for_command_line
    if use_clock:
        command = command + ['--clock']
    play_proc = utils.new_process(command, output_to_console=True)
    return play_proc, duration_in_seconds
def launch_rviz(config_path=None):
    command = ['rviz']
    if config_path is not None:
        command += ['-d', config_path]
    utils.new_process(command)
def kill_master():
    _logger.info('Killing all live ROS master processes')
    for proc_name in ['roscore', 'rosmaster', 'rosout']:
        utils.kill_process(proc_name)
    roslaunch_kill_proc = utils.new_process(['killall', 'roslaunch', '-2'])
    roslaunch_kill_proc.communicate()
def save_map(map_name, dir_name):
    save_map_proc = utils.new_process(
        ['rosrun', 'map_server', 'map_saver', '-f', map_name], cwd=dir_name)
    time.sleep(1)
    save_map_proc.kill()