Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 import sys
00019
00020 import rospy
00021 import copy
00022 import math
00023 from gazebo_msgs.srv import DeleteModel, DeleteModelRequest
00024 import tf.transformations as tft
00025
00026 parents = {}
00027 compound_keys={}
00028
00029 def get_flat_dict(objects, parent_name):
00030 """expands all objects to a flat dictionary"""
00031 flat_objects = {}
00032 for key, value in objects.iteritems():
00033
00034
00035 if(parent_name!=None):
00036
00037 if('parent_name' not in parents):
00038 parents[key] = parent_name
00039
00040 compound_keys[parent_name] = parent_name
00041
00042 compound_keys[key] = key+'_'+compound_keys[parent_name]
00043
00044 else:
00045 compound_keys[key] = key
00046
00047 if "children" in value:
00048
00049 tmp_value = copy.deepcopy(value)
00050 to_process = tmp_value["children"]
00051
00052
00053 for child_key, child_value in tmp_value["children"].iteritems():
00054 yaw = objects[key]["orientation"][2]
00055 x = objects[key]["position"][0] + math.cos(yaw) * child_value["position"][0] - math.sin(yaw) * child_value["position"][1]
00056 y = objects[key]["position"][1] + math.sin(yaw) * child_value["position"][0] + math.cos(yaw) * child_value["position"][1]
00057 child_value["position"][0] = x
00058 child_value["position"][1] = y
00059 child_value["position"][2] = objects[key]["position"][2] + child_value["position"][2]
00060 child_value["orientation"][2] = yaw + child_value["orientation"][2]
00061 del tmp_value["children"]
00062 flat_objects.update({compound_keys[key]:tmp_value})
00063
00064 flat_objects.update(get_flat_dict(to_process, compound_keys[key]))
00065
00066 else:
00067
00068 flat_objects.update({compound_keys[key]:value})
00069
00070 return flat_objects
00071
00072 if __name__ == "__main__":
00073 if len(sys.argv) < 2:
00074 print '[remove_object.py] Please specify the names of the objects to be removed'
00075 sys.exit()
00076
00077 rospy.init_node("object_remover")
00078
00079
00080 if not rospy.has_param("/objects"):
00081 rospy.logerr("No objects uploaded to /objects")
00082 sys.exit()
00083 objects = rospy.get_param("/objects")
00084 flat_objects = get_flat_dict(objects,None)
00085
00086
00087 if rospy.has_param("/object_groups"):
00088 groups = rospy.get_param("/object_groups")
00089 else:
00090 groups = {}
00091 rospy.loginfo('No object-groups uploaded to /object_groups')
00092
00093
00094 if "all" in sys.argv:
00095 objects = flat_objects
00096 elif not set(groups.keys()).isdisjoint(sys.argv):
00097
00098 found_groups = set(groups.keys()) & set(sys.argv)
00099
00100 object_names = [groups[k] for k in found_groups]
00101
00102 object_names = [item for sublist in object_names for item in sublist]
00103
00104 objects = {}
00105 for object_name in object_names:
00106 if object_name in flat_objects.keys():
00107 objects.update({object_name:flat_objects[object_name]})
00108 elif sys.argv[1] not in flat_objects.keys():
00109 rospy.logerr("Object %s not found", sys.argv[1])
00110 sys.exit()
00111 else:
00112 objects = {sys.argv[1]:flat_objects[sys.argv[1]]}
00113
00114 rospy.loginfo("Trying to remove %s", objects.keys())
00115
00116 for name in objects:
00117
00118 srv_delete_model = rospy.ServiceProxy('gazebo/delete_model', DeleteModel)
00119 req = DeleteModelRequest()
00120 req.model_name = name
00121 exists = True
00122 try:
00123 rospy.wait_for_service('/gazebo/delete_model')
00124 res = srv_delete_model(name)
00125 except rospy.ServiceException, e:
00126 exists = False
00127 rospy.logdebug("Model %s does not exist in gazebo.", name)
00128
00129 if exists:
00130 rospy.loginfo("Model %s removed.", name)
00131 else:
00132 rospy.logerr("Model %s not found in gazebo.", name)