configuration.py
Go to the documentation of this file.
1 import re
2 import os
3 import xml.etree.ElementTree
4 import yaml
5 import stat
6 from move import *
7 from general_ros import *
8 from tmux import restartPanes
9 from math import *
10 
11 def getFile(fileLoc):
12  try:
13  f = file(fileLoc, "r")
14  content = f.read()
15  f.close()
16  except IOError:
17  print("invalid file")
18  return None
19  return content
20 
21 # log dir
22 homeDir = os.path.expanduser("~")
23 logDir = homeDir + "/log/"
25  folders = filter(lambda x: os.path.isdir(logDir + x), os.listdir(logDir))
26  # create (folder, time) tuples
27  foldersTime = map(lambda folder: (os.stat(logDir + folder)[stat.ST_MTIME], logDir + folder), folders)
28  foldersTime.sort()
29  # folder contains now sorted log directory by creation time
30  logFolders = map(lambda x: x[1], foldersTime)
31  return logFolders
32 
34  logFolders = getLogFolders()
35  return logFolders[-1]
36 
37 def rmFile(filename):
38  try:
39  os.remove(filename)
40  except:
41  pass
42 
43 def reduceLogSize(logFolder):
44  rmFile(logFolder + "/OBJ.bag.active")
45  rmFile(logFolder + "/OBJ.bag")
46  rmFile(logFolder + "/ism.log")
47  rmFile(logFolder + "/world_model.log")
48 
49 logFolders = getLogFolders()
50 
51 def getLogSubFolders(folder):
52  results = []
53  for subfolder in os.listdir(folder):
54  if os.path.isdir(folder + "/" + subfolder):
55  if isLogFolder(folder + "/" + subfolder):
56  results.append(folder + "/" + subfolder)
57  else:
58  results.extend(getLogSubFolders(folder + "/" + subfolder))
59  return results
60 
61 def isLogFolder(folder):
62  return os.path.isfile(folder + "/state_machine.log")
63 
64 # READ: this is pretty neat, but it removes comments, so be careful
65 def setConfig(cfg, restart = True):
66  writeWorldLaunchFile(world_model_path + "/launch/world_model.launch", cfg)
67  writeWorldLaunchFile(world_model_path + "/launch/whole_test_system.launch", cfg)
68  writeDatabase(recognizer_prediction_ism_path + "/param/scene_recognition.yaml", cfg)
69  writeFakeRecognizer(fake_object_recognition_path + "/param/params.yaml", cfg)
70  if restart:
71  print("restarting...")
73 
74 # to write config to files
75 def writeWorldLaunchFile(file, cfg):
76  et = xml.etree.ElementTree.parse(file)
77  rosparams = et.findall(".//rosparam")
78  rosparams = filter(lambda x : "world_description" in x.attrib["file"], rosparams)
79  assert(len(rosparams) == 1)
80  rosparams[0].attrib["file"] = cfg["world_description"]
81  et.write(file)
82 
83 def writeDatabase(file, cfg):
84  dataMap = getYaml(file)
85  dataMap["dbfilename"] = cfg["database"]
86  saveYaml(file, dataMap)
87 
88 def writeFakeRecognizer(file, cfg):
89  dataMap = getYaml(file)
90  dataMap["config_file_path"] = cfg["fake_recognizer_objs"]
91  saveYaml(file, dataMap)
92 
93 # yaml
94 def getYaml(file):
95  f = open(file)
96  dataMap = yaml.load(f)
97  f.close()
98  return dataMap
99 
100 def saveYaml(file, dataMap, flow_style=False):
101  f = open(file, "w")
102  if flow_style is True:
103  yaml.dump(dataMap, f)
104  else:
105  yaml.dump(dataMap, f, default_flow_style=False)
106  f.close()
107 
108 roscorePane = (0, 0)
109 gazeboPane = (0, 1)
110 controlPane = (2, 0)
111 rvizPane = (3, 0)
112 ismPane = (4, 0)
113 nbvPane = (5, 0)
114 worldModelPane = (5, 1)
115 vizServerPane = (6, 0)
116 smPane = (7, 0)
117 initialSearchmanagerPane = (8, 0)
118 movePane = (9, 0)
119 ptuDriverPane = (10, 0)
120 ptuControllerPane = (10, 1)
121 ptuControllerCliPane = (10, 2)
122 ptuRefreshFrustumPane = (10, 3)
123 fakeRecognizerPane = (11, 0)
124 allPanes = [
125  ismPane,
126  nbvPane,
127  worldModelPane,
128  vizServerPane,
129  smPane,
130  initialSearchmanagerPane,
131  movePane,
132  ptuDriverPane,
133  ptuControllerPane,
134  fakeRecognizerPane
135 ]
136 
137 configPanes = [
138  ismPane,
139  worldModelPane,
140  smPane,
141  fakeRecognizerPane
142 ]
143 
144 orientations = [
145  (0, -35)
146 ]
147 
148 positions = [
149  #
150  [
151  (-1.1, -0.34, 110),
152  (-1.4, -0.22, 90),
153  (-0.6, -0.13, 135),
154  (-0.48, -0.09, 140),
155  (-0.44, 0.27, 165)
156  ],
157  #
158  [
159  (0.38, -1.81, 160),
160  (0.37, -1.48, 180),
161  (0.34, -1.13, 200),
162  (0.27, -0.87, 215),
163  (0.086, -0.64, 230),
164  (-0.11, -0.5, 245),
165  (-0.41, -0.53, 260),
166  (-0.685, -0.54, 275),
167  (-0.956, -0.56, 290),
168  (-1.183, -0.56, 300)
169  ],
170  #
171  [
172  (2.147, -1.9, 110),
173  (1.78, -1.9, 90),
174  (1.39, -1.87, 65),
175  (1.06, -1.67, 45),
176  (0.93, -1.37, 25),
177  (0.9, -1.04, 5),
178  (0.9, -0.77, 0),
179  (0.91, -0.45, -20),
180  (0.96, -0.185, -30),
181  (1.09, 0.02, -40),
182  (1.31, 0.16, -60)
183  ],
184  #
185  [
186  (1.42, 0.196, 0),
187  (1.41, 0.43, -30),
188  (1.288, 0, 0)
189  ],
190  #
191  [
192  (-0.44, 1.9, 90),
193  (-0.18, 1.88, 90),
194  (-0.11, 2.02, 90)
195  ]
196 ]
197 
198 # setConfig(cfg_story1)
199 # configuration for story1
200 # world_description is a setting in world_model/launch launch files
201 # database contains the scene and is defined in asr_recognizer_prediction_ism/rsc/scene_recognition.yaml
202 # fake_recognizer_objs contains detectable objects and is defiend in asr_fake_object_recognition/param/params.yaml
203 cfg_story1_missing_knife = {
204  "world_description" : "$(find asr_world_model)/rsc/world_descriptions/world_description.yaml",
205  "database" : resources_for_active_scene_recognition_path + "/scene_recordings/story_1.sqlite",
206  "fake_recognizer_objs" : "./config/story_1.xml",
207  "startPositions" : [(positions[0][0], orientations[0])]
208 }
209 
210 cfg_story1_schrank = {
211  "world_description" : "$(find asr_world_model)/rsc/world_descriptions/world_description.yaml",
212  "database" : resources_for_active_scene_recognition_path + "/scene_recordings/story_1.sqlite",
213  "fake_recognizer_objs" : "./config/story_1_schrank.xml",
214  "startPositions" : [(positions[4][0], orientations[0])]
215 }
216 
217 # configuration for story2
218 cfg_story2 = {
219  "world_description" : "$(find asr_world_model)/rsc/world_descriptions/world_description.yaml",
220  "database" : resources_for_active_scene_recognition_path + "/scene_recordings/story_1.sqlite", # TODO: not there
221  "fake_recognizer_objs" : "./config/story_2_tisch.xml" # TODO: might vary from story 2 mission
222 }
223 
224 # configuration for 25th may
225 cfg_may25 = {
226  "world_description" : "$(find asr_world_model)/rsc/world_descriptions/world_description_25th_may.yaml",
227  "database" : resources_for_active_scene_recognition_path + "/scene_recordings/scene_25th_may.sqlite",
228  "fake_recognizer_objs" : "./config/scene_25th_may.xml",
229  "startPositions" : [(positions[2][5], orientations[0])]
230 }
231 
232 cfgs = [
233  cfg_story1_missing_knife,
234  cfg_story1_schrank,
235  cfg_story2,
236  cfg_may25
237 ]
238 
239 def getConfig(folder):
240  cfg = {}
241  highest_idx = -1
242  for file in os.listdir(folder):
243  if os.path.isfile(folder + "/" + file):
244  if file.endswith("sqlite"):
245  cfg["database"] = os.path.abspath(folder + "/" + file)
246  constellation_indices = re.findall("recognition_(.*)_constellation.*xml", file)
247  constellation_idx = int(constellation_indices[0]) if len(constellation_indices) > 0 else -1
248  if constellation_idx > highest_idx:
249  highest_idx = constellation_idx
250  cfg["world_description"] = "$(find world_model)/rsc/world_descriptions/world_description.yaml"
251  cfg["fake_recognizer_objs"] = os.path.abspath(folder + "/" + "recognition_" + str(highest_idx) + "_constellation_0.xml")
252  return cfg
253 
254 def getStartPosition(folder):
255  f = open(folder + "/state_machine.log", "r")
256  content = f.read()
257  result = re.findall("Initial robot state:\r$\n^(.*\r$\n^.*\r$\n^.*\r$\n^.*\r$\n^.*)\r", content, re.M)
258  if len(result) == 0:
259  return None
260  robot_state_str = result[0]
261  rows = robot_state_str.split("\r\n")
262  robot_state_map = {}
263  for row in rows:
264  key = row.split(":")[0].strip()
265  value = float(row.split(":")[1].strip())
266  robot_state_map[key] = value
267  pos = (robot_state_map["x"], robot_state_map["y"], degrees(robot_state_map["rotation"]))
268  orientation = (robot_state_map["pan"], robot_state_map["tilt"])
269  return (pos, orientation)
270 
272  restartPanes(configPanes)
273 
275  dataMap = getYaml(fake_object_recognition_path + "/param/params.yaml")
276  for cfg in cfgs:
277  if cfg["fake_recognizer_objs"] == dataMap["config_file_path"]:
278  return cfg
279 
281  startPos = getCurrentConfig()["startPositions"][0]
282  setPositionAndOrientation(startPos)
def restartPanes(windowAndPanes)
Definition: tmux.py:130
def setConfig(cfg, restart=True)
def getLogSubFolders(folder)
def writeDatabase(file, cfg)
def getStartPosition(folder)
def setPositionAndOrientation(positionAndOrientation)
Definition: move.py:93
def reduceLogSize(logFolder)
def rmFile(filename)
def getLogFolders()
def isLogFolder(folder)
def getFile(fileLoc)
def getCurrentConfig()
def getYaml(file)
def saveYaml(file, dataMap, flow_style=False)
def restartConfigPanes()
def writeFakeRecognizer(file, cfg)
def latestLogFolder()
def writeWorldLaunchFile(file, cfg)
def getConfig(folder)


asr_resources_for_active_scene_recognition
Author(s): Allgeyer Tobias, Aumann Florian, Borella Jocelyn, Karrenbauer Oliver, Marek Felix, Mei├čner Pascal, Stroh Daniel, Trautmann Jeremias
autogenerated on Tue Feb 18 2020 03:14:15