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
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 import sys
00060 import roslib
00061 roslib.load_manifest('cob_bringup_sim')
00062
00063 import rospy
00064 import os
00065
00066 from gazebo.srv import *
00067 from geometry_msgs.msg import *
00068 import tf.transformations as tft
00069 from std_msgs.msg import Empty as EmptyMsg
00070
00071 if __name__ == "__main__":
00072 if len(sys.argv) < 2:
00073 print '[spawn_object.py] Please specify the names of the objects to be loaded'
00074 sys.exit()
00075
00076 rospy.init_node("object_spawner")
00077
00078
00079 rospy.sleep(5)
00080 rospy.wait_for_service("/gazebo/spawn_urdf_model")
00081
00082
00083 if not rospy.has_param("/objects"):
00084 rospy.logerr("No objects uploaded to /objects")
00085 sys.exit()
00086 all_object_names = rospy.get_param("/objects").keys()
00087
00088
00089 if "all" in sys.argv:
00090 object_names = all_object_names
00091 else:
00092 object_names = sys.argv
00093 object_names.pop(0)
00094
00095 rospy.loginfo("Trying to spawn %s",object_names)
00096
00097 for name in object_names:
00098
00099 if not rospy.has_param("/objects/%s" % name):
00100 rospy.logerr("No description for " + name + " found at /objects/" + name)
00101 continue
00102
00103
00104 if not rospy.has_param("/objects/%s/model" % name):
00105 rospy.logerr("No model for " + name + " found at /objects/" + name + "/model")
00106 continue
00107 model = rospy.get_param("/objects/%s/model" % name)
00108
00109
00110 if not rospy.has_param("/objects/%s/model_type" % name):
00111 rospy.logerr("No model_type for " + name + " found at /objects/" + name + "/model_type")
00112 continue
00113 model_type = rospy.get_param("/objects/%s/model_type" % name)
00114
00115
00116 if not rospy.has_param("/objects/%s/position" % name):
00117 rospy.logerr("No position for " + name + " found at /objects/" + name + "/position")
00118 continue
00119 position = rospy.get_param("/objects/%s/position" % name)
00120
00121
00122 if not rospy.has_param("/objects/%s/orientation" % name):
00123 rospy.logerr("No orientation for " + name + " found at /objects/" + name + "/orientation")
00124 continue
00125
00126 orientation = rospy.get_param("/objects/%s/orientation" % name)
00127 quaternion = tft.quaternion_from_euler(orientation[0], orientation[1], orientation[2])
00128 object_pose = Pose()
00129 object_pose.position.x = float(position[0])
00130 object_pose.position.y = float(position[1])
00131 object_pose.position.z = float(position[2])
00132 object_pose.orientation.x = quaternion[0]
00133 object_pose.orientation.y = quaternion[1]
00134 object_pose.orientation.z = quaternion[2]
00135 object_pose.orientation.w = quaternion[3]
00136
00137 try:
00138 file_localition = roslib.packages.get_pkg_dir('cob_gazebo_objects') + '/objects/' + model + '.' + model_type
00139 except:
00140 print "File not found: cob_gazebo_objects" + "/objects/" + model + "." + model_type
00141 continue
00142
00143
00144 if model_type == "urdf":
00145 srv_spawn_model = rospy.ServiceProxy('/gazebo/spawn_urdf_model', SpawnModel)
00146 file_xml = open(file_localition)
00147 xml_string=file_xml.read()
00148
00149 elif model_type == "urdf.xacro":
00150 p = os.popen("rosrun xacro xacro.py " + file_localition)
00151 xml_string = p.read()
00152 p.close()
00153 srv_spawn_model = rospy.ServiceProxy('/gazebo/spawn_urdf_model', SpawnModel)
00154
00155 elif model_type == "model":
00156 srv_spawn_model = rospy.ServiceProxy('/gazebo/spawn_gazebo_model', SpawnModel)
00157 file_xml = open(file_localition)
00158 xml_string=file_xml.read()
00159 else:
00160 rospy.logerr('Model type not know. model_type = ' + model_type)
00161 continue
00162
00163
00164
00165 srv_delete_model = rospy.ServiceProxy('gazebo/delete_model', DeleteModel)
00166 req = DeleteModelRequest()
00167 req.model_name = name
00168 exists = True
00169 try:
00170 res = srv_delete_model(name)
00171 except rospy.ServiceException, e:
00172 exists = False
00173 rospy.logdebug("Model %s does not exist in gazebo.", name)
00174
00175 if exists:
00176 rospy.loginfo("Model %s already exists in gazebo. Model will be updated.", name)
00177
00178
00179 req = SpawnModelRequest()
00180 req.model_name = name
00181 req.model_xml = xml_string
00182 req.initial_pose = object_pose
00183
00184 res = srv_spawn_model(req)
00185
00186
00187 if res.success == True:
00188 rospy.loginfo(res.status_message + " " + name)
00189 else:
00190 print "Error: model %s not spawn. error message = "% name + res.status_message
00191
00192 sim = rospy.get_param('/use_sim_time')
00193
00194 if sim is True:
00195
00196 rospy.loginfo('Running in simulation, publishing to /sim_spawned topic')
00197
00198 pub = rospy.Publisher('/sim_spawned', EmptyMsg,latch=True)
00199 pub.publish(EmptyMsg())
00200 pub.publish(EmptyMsg())
00201 pub.publish(EmptyMsg())
00202
00203 rospy.spin()
00204