00001
00002
00003
00004
00005
00006
00007 import subprocess as sp
00008 import os.path as op
00009 import os
00010 import optparse
00011 import time
00012
00013 def make_dirname(i):
00014 return 'laser_slam.{0}'.format(i)
00015
00016 def setup_dir(path):
00017 """
00018 Set up new location for storing output files
00019 """
00020 path = op.expanduser(op.expandvars(path))
00021 if not op.exists(path):
00022 os.makedirs(path)
00023 i = 0
00024 loc = op.join(path, make_dirname(i))
00025 while op.exists(loc):
00026 i += 1
00027 loc = op.join(path, make_dirname(i))
00028 loc = op.join(path, make_dirname(i))
00029 os.mkdir(loc)
00030 latest = op.join(path, 'latest')
00031 if op.exists(latest):
00032 os.remove(latest)
00033 os.symlink(loc, latest)
00034 return loc
00035
00036
00037 def main(path, max_time, bag):
00038 loc = setup_dir(path)
00039 print('Using output directory {0}'.format(loc))
00040
00041 env = os.environ.copy()
00042 env['PYTHONUNBUFFERED'] = 'True'
00043
00044
00045 roslaunch_outfile = open(op.join(loc, 'roslaunch.out'), 'w')
00046 args = ['roslaunch', 'laser_slam', 'laser_slam_bag.launch',
00047 'graph_loc:={0}'.format(op.join(loc, 'graph')),
00048 'bag:={0}'.format(op.expanduser(bag))]
00049 slam_launch = sp.Popen(args, shell=False, stdout=roslaunch_outfile,
00050 stderr=sp.STDOUT, bufsize=1, env=env)
00051 print('Invoked {0}'.format(' '.join(args)))
00052
00053
00054 i = 0
00055 while True:
00056 time.sleep(1)
00057 i += 1
00058 if i % 100 == 0:
00059 print('t = {0}'.format(i))
00060 if max_time > 0 and i > max_time:
00061 print('Time limit reached')
00062 slam_launch.terminate()
00063 break
00064 if (slam_launch.poll()):
00065 print('roslaunch exited.')
00066 break
00067
00068
00069
00070
00071 if __name__ == "__main__":
00072 parser = optparse.OptionParser()
00073 parser.add_option('-d', '--output_dir', dest='path',
00074 action='store', type='string', default='~/.ros/laser_slam')
00075 parser.add_option('-t', '--time', dest='time', action='store', type='int',
00076 default=0)
00077 parser.add_option('-b', '--bag', dest='bag',
00078 action='store', type='string')
00079 (options, args) = parser.parse_args()
00080 main(options.path, options.time, options.bag)