remove_object.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00004 #
00005 # Licensed under the Apache License, Version 2.0 (the "License");
00006 # you may not use this file except in compliance with the License.
00007 # You may obtain a copy of the License at
00008 #
00009 #   http://www.apache.org/licenses/LICENSE-2.0
00010 #
00011 # Unless required by applicable law or agreed to in writing, software
00012 # distributed under the License is distributed on an "AS IS" BASIS,
00013 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014 # See the License for the specific language governing permissions and
00015 # limitations under the License.
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         # check if we have an atomic object
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             # add parent object without children to flat_objects
00049             tmp_value = copy.deepcopy(value)
00050             to_process = tmp_value["children"]
00051 
00052             # add position and orientation of parent to all children
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             # add flattened children to flat_objects
00064             flat_objects.update(get_flat_dict(to_process, compound_keys[key]))
00065 
00066         else:
00067             # add object to flat_objects
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     # check for all objects on parameter server
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     # check for all object groups on parameter server
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     # if keyword all is in list of object names we'll load all models uploaded to parameter server
00094     if "all" in sys.argv:
00095         objects = flat_objects
00096     elif not set(groups.keys()).isdisjoint(sys.argv):
00097         # get all key that are in both, the dictionary and argv
00098         found_groups = set(groups.keys()) & set(sys.argv)
00099         # get all object_names from keys that are in argv
00100         object_names = [groups[k] for k in found_groups]
00101         # flatten list of lists 'object_names' to a list
00102         object_names = [item for sublist in object_names for item in sublist]
00103         # save all dict-objects with names in 'object_names' in 'objects'
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         # check if object is already spawned
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)


cob_bringup_sim
Author(s): Felix Messmer , Nadia Hammoudeh Garcia , Florian Weisshardt
autogenerated on Sat Jun 8 2019 18:52:45