2.3 Create Launch Files to Display the URDF in RViz

In our last tutorial, we launch the URDF file based on urdf-tutorial package as follows:

sudo apt install ros-jazzy-urdf-tutorial
ros2 launch urdf_tutorial display.launch.py model:=/home/robotlabs/ros2_ws/src/mec_mobile/mec_mobile_description/urdf/robots/robot_3d.urdf.xacro

Now, let's create our own launch file and load our own .rviz file as well.

Add RViz Configuration File

ROS provides several tools to visualize various data like rqt and RViz. We can start the RViz with the rviz2 command, then we can add the Robot Model view, browse for our URDF file and type base_link as Fixed Frame etc. alt text

Although is possible to use RViz with command like this, it's not the most convenient way, as we can move all these tasks into a ROS launch file with loading a .rviz file. Let's add the RViz configuration file first and then use it in our launch file.

Create a file inside rviz folder and name it with mec_mobile_description.rviz

mec_mobile_description.rviz

Note that we don't need to understand all the details of this configuration file. It can be generated automatically through RViz.

The RViz configuration files set up an RViz with a grid, a robot model, and coordinate frame visualizations etc. It also enables the camera movement tool and sets the initial camera view to an orbit view, which allows orbiting around a focal point in the scene.

When RViz is launched with this configuration file, it will display the robot model and allow interaction and visualization of the robot.

Launch file based on urdf-tutorial

The source of urdf-tutorial package is here. This package does exactly what we need, but it's not good to build up our tools onto it, so we can create our own launch file based on this package.

Let's create the check_urdf.launch.py file in the launch folder inside mec_mobile_description:

check_urdf.launch.py

This launch file is very useful to visualize our URDF during development. Now add the launch to the CMakeLists.txt file and build the workspace to try it:

Create another Launch File

We can also create a custom launch file to launch a mobile robot in RViz as ros2 official docs. Launch files in ROS2 allows you to start multiple nodes and set parameters with a single command.

Open a terminal window, and move to the mobile robot directory.

Add a file with VS Code in the launch folder named robot_state_publisher.launch.py (with robot state and joint state publisher nodes more than the previous check_urdf.launch.py):

robot_state_publisher.launch.py

Edit CMakeLists.txt

Now we need to edit CMakeLists.txt for the mec_mobile_description package, so build system can find our new folders, launch and rviz.

Add following code with VS Code to CMakeLists.txt:

Build the Package

Now let’s build the package.

Launch the Launch Files

Launch your launch files:

You can also add launch arguments. To see the available launch arguments, type:

If you want to launch the robots with no GUIs, do this:

Note: The ARGUMENTS in launch.py defines the robot’s configurable parameters.

Launch File Explanation

robot_state_publisher.launch.py

Starting with the imports, we bring in essential ROS 2 launch utilities.

The launch file’s core purpose is to arrange multiple nodes:

  • Robot State Publisher: Broadcasts the robot’s current pose

  • Joint State Publisher: Manages the robot’s joint positions

  • RViz: Provides the 3D visualization

The ARGUMENTS list defines the robot’s configurable parameters:

These arguments allow users to customize the robot’s configuration without changing the code – like using command-line switches to modify a program’s behavior.

The generate_launch_description() function is where the magic happens. First, it sets up the file paths:

The robot’s description is loaded from a XACRO file (an XML macro file) and converted into a robot_description_content parameter:

Then we create three key nodes:

1. Robot State Publisher:

This node takes the robot description and joint states and publishes the 3D poses of all robot links – like a real-time monitoring system for each part of the robot.

2. Joint State Publisher (with optional GUI):

This node publishes joint positions – imagine it as the robot’s muscle control center. The GUI version allows manual control of these joints.

3. RViz:

This is our visualization tool, loading a pre-configured layout specified in the RViz config file.

Finally, everything is assembled into a LaunchDescription and returned:

This launch file structure is common in ROS 2 applications, providing a clean way to start multiple nodes simultaneously.

That’s all.

Last updated