00001 #!/usr/bin/env python 00002 """ 00003 Publish all visuals within a SDF file as rviz Markers 00004 """ 00005 00006 import argparse 00007 00008 import rospy 00009 from visualization_msgs.msg import Marker 00010 from tf.transformations import * 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 markers = [] 00022 00023 00024 00025 def prepare_link_marker(link, full_linkname): 00026 marker_msg = link2marker_msg(link, full_linkname, use_collision, rospy.Duration(2 * updatePeriod)) 00027 if marker_msg: 00028 markers.append(marker_msg) 00029 00030 00031 def prepare_markers(prefix): 00032 world.for_all_links(prepare_link_marker) 00033 for marker in markers: 00034 marker.header.frame_id = prefix + pysdf.sdf2tfname(marker.header.frame_id) 00035 marker.ns = prefix + pysdf.sdf2tfname(marker.ns) 00036 00037 00038 def publishMarkers(): 00039 for marker in markers: 00040 #print(marker) 00041 markerPub.publish(marker) 00042 00043 00044 00045 def main(): 00046 parser = argparse.ArgumentParser() 00047 parser.add_argument('-f', '--freq', type=float, default=2, help='Frequency Markers are published (default: 2 Hz)') 00048 parser.add_argument('-p', '--prefix', default='', help='Publish with prefix') 00049 parser.add_argument('-c', '--collision', action='store_true', help='Publish collision instead of visual elements') 00050 parser.add_argument('sdf', help='SDF model to publish (e.g. coke_can)') 00051 args = parser.parse_args(rospy.myargv()[1:]) 00052 00053 rospy.init_node('sdf2marker') 00054 00055 global submodelsToBeIgnored 00056 submodelsToBeIgnored = rospy.get_param('~ignore_submodels_of', '').split(';') 00057 rospy.loginfo('Ignoring submodels of: ' + str(submodelsToBeIgnored)) 00058 00059 global updatePeriod 00060 updatePeriod = 1. / args.freq 00061 00062 global use_collision 00063 use_collision = args.collision 00064 00065 global markerPub 00066 markerPub = rospy.Publisher('/visualization_marker', Marker) 00067 00068 global world 00069 sdf = pysdf.SDF(model=args.sdf) 00070 world = sdf.world 00071 00072 prepare_markers(args.prefix) 00073 00074 rospy.loginfo('Spinning') 00075 r = rospy.Rate(args.freq) 00076 while not rospy.is_shutdown(): 00077 publishMarkers(); 00078 r.sleep() 00079 00080 if __name__ == '__main__': 00081 main()