CodeTheRobot

ROS 2 Lyrical Luth · Lesson 16 of 86

RViz2 basics

Displays, fixed frame, saving configs and visualizing topics.

Estimated time · 45–60 min

Learning goals

What you will understand

  • Explain rviz2 basics in your own words
  • Use it in a small robotics example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

RViz2 is the 3D visualization tool. It does not simulate anything: it subscribes to topics and draws them. You add displays (RobotModel, TF, LaserScan, Image, Map, Path, Marker) and each display reads one topic or parameter.

The Fixed Frame is the reference frame everything is drawn in; if it does not exist in TF, displays show errors. Save your layout as a .rviz file and load it from a launch file with the -d argument so every run looks the same.

Guided lesson

Work through these steps

  1. 1.

    Source the ROS 2 Lyrical environment in a fresh terminal.

  2. 2.

    Read the rviz2 basics example and identify what each line does.

  3. 3.

    Build only the affected package, source the workspace overlay, and run the example.

  4. 4.

    Inspect the result from a second terminal with the ros2 CLI or RViz2 and compare it with the expected result.

Worked example

Read it before you run it

rviz2
# Start RViz2 with an empty layout.
ros2 run demo_nodes_cpp talker
# Any publisher can be visualized once its type has a display.
ros2 run rviz2 rviz2 -d ~/robot_ws/config/robot.rviz
# Start with a saved configuration.
ros2 run tf2_ros static_transform_publisher --frame-id map --child-frame-id odom
# Publish a frame so the Fixed Frame can be set to map.
ros2 topic pub -r 1 /marker visualization_msgs/msg/Marker '{header: {frame_id: map}, type: 2, action: 0, scale: {x: 0.3, y: 0.3, z: 0.3}, color: {r: 1.0, a: 1.0}, pose: {orientation: {w: 1.0}}}'
# Publish a red sphere marker at 1 Hz.

What the example is doing

  1. 1.The ros2 commands query the running graph; they do not prove that a node's internal behavior is correct.
  2. 2.Build from the workspace root, then source install/setup.bash so the shell can discover the new package.
  3. 3.Use a second sourced terminal to inspect nodes and interfaces while the example runs.

Watch it run · Robot arm

See what this code does

The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.

tip x110 y58ROBOT ARM

Demo for RViz2 basics. Expected result: RViz2 opens with the saved layout

Practice code

Now type it yourself

Copy the starter into your ROS 2 package, replace every TODO, then build with colcon build and run it from a sourced terminal.

# launch/view.launch.py
from launch import LaunchDescription
from launch_ros.actions import Node


def generate_launch_description():
    # TODO: start rviz2 with the config file config/robot.rviz using the -d argument
    return LaunchDescription([rviz])

Expected output

RViz2 opens with the saved layout
Stuck? Show the full solution (try it yourself first)
# launch/view.launch.py
from launch import LaunchDescription
from launch_ros.actions import Node


def generate_launch_description():
    rviz = Node(package='rviz2', executable='rviz2', arguments=['-d', 'config/robot.rviz'])
    return LaunchDescription([rviz])

Expected result

How you know it worked

  • ROS_DISTRO is lyrical in every terminal used for the exercise.
  • The complete package from the exercise builds and the expected graph entity appears in a second terminal.

Practical exercise

Apply the lesson

Open RViz2, set the Fixed Frame to map, add TF and Marker displays, publish the sphere marker from the CLI, save the config, and reopen it with -d.

Before you continue

  • ROS_DISTRO reports lyrical in every terminal you used
  • The example builds and runs without errors
  • You checked the result with a ros2 command, RViz2 or a test, not just by reading the code

If something goes wrong

ros2 cannot find a package or executable.

Source /opt/ros/lyrical/setup.bash, build from the workspace root, then source install/setup.bash in that terminal.

Nodes run but cannot discover each other.

Confirm both terminals use Lyrical, the same ROS_DOMAIN_ID, and compatible network/discovery settings.