Markers help you show shapes or points in 3D space so you can see data or robot parts visually.
Marker display for custom visualization in ROS
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
ROS
visualization_msgs::Marker marker; marker.header.frame_id = "frame_name"; marker.header.stamp = ros::Time::now(); marker.ns = "namespace"; marker.id = unique_id; marker.type = visualization_msgs::Marker::SPHERE; // or CUBE, ARROW, etc. marker.action = visualization_msgs::Marker::ADD; marker.pose.position.x = x; marker.pose.position.y = y; marker.pose.position.z = z; 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 = size_x; marker.scale.y = size_y; marker.scale.z = size_z; marker.color.r = red_value; // 0 to 1 marker.color.g = green_value; marker.color.b = blue_value; marker.color.a = alpha_value; // transparency, 0 to 1 marker.lifetime = ros::Duration(duration_seconds);
The frame_id sets the coordinate frame for the marker.
The id must be unique within the namespace to update or remove markers correctly.
Examples
ROS
visualization_msgs::Marker marker; marker.header.frame_id = "base_link"; marker.ns = "shapes"; marker.id = 0; marker.type = visualization_msgs::Marker::CUBE; marker.action = visualization_msgs::Marker::ADD; marker.pose.position.x = 1.0; marker.pose.position.y = 0.0; marker.pose.position.z = 0.5; marker.scale.x = 0.2; marker.scale.y = 0.2; marker.scale.z = 0.2; marker.color.r = 0.0; marker.color.g = 1.0; marker.color.b = 0.0; marker.color.a = 1.0; marker.lifetime = ros::Duration();
ROS
visualization_msgs::Marker marker; marker.header.frame_id = "map"; marker.ns = "points"; marker.id = 1; marker.type = visualization_msgs::Marker::SPHERE; marker.action = visualization_msgs::Marker::ADD; marker.pose.position.x = 2.0; marker.pose.position.y = 3.0; marker.pose.position.z = 0.0; marker.scale.x = 0.1; marker.scale.y = 0.1; marker.scale.z = 0.1; marker.color.r = 1.0; marker.color.g = 0.0; marker.color.b = 0.0; marker.color.a = 0.8; marker.lifetime = ros::Duration(5);
Sample Program
This program publishes a blue sphere marker at position (0.5, 0, 0.5) relative to the robot's base frame repeatedly at 1 Hz. You can see it in RViz by subscribing to the visualization_marker topic.
ROS
#include <ros/ros.h> #include <visualization_msgs/Marker.h> int main(int argc, char** argv) { ros::init(argc, argv, "simple_marker_node"); ros::NodeHandle nh; ros::Publisher marker_pub = nh.advertise<visualization_msgs::Marker>("visualization_marker", 1); ros::Rate r(1); // 1 Hz while (ros::ok()) { visualization_msgs::Marker marker; marker.header.frame_id = "base_link"; marker.header.stamp = ros::Time::now(); marker.ns = "basic_shapes"; marker.id = 0; marker.type = visualization_msgs::Marker::SPHERE; marker.action = visualization_msgs::Marker::ADD; marker.pose.position.x = 0.5; marker.pose.position.y = 0.0; marker.pose.position.z = 0.5; 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 = 0.2; marker.scale.y = 0.2; marker.scale.z = 0.2; marker.color.r = 0.0f; marker.color.g = 0.0f; marker.color.b = 1.0f; marker.color.a = 1.0f; marker.lifetime = ros::Duration(); marker_pub.publish(marker); ROS_INFO("Published a blue sphere marker at (0.5, 0, 0.5)"); r.sleep(); } return 0; }
Important Notes
Always set header.stamp to current time to avoid stale markers.
Use unique id values to update or remove specific markers.
Set color.a to 0 to hide a marker without deleting it.
Summary
Markers let you show shapes in 3D space for visualization in ROS.
Set position, scale, color, and frame to control how markers appear.
Publish markers repeatedly to keep them visible in RViz.
Practice
1. What is the main purpose of using markers in ROS visualization tools like RViz?
easy
Solution
Step 1: Understand marker usage in ROS
Markers are used to create visual shapes like arrows, cubes, or spheres in RViz to represent data.Step 2: Differentiate markers from control commands
Markers do not control robot movement or store files; they only help visualize data.Final Answer:
To draw shapes in 3D space to help visualize robot data -> Option CQuick Check:
Markers visualize data = B [OK]
Hint: Markers show shapes for visualization, not control [OK]
Common Mistakes:
- Thinking markers control robot movement
- Confusing markers with configuration files
- Assuming markers send commands to motors
2. Which of the following is the correct way to set the type of a marker to a sphere in ROS using the visualization_msgs/Marker message?
easy
Solution
Step 1: Identify marker type constants
ROS defines marker types like CUBE, SPHERE, LINE_STRIP, TEXT_VIEW_FACING as constants in visualization_msgs::Marker.Step 2: Match sphere type
The constant for a sphere is visualization_msgs::Marker::SPHERE.Final Answer:
marker.type = visualization_msgs::Marker::SPHERE; -> Option AQuick Check:
SPHERE type constant = A [OK]
Hint: Use Marker::SPHERE constant to set sphere type [OK]
Common Mistakes:
- Using CUBE instead of SPHERE
- Confusing LINE_STRIP with sphere
- Using text marker type for shapes
3. Given the following ROS Python code snippet, what will be the color of the marker displayed in RViz?
marker.color.r = 0.0 marker.color.g = 1.0 marker.color.b = 0.0 marker.color.a = 0.5
medium
Solution
Step 1: Analyze RGB color values
Red = 0.0, Green = 1.0, Blue = 0.0 means pure green color.Step 2: Check alpha (opacity) value
Alpha = 0.5 means 50% transparency, so semi-transparent.Final Answer:
Semi-transparent green -> Option DQuick Check:
Green=1 and alpha=0.5 = D [OK]
Hint: Alpha <1 means transparency; RGB sets color [OK]
Common Mistakes:
- Ignoring alpha and assuming opaque
- Mixing up RGB values for color
- Thinking alpha=0.5 means fully transparent
4. You wrote this ROS C++ code to publish a marker, but the marker does not appear in RViz. What is the likely error?
visualization_msgs::Marker marker; marker.header.frame_id = "map"; marker.type = visualization_msgs::Marker::SPHERE; marker.action = visualization_msgs::Marker::ADD; marker.pose.position.x = 1.0; marker.pose.position.y = 2.0; marker.pose.position.z = 0.0; marker.scale.x = 0.5; marker.scale.y = 0.5; marker.scale.z = 0.5; marker.color.r = 1.0; marker.color.g = 0.0; marker.color.b = 0.0; marker.color.a = 1.0; marker_pub.publish(marker);
medium
Solution
Step 1: Check marker header completeness
ROS markers require header.stamp to be set to current time for RViz to display them properly.Step 2: Verify other parameters
Marker type, scale, and color are valid and visible; alpha is 1.0 (opaque).Final Answer:
Missing setting of marker.header.stamp to current time -> Option BQuick Check:
Header stamp missing = A [OK]
Hint: Always set header.stamp to ros::Time::now() [OK]
Common Mistakes:
- Forgetting to set header.stamp
- Using zero alpha making marker invisible
- Assuming scale too small to see
5. You want to display multiple markers at different positions using a single ROS publisher. Which approach correctly updates and publishes markers to show all of them in RViz?
hard
Solution
Step 1: Understand marker IDs and topics
Each marker must have a unique ID to be displayed simultaneously on the same topic.Step 2: Avoid overwriting markers
Publishing markers with the same ID overwrites previous ones, so unique IDs are needed.Final Answer:
Publish each marker separately with unique IDs and the same topic -> Option AQuick Check:
Unique IDs per marker = C [OK]
Hint: Use unique IDs for each marker on one topic [OK]
Common Mistakes:
- Using same ID for multiple markers
- Publishing only one marker and expecting all to show
- Not setting frame_id causing display errors
