23 from gazebo_msgs.srv
import DeleteModel, DeleteModelRequest
24 import tf.transformations
as tft
30 """expands all objects to a flat dictionary""" 32 for key, value
in objects.items():
35 if(parent_name!=
None):
37 if(
'parent_name' not in parents):
38 parents[key] = parent_name
40 compound_keys[parent_name] = parent_name
42 compound_keys[key] = compound_keys[parent_name] +
'_' + key
45 compound_keys[key] = key
47 if "children" in value:
49 tmp_value = copy.deepcopy(value)
50 to_process = tmp_value[
"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})
64 flat_objects.update(
get_flat_dict(to_process, compound_keys[key]))
68 flat_objects.update({compound_keys[key]:value})
72 if __name__ ==
"__main__":
74 print(
'[remove_object.py] Please specify the names of the objects to be removed')
77 rospy.init_node(
"object_remover")
80 if not rospy.has_param(
"/objects"):
81 rospy.logerr(
"No objects uploaded to /objects")
83 objects = rospy.get_param(
"/objects")
87 if rospy.has_param(
"/object_groups"):
88 groups = rospy.get_param(
"/object_groups")
91 rospy.loginfo(
'No object-groups uploaded to /object_groups')
95 objects = flat_objects
96 elif not set(groups.keys()).isdisjoint(sys.argv):
98 found_groups = set(groups.keys()) & set(sys.argv)
100 object_names = [groups[k]
for k
in found_groups]
102 object_names = [item
for sublist
in object_names
for item
in sublist]
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])
112 objects = {sys.argv[1]:flat_objects[sys.argv[1]]}
114 rospy.loginfo(
"Trying to remove %s", list(objects.keys()))
118 srv_delete_model = rospy.ServiceProxy(
'gazebo/delete_model', DeleteModel)
119 req = DeleteModelRequest()
120 req.model_name = name
123 rospy.wait_for_service(
'/gazebo/delete_model')
125 except rospy.ServiceException:
127 rospy.logdebug(
"Model %s does not exist in gazebo.", name)
130 rospy.loginfo(
"Model %s removed.", name)
132 rospy.logerr(
"Model %s not found in gazebo.", name)
def get_flat_dict(objects, parent_name)