20 from visualization_msgs.msg
import Marker, MarkerArray
21 from gazebo_msgs.srv
import GetModelState
30 rospy.wait_for_service(
'/gazebo/get_model_state', 100)
32 except rospy.ROSException:
33 print(
'/gazebo/get_model_state service is unavailable')
36 if rospy.has_param(
'~meshes'):
37 meshes = rospy.get_param(
'~meshes')
38 if type(meshes) != dict:
39 raise rospy.ROSException(
'A list of mesh filenames is required')
43 self.
_mesh_topic = rospy.Publisher(
'/world_models', MarkerArray, queue_size=1)
45 rate = rospy.Rate(0.1)
46 while not rospy.is_shutdown():
53 print(
'Model %s already exists' % model)
58 new_model[
'position'] = [0, 0, 0]
59 new_model[
'orientation'] = [0, 0, 0, 1]
60 new_model[
'scale'] = [1, 1, 1]
62 if 'pose' in models[model]:
63 if 'position' in models[model][
'pose']:
64 if len(models[model][
'pose'][
'position']) == 3:
65 new_model[
'position'] = models[model][
'pose'][
'position']
66 if 'orientation' in models[model][
'pose']:
67 if len(models[model][
'pose'][
'orientation']) == 3:
70 if 'scale' in models[model]:
71 if len(models[model][
'scale']) == 3:
72 new_model[
'scale'] = models[model][
'scale']
74 if 'mesh' in models[model]:
75 new_model[
'mesh'] = models[model][
'mesh']
77 if 'model' in models[model]:
78 model_name = models[model][
'model']
81 new_model[
'position'] = [prop.pose.position.x,
84 new_model[
'orientation'] = [prop.pose.orientation.x,
85 prop.pose.orientation.y,
86 prop.pose.orientation.z,
87 prop.pose.orientation.w]
89 print(
'Model %s not found in the current Gazebo scenario' % model)
91 print(
'Information about the model %s for the mesh %s could not be ' 92 'retrieved' % (model, models[model][
'mesh']))
93 elif 'plane' in models[model]:
94 new_model[
'plane'] = [1, 1, 1]
95 if 'plane' in models[model]:
96 if len(models[model][
'plane']) == 3:
97 new_model[
'plane'] = models[model][
'plane']
99 print(
'Invalid scale vector for ' + model)
104 print(
'New model being published: %s' % model)
105 print(
'\t Position: ' + str(self.
_model_paths[model][
'position']))
106 print(
'\t Orientation: ' + str(self.
_model_paths[model][
'orientation']))
107 print(
'\t Scale: ' + str(self.
_model_paths[model][
'scale']))
110 markers = MarkerArray()
112 total_models = len(self._model_paths.keys())
117 marker.type = Marker.MESH_RESOURCE
123 marker.type = Marker.CUBE
128 marker.header.frame_id =
'world' 129 marker.header.stamp = rospy.get_rostime()
132 marker.action = Marker.ADD
133 marker.pose.position.x = self.
_model_paths[model][
'position'][0]
134 marker.pose.position.y = self.
_model_paths[model][
'position'][1]
135 marker.pose.position.z = self.
_model_paths[model][
'position'][2]
136 marker.pose.orientation.x = self.
_model_paths[model][
'orientation'][0]
137 marker.pose.orientation.y = self.
_model_paths[model][
'orientation'][1]
138 marker.pose.orientation.z = self.
_model_paths[model][
'orientation'][2]
139 marker.pose.orientation.w = self.
_model_paths[model][
'orientation'][3]
143 marker.color.b = 1.0 - float(i) / total_models
145 markers.markers.append(marker)
148 self._mesh_topic.publish(markers)
151 if __name__ ==
'__main__':
152 print(
'Start publishing vehicle footprints to RViz')
153 rospy.init_node(
'publish_world_models')
158 except rospy.ROSInterruptException:
159 print(
'caught exception')
def add_meshes(self, models)