ros node that publishes spongebob in markers to rviz in python

To publish Spongebob as a marker in RViz using Python, you can follow these steps:

  1. Import necessary libraries and messages:
main.py
#!/usr/bin/env python
import rospy
from visualization_msgs.msg import Marker
from geometry_msgs.msg import Vector3
from std_msgs.msg import ColorRGBA
150 chars
6 lines
  1. Create a publisher for the marker:
main.py
rospy.init_node('spongebob_marker_publisher')
marker_pub = rospy.Publisher('spongebob_marker', Marker, queue_size=10)
118 chars
3 lines
  1. Create a Marker message and set its parameters:
main.py
spongebob_marker = Marker()
spongebob_marker.header.frame_id = "/map"
spongebob_marker.type = Marker.MESH_RESOURCE
spongebob_marker.mesh_resource = "package://my_package/meshes/spongebob.stl"
spongebob_marker.scale = Vector3(1, 1, 1)
spongebob_marker.color = ColorRGBA(1.0, 0.5, 0.0, 1.0)
289 chars
7 lines
  1. Publish the marker repeatedly with different positions to animate Spongebob's movement:
main.py
while not rospy.is_shutdown():
    spongebob_marker.header.stamp = rospy.Time.now()
    spongebob_marker.pose.position.x = 1.0 # change this value to animate Spongebob's movement
    marker_pub.publish(spongebob_marker)
    rospy.sleep(0.1) # change this value to adjust the animation speed
291 chars
6 lines
  1. Visualize Spongebob in RViz:
main.py
roslaunch my_package rviz.launch
33 chars
2 lines
  1. Add a Marker Display to RViz and set its topic to /spongebob_marker.

Your final Python script should look like this:

main.py
#!/usr/bin/env python
import rospy
from visualization_msgs.msg import Marker
from geometry_msgs.msg import Vector3
from std_msgs.msg import ColorRGBA

rospy.init_node('spongebob_marker_publisher')
marker_pub = rospy.Publisher('spongebob_marker', Marker, queue_size=10)

spongebob_marker = Marker()
spongebob_marker.header.frame_id = "/map"
spongebob_marker.type = Marker.MESH_RESOURCE
spongebob_marker.mesh_resource = "package://my_package/meshes/spongebob.stl"
spongebob_marker.scale = Vector3(1, 1, 1)
spongebob_marker.color = ColorRGBA(1.0, 0.5, 0.0, 1.0)

while not rospy.is_shutdown():
    spongebob_marker.header.stamp = rospy.Time.now()
    spongebob_marker.pose.position.x = 1.0 # change this value to animate Spongebob's movement
    marker_pub.publish(spongebob_marker)
    rospy.sleep(0.1) # change this value to adjust the animation speed
851 chars
22 lines

Note: Replace my_package with the name of your ROS package, and spongebob.stl with the filename and extension of your Spongebob mesh file.

gistlibby LogSnag