00001
00002
00003 import math
00004 import sys
00005 import getopt
00006 import commands
00007
00008 def create_world_file(argv):
00009 numRobots = 0
00010 circleSize = 0
00011 centerX = -2
00012 centerY = -2.2
00013 omni = False
00014 localization = True
00015 simulation = True
00016 runExperiments = False
00017 scaleRadius = True
00018 useNoise = False
00019 useBagFile = False
00020 bagFileName = "collvoid.bag"
00021 useSticks = False
00022 try:
00023 opts, args= getopt.getopt(argv, "hn:s:olxf:S", ["help","numRobots=","circleSize=","omni","localization","experiments","bagFileName=","Sticks"])
00024 except getopt.GetoptError:
00025 print 'create.py -n <numRobots> -s <circleSize> <-h> <-l> <-x> <-f> bagFile'
00026 sys.exit(2)
00027 for opt, arg in opts:
00028 if opt == '-h':
00029 print 'create.py -n <numRobots> -s <circleSize> <-h> <-l> <-x> <-f> bagfile'
00030 sys.exit(2)
00031 elif opt in ("-n","--numRobots"):
00032 numRobots = int(arg)
00033 elif opt in ("-s","--circleSize"):
00034 circleSize = float(arg)
00035 elif opt in ("-o","--omni"):
00036 omni = True
00037 elif opt in ("-l","--localization"):
00038 localization = False
00039 elif opt in ("-x", "--experiments"):
00040 runExperiments = True
00041 elif opt in ("-f", "--bagFileName"):
00042 useBagFile = True
00043 bagFileName = str(arg)
00044
00045
00046
00047
00048 direct = commands.getoutput('rospack find collvoid_stage')
00049 worldFileTemp = open(direct + '/world/swarmlab_template.world','r')
00050 worldFileNew = open(direct + '/world/swarmlab_created.world','w')
00051 worldFileNew.write(worldFileTemp.read())
00052 direct = commands.getoutput('rospack find stage')
00053
00054 colors = open(direct + '/share/stage/rgb.txt','r')
00055 line = colors.readline()
00056 line = colors.readline()
00057 cols = []
00058 while line:
00059 cols.append(line[line.rfind("\t")+1:line.rfind("\n")])
00060 line = colors.readline()
00061 colors.close()
00062
00063 for x in range(numRobots):
00064 angle = 360.0 / numRobots
00065 anglePrint = x * angle-180-45
00066 angle = x * angle - 45
00067 posX = circleSize*math.cos(angle/360*2*math.pi)
00068
00069 posY = circleSize*math.sin(angle/360*2*math.pi)
00070 if (omni or useSticks):
00071 if(not useSticks):
00072 worldFileNew.write('pr2( pose [ {0:f} {1:f} 0 {2:f} ] name "robot_{3:d}" color "{4}")\n'.format(centerX+posX,centerY+posY,anglePrint,x,cols[40 + 10 * x]))
00073 else:
00074 worldFileNew.write('stick( pose [ {0:f} {1:f} 0 {2:f} ] name "robot_{3:d}" color "{4}")\n'.format(centerX+posX,centerY+posY,anglePrint,x,cols[40 + 10 * x]))
00075 else:
00076 worldFileNew.write('roomba( pose [ {0:f} {1:f} 0 {2:f} ] name "robot_{3:d}" color "{4}")\n'.format(centerX+posX,centerY+posY,anglePrint,x,cols[40 + 10 * x]))
00077
00078
00079
00080 worldFileTemp.close()
00081 worldFileNew.close()
00082 create_yaml_file(circleSize,numRobots,omni,simulation,localization,centerX,centerY,scaleRadius,useNoise, useSticks)
00083 create_launch_file(numRobots,omni,runExperiments,bagFileName,localization,useBagFile, useSticks)
00084
00085
00086 def create_yaml_file(circleSize, numRobots,omni,simulation,localization,centerX,centerY,scaleRadius,useNoise, useSticks):
00087 direct = commands.getoutput('rospack find collvoid_stage')
00088 yamlWrite = open(direct + '/params_created.yaml','w')
00089 yamlWrite.write("/use_sim_time: true\n")
00090
00091 angle = 360.0 / numRobots
00092 for x in range(numRobots):
00093 angleX = 90 + x * angle - 45
00094 posX = circleSize*math.cos(angleX/360*2*math.pi)
00095 posY = circleSize*math.sin(angleX/360*2*math.pi)
00096
00097 yamlWrite.write('robot_{0}:\n'.format(x))
00098 yamlWrite.write(' goals:\n')
00099 yamlWrite.write(' x: [{0:f}]\n'.format(centerY+posX))
00100 yamlWrite.write(' y: [{0:f}]\n'.format(-centerX+posY))
00101 yamlWrite.write(' ang: [{0:f}]\n'.format((angleX) / 360.0 * 2 * math.pi))
00102
00103 yamlWrite.close()
00104
00105 def create_launch_file(numRobots,omni,runExperiments, bagFilename, localization,useBagFile, useSticks):
00106 direct = commands.getoutput('rospack find collvoid_stage')
00107
00108 launchWrite = open(direct + '/launch/sim_created.launch','w')
00109 launchWrite.write("<launch>\n")
00110 launchWrite.write(' <node name="map_server" pkg="map_server" type="map_server" args="$(find collvoid_stage)/world/swarmlab_map.yaml"/>\n')
00111 launchWrite.write(' <rosparam command="load" file="$(find collvoid_stage)/params_created.yaml"/>\n')
00112 if runExperiments:
00113 launchWrite.write(' <node pkg="stage" type="stageros" name="stageros" args="-g $(find collvoid_stage)/world/swarmlab_created.world" respawn="false" output="screen" />\n')
00114 else:
00115 launchWrite.write(' <node pkg="stage" type="stageros" name="stageros" args="$(find collvoid_stage)/world/swarmlab_created.world" respawn="false" output="screen" />\n')
00116
00117 for x in range(numRobots):
00118 if (localization):
00119 if (omni):
00120 launchWrite.write(' <include file="$(find collvoid_stage)/launch/amcl_omni_multi.launch">\n')
00121 else:
00122 launchWrite.write(' <include file="$(find collvoid_stage)/launch/amcl_diff_multi.launch">\n')
00123 launchWrite.write(' <arg name="robot" value="robot_{0}"/>\n'.format(x))
00124 launchWrite.write(' </include>\n')
00125 else:
00126 launchWrite.write(' <node name="fake_localization" pkg="fake_localization" ns="robot_{0}" type="fake_localization" respawn="false">\n'.format(x))
00127 launchWrite.write(' <param name="~tf_prefix" value="robot_{0}" />\n'.format(x))
00128 launchWrite.write(' <param name="~odom_frame_id" value="/robot_{0}/odom" />\n'.format(x))
00129 launchWrite.write(' <param name="~base_frame_id" value="/robot_{0}/base_link" />\n'.format(x))
00130 launchWrite.write(' </node>')
00131
00132
00133
00134 launchWrite.write(' <node pkg="move_base" type="move_base" respawn="true" name="move_base" ns="robot_{0}" output="screen">\n'.format(x))
00135 if (omni) and not useSticks:
00136 launchWrite.write(' <rosparam command="load" file="$(find collvoid_stage)/params/params_pr2.yaml" />\n')
00137 elif useSticks:
00138 launchWrite.write(' <rosparam command="load" file="$(find collvoid_stage)/params/params_stick.yaml" />\n')
00139 else:
00140 launchWrite.write(' <rosparam command="load" file="$(find collvoid_stage)/params/params_turtle.yaml" />\n')
00141
00142 if(runExperiments):
00143 launchWrite.write(' <rosparam command="load" file="$(find collvoid_stage)/params/collvoid_exp_created.yaml" />\n')
00144 else:
00145 launchWrite.write(' <rosparam command="load" file="$(find collvoid_stage)/params/collvoid_config.yaml" />\n')
00146
00147 launchWrite.write(' <remap from="map" to="/map" />\n')
00148 launchWrite.write(' <param name="~tf_prefix" value="robot_{0}" />\n'.format(x))
00149 launchWrite.write(' <param name="~/global_costmap/robot_base_frame" value="robot_{0}/base_link" /> \n <param name="~/local_costmap/robot_base_frame" value="robot_{1}/base_link" /> \n <param name="~/local_costmap/global_frame" value="robot_{0}/odom" /> \n'.format(x,x,x))
00150 launchWrite.write(' <param name="base_local_planner" value="collvoid_local_planner/CollvoidLocalPlanner" />\n')
00151 launchWrite.write(' <param name="base_global_planner" value="collvoid_simple_global_planner/CollvoidSimpleGlobalPlanner" />\n')
00152 launchWrite.write(' <remap from="/position_share_in" to="/position_share" />\n')
00153 launchWrite.write(' <remap from="/position_share_out" to="/position_share" />\n')
00154
00155
00156
00157 launchWrite.write(' </node> \n')
00158 launchWrite.write(' <node pkg="collvoid_controller" type="controllerRobots.py" name="controllerRobots" ns="robot_{0}" output="screen" />\n'.format(x))
00159 if (runExperiments):
00160 launchWrite.write(' <node pkg="collvoid_controller" type="watchdog.py" name="watchdog" output="screen"/>\n')
00161 else:
00162 launchWrite.write(' <node pkg="collvoid_controller" type="controller.py" name="controller" output="screen"/>\n')
00163 launchWrite.write(' <node pkg="rviz" type="rviz" name="rviz" args="-d $(find collvoid_stage)/double_view.vcg" output="screen" />\n')
00164
00165 s = ""
00166
00167 for x in range(numRobots):
00168 s += "/robot_%d/base_pose_ground_truth "%(x)
00169 if useBagFile:
00170 launchWrite.write(' <node pkg="rosbag" type="record" name="rosbag" args="record {0} /position_share /stall /stall_resolved /num_run /exceeded -O $(find collvoid_stage)/bags/{1}" output="screen"/>\n'.format(s,bagFilename))
00171
00172 launchWrite.write("</launch>\n")
00173 launchWrite.close()
00174
00175
00176 if __name__ == "__main__":
00177 create_world_file(sys.argv[1:])
00178