Working with Robot 3D Modeling in ROS 2
local graph
Chapter 4 — Working with Robot 3D Modeling in ROS 2
Section titled “Chapter 4 — Working with Robot 3D Modeling in ROS 2”In the last chapter, we discussed implementing different ROS 2 concepts, such as topics, services, actions, and parameters, using C++ and the rclcpp client library. This chapter will show how we can apply these communication concepts to robot modeling, simulation, and control.
In this chapter, we focus more on robot modeling in ROS 2 using the Universal Robot Description Format (URDF), which can model the kinematics and dynamics of the robot. This 3D model can be used for visualization, simulation, motion planning, sensor and actuator integration, and real robot control. URDF is a useful feature in ROS 2 to keep the robot representation simple and modular.
We will start by discussing the importance of robot modeling and see the basics of URDF/Xacro modeling. After discussing the basics of URDF/Xacro, we will see how to convert a 3D CAD model of a robot to URDF. After building the URDF model, we will visualize it in RViz. Finally, we will discuss the best practices for creating a URDF/Xacro model. In this chapter, we will discuss the following important topics:
- Introduction to robot modeling in ROS 2
- Introduction to URDF and Xacro
- Exporting 3D CAD models to URDF
- Visualizing URDF models of existing robots
- Best practices of URDF/Xacro modeling
Technical requirements
Section titled “Technical requirements”To follow along with this chapter, you should install Ubuntu 24.04 LTS with a ROS 2 Jazzy desktop. It can be in a VM, Docker, WSL 2, or a real machine. The reference materials for this chapter can be found in the Chapter04 folder of the following GitHub repository: https://github.com/PacktPublishing/Mastering-ROS-2-for-Robotics-Programming/tree/main/Chapter04.
Introduction to robot modeling in ROS 2
Section titled “Introduction to robot modeling in ROS 2”Let’s start by understanding robot modeling and its relevance in building robot software.
What is robot modeling?
Section titled “What is robot modeling?”Robot modeling is the process of developing a mathematical and conceptual representation of a robot’s structure, kinematics, dynamics, and functionality. The process mainly concerns the robot’s physical and mechanical characteristics, such as how the robot looks, how many links and joints are present, and the shape and size of each link. It also defines dynamic parameters like mass and inertia and additional information like sensors/actuators present in the robot.
Here are important types of modeling within robot modeling:
- Kinematic modeling: A robot is a combination of links and joints. Links are the rigid body part of the robotic system, and joints are the interconnection of multiple links. The links and joints are mainly responsible for the robot’s motion. Robot kinematics is the study of a robot’s motion without considering the forces or torques that cause the motion. Kinematic modeling is an important part of robot modeling. In the robot model, you can find accurate information regarding the position of each link and joint, types of joints, limits of joints, and shape of joints.
- Dynamics modeling: In this modeling, we consider the forces/torque acting on the robot links and joints. Dynamic modeling is very important because these parameters help to simulate the robot model before prototyping the actual robot. Tuning these parameters in simulation will help to build better hardware components.
- Geometric modeling: In this modeling, we define the shape, size, and physical structure of the robot’s links or parts. This is essential for visualizing the robot and detecting collisions with its environment. The robot’s geometry helps it plan its path better and avoid obstacles.
- Control modeling: In control modeling, we develop a control system that allows the robot to perform certain tasks, such as following a path or picking up objects. This modeling helps create better motion control and feedback loops in the robot.
- Sensor and actuator modeling: The sensor and actuator models help to simulate both virtually in a robot simulator. These models help to iterate different sensors on the robot before buying them for the real robot. The same applies to actuators. Modeling different actuator profiles in a simulation helps us understand which actuator has to be chosen for the real robot.
- Environmental modeling: This involves modeling the physical surroundings, allowing the robot to interact and navigate. Accurate environmental modeling is important for testing the robot for multiple use cases.
Here is an example of a 3D CAD model of a two-wheeled robot in Fusion 360 [1]. These types of CAD tools allow us to model the robot’s geometry, kinematics, and dynamics.

Figure 4.1: 3D CAD model of a two-wheeled robot in Fusion 360
Figure 4.1: 3D CAD model of a two-wheeled robot in Fusion 360
In the upcoming sections, we will understand the importance of robot modeling and learn more about CAD tools and compare them in the context of robot modeling.
Why is robot modeling important?
Section titled “Why is robot modeling important?”We have seen the different types of modeling involved in robot modeling. You may already understand the importance of robot modeling, but let’s summarize the main points here:
- Visualization and development: A 2D or 3D CAD model of the robot helps the developers visualize it and its movement during robot application development. Visualization is not a simulation; it is for visualizing the motion of each joint of the robot and checking whether the robot is in a collision. Visualizing the model allows us to evaluate the kinematics and the robot’s response to each motion command.
- Robot simulation: Once we have an accurate robot model, we can perform a robot simulation. The robot simulation helps the developer identify design flaws and optimize them. Using simulators, developers can reiterate the design without spending any money. The reiterated design will allow the developer to construct and choose more accurate hardware components, resulting in successful robotic products. Even though the simulators are not an accurate replica of the actual robot, they can still give a good idea of the working of your actual robot.
- Controls: With an accurate robot model, we can even tune control algorithms to ensure smoother movement in the actual robot.
- Planning and navigation: With the help of a simulator, we can use robot models to test different robotic applications, such as pick-and-place and autonomous navigation.
Now, let’s have a look at some of the popular CAD modeling tools used for robot modeling.
Robot modeling tools
Section titled “Robot modeling tools”3D CAD tools are mainly used to model the kinematics and dynamics of the robot. CAD tools like SolidWorks [2], Autodesk Fusion 360 [3], and CATIA [4] are widely used in modeling robots. These tools provide the option to model the links and joints of the robot and can assign dynamic properties, like mass, inertia, etc., to the robot’s links. Tools like SolidWorks and Fusion 360 offer options to simulate the joints and help to do structural analysis.

Figure 4.2: Popular 3D CAD tools for robotics
Figure 4.2: Popular 3D CAD tools for robotics
Here is a comparison of the features of popular CAD tools used in robot modeling:
| Feature | Autodesk Fusion 360 | SolidWorks | CATIA |
|---|---|---|---|
| Major focus | Quick and easy 3D CAD designing, simulation, and collaboration. | Advanced 3D CAD designing for engineering and manufacturing. | High-end engineering and complex assembly and design. Good with surface modeling. |
| Simulation | Perform basic Finite Element Analysis (FEA) and basic motion studies. | Advanced FEA, motion studies, thermal and kinematic analysis. | High-level FEA, motion, structural, and kinematics analysis. |
| Assembly modeling | Handles simple to moderate assemblies. | Ideal for complex assemblies and dependencies. | Best in class for large and complex assemblies. |
| Collaboration | It has excellent cloud collaboration version control. | Limited cloud option, mainly using local file management. | It is integrated with Product Lifecycle Management (PLM) tools. |
| User interface | Easy to use, clean, cloud-connected UI, and intuitive. | It features a rich UI and has a powerful and steeper learning curve. | The UI is complex, customizable, and has an industrial interface. |
| Parametric modeling | User-friendly and strong parametric tools. | Industry-leading parametric modeling. | Extremely robust, suited for complex, high-precision assemblies. |
| File export for robotics | Supports STL, OBJ, step, and third-party plugins for ROS URDF export. | Supports STL, OBJ, step, and third-party plugins for ROS URDF export. | Supports STL, OBJ, step, and third-party plugins for ROS URDF export. |
| Rendering | It has basic visualization with cloud rendering. | Good-quality rendering with Photoview 360 or add-ons. | It has advanced rendering and visualization features. |
| Cost | It must have a subscription-based policy (free and affordable for students and startups). | High-cost licensing for the full suite. | Very expensive and designed for enterprise usage. |
| Platform compatibility | Cloud-based, works with Windows and macOS. | Primarily works with Windows and some cloud-based features. | Primarily work with Windows. |
| Learning curve | Moderately easy for beginners. | Steeper for beginners, but powerful once mastered. | Steeper for beginners, but powerful once mastered. |
Table 4.1: Comparison of popular CAD tools for robotics
In the preceding table, you can see that Autodesk Fusion 360 is good for beginners, but if you plan to create a complex system assembly, it is better to go with SolidWorks or CATIA.
Now, we can discuss the next step after the robot’s 3D CAD modeling. After developing the CAD model, we need to export it to other formats for further simulation, visualization, and control of the physical robot. So next, we will see how to export CAD models into formats compatible with robot simulators like Gazebo, MuJoCo, Webots, NVIDIA Isaac Sim, and CoppeliaSim, and robotics frameworks such as ROS 2 for robot control.
In this chapter, we will focus on exporting the CAD model to a ROS 2-compatible robot model so that we can use it for simulation and ROS 2-based hardware control. Before starting to discuss the export operation, it is good to understand the robot model format in ROS 2 for visualization, simulation, and control: URDF/Xacro.
Introduction to URDF and Xacro
Section titled “Introduction to URDF and Xacro”In ROS 2, we use the URDF / Xacro format to represent the robot model. URDF is an XML format, and inside URDF, there are tags to describe the robot’s structure, kinematics, appearances, collision, and basic dynamics parameters. It can also hold simulation tags, which can be used for robot simulators like Gazebo.
Writing URDF can sometimes be lengthy and complex. It doesn’t offer any programmability. We need to add hardcoded values for joints and links. That’s where Xacro comes in. Xacro (or XML Macros) is a tool in ROS 2 to simplify complex URDF files. As the name suggests, it enables us to use macros/functions inside URDF.
Along with macros, we can use variables and conditional statements with XML, making it easier to build, reuse, and modify large robot models. Using Xacro, we can create reusable templates and parameters to make URDF development easier. After creating the URDF model using Xacro, we will use the Xacro tool in ROS 2 to parse the file and convert it to regular URDF. The developer does not need to worry about conversion; the Xacro tool will take care of it. Writing URDF with Xacro is preferred because it offers developers more flexibility and programmability.
The main limitation of URDF/Xacro modeling is that we can only represent tree structures, so parallel robots can’t be modeled inside URDF. Also, we assume robots are rigid bodies connected by rigid links connected by joints. Flexible elements are not supported.
We can create URDF/Xacro manually or with the help of export plugins from CAD tools. To create a model or edit existing models manually, it is good to understand the basic components of URDF. The following section describes the basic components of URDF.
Components of URDF/Xacro
Section titled “Components of URDF/Xacro”As already discussed, URDF has pre-defined XML tags to build the robot model. Here are the main components or tags in URDF/Xacro:
<robot> </robot><link> </link><joint> </joint><transmission> </transmission><ros2_control> </ros2_control><sensor> </sensor><gazebo> </gazebo>
We will mainly discuss tags such as <robot>, <link>, and <joint> in this chapter. We will discuss the rest of the tags in Chapter 5, which discusses the simulation of the robot, as the remaining tags are mainly used in simulation.
<robot> tag
Section titled “<robot> tag”The <robot> tag is the root element in URDF, which contains the complete definition of the robot model, consisting of several rigid components, also referred to as links, which are connected through joints. Joints can be either fixed, unactuated, or controlled by actuators. Additionally, sensors can be added to URDF, which is particularly useful for simulation. The <robot> tag has an attribute called name, a unique identifier for the robot model.

Figure 4.3: Structure of the tag (source: https://wiki.ros.org/urdf)
Figure 4.3: Structure of the <robot> tag (source: https://wiki.ros.org/urdf)
In Figure 4.3, you can see how the links and joints are interconnected inside the robot tag. Here is an example of the <robot> tag in a URDF file. In this template, master_robot is the name of the robot definition. Inside the robot tag, we are defining all other tags:
<robot name="master_robot"> <link> ... </link> <link> ... </link> <joint> .... </joint> <joint> .... </joint> <transmission> </transmission> <sensor> </sensor> <gazebo> .... </gazebo></robot>We can see more details about each tag we used inside the <robot> tag. Here is a reference link for the <robot> tag [5].
Now, let’s look at the <link> tag.
<link> tag
Section titled “<link> tag”This tag represents a part of the robot called a link. The link can be a wheel, an arm, or a sensor. The link component has subcomponents like geometry, visual properties, collision properties, and physical characteristics. Figure 4.4 shows various components in a <link> tag.

Figure 4.4: tag (source: https://wiki.ros.org/urdf)
Figure 4.4: <link> tag (source: https://wiki.ros.org/urdf)
Here is an example of the definition of a link in URDF:
<link name="base_link"><!-- Visual tag --> <visual> <origin xyz="0 0 0" rpy="0 0 0 1"/> <geometry> <box size="0.5 0.5 0.1"/> </geometry> <material name="blue"/> </visual><!-- Collision tag --> <collision> <origin xyz="0 0 0" rpy="0 0 0 1"/> <geometry> <box size="0.5 0.5 0.1"/> </geometry> </collision><!-- Inertial tag --><inertial> <mass value="2.0"/> <origin xyz="0 0 0.1" rpy="0 0 0" /> <inertia ixx="0.1" iyy="0.1" izz="0.1" ixy="0.0" ixz="0.0" iyz="0.0"/> </inertial></link>In this example, you can see that the definition of a link starts with the <link name="base_link"> tag. The base_link is the name of the link we are going to define. The </link> tag is used to end a link definition. Inside the definition of a link, you can find visual, collision, and inertial tags:
<visual>tag: This describes the link’s appearance, including geometry, materials, and, importantly, where the geometry’s origin is. The visual of a link can be a primitive shape like a box or even a mesh file. URDF supports primitive shapes like boxes, spheres, and cylinders. We can easily create simple robots using these shapes.<collision>tag: This tag defines the collision geometry of the link. This is like a visual tag; the only difference is that collision geometry is for detecting whether the robot’s link is in a collision. It is not for visualization purposes like visual tags. The collision geometry overlays on top of the visual model. The collision meshes or models are simpler compared to visual meshes. This can make collision checking fast and efficient.<inertial>tag: This tag defines the mass properties of a link in a robot, the center of mass, and the inertia matrix. These parameters are important for physics simulation and control calculations in robotics.
Check out [6] for a detailed reference to the <link> tag.
Now let’s see what the <joint> tag is.
<joint> tag
Section titled “<joint> tag”In URDF, the <joint> tag defines the interconnection of two links in a robot, allowing them to move relative to each other. Each <joint> tag can specify the name of the joint, the type of joint, the axis of movement, the origin of the joint, and properties like joint limits, damping, etc. Figure 4.5 shows the different parameters of a joint tag.

Figure 4.5: tag (source: https://wiki.ros.org/urdf)
Figure 4.5: <joint> tag (source: https://wiki.ros.org/urdf)
Here is an example code snippet of the <joint> tag in URDF:
<joint name="base_link_joint" type="revolute"> <parent link="base_link" /> <child link="elbow_link" /> <origin xyz="0 0 1" rpy="0 0 0" /> <axis xyz="0 0 1" /> <limit effort="5.0" velocity="1.5" lower="0.0" upper="1.57" /> <dynamics damping="0.1" friction="0.01" /></joint><link name="elbow_link">...............</link>In this example, you can see that the name of the joint is base_link_joint, and we define the <parent> link and the <child> link. The <parent> link acts as a reference for the joint movement. The <child> link is a link that moves relative to the <parent> link. In this example, the parent link is base_link, as seen in the link section, and the child link is elbow_link. The elbow_link is an imaginary link just for an explanation of the joint. In the <joint> tag, we can see the origin of the joint; the axis of rotation; the limits of the joint on position, velocity, and torque/effort; and parameters like damping and friction. Another important parameter inside <joint> is the type of joint. There are multiple types of joints available:
- Fixed: This type of joint creates a rigid connection between two links. It is preferred when no relative motion between parent and child links is required.
- Revolute: This kind of joint is like a hinge joint, allowing rotation through a certain axis within a minimum and maximum angle. These joints are applied in robotic arms and similar structures that require limited rotational movement.
- Continuous: This type of joint is like a revolute joint, but the rotation axis is not limited. It is used in the wheels of a robot or any joint with continuous rotation.
- Prismatic: This joint allows linear motion in a single axis, with a limit on the range. It helps create sliding mechanics like a piston or links that extend.
- Floating: This allows six degrees of freedom (translation and rotation around the X, Y, and Z axes). It is typically used to represent the robot’s free movement in space, such as
base_link. - Planar: This type of joint can move in the X and Y axes and rotate around the Z axis. It is good for mobile robots or structures where parts need to slide and rotate in a 2D plane.
Check out [7] for a complete reference on the <joint> tag in URDF.
After discussing the basic tags in URDF, let’s do some basic modeling, first with URDF and then with Xacro.
Building our first URDF model
Section titled “Building our first URDF model”Let’s start modeling a basic robot using URDF. You can find the URDF code in Chapter04 | simple_rrbot.urdf. This is a 2-DOF (short for two degrees of freedom) robotic arm called rrbot. As we have already seen in the above sections, the URDF code is primarily the combination of <link> and <joint> tags. ROS 2 provides tools to visualize, parse, and interact with URDF/Xacro-based models.
The best practice to manage URDF/Xacro files in ROS 2 is to create a separate ROS 2 package named <robot_name>_description. This package will be used only to keep URDF/Xacro files, mesh files we used for URDF, configuration files, and launch files. For example, URDF/Xacro models of universal robots (URs) are kept in ur_description [8]. You can find more examples on the ROS package index website [9].
We can also keep the URDF file without a package for learning purposes if it is a single file without many dependencies. For example, simple_rrbot.urdf is a URDF file that is not in a package but we can still visualize and analyze it.
Here are some important things to keep in mind when creating a URDF file manually:
- Time-consuming: Creating a URDF file manually takes time. We have to manually identify the link and joint origin, link geometry, collision, and inertia. This process will take longer and may have to be repeated if the links or joints are modified.
- Finding links and joint values: It is better to have the 2D sketch of the robot ready before starting to model the robot in URDF. The links and joints in URDF are modeled relative to the parent link. It is recommended to have a look at the official ROS 2 URDF tutorial to understand this process correctly [10]. This tutorial will ensure you have a proper idea of how to model
<link>and<joint>in URDF. Discussing this goes beyond the scope of this chapter. - Trial and error: Creating a URDF file may involve a lot of trial and error. We may need to do it to correct the axis of rotation, place the joint origin, etc.
- Using the VSCode IDE: VSCode IDE helps streamline URDF modeling. It has a ROS extension, which has the option to preview the URDF file in the IDE itself [11]. This will increase the speed of URDF development.
After going through the basics of URDF from the official tutorial, let’s go through ROS 2 URDF tools to analyze and visualize the simple_rrbot.urdf file.
Verifying URDF files
Section titled “Verifying URDF files”The check_urdf command in the ros-jazzy-desktop-full installation can parse a URDF file, show the kinematic chain of the robot, or show an error message if there is any.
Open a terminal inside the Chapter04 folder and run this command:
check_urdf simple_rrbot.urdfIf the command is successful, you will get the following output in the terminal:
robot name is: rrbot---------- Successfully Parsed XML ---------------root Link: world has 1 child(ren) child(1): single_rrbot_link1 child(1): single_rrbot_link2 child(1): single_rrbot_link3The output means that the URDF file has parsed successfully and shows the root links as world. We can see the respective child links as well.
Visualizing URDF links and joints
Section titled “Visualizing URDF links and joints”If we want to visualize the tree structure of the URDF file, we can use the following command. This is also installed along with the ros-jazzy-desktop-full installation:
urdf_to_graphiz simple_rrbot.urdfIf it is successful, it will create a PDF file showing the tree structure of links, joints, and the translation and rotation between each joint.
Using the following command, you can visualize the PDF:
evince rrbot.pdfThe output looks as shown in Figure 4.6.

Figure 4.6: URDF tree structure of the rrbot robot
Figure 4.6: URDF tree structure of the rrbot robot
In the output diagram, we can see the tree structure of the robot’s joints. The name of the joints and the translation and rotation of each joint relative to the previous joint are also shown.
After visualizing the robot links and joints, let’s see how to visualize the robot in 3D with the help of the ROS 2 RViz tool. As you know, RViz helps the developer to visualize the robot model in 3D.
There is already an official tutorial and package available in ROS 2 to get started with URDF modeling. We will see how to install this package and how we can use it to visualize our models as well.
Visualizing URDF in RViz
Section titled “Visualizing URDF in RViz”To visualize the URDF file in RViz, we have to run a set of ROS 2 nodes. Usually, we must write a launch file for it or reuse an existing one.
Let’s install the urdf-tutorial package, which gives us sample URDF files as well as a launch file to visualize any URDF files. The following command will install the urdf-tutorial package on your system:
sudo apt install -y ros-jazzy-urdf-tutorialYou can also find the source code of this package at [12].
After installing this package, we can start visualizing simple_rrbot.urdf using the following command; make sure you are inside the Chapter04 folder before executing the command:
ros2 launch urdf_tutorial display.launch.py model:=$PWD/simple_rrbot.urdfYou will get the RViz window shown in Figure 4.7 with the robot model loaded into it. To see the model in RViz, make sure Fixed Frame in RViz is set to world. You can also find a GUI with a slider called Joint State Publisher. Using the slider, you can move the robot’s joint.

Figure 4.7: URDF visualization in RViz
Figure 4.7: URDF visualization in RViz
You can see the robot model in the 3D viewport of RViz. The root frame of the URDF is the world. If we change Fixed Frame to world, the model will show the origin with regard to the world frame. In RViz, we can add different visualizations by adding display types. In Figure 4.7, you can see a display type called RobotModel, which loads the URDF model to the 3D viewport. The model you see in RViz is not a simulation but a visualization; it has joints and links that you can move based on the joint type, and it doesn’t apply the dynamics parameters of the robot model, like mass, inertia, etc.
Now, let’s have a look at how a URDF file is visualized using the launch file we have used.
Parsing a URDF file
Section titled “Parsing a URDF file”Once we start display.launch.py with a command-line argument called model, which contains the path of the URDF file, the launch file will start several nodes to parse the URDF file, publish the joint states, and publish the Transform (TF) of each joint. We will discuss TF further in the next chapter.
Figure 4.8 shows how a URDF file is read and visualized in RViz.

Figure 4.8: Parsing and visualizing URDF in RViz
Figure 4.8: Parsing and visualizing URDF in RViz
When we launch the display.launch.py file, it will start with two main nodes, which are already installed on ROS 2. Here is an explanation of the nodes that are started along with the launch file:
robot_state_publisher: This is an important node inside therobot_state_publisherpackage in ROS 2. The role of this node is to read the URDF file, parse the tags, and publish the URDF content in a topic called/robot_descriptionwith a ROS message type ofstd_msgs/msg/String. It also publishes TF [13], which is the relationship between joints and links in the kinematic structure of the robot. In ROS 2, we use the tf2 library to broadcast this information. Therobot_state_publisheruses this library to send this information to a topic called/tf, which has a ROS 2 message type calledtf2_msgs/msg/TFMessage. It also subscribes to a topic calledjoint_state, which has a ROS message type ofsensor_msgs/msg/JointState. Thejoint_statetopic has updated the values of robot model joints. It only contains the actuated joints, not the fixed joints. The TF topic values will be updated based on the new joint values, and you can view the change in RViz.joint_state_publisher: This node subscribes to the/robot_descriptiontopic and publishes the/joint_statetopic. The/joint_statetopic updates when we move the slider in the GUI, and therobot_state_publishertopic updates the TF topic based on the new values. In Figure 4.7, you can see a 3D coordinate frame attached to each robot joint, which is what the TF is. We need to add the TF display type by pressing the Add button in RViz to visualize the TF message.
The next section will discuss creating a new ROS 2 package to keep the robot model description in Xacro format. We will also write a launch file to visualize it.
Working with the Xacro model
Section titled “Working with the Xacro model”As we have already discussed, the Xacro format in URDF adds programming capabilities to URDF, such as using macros/functions, constants, variables, and math equations, which makes URDF development easier and more efficient. We can see an example of Xacro’s implementation of rrbot in the Chapter04 | rrbot_description package. You can use this package as a template for creating your robot description packages.
Here is the folder structure of this package:

Figure 4.9: rrbot_description package structure
Figure 4.9: rrbot_description package structure
Let’s look at the use of each file in this package:
rrbot.launch.py: This will startrobot_state_publisher,joint_state_publisher, and RViz and visualize the rrbot model in RViz.rrbot.urdf.xacro: Xacro files have the extension.xacro. We can write Xacro files for each section of the robot model and include them in the main Xacro file. This will increase the modularity of the robot model. Therrbot.urdf.xacrois the main Xacro file, and you can find macro/function definitions inside therrbot_description.urdf.xacroandrrbot.materials.xacrofiles. These two files are included in this main Xacro file.
To visualize the Xacro model, you can copy the rrbot_description package to a ROS 2 workspace and do the building of the workspace. After that, you can use the launch file to visualize the model:
ros2 launch rrbot_description rrbot.launch.pyLet’s walk through the main Xacro file, which is rrbot.urdf.xacro.
In normal URDF, we use the <robot> tag with the name, but in Xacro, we have to mention an XML namespace in the robot tag for Xacro functions. It will allow the developer to create a Xacro definition inside the URDF file:
<robot xmlns:xacro="http://www.ros.org/wiki/xacro" name="simple_rrbot">Now we can see how we can include another Xacro file definition inside a Xacro file:
<xacro:include filename="$(find rrbot_description)/urdf/rrbot_description.urdf.xacro" />The above definition includes the main Xacro definition of the robot model. This Xacro file has a macro called rrbot. If we call the macro, we will get the definition of the joints and links mentioned inside this macro. Like the rrbot macro, we import material Xacro files in this file. This is one flexibility that Xacro offers to developers. If the robot model is big, we can write macro definitions and keep them in separate Xacro files. This will increase the modularity and efficiency of the robot model development.
Defining and calling a macro in a Xacro file
Section titled “Defining and calling a macro in a Xacro file”As we know, rrbot_description.urdf.xacro contains the macro called rrbot. Here is the template for defining a macro/function in a Xacro file:
<xacro:macro name="rrbot" params="parent prefix *origin">. . .. . .</xacro>The definition of macros will always be inside a <robot> tag. If you define multiple macros for your robot model in separate files, ensure they are all inside the <robot> tag. If you check the macro definition, you can see parameters, which are the parent link of the robot, which, in our case, is its world. The prefix, which is optional, and the *origin tag allow the user to pass the origin values while calling this macro. We can have different origin values whenever we call this macro.
Now, let’s see how we can call this macro inside the rrbot.urdf.xacro file. Here is how we can call macro and pass parameters. You can find this call on rrbot.urdf.xacro:
<xacro:rrbot parent="world" prefix="$(arg prefix)"> <origin xyz="0 0 0" rpy="0 0 0" /></xacro:rrbot>We are passing arguments to this macro, such as parent links, prefixes, and the origin of the robot base. We can call the macro to replicate the robot model multiple times; there is no need to rewrite the section again and again like in URDF.
Constants and variables in Xacro
Section titled “Constants and variables in Xacro”In rrbot_description.urdf.xacro, you can find the use of constants.
In the following snippet, we are defining the mass and width of a link, you can see that xacro:property is used to define a constant. Here, the mass is assigned to 1 and the width is assigned to 0.1.
<xacro:property name="mass" value="1" /><xacro:property name="width" value="0.1" />Note
ROS 2 utilizes SI units for measurements and coordinate conventions. Here, mass is in kg and width is in meter.
To access the constant values, we can use "${mass}" and "${width}". Here is a code snippet showing how the <visual> tag uses constants to set the origin and geometry of the link:
<visual> <origin xyz="0 0 ${height1/2}" rpy="0 0 0"/> <geometry> <box size="${width} ${width} ${height1}"/>This is a very useful feature in Xacro; we don’t have to put hardcoded values inside URDF, but we can use constants. We can change only those constant values and adjust different parameters quickly.
Math equations in Xacro
Section titled “Math equations in Xacro”In Xacro, we can use math equations, as shown below. The <inertia> tag shows that the constants we define are used in the equation to compute the inertial matrix:
<inertia ixx="${mass / 12.0 * (width*width + height1*height1)}" ixy="0.0" ixz="0.0" iyy="${mass / 12.0 * (height1*height1 + width*width)}" iyz="0.0" izz="${mass / 12.0 * (width*width + width*width)}"/></inertial>The math equations in Xacro avoid manual computation by the developer, but instead, they can be computed from the Xacro itself.
Converting Xacro to URDF
Section titled “Converting Xacro to URDF”ROS 2 has a xacro-to-urdf converter tool, which is inside the xacro package. We can use the following command to convert the Xacro to URDF:
xacro <input_file.xacro> -o <output_file.urdf>or
ros2 run xacro xacro <input_file.xacro> -o <output_file.urdf>Here is an example command to convert rrbot Xacro to a URDF file. Make sure you are inside the urdf folder of rrbot_description before executing this command:
xacro rrbot.urdf.xacro -o rrbot.urdfor
ros2 run xacro xacro rrbot.urdf.xacro -o rrbot.urdfAfter running this command, you will see that rrbot.urdf was created.
We discussed this conversion because it is using the launch file we are going to discuss in the next section.
Writing the ROS 2 launch file for visualizing a URDF/Xacro file
Section titled “Writing the ROS 2 launch file for visualizing a URDF/Xacro file”The nodes that started visualizing and interacting with URDF/Xacro are robot_state_publisher, joint_state_publisher, and the rviz2 node. We can use the xacro command to convert the Xacro file to URDF and pass it to the robot_state_publisher node. The xacro command will also work if you directly pass the *.URDF files without any Xacro elements. Go to Chapter04 | rrbot_description/launch/rrbot.launch.py for the launch file for the rrbot visualization.
So far, we have seen how to model robots manually using URDF. The following section will give you an idea of how we can model a robot using a CAD tool and export it to URDF.
Exporting a 3D CAD robot model to URDF
Section titled “Exporting a 3D CAD robot model to URDF”Creating a URDF/Xacro file manually is a time-consuming task. If the model is not complex, we can use manual URDF modeling. But in cases of building a complex robot model, we may have to consider converting the 3D CAD model into URDF directly. So, is this possible? Yes, it is possible for a few CAD tools. We can convert the designed model directly to a ROS 2 package with the help of plugins. Very few plugins are official; they are mostly third-party plugins.
Here are some plugins and tools that are available to export CAD models in different software to URDF/Xacro format. The reference link has the instructions to set the plugin:
- SolidWorks to URDF Exporter: This is a third-party open source plugin available for SolidWorks to convert its assembly format, SLDASM, to URDF. When we export the model, the plugin can auto-generate the robot description package and generate URDF inside it. ROS 1 package generation is currently supported, but we can use that URDF file and move it into our ROS 2 description package. We can expect that the ROS 2 update will be supported soon. Here is where the URDF exporter plugin for SolidWorks can be found [14].
- Fusion 360 to URDF: This third-party open source Python script is available to change the Fusion 360 model to URDF. We must ensure the CAD model is compatible with the export script. The instructions are given in the related GitHub repository. Like the SolidWorks plugin, it will create a robot description package. Both ROS 1 and ROS 2 Python scripts are available. If the ROS 2 script is not working for you, try the ROS 1 script and copy the URDF file into your ROS 2 description package. Here is a link to the add-in script for exporting Fusion 360 to URDF for ROS 2 [15].
- OnShape to URDF: This is a third-party open source tool available based on OnShape APIs to get the OnShape assemblies and build a URDF model with the dynamic parameters. Here is a plugin to convert an OnShape model to URDF using the OnShape API [16].
- Blender to URDF: There are multiple third-party tools available for converting Blender models to URDF. One of the popular tools or add-ins available is Phobos. The Phobos project is a CLI tool and add-in for Blender for converting a model into a URDF model. Here is where you can find this project [17].
- Export URDF: This project is a URDF converter library for CAD programs like Fusion 360, OnShape, and SolidWorks [18].
Refer to [19] for details about more URDF export plugins.
The generated URDF/Xacro files can again be optimized. You can add more macros to Xacro by manually editing each file if needed. In some models, there may be some tuning required, especially in inertia values during simulation. Also, editing is required to add a ROS 2 controller like a differential drive and sensors like LiDAR to a robot.
In the next section, we will see how to install Autodesk Fusion 360, model a two-wheeled robot, and export the model to a ROS 2 URDF description package.
Installing Autodesk Fusion 360
Section titled “Installing Autodesk Fusion 360”Modeling a robot in Autodesk Fusion 360 to export to a ROS 2 URDF model has the following prerequisites.
The first prerequisite is to install Fusion 360 if it is not already installed on your machine. We can install Fusion 360, which is primarily supported in Windows, but there is an experimental project that can also help with Fusion 360 in Linux. Experimental projects may not be fully stable. Here is a link to install Fusion 360 on Windows [26]. Fusion 360 is free for students, educators, hobbyists, and some startup companies. The installation is straightforward in Windows. The main hardware requirement while working with Fusion 360 is a good GPU, preferably from NVIDIA. If you want to try the experimental Fusion 360 setup in Ubuntu, you can refer to the following project [27]. After setting up Fusion 360, you may have to sign up for an account that will help you to upload and share the CAD model over the cloud.
After installing Fusion 360, the next prerequisite is to install the Fusion 360 to URDF plugin/add-in to convert the designed model to URDF.
Installing the Fusion 360 to URDF plugin
Section titled “Installing the Fusion 360 to URDF plugin”Installing the Fusion 360 to URDF plugin is straightforward. Download the following GitHub repository [28], which has the plugin as a ZIP file. Unzip the file and extract it to some location first. After extracting the file, open Fusion 360 and press Shift + S to show the Scripts and Add-Ins window in Fusion 360. Inside this window, locate the option called MyScripts, and to add a new plugin, press the green + icon, which is shown as the first step in Figure 4.10
When we click this icon, a file dialog will pop up and we can browse the path to the folder called Fusion_URDF_Exporter_ROS2, as shown in Figure 4.10. This is the folder that has the plugin.
Click on the selected folder, and if everything works, you will see Fusion_URDF_Exporter_ROS2, as shown in the second step in Figure 4.10:

Figure 4.10: Fusion 360 Scripts and Add-Ins window
Figure 4.10: Fusion 360 Scripts and Add-Ins window
Once we set up the Fusion URDF Export ROS 2 plugin, we can start modeling the robot. In the next section, we will examine the design practices we must follow before modeling a robot in Fusion 360 to make it compatible with the Fusion URDF Export ROS 2 plugin.
Important design practices for the URDF Export plugin
Section titled “Important design practices for the URDF Export plugin”Here’s a list of things you must consider before you start modeling the robot to make it work with the script.
Correct the Fusion 360 coordinate system
Section titled “Correct the Fusion 360 coordinate system”When designing the robot model, we must follow the right-hand rule for the cartesian coordinate system so that it will properly export with the correct orientation in ROS 2 URDF. This section describes the coordinate system used in the project. Figure 4.11 shows the right-hand-rule demonstration with the robot.

Figure 4.11: Right-hand-rule demonstration
Figure 4.11: Right-hand-rule demonstration
In the figure:
- Index finger (X axis): Represents the positive direction of the X axis; the robot model should always point to the +X direction in Fusion 360. This axis corresponds with the length of the robot.
- Thumb (Z axis): Represents the positive direction of the Z axis. This axis corresponds with the height of the robot.
- Middle finger (Y axis): Represents the positive direction of the Y axis. This axis corresponds with the robot’s width.
We need to follow this coordinate system to visualize and spawn the robot model correctly in RViz and Gazebo in ROS 2.
Defining links of the robot
Section titled “Defining links of the robot”We must ensure that each robot link is defined as a separate component. The root link of the robot must be named base_link.
Defining joints of the robot
Section titled “Defining joints of the robot”When defining joints, parent links must be set as Component2, not as Component1.
Defining a component of the robot
Section titled “Defining a component of the robot”Components should contain only bodies. Nested components are not supported. Avoid components that have other components inside them. Figure 4.12 shows the components and their bodies in the rosbot robot.

Figure 4.12: Components and bodies of the rosbot robot
Figure 4.12: Components and bodies of the rosbot robot
Here are some limitations of the Fusion 360 to URDF plugin:
- Supported joint types: Rigid, Slider, and Revolute.
- Avoid using Fusion 360’s inbuilt joint editor for positioning joints in complex kinematic loops.
- Misalignments can occur during initial joint positioning in Fusion. Manual adjustments can cause cascading issues with visual and collision properties.
- Turn off Capture design history before exporting. Use distinct names for components and save individual components in separate folders to prevent issues.
Now, let’s start modeling our first robot, named rosbot, which is shown in Figure 4.11.
Modeling a robot in Fusion 360
Section titled “Modeling a robot in Fusion 360”Here is a set of instructions for modeling a sample robot called rosbot using Autodesk Fusion 360.
Setting the design plane for export
Section titled “Setting the design plane for export”We must follow the right-hand rule to set the plane before starting the modeling. If we do this, the exported URDF model will face the +X axis. Figure 4.13 shows the top view of the Fusion 360 viewport.

Figure 4.13: Setting the design plane of Fusion 360 for robot modeling
Figure 4.13: Setting the design plane of Fusion 360 for robot modeling
This is the axis we need when we visualize robots in ROS 2.
Sketching the robot base
Section titled “Sketching the robot base”After selecting the plane, we can start sketching the robot’s base. We have to draw a 2D sketch of the plane first and extrude it to create the box, which acts as the robot base.
Select Menu | SOLID | Create Sketch. After pressing this option, it will ask which plane we have to draw the sketch. It will show different planes, and choosing the exact plane we want may be confusing. We can use the Shift + Mouse Center button to rotate the 3D view and select the same plane we saw in the first step.

Figure 4.14: Choosing plane for drawing sketch
Figure 4.14: Choosing plane for drawing sketch
After selecting the plane, orbit the 3D scene to make the +X axis always the front.
Now, you can select the 2-point rectangle from SKETCH, as shown in Figure 4.15. From the Sketch palette window on the right side, choose the center rectangle option, as shown below. This option can draw a rectangle from a center point.
Press the Tab key to switch between the square dimensions and enter the dimensions. You can give 200 mm as the width and length for base_link.

Figure 4.15: Drawing a rectangular sketch
Figure 4.15: Drawing a rectangular sketch
After drawing the correct dimensions, you can click the FINISH SKETCH button at the top right to complete the sketch. After completing the sketch, you can click on the top to select the sketch’s top area.
Now press the Q key (Menu Solid | Modify | Press Pull) to extrude the sketch surface to make a box. Once you press the Q button, you will see an arrow key that will extrude the sketch to some height. You can give a value of 50 mm as the height.

Figure 4.16: Extruding the base plane
Figure 4.16: Extruding the base plane
After setting the height to 50 mm, select the Operation option from the Extrude window on the right side. We must choose New Body. After selecting this option, we can press Ok. The above steps are shown in Figure 4.16.
Adding wheels to the robot base
Section titled “Adding wheels to the robot base”We can add wheels after making the robot’s main chassis/ base_link. The chassis must have two active and two passive caster wheels. Figure 4.17 shows the Z-X plane, which shows the side of the chassis on which we have to add wheels.

Figure 4.17: Side view of the chassis
Figure 4.17: Side view of the chassis
We can start a sketch and extrude it like we did for the chassis to create wheels. In Figure 4.18, we can see that you have chosen the Sketch option and chose a circle with a center-diameter circle. Then, select the center of the chassis, as shown in Figure 4.18. Give a diameter of 100 mm and press Finish.

Figure 4.18: Sketching the wheels
Figure 4.18: Sketching the wheels
Now, you can click on the wheel sketch. You may have to press the Shift key to select each wheel segment. Now, press Q to extrude it and make the distance 25 mm, the thickness of the wheel. The important thing to note is that we have to make the wheel a new body in the operation option in extrude. This makes a new wheel a new body.

Figure 4.19: Extruding the wheels
Figure 4.19: Extruding the wheels
The steps are shown in Figure 4.19. We can use the same process to create the next wheel.
Moving the robot to ground level
Section titled “Moving the robot to ground level”After creating two wheels and the base_link, we have three independent bodies. The next step is to move the entire robot above the design plane. You can compute the distance by measuring it and moving the robot using the Move button. Make sure you have selected all three bodies before you move. To lift the robot from the design floor, you can roughly put 25 mm as the Z value in the Move window. You can find the robot’s position in Figure 4.20.

Figure 4.20: Lifting the robot to ground level
Figure 4.20: Lifting the robot to ground level
The next step is to create caster wheels for the robot. The caster wheels provide support for the robot.
Adding caster wheels to the base of the robot
Section titled “Adding caster wheels to the base of the robot”After lifting the robot, the next step is to add caster wheels. We have to add caster wheels on the front and back of the robot, as shown in Figure 4.21. As already discussed, casters are passive wheels that help the robot balance and distribute weight. To add two casters, we must create two small cylinders on the bottom side of the chassis. You can put the center of the circle 25 mm from both sides and have a diameter of 24 mm. To create a cylinder, we can extrude a circle sketch. The length of the extrude can be 24 mm, a few millimeters less than the robot’s height from the ground.

Figure 4.21: Sketching the caster wheel
Figure 4.21: Sketching the caster wheel
Figure 4.22 shows how we extrude the caster wheel. Make sure the Operation is a Joint, NOT a New body. The caster is a part of the chassis.

Figure 4.22: Extruding the caster wheel
Figure 4.22: Extruding the caster wheel
After creating the cylinder on both sides, we can make the bottom face of the cylinder spherical. Select the face of the cylinder, press F or go to Menu Surface | Modify | Fillet, and using the mouse, create a spherical face from the flat surface.

Figure 4.23: Shaping the caster wheel
Figure 4.23: Shaping the caster wheel
We can do the same operation for both of the caster wheels, and the final output will look like Figure 4.24.

Figure 4.24: Final caster wheel design
Figure 4.24: Final caster wheel design
In the next step, we will see how we can add LIDAR to the robot.
Adding a LiDAR base and lidar to the robot
Section titled “Adding a LiDAR base and lidar to the robot”Once the caster design is completed, we can add lidar to the front of the robot. First, we have to build a fixed platform. After adding the base, we can create a cylinder shape to replicate lidar, as shown below. You can draw a centered rectangle sketch of 70 mm (length) x 60 mm (width) with an extrude height of 45 mm (height). The center of the box can be placed 50 mm from the front of the robot. The box height can vary based on the obstacle profile in the environment. You can vary the height of the box based on the object’s height. A typical lidar platform is shown in Figure 4.25.

Figure 4.25: Creating a LIDAR support box
Figure 4.25: Creating a LIDAR support box
After creating the box, we can make a circle (29.5 mm radius) on the top and create a cylinder by extruding the circle by 20 mm. Make sure you are creating a new body when you extrude the object. The lidar in the robot is shown in Figure 4.26.
After creating all the bodies, you can rename them to meaningful names as shown in Figure 4.26.

Figure 4.26: Naming robot bodies
Figure 4.26: Naming robot bodies
The chassis has to be named base_link; this is mandatory. You can give the left and right wheels the same name, and you can also add a name to the lidar structure.
Adding a material type and color
Section titled “Adding a material type and color”After creating the model, we can go to each body, right-click on it, and choose Physical materials. This allows us to assign material properties to each link, as shown in Figure 4.27.

Figure 4.27: Adding materials to robot bodies
Figure 4.27: Adding materials to robot bodies
Search for ABS Plastic for the base link. Just click and drag the material to the link to apply the material. Choose the material Rubber black for the wheels and laminate blue for lidar.
Converting bodies to components
Section titled “Converting bodies to components”After adding the material’s properties, we must convert the individual bodies to components. The conversion from bodies to components is easy.
Here is how we can do it. Click on the Bodies option and choose the Components from Bodies option, as shown in Figure 4.28. A body is a single shape within a component, which can hold multiple bodies. To export to URDF, we need to make individual robot components.

Figure 4.28: Adding materials to robot bodies
Figure 4.28: Adding materials to robot bodies
After converting bodies to individual components, we can assign joints to the robot.
Adding joints to the robot
Section titled “Adding joints to the robot”After assigning the materials, we must assign the joints for connecting wheels to base_link and attach the lidar link. This is the most crucial step in modeling. Without assigning joints, the robot cannot move. Here are the steps to assign a joint in Fusion 360.
First, we can check how to create a joint between the wheels and the base_link. First, we must hide the base_link component and press J (Menu Solid | Assemble | Joint) to create a new joint. After invoking the joint option, we must provide the components (link) in the joint. We can create a revolute joint between base_link and the two wheels. To create a joint, we need two components: the first component should be the wheel, and the second will be the base_link, as shown in Figure 4.29.
We have to hide the base_link first and click on the center of the wheel, as shown in Figure 4.29, which is attached to the base_link.

Figure 4.29: Adding materials to robot bodies
Figure 4.29: Adding materials to robot bodies
After clicking on the center of the wheel, we can see a coordinate frame on the wheel. Next, we can hide the wheels and attach the frame in the base_link, as shown in Figure 4.30.

Figure 4.30: Assigning joints in the wheel
Figure 4.30: Assigning joints in the wheel
Once it is done, you can press the Motion tab in the Edit Joint option to select the type of joint. We need the revolute joint for the wheels, so select it, and you can preview the motion of the joint, as shown in Figure 4.31. After setting one joint, you can also do the same for the next wheel.

Figure 4.31: Testing the joints of the wheel
Figure 4.31: Testing the joints of the wheel
We have to create a rigid link between the lidar component and base_link for the lidar link. In the Browser section of Fusion 360, go to Joints and rename each joint as right_wheel_joint, left_wheel_joint, and lidar_joint.
Congratulations! You have completed the robot’s modeling phase. The final model is shown in Figure 4.32. Now, we can export the model to URDF.

Figure 4.32: Final robot model
Figure 4.32: Final robot model
Converting a Fusion 360 model to URDF for ROS 2
Section titled “Converting a Fusion 360 model to URDF for ROS 2”After completing the CAD model in Fusion 360, press Shift + S to open the script box and select the Fusion_URDF_Exporter_ROS2 script from My Scripts.

Figure 4.33: Converting the model to a ROS 2 package
Figure 4.33: Converting the model to a ROS 2 package
Figure 4.33 shows the steps in converting the Fusion 360 model to a URDF package. Let’s see each step in detail:
- We will see a welcome screen with basic information about the script, which will ask the user whether to proceed with the conversion.
- After pressing the OK button, the program will ask for the folder where the ROS 2 package must be created.
- When we press the OK button, the browse dialog will appear, and we can select a folder.
- After selecting the folder, we need to select the Gazebo version we want. There are two options currently available (Gazebo Harmonic or Classic). Based on the input, the launch file will be created.
- Once we select the Gazebo version, the final message will indicate whether the conversion was successful. After this conversion, the ROS 2 description package will be created.
After converting the robot model to ROS 2 packages, we can see how to visualize the URDF model.
Building a ROS 2 description package
Section titled “Building a ROS 2 description package”After creating the ROS 2 package for your robot, you can copy it to your ROS 2 workspace. If you work in Windows 11, you can work on ROS 2 using WSL or a virtual machine. Otherwise, you can reboot and select Ubuntu 22.04 for ROS 2 Humble/Ubuntu 24.04 for ROS 2 Jazzy.
For example, if you use the rosbot model from the demos folder and convert it to the ROS 2 package, you will get a package named rosbot_description. The package has also been put in the demos folder for your reference. Copy it to your ROS 2 workspace.
For example, let’s say master_ros2_ws is the name of the workspace, and you copied the package to the src folder of the workspace.
Switch to the ROS 2 workspace folder and build the new packages using the following command.
cd ~/master_ros2_wscolcon buildAfter building the package, run source on the workspace:
source install/setup.bashAfter running source on the workspace, we can visualize and simulate the robot.
Visualizing the robot in RViz
Section titled “Visualizing the robot in RViz”Here is the command to visualize the robot in RViz:
ros2 launch rosbot_description display.launch.pyThis will show RViz along with the joint_state_publisher_gui node, as shown in Figure 4.34.

Figure 4.34: Visualizing rosbot in RViz
Figure 4.34: Visualizing rosbot in RViz
In the next section, we will see the visualization of the URDF models of some existing robots. These models can be used as a reference when creating our own URDF models.
Visualizing URDF models of existing robots
Section titled “Visualizing URDF models of existing robots”To visualize some of the existing robot URDF models, you can download the ROS 2 description packages that are publicly available, such as on GitHub, or install binaries of robot models from the apt package manager.
Here are some of the models you can install and visualize in RViz:
- Universal Robot [20]
- TurtleBot 4 [21]
- Leo Rover [22]
Here is a command to install those description packages using the apt package manager:
sudo apt install -y ros-jazzy-ur-description ros-jazzy-turtlebot4-description ros-jazzy-leo-descriptionAfter installing the packages, let’s see how to visualize a UR robot in the following section.
Visualizing the Universal Robot model
Section titled “Visualizing the Universal Robot model”We can change the type of robot and visualize multiple types of robots. Here are the options available (ur3, ur3e, ur5, ur5e, ur10, ur10e, ur16e, ur20, and ur30). We will use ur30 as an example:
ros2 launch ur_description view_ur.launch.py ur_type:=ur30Figure 4.10 shows the visualization of the UR30 robot model.

Figure 4.35: UR robot visualization in RViz
Figure 4.35: UR robot visualization in RViz
To visualize other description packages, we need to switch to those installed paths and visualize using the display.launch.py launch file inside urdf_tutorials.
Visualizing the TurtleBot 4 model
Section titled “Visualizing the TurtleBot 4 model”To visualize the TurtleBot 4 model, you must switch to the turtlebot4_description package to visualize the URDF file.
First, we must switch to the urdf folder of the turtlebot4_description package.
Here is the command to switch to the folder.
cd /opt/ros/jazzy/share/turtlebot4_description/urdf/standard/Execute the following command to visualize turtlebot4 in RViz:
ros2 launch urdf_tutorial display.launch.py model:=$PWD/turtlebot4.urdf.xacroFigure 4.11 shows the visualization of the turtlebot4 robot model:

Figure 4.36: TurtleBot 4 visualization in RViz
Figure 4.36: TurtleBot 4 visualization in RViz
Visualizing the Leo Rover model
Section titled “Visualizing the Leo Rover model”To visualize Leo Rover model, you have to switch to the leo_description package to visualize the URDF file.
First, we must switch to the urdf folder of the leo_description package.
Here is the command to switch to the folder:
cd /opt/ros/jazzy/share/leo_description/urdf/Execute the following command to visualize the Leo Rover in RViz:
ros2 launch urdf_tutorial display.launch.py model:=$PWD/leo.urdf.xacroFigure 4.12 shows the visualization of the Leo Rover robot model.

Figure 4.37: Leo Rover visualization in RViz
Figure 4.37: Leo Rover visualization in RViz
We have completed the visualization of multiple robots in RViz; now let’s see what the best practices are for creating robot models using URDF/Xacro.
Official ROS 2 URDF tutorials
Section titled “Official ROS 2 URDF tutorials”In this section, we will discuss the official ROS 2 tutorials for URDF modeling. The urdf_tutorial tutorial package is provided to help with understanding URDF modeling from scratch. You can find the source code of the urdf_tutorial package in Chapter04 | urdf_tutorial or on GitHub [23].
The official tutorials can be found at this link [24].
In order to visualize the robot, you can either install urdf_tutorial from the apt package manager or copy the source code to the ROS 2 workspace and build.
After setting this package, open a terminal and switch the path to Chapter04 | urdf_tutorial/urdf.
You can see the development of the r2d2 robot from the basics to the Xacro model. This tutorial will give you a walkthrough of URDF modeling from scratch.
The following command visualizes the final Xacro model of R2D2:
ros2 launch urdf_tutorial display.launch.py model:=$PWD/08-macroed.urdf.xacroHere is the visualization of the r2d2 robot in RViz:

Figure 4.38: R2D2 robot visualization in RViz
Figure 4.38: R2D2 robot visualization in RViz
The following section will show how to configure Docker, Docker Compose, and Dev Container for ROS 2 URDF development.
Setting up ROS 2 Jazzy URDF development in Docker
Section titled “Setting up ROS 2 Jazzy URDF development in Docker”The scripts and Dockerfiles from Chapter04/docker_urdf can be used to build and run the ROS 2 packages in Chapter04 using Docker.
Here is the command to build the ROS 2 Jazzy custom image.
./build_image.sh ros2_urdf:v0.1 master_ros2_ws robotHere, ros2_urdf:v0.1 is the name of the custom image, master_ros2_ws is our ROS 2 workspace where we can put our Chapter04 ROS 2 packages, and the robot is the user’s name inside the container.
After building the ROS 2 custom image, we can create the container using the following command.
./create_container.sh ros2_urdf:v0.1 master_ros2_ws ros2_urdfWe need to provide the image name, the ROS 2 workspace name, and the container name we want to create.
After creating the container, you can see that the master_ros2_ws is mounted inside the container. We can build the ROS 2 workspace inside the container and run the nodes.
If we want to open more terminals for the same container, you can use the following command:
./start_container.sh ros2_urdfThis will attach a new terminal if the container is already running or start it if it has stopped. You need to provide the container name as the argument for this script.
After attaching a new terminal, make sure you have sourced the ros_entry_point script using the following command:
source /ros_entrypoint.shThe following section provides instructions for building, creating, and running containers using Docker Compose.
Setting up ROS 2 URDF Development in Docker Compose
Section titled “Setting up ROS 2 URDF Development in Docker Compose”Let’s build and start containers for Chapter04 packages using Docker Compose.
Switch to Chapter04 | docker_compose_urdf folder and execute the following command to start building the Docker image and starting the container:
- Use the following command if you are using a non-NVIDIA graphics card on your PC.
docker compose -f docker-compose-gui.yml builddocker compose -f docker-compose-gui.yml up
- If you do have an NVIDIA graphics card and the driver is properly installed, use the following command to start the container.
docker compose -f docker-compose-nvidia.yml builddocker compose -f docker-compose-nvidia.yml up
Once you start this command, the image will be built first, and the container will start. You have to use another terminal to attach to the container. You don’t have to close this terminal; if you cancel the command by pressing Ctrl + C, it will stop the running container.
Using the following command, you can create a new terminal from the running container.
./start_container.sh ros2_urdfNow source the ROS 2 environment using the following command and you can build the ROS 2 workspace which is mounted inside the container.
source /ros_entrypoint.shAfter starting the container, if you want to stop or remove it, you can execute the command from a new host terminal. Make sure you are executing inside the docker_compose_urdf folder.
docker compose -f docker-compose-gui.yml downor
docker compose -f docker-compose-nvidia.yml downNow, let’s set up VS Code Dev Container for ROS 2 URDF development.
ROS 2 URDF development using VS Code Dev Container
Section titled “ROS 2 URDF development using VS Code Dev Container”If you want to set up a Dev Container in VS Code for ROS 2 URDF development, make sure you have created the ROS 2 workspace named master_ros2_ws and open the Chapter04 folder using VS Code. This folder has a .devcontainer folder, which has the devcontainer.json file, and the instructions for using the docker-compose file we have used in the above section for the container. When you open the folder, it will prompt you to install the Dev Container extension and Reopen in Container. If you select this option, it will build the container, set the environment, mount the ROS 2 workspace as a volume, and connect the editor to it. We can also see the mounted volume, which is the master_ros2_ws folder in VS Code itself.
Finally, we will discuss some of the best practices that we can follow during URDF/Xacro modeling.
Best practices of URDF/Xacro modeling
Section titled “Best practices of URDF/Xacro modeling”Here are some best practices and tips for developing a robot model using URDF/Xacro:
- Use Xacro for modeling: As you know, Xacro provides more flexibility than URDF. It increases modularity and also enables code reuse. So, Xacro is the best choice for starting robot modeling in ROS 2.
- Follow REP (ROS Enhancement Proposals) conventions: Use REP [25] to name the links and joints in URDF.
- Follow the modular design of models: Split the robot model into macros, which can be kept inside separate Xacro files and organized inside a package properly.
- Use the proper naming convention: Using proper naming for joints and links avoids complexity in the URDF modeling.
- Use TF prefixing: It is important to use prefixes in Xacro in case you want to use multiple sensors at the same time or multiple robots. For example, in this macro definition,
<xacro:macro name="sensor_macro" params="prefix">, we can add as many sensors as we want by providing a different prefix. - Organize models in a package: Create a robot description package and organize all Xacro, URDF, and launch files within the package.
- Minimize hardcoding values: We can use Xacro constants to minimize the hardcoding of values in the model.
- Add meaningful comments: Add comments in URDF; it will help with understanding the model, especially if it is complex.
- Keep testing: We can keep testing and fine-tuning the model parameters as much as possible, which will improve the model’s overall accuracy.
- Optimize for performance: We can use mesh files like STL, DAE, and OBJ inside URDF as the geometry of visual and collision of a link. Adding more complex meshes will increase the computation and the overall performance will be reduced. Make sure meshes are down-sampled properly before using the URDF model. We can also use simple shapes like spheres and boxes for visual and collision links, which can minimize the overall computation for visualizing and simulating the URDF model. Simplifying the collision meshes is more important than visual meshes.
- Use the proper texture and colors: We can use the proper texture for mesh files and shapes to distinguish the parts and make the model visually appealing.
- Create a flexible design for future-proofing: Create a modular and flexible design so that sensors and actuators can be added to the robot model in the future.
- Implement documentation and version control: It is a best practice to maintain version control and add documentation to understand the model design.
We have seen some of the best practices and tips for developing a better robot model. In the next chapter, we will look at simulating robots.
Summary
Section titled “Summary”This chapter covered creating 3D robot models in ROS 2, using URDF and Xacro to define a robot’s structure, kinematics, dynamics, and control. It began by introducing different modeling types (kinematic, dynamic, geometric, control, and sensor-actuator) that are essential for accurate robot visualization and simulation. Key elements like <link> and <joint> tags were explained, enabling the representation of a robot’s physical and functional aspects. The chapter also outlined exporting CAD models to URDF for ROS compatibility, utilizing ROS 2 tools to verify and visualize models, and applying best practices like modular design and minimal hardcoding to create efficient, adaptable robot models.
In the next chapter, we will discuss how to simulate the robot we have modeled in this chapter using the Gazebo simulator and ROS 2. We will also explore different URDF tags that we use mainly for simulating the robot in Gazebo.
References
Section titled “References”- [1] https://www.autodesk.com/in/products/fusion-360/
- [2] https://www.solidworks.com/
- [3] https://www.autodesk.com/in/campaigns/education/fusion-360-education
- [4] https://www.3ds.com/products/catia
- [5] https://wiki.ros.org/urdf/XML/robot
- [6] https://wiki.ros.org/urdf/XML/link
- [7] https://wiki.ros.org/urdf/XML/joint
- [8] https://index.ros.org/p/ur_description/
- [9] https://index.ros.org/packages/
- [10] https://docs.ros.org/en/jazzy/Tutorials/Intermediate/URDF/Building-a-Visual-Robot-Model-with-URDF-from-Scratch.html
- [11] https://marketplace.visualstudio.com/items?itemName=ms-iot.vscode-ros
- [12] https://github.com/ros/urdf_tutorial/tree/ros2
- [13] https://docs.ros.org/en/jazzy/Tutorials/Intermediate/Tf2/Tf2-Main.html
- [14] https://github.com/ros/solidworks_urdf_exporter
- [15] https://github.com/runtimerobotics/fusion2urdf-ros2
- [16] https://github.com/Rhoban/onshape-to-robot
- [17] https://github.com/dfki-ric/phobos
- [18] https://github.com/david-dorf/ExportURDF/tree/main
- [19] https://docs.ros.org/en/jazzy/Tutorials/Intermediate/URDF/Exporting-an-URDF-File.html
- [20] https://www.universal-robots.com/
- [21] https://clearpathrobotics.com/turtlebot-4/
- [22] https://www.leorover.tech/
- [23] https://github.com/ros/urdf_tutorial/tree/ros2
- [24] https://docs.ros.org/en/jazzy/Tutorials/Intermediate/URDF/URDF-Main.html
- [25] https://ros.org/reps/rep-0000.html
- [26] https://www.autodesk.com/in/education/edu-software/fusion
- [27] https://github.com/cryinkfly/Autodesk-Fusion-360-for-Linux
- [28] https://github.com/runtimerobotics/fusion360-urdf-ros2