remove_object.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 
17 
18 import sys
19 
20 import rospy
21 import copy
22 import math
23 from gazebo_msgs.srv import DeleteModel, DeleteModelRequest
24 import tf.transformations as tft
25 
26 parents = {}
27 compound_keys={}
28 
29 def get_flat_dict(objects, parent_name):
30  """expands all objects to a flat dictionary"""
31  flat_objects = {}
32  for key, value in objects.items():
33  # check if we have an atomic object
34 
35  if(parent_name!=None):
36 
37  if('parent_name' not in parents):
38  parents[key] = parent_name
39 
40  compound_keys[parent_name] = parent_name
41 
42  compound_keys[key] = compound_keys[parent_name] + '_' + key
43 
44  else:
45  compound_keys[key] = key
46 
47  if "children" in value:
48  # add parent object without children to flat_objects
49  tmp_value = copy.deepcopy(value)
50  to_process = tmp_value["children"]
51 
52  # add position and orientation of parent to all children
53  for child_key, child_value in tmp_value["children"].items():
54  yaw = objects[key]["orientation"][2]
55  x = objects[key]["position"][0] + math.cos(yaw) * child_value["position"][0] - math.sin(yaw) * child_value["position"][1]
56  y = objects[key]["position"][1] + math.sin(yaw) * child_value["position"][0] + math.cos(yaw) * child_value["position"][1]
57  child_value["position"][0] = x
58  child_value["position"][1] = y
59  child_value["position"][2] = objects[key]["position"][2] + child_value["position"][2]
60  child_value["orientation"][2] = yaw + child_value["orientation"][2]
61  del tmp_value["children"]
62  flat_objects.update({compound_keys[key]:tmp_value})
63  # add flattened children to flat_objects
64  flat_objects.update(get_flat_dict(to_process, compound_keys[key]))
65 
66  else:
67  # add object to flat_objects
68  flat_objects.update({compound_keys[key]:value})
69 
70  return flat_objects
71 
72 if __name__ == "__main__":
73  if len(sys.argv) < 2:
74  print('[remove_object.py] Please specify the names of the objects to be removed')
75  sys.exit()
76 
77  rospy.init_node("object_remover")
78 
79  # check for all objects on parameter server
80  if not rospy.has_param("/objects"):
81  rospy.logerr("No objects uploaded to /objects")
82  sys.exit()
83  objects = rospy.get_param("/objects")
84  flat_objects = get_flat_dict(objects,None)
85 
86  # check for all object groups on parameter server
87  if rospy.has_param("/object_groups"):
88  groups = rospy.get_param("/object_groups")
89  else:
90  groups = {}
91  rospy.loginfo('No object-groups uploaded to /object_groups')
92 
93  # if keyword all is in list of object names we'll load all models uploaded to parameter server
94  if "all" in sys.argv:
95  objects = flat_objects
96  elif not set(groups.keys()).isdisjoint(sys.argv):
97  # get all key that are in both, the dictionary and argv
98  found_groups = set(groups.keys()) & set(sys.argv)
99  # get all object_names from keys that are in argv
100  object_names = [groups[k] for k in found_groups]
101  # flatten list of lists 'object_names' to a list
102  object_names = [item for sublist in object_names for item in sublist]
103  # save all dict-objects with names in 'object_names' in 'objects'
104  objects = {}
105  for object_name in object_names:
106  if object_name in list(flat_objects.keys()):
107  objects.update({object_name:flat_objects[object_name]})
108  elif sys.argv[1] not in list(flat_objects.keys()):
109  rospy.logerr("Object %s not found", sys.argv[1])
110  sys.exit()
111  else:
112  objects = {sys.argv[1]:flat_objects[sys.argv[1]]}
113 
114  rospy.loginfo("Trying to remove %s", list(objects.keys()))
115 
116  for name in objects:
117  # check if object is already spawned
118  srv_delete_model = rospy.ServiceProxy('gazebo/delete_model', DeleteModel)
119  req = DeleteModelRequest()
120  req.model_name = name
121  exists = True
122  try:
123  rospy.wait_for_service('/gazebo/delete_model')
124  res = srv_delete_model(name)
125  except rospy.ServiceException:
126  exists = False
127  rospy.logdebug("Model %s does not exist in gazebo.", name)
128 
129  if exists:
130  rospy.loginfo("Model %s removed.", name)
131  else:
132  rospy.logerr("Model %s not found in gazebo.", name)
def get_flat_dict(objects, parent_name)


cob_gazebo_tools
Author(s): Felix Messmer , Nadia Hammoudeh Garcia , Florian Weisshardt
autogenerated on Sat Dec 5 2020 03:59:48