ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
2

Minimal working example for RViz marker publishing

asked 2021-03-11 05:22:53 -0600

Py gravatar image

Does anyone know of a complete minimal working example to publish a marker that can be visualised in RViz written in Python?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
3

answered 2021-03-11 06:11:07 -0600

Roberto Z. gravatar image

This is a minimal working example of a ROS node in Python with a Rviz marker publisher:

#! /usr/bin/env python

import rospy
from visualization_msgs.msg import Marker

rospy.init_node('rviz_marker')

marker_pub = rospy.Publisher("/visualization_marker", Marker, queue_size = 2)

marker = Marker()

marker.header.frame_id = "/map"
marker.header.stamp = rospy.Time.now()

# set shape, Arrow: 0; Cube: 1 ; Sphere: 2 ; Cylinder: 3
marker.type = 2
marker.id = 0

# Set the scale of the marker
marker.scale.x = 1.0
marker.scale.y = 1.0
marker.scale.z = 1.0

# Set the color
marker.color.r = 0.0
marker.color.g = 1.0
marker.color.b = 0.0
marker.color.a = 1.0

# Set the pose of the marker
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

while not rospy.is_shutdown():
  marker_pub.publish(marker)
  rospy.rostime.wallsleep(1.0)
edit flag offensive delete link more

Comments

That's really great, just what I needed! My next task is to colour code each marker according to a sensor value like a heat map. Any ideas how I'd achieve this?

Py gravatar image Py  ( 2021-03-15 14:27:49 -0600 )edit

I am glad that this resolved your question. To change to color of one marker use the attribute .color , it is shown above. For a heatmap it seems to me that you need many markers. Take a look at visualization_msgs/MarkerArray message. It is a bit broad subject to discuss here in details.

Roberto Z. gravatar image Roberto Z.  ( 2021-03-15 14:48:28 -0600 )edit

For future readers: This wasn't working for me, I was getting the error For frame [/map]: Frame [/map] does not exist. After spending probably way too long on this, I realized that /map indeed does not exist in the tf tree (run rosrun tf view_frames to get a pdf of the tree) - but map IS in the tree. I changed the frame_id to map and then it worked.

TLRD: If your getting For frame [/map]: Frame [/map] does not exist try changing marker.header.frame_id = "/map" to marker.header.frame_id = "map"

jstm gravatar image jstm  ( 2022-12-06 15:58:40 -0600 )edit
2

answered 2021-03-11 06:00:38 -0600

Ranjit Kathiriya gravatar image

updated 2021-03-11 06:13:50 -0600

Hello @Py,

Just take a look at the link below might be you can get help from this python code.

You can publish transformation(tf2) of each markers by sendTransform() function.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2021-03-11 05:22:53 -0600

Seen: 6,282 times

Last updated: Mar 11 '21