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.xacroNow, 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.

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
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:
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):
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
Last updated