Introduction 这个过程中,其实只是在定义publisher的node, 而rviz已经注册好的订阅器,指定了需要订阅的topic主题和消息类型,所以定义的node只需要写好发布器就行。
教程中给了如何使用cpp实现该功能,Markers:Sending Basic Shapes
所以我们这里使用python实现一遍。
Create Package 1 2 3 4 catkin_create_pkg using_markers rospy visualization_msgs catkin_make . devel/setup.bash
Publisher node 1 2 3 roscd using_markers mkdir scripts vim basic_shapes.py
shape.py内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 import rospyimport time from visualization_msgs.msg import Marker def main () : rospy.init_node("basic_shapes" , anonymous=True ) rate = rospy.Rate(1 ) pub = rospy.Publisher("visualization_marker" , Marker, queue_size=1 ) shape = Marker.CUBE while not rospy.is_shutdown(): marker = Marker() marker.header.frame_id = "/my_frame" marker.header.stamp = rospy.Time.now() marker.ns = "basic_shapes" marker.id = 0 marker.type = shape marker.action = Marker.ADD marker.pose.position.x = 0 marker.pose.position.y = 0 marker.pose.position.z = 0 marker.pose.orientation.x = 0.0 marker.pose.orientation.y = 0.0 marker.pose.orientation.z = 0.0 marker.pose.orientation.w = 1.0 marker.scale.x = 1.0 marker.scale.y = 1.0 marker.scale.z = 1.0 marker.color.r = 0.0 marker.color.g = 1.0 marker.color.b = 0.0 marker.color.a = 1.0 marker.lifetime = rospy.Duration() while pub.get_num_connections() < 1 : if rospy.is_shutdown(): exit(1 ) rospy.loginfo("Please create a subscriber to the marker!" ) time.sleep(1 ) pub.publish(marker) if shape == Marker.CUBE: shape = Marker.SPHERE elif shape == Marker.SPHERE: shape = Marker.ARROW elif shape == Marker.ARROW: shape = Marker.CYLINDER elif shape == Marker.CYLINDER: shape = Marker.CUBE rate.sleep() if __name__== "__main__" : try : main() except rospy.ROSInterruptException: pass
build and run 1 2 3 cd ~/catkin_wscatkin_make roscore
另起两个终端分别执行:
1 rosrun using_markers basic_shapes.py
则将两个线程都跑起来,接下来还需要设置rviz,才能看到效果。
设置rviz rviz刚打开的界面
然后将左边栏中的 Fixed Frame的值修改为 /my_frame
即代码中header.frame_id
定义的量。
然后点击左下角的Add
选择要添加的type, 显然我们这里传递的消息类型是Marker
, 所以选择Marker
然后就能看到结果了。