Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 import os
00008 import subprocess
00009 import tempfile
00010
00011
00012 def reformat_position_vector(position_vector):
00013 """
00014 Reformats position vector so that it can be used with the segbot launch script
00015 """
00016 if len(position_vector) == 2:
00017 reformatted_vector = [position_vector[0], position_vector[1], 0.0, 0.0, 0.0, 0.0]
00018 elif len(position_vector) == 3:
00019 reformatted_vector = [position_vector[0], position_vector[1], 0.0, 0.0, 0.0, position_vector[2]]
00020 else:
00021 reformatted_vector = [position_vector[x] if x < len(position_vector) else 0.0 for x in range(6)]
00022
00023 location = {}
00024 location['loc_x'] = reformatted_vector[0]
00025 location['loc_y'] = reformatted_vector[1]
00026 location['loc_z'] = reformatted_vector[2]
00027 location['loc_roll'] = reformatted_vector[3]
00028 location['loc_pitch'] = reformatted_vector[4]
00029 location['loc_yaw'] = reformatted_vector[5]
00030
00031 return location
00032
00033
00034 def generate_spawn_robot_launch_script(name, location, world_namespace, launch_file, args):
00035 """
00036 Generates the roslaunch script for a single robot, wrapping the appropriate
00037 launch file in gazebo robot
00038 """
00039 launch_text = '<launch>\n'
00040 launch_text += ' <include ns="%s" file="%s">\n' %(name, launch_file)
00041 launch_text += ' <arg name="name" value="%s"/>\n'%(name)
00042 launch_text += ' <arg name="world_namespace" value="%s"/>\n'%world_namespace
00043 for loc_key, loc_value in location.items():
00044 launch_text += ' <arg name="%s" value="%s"/>\n'%(loc_key, loc_value)
00045
00046 if args:
00047 for arg_name, arg_value in args.items():
00048 launch_text += ' <arg name="%s" value="%s"/>\n'%(arg_name, arg_value)
00049 launch_text += ' </include>\n'
00050 launch_text += '</launch>'
00051 return launch_text
00052
00053
00054 def start_roslaunch_process(launch_script):
00055 """
00056 Robots are spawned using roslaunch instead of gazebo/spawn_model so that
00057 a few other scripts such as robot_state_publisher can also be launched.
00058 This convenience function helps launch a roslaunch script
00059 """
00060 temp = tempfile.NamedTemporaryFile(mode='w+t', delete=False)
00061 temp.write(launch_script)
00062 temp.close()
00063 command_args = ['roslaunch', temp.name]
00064 command_args.append('--screen')
00065 roslaunch_env = os.environ.copy()
00066 try:
00067
00068
00069 del roslaunch_env['ROS_NAMESPACE']
00070 except KeyError:
00071 pass
00072 process = subprocess.Popen(command_args, env=roslaunch_env)
00073 return process