Go to the documentation of this file.00001
00002
00003 from __future__ import print_function
00004
00005 import argparse
00006
00007 import rospy
00008 import tf
00009 from gazebo_msgs.msg import ModelStates
00010 from visualization_msgs.msg import Marker
00011
00012 import pysdf
00013 from gazebo2rviz import *
00014
00015
00016 updatePeriod = 0.5
00017 use_collision = False
00018 submodelsToBeIgnored = []
00019 markerPub = None
00020 world = None
00021 model_cache = {}
00022 lastUpdateTime = None
00023
00024
00025
00026 def publish_link_marker(link, full_linkname, **kwargs):
00027 full_linkinstancename = full_linkname
00028 if 'model_name' in kwargs and 'instance_name' in kwargs:
00029 full_linkinstancename = full_linkinstancename.replace(kwargs['model_name'], kwargs['instance_name'], 1)
00030 marker_msg = link2marker_msg(link, full_linkinstancename, use_collision, rospy.Duration(2 * updatePeriod))
00031 if marker_msg:
00032 markerPub.publish(marker_msg)
00033
00034
00035 def on_model_states_msg(model_states_msg):
00036 global lastUpdateTime
00037 sinceLastUpdateDuration = rospy.get_rostime() - lastUpdateTime
00038 if sinceLastUpdateDuration.to_sec() < updatePeriod:
00039 return
00040 lastUpdateTime = rospy.get_rostime()
00041
00042 for (model_idx, modelinstance_name) in enumerate(model_states_msg.name):
00043
00044 model_name = pysdf.name2modelname(modelinstance_name)
00045
00046 if not model_name in model_cache:
00047 sdf = pysdf.SDF(model=model_name)
00048 model_cache[model_name] = sdf.world.models[0] if len(sdf.world.models) >= 1 else None
00049 if model_cache[model_name]:
00050 print('Loaded model: %s' % model_cache[model_name].name)
00051 else:
00052 print('Unable to load model: %s' % model_name)
00053 model = model_cache[model_name]
00054 if not model:
00055 continue
00056
00057 model.for_all_links(publish_link_marker, model_name=model_name, instance_name=modelinstance_name)
00058
00059
00060 def main():
00061 parser = argparse.ArgumentParser()
00062 parser.add_argument('-f', '--freq', type=float, default=2, help='Frequency Markers are published (default: 2 Hz)')
00063 parser.add_argument('-c', '--collision', action='store_true', help='Publish collision instead of visual elements')
00064 args = parser.parse_args(rospy.myargv()[1:])
00065
00066 rospy.init_node('gazebo2marker')
00067
00068 global submodelsToBeIgnored
00069 submodelsToBeIgnored = rospy.get_param('~ignore_submodels_of', '').split(';')
00070 rospy.loginfo('Ignoring submodels of: ' + str(submodelsToBeIgnored))
00071
00072
00073 global updatePeriod
00074 updatePeriod = 1. / args.freq
00075
00076 global use_collision
00077 use_collision = args.collision
00078
00079 global markerPub
00080 markerPub = rospy.Publisher('/visualization_marker', Marker)
00081 rospy.sleep(rospy.Duration(0, 100 * 1000))
00082
00083 global lastUpdateTime
00084 lastUpdateTime = rospy.get_rostime()
00085 modelStatesSub = rospy.Subscriber('gazebo/model_states', ModelStates, on_model_states_msg)
00086
00087 rospy.loginfo('Spinning')
00088 rospy.spin()
00089
00090 if __name__ == '__main__':
00091 main()