In this tutorial, we will create a mecanum mobile robot from scratch with URDF (Unified Robot Description Format), and then visualize the robot in RViz, a 3D visualization tool for ROS 2.
A Mecanum wheel is an omnidirectional wheel design to move in any direction, which is widely used in various robot products. It is far more fun and helpful to create a URDF file for a real-world robot you designed or you work with at your job or school.
The first step is to create a ROS 2 package in your ros workspace. Open a new terminal window, and create a new folder named mec_mobile.
mkdir -p ~/ros2_ws/src
cd ~/ros2_ws/src
mkdir mec_mobile
cd mec_mobile
1. Create the package where we will store our URDF file.
The mec_mobile_description package contains the robot description files that define the physical aspects of a robot, including its geometry, kinematics, dynamics, and visual aspects.
3. Add a README.md to describe what the package is about.
cd ..
gedit README.md
---------- Add following ----------
# mec_mobile #
I also recommend adding placeholder README.md files to the mec_mobile folder. The mec_mobile package is a metapackage. It contains lists of dependencies to other packages.
cd ~/ros2_ws/src/mec_mobile/mec_mobile
gedit README.md
---------- Add following ----------
# mec_mobile #
As well as the mec_mobile_description folder.
cd ~/ros2_ws/src/mec_mobile/mec_mobile_description
gedit README.md
---------- Add following ----------
# mec_mobile_description #
4. Now let’s build our new package:
cd ~/ros2_ws
colcon build
Check our new package. Open a new terminal window or source the .bashrc first in current:
Mesh files make your robot look realistic in robotics simulation and visualization programs. They visually represent the 3D shape of the robot parts, and are typically in formats such as STL (Stereo Lithography – .stl) or COLLADA (.dae).
The mesh files we use here were already available in this GitHub repository. However, if you want to create your own custom 3D printed robot from scratch, and make your own mesh files:
Design the robot’s 3D models using CAD programs like Onshape, Fusion 360, or Solidworks.
Export the 3D models as mesh files in formats like STL or COLLADA. These files contain information about the robot’s shape, including vertices, edges, and faces.
If needed, use a tool like Blender to simplify the mesh files. This makes them easier to use in simulations and visualizations.
Add the simplified mesh files to your URDF file to visually represent what the robot looks like.
Just pull these mesh files to corresponding directory of our package.
cd ~/ros2_ws/src/mec_mobile/mec_mobile_description/meshes
Configure CMakeLists.txt and package.xml
Let’s open Visual Studio Code and config the files for mec_mobile_description package.
cd ~/ros2_ws/src/mec_mobile/
code .
Configure the CMakeLists.txt for the mec_mobile_description package:
CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
project(mec_mobile_description)
# Check if the compiler being used is GNU's C++ compiler (g++) or Clang.
# Add compiler flags for all targets that will be defined later in the
# CMakeLists file. These flags enable extra warnings to help catch
# potential issues in the code.
# Add options to the compilation process
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
# Locate and configure packages required by the project.
find_package(ament_cmake REQUIRED)
find_package(urdf_tutorial REQUIRED)
# Copy necessary files to designated locations in the project
install (
DIRECTORY meshes urdf
DESTINATION share/${PROJECT_NAME}
)
# Automates the process of setting up linting for the package, which
# is the process of running tools that analyze the code for potential
# errors, style issues, and other discrepancies that do not adhere to
# specified coding standards or best practices.
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# comment the line when a copyright and license is added to all source files
set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# comment the line when this package is in a git repo and when
# a copyright and license is added to all source files
set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()
ament_package()
Make sure your package.xml for the mec_mobile_description package as follows:
package.xml
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>mec_mobile_description</name>
<version>0.0.0</version>
<description>Contains the robot description files that define the physical
aspects of a robot, including its geometry, kinematics, dynamics, and
visual aspects.</description>
<maintainer email="robotlabs@todo.todo">ubuntu</maintainer>
<license>BSD-3-Clause</license>
<buildtool_depend>ament_cmake</buildtool_depend>
<depend>urdf_tutorial</depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
<export>
<build_type>ament_cmake</build_type>
</export>
</package>
Build the Package
Before building the package, install dependencies:
cd ~/ros2_ws/
rosdep install -i --from-path src --rosdistro $ROS_DISTRO -y
You should see: All required rosdeps installed successfully. If not, type your password, and install the required dependencies.
Now let’s create our URDF file. We will actually create it in XACRO format. XACRO files using macros and variables to simplify complex robot descriptions.
Before a ROS tool can use the XACRO file, it must be processed (translated) into a URDF file first, which allows for the dynamic generation of robot descriptions based on the XACRO file.
Open a terminal window, and type this command to create all the files we need:
Create the RGBD camera: rgbd_camera.urdf.xacro. An RGBD camera that captures colors (RGB) information and measures depth (the D stands for depth). it allows the camera to create 3D maps of its surroundings.
The main camera link holds the visual 3D model (loaded from an STL file) and can optionally include other physical properties like collision geometry for simulation.
What makes RGBD links sophisticated is its handling of multiple camera frames:
Depth frame: Captures distance information
Infrared frames (infra1 and infra2): Used for stereo depth perception
Color frame: Regular RGB camera
Each frame is connected by fixed joints with specific offsets.
An IMU (Inertial Measurement Unit) is a sensor that measures movement, specifically acceleration, rotation, and sometimes magnetic fields, to help determine an object’s position and motion.
Full Robot
Finally, let’s create the full robot, bringing together all the components we have created:
cd ~/ros2_ws
colcon build
source ~/ros2_ws/install/setup.bash
Visualize the URDF in RViz
Let’s visualize the URDF files in RViz.
RViz (short for “ROS Visualization”) is a 3D visualization tool for robots that allows you to view the robot’s sensors, environment, and state. I use it all the time to visualize and debug my robots.
Now launch the URDF files. The conversion from XACRO to URDF happens behind the scenes. Be sure to have the correct path to your XACRO file.
Make sure your have installed urdf-tutorial package, if not, run following command.
sudo apt-get install ros-${ROS_DISTRO}-urdf-tutorial
model:=/home/robotlabs/ros2_ws/src/mec_mobile/mec_mobile_description...
Replace this directory according to your own project, like workspace name etc.
By convention, the red axis is the x-axis, the green axis in the y-axis, and the blue axis is the z-axis. Uncheck the TF checkbox to turn off the axes.
You can use the Joint State Publisher GUI pop-up window to move the links around.
On the left panel under Displays, play around by checking and unchecking different options.
You can also see what simulation engines will use to detect collisions: Uncheck “Visual Enabled” under Robot Model and check “Collision Enabled.”
Under Robot Model, you can see how the mass distribution by unchecking “Visual Enabled” and “Collision Enabled” and checking the “Mass” checkbox under “Mass Properties”.
You can also see the coordinate frames. Open a new terminal window, and type the following commands:
cd ~/Documents/
ros2 run tf2_tools view_frames
To see the coordinate frames, type:
dir
evince frames_YYYY-MM-DD_HH.MM.SS.pdf
TF Tree
Another useful tool of ROS is the TF Tree. This tool helps visualizing the transformations between the reference frames of the robot. First we need to install the tool:
sudo apt install ros-jazzy-rqt-tf-tree
After that, let's view our robot with the previous command in RViz:
You might experience an issue during the first start of TF Tree, in this case make sure that this rqt plugin is discovered:
ros2 run rqt_tf_tree rqt_tf_tree --force-discove
The official tutorial for creating a URDF file is . The URDF file gives the robot a digital body that software can interact with, and it allows software tools to understand the robot’s structure, enabling tasks like simulation, motion planning, and sensor data interpretation etc.
You have a Ubuntu 24.04 PC or VM with ROS2 Jazzy and Gazebo installed, if not, check our .
You can find the code here on .
If you want to save the code on Github, .
A metapackage doesn’t contain anything except a list of dependencies to other packages. You can use a metapackage to make it easier to install multiple related packages at once. .
Optical frames: Aligned with
You can find heaps of sensors that can be implemented in Gazebo simulator in .
Create the : lidar.urdf.xacro
We will add the so we can generate simulated LIDAR data in a future tutorial.