generate_worlds.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import yaml
3 import rospy
4 import os
5 from collections import OrderedDict
6 
7 from .. utils import create_xacro_file
8 
9 
10 def main():
11  s = open(rospy.get_param('requested'))
12  yaml_name = rospy.get_param('requested')[
13  rospy.get_param('requested').rfind('/')+1:
14  rospy.get_param('requested').rfind('.yaml')]
15  # get the yaml as a dict
16  master = yaml.safe_load(s)
17  # get lsit of all coordinates that the master dict maps out
18  coordinates = linear_combinations(master)
19  # create a world xacro and subsiquent world file for each coordinate
20  for num, i in enumerate(coordinates):
21  create_xacro_file(xacro_target=rospy.get_param('world_xacro_target') +
22  yaml_name + str(num) + '.world.xacro',
23 
24  requested_macros=world_gen(coordinate=i,
25  master=master),
26  boiler_plate_top='<?xml version="1.0" ?>\n' +
27  '<sdf version="1.6" ' +
28  'xmlns:xacro="http://ros.org/wiki/xacro">\n' +
29  '<!-- COORDINATE: ' + str(i) + ' -->\n' +
30  '<world name="robotx_example_course">\n' +
31  ' <xacro:include filename="$(find vrx_gazebo)' +
32  '/worlds/xacros/include_all_xacros.xacro" />\n' +
33  ' <xacro:include_all_xacros />\n',
34  boiler_plate_bot='</world>\n</sdf>')
35  os.system('rosrun xacro xacro --inorder -o' +
36  rospy.get_param('world_target') + yaml_name + str(num) +
37  '.world ' +
38  rospy.get_param('world_xacro_target') + yaml_name +
39  str(num) + '.world.xacro')
40  print 'All ', len(coordinates), ' worlds generated'
41 
42 
43 def world_gen(coordinate={}, master={}):
44  world = {}
45  for axis_name, axis in master.iteritems():
46  # if a sequence override defined for this axis at this step
47  if axis['sequence'] is not None and \
48  coordinate[axis_name] in axis['sequence']:
49  # instances of macros already present in the world dict
50  for i in world:
51  if i in axis['sequence'][coordinate[axis_name]]:
52  world[i] += axis['sequence'][coordinate[axis_name]]
53  # add all unique macros in sequence to world
54  for i in axis['sequence'][coordinate[axis_name]]:
55  if i not in world:
56  if axis['sequence'][coordinate[axis_name]][i] == [None]:
57  world[i] = [{}]
58  else:
59  world[i] = axis['sequence'][coordinate[axis_name]][i]
60  # for the non-sequence override case:
61  else:
62  for macro_name, macro_calls in axis['macros'].iteritems():
63  # if this one is new
64  if macro_name not in world:
65  world[macro_name] = []
66  for params in macro_calls:
67  if params is not None:
68  evaluated_params = {}
69  for param, value in params.iteritems():
70  # values flanked by ' evaluated as strings
71  if value[0] == "'" and value[-1] == "'":
72  evaluated_params[param] = value[1:-1]
73  # values not flanked by ' evaluated as lambdas
74  else:
75  f = str(value)
76  evaluated_params[param] =\
77  (lambda n: eval(f))(coordinate[axis_name])
78  world[macro_name].append(evaluated_params)
79  else:
80  world[macro_name].append({})
81  return world
82 
83 
84 def linear_combinations(master={}):
85  combinations = []
86  axies = OrderedDict()
87  start = {}
88  # make the starting spot and max
89  # NOTE:the start coordinate <= all coordinates on an axis <= axies max
90  for axis, value in master.iteritems():
91  start[axis] = 0
92  axies[axis] = value['steps']-1
93  iterate(axies_max=axies, coordinates=combinations,
94  current_coordinate=start)
95  return combinations
96 
97 
98 def iterate(axies_max={}, coordinates=[], current_coordinate={}):
99  coordinates.append(current_coordinate.copy())
100  # if we are at the max coordinate, return
101  if axies_max == current_coordinate:
102  return
103  else:
104  for axis in current_coordinate:
105  if current_coordinate[axis] < axies_max[axis]:
106  current_coordinate[axis] += 1
107  break
108  else:
109  current_coordinate[axis] = 0
110  iterate(axies_max=axies_max, coordinates=coordinates,
111  current_coordinate=current_coordinate)
def iterate(axies_max={}, coordinates=[], current_coordinate={})


vrx_gazebo
Author(s): Brian Bingham , Carlos Aguero
autogenerated on Thu May 7 2020 03:54:56